blob: f771c6c89852d8e925542846b6d4833051422cd3 [file] [log] [blame]
Aviv Keshetd4a04302013-04-30 15:48:30 -07001#!/usr/bin/python
2# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5# pylint: disable-msg=C0111
6
7import os, unittest
8import mox
9import common
Aviv Keshet82683132013-06-18 10:44:41 -070010import subprocess
Aviv Keshet7cd12312013-07-25 10:25:55 -070011import types
Fang Deng2db96762013-10-03 16:45:31 -070012from autotest_lib.server import utils
13from autotest_lib.server.cros.dynamic_suite import constants
Aviv Keshetd4a04302013-04-30 15:48:30 -070014from autotest_lib.site_utils import test_that
15
Aviv Keshet30322f92013-07-18 13:21:52 -070016
Aviv Keshetd4a04302013-04-30 15:48:30 -070017class StartsWithList(mox.Comparator):
18 def __init__(self, start_of_list):
Aviv Keshet30322f92013-07-18 13:21:52 -070019 """Mox comparator which returns True if the argument
20 to the mocked function is a list that begins with the elements
21 in start_of_list.
22 """
Aviv Keshetd4a04302013-04-30 15:48:30 -070023 self._lhs = start_of_list
24
25 def equals(self, rhs):
26 if len(rhs)<len(self._lhs):
27 return False
Fang Deng9a841232013-09-17 16:29:14 -070028 for (x, y) in zip(self._lhs, rhs):
Aviv Keshetd4a04302013-04-30 15:48:30 -070029 if x != y:
30 return False
31 return True
32
Aviv Keshet30322f92013-07-18 13:21:52 -070033
34class ContainsSublist(mox.Comparator):
35 def __init__(self, sublist):
36 """Mox comparator which returns True if the argument
37 to the mocked function is a list that contains sublist
38 as a sub-list.
39 """
40 self._sublist = sublist
41
42 def equals(self, rhs):
43 n = len(self._sublist)
44 if len(rhs)<n:
45 return False
46 return any((self._sublist == rhs[i:i+n])
47 for i in xrange(len(rhs) - n + 1))
48
49
Aviv Keshetd4a04302013-04-30 15:48:30 -070050class TestThatUnittests(unittest.TestCase):
51 def test_validate_arguments(self):
52 # Deferred until validate_arguments allows for lab runs.
53 pass
54
55 def test_parse_arguments(self):
56 args = test_that.parse_arguments(
57 ['-b', 'some_board', '-i', 'some_image', '--args', 'some_args',
58 'some_remote', 'test1', 'test2'])
59 self.assertEqual('some_board', args.board)
60 self.assertEqual('some_image', args.build)
61 self.assertEqual('some_args', args.args)
62 self.assertEqual('some_remote', args.remote)
63 self.assertEqual(['test1', 'test2'], args.tests)
64
Aviv Keshet20b20c72014-04-02 11:54:28 -070065 def test_fetch_local_suite(self):
66 # Deferred until fetch_local_suite knows about non-local builds.
Aviv Keshetd4a04302013-04-30 15:48:30 -070067 pass
68
Aviv Keshet7cd12312013-07-25 10:25:55 -070069 def test_get_predicate_for_test_arg(self):
70 # Assert the type signature of get_predicate_for_test(...)
71 # Because control.test_that_wrapper calls this function,
72 # it is imperative for backwards compatilbility that
73 # the return type of the tested function does not change.
74 tests = ['dummy_test', 'e:name_expression', 'f:expression',
75 'suite:suitename']
76 for test in tests:
77 pred, desc = test_that.get_predicate_for_test_arg(test)
78 self.assertTrue(isinstance(pred, types.FunctionType))
79 self.assertTrue(isinstance(desc, str))
Aviv Keshetd4a04302013-04-30 15:48:30 -070080
81 def test_run_job(self):
82 class Object():
83 pass
84
85 autotest_path = 'htap_tsetotua'
86 autoserv_command = os.path.join(autotest_path, 'server', 'autoserv')
87 remote = 'etomer'
Aviv Keshetc8824402013-06-29 20:37:30 -070088 results_dir = '/tmp/fakeresults'
Christopher Wileyf6b5aae2013-07-09 10:14:02 -070089 fast_mode = False
Aviv Keshetad7e34e2013-08-19 18:09:27 -070090 job1_results_dir = '/tmp/fakeresults/results-1-gilbert'
91 job2_results_dir = '/tmp/fakeresults/results-2-sullivan'
Aviv Keshet30322f92013-07-18 13:21:52 -070092 args = 'matey'
93 expected_args_sublist = ['--args', args]
Fang Deng2db96762013-10-03 16:45:31 -070094 experimental_keyval = {constants.JOB_EXPERIMENTAL_KEY: False}
Aviv Keshetd4a04302013-04-30 15:48:30 -070095 self.mox = mox.Mox()
96
97 # Create some dummy job objects.
98 job1 = Object()
99 job2 = Object()
100 setattr(job1, 'control_type', 'cLiEnT')
101 setattr(job1, 'control_file', 'c1')
Aviv Keshetc8824402013-06-29 20:37:30 -0700102 setattr(job1, 'id', 1)
Aviv Keshetad7e34e2013-08-19 18:09:27 -0700103 setattr(job1, 'name', 'gilbert')
Fang Deng2db96762013-10-03 16:45:31 -0700104 setattr(job1, 'keyvals', experimental_keyval)
Aviv Keshetad7e34e2013-08-19 18:09:27 -0700105
Aviv Keshetd4a04302013-04-30 15:48:30 -0700106 setattr(job2, 'control_type', 'Server')
107 setattr(job2, 'control_file', 'c2')
Aviv Keshetc8824402013-06-29 20:37:30 -0700108 setattr(job2, 'id', 2)
Aviv Keshetad7e34e2013-08-19 18:09:27 -0700109 setattr(job2, 'name', 'sullivan')
Fang Deng2db96762013-10-03 16:45:31 -0700110 setattr(job2, 'keyvals', experimental_keyval)
Aviv Keshetd4a04302013-04-30 15:48:30 -0700111
Aviv Keshet5e33c172013-07-16 05:00:49 -0700112 id_digits = 1
113
Aviv Keshet82683132013-06-18 10:44:41 -0700114 # Stub out subprocess.Popen and wait calls.
115 # Make them expect correct arguments.
Aviv Keshete43bccf2013-08-14 14:11:59 -0700116 def fake_readline():
117 return b''
Aviv Keshet82683132013-06-18 10:44:41 -0700118 mock_process_1 = self.mox.CreateMock(subprocess.Popen)
119 mock_process_2 = self.mox.CreateMock(subprocess.Popen)
Aviv Keshete43bccf2013-08-14 14:11:59 -0700120 fake_stdout = self.mox.CreateMock(file)
Fang Deng9a841232013-09-17 16:29:14 -0700121 fake_returncode = 0
Aviv Keshete43bccf2013-08-14 14:11:59 -0700122 mock_process_1.stdout = fake_stdout
Fang Deng9a841232013-09-17 16:29:14 -0700123 mock_process_1.returncode = fake_returncode
Aviv Keshete43bccf2013-08-14 14:11:59 -0700124 mock_process_2.stdout = fake_stdout
Fang Deng9a841232013-09-17 16:29:14 -0700125 mock_process_2.returncode = fake_returncode
Aviv Keshete43bccf2013-08-14 14:11:59 -0700126
Fang Deng2db96762013-10-03 16:45:31 -0700127 self.mox.StubOutWithMock(os, 'makedirs')
128 self.mox.StubOutWithMock(utils, 'write_keyval')
Aviv Keshet82683132013-06-18 10:44:41 -0700129 self.mox.StubOutWithMock(subprocess, 'Popen')
Aviv Keshet30322f92013-07-18 13:21:52 -0700130
Fang Deng2db96762013-10-03 16:45:31 -0700131 os.makedirs(job1_results_dir)
132 utils.write_keyval(job1_results_dir, experimental_keyval)
Aviv Keshete43bccf2013-08-14 14:11:59 -0700133 arglist_1 = [autoserv_command, '-p', '-r', job1_results_dir,
Aviv Keshetad7e34e2013-08-19 18:09:27 -0700134 '-m', remote, '--no_console_prefix', '-l', 'gilbert',
135 '-c']
Aviv Keshet30322f92013-07-18 13:21:52 -0700136 subprocess.Popen(mox.And(StartsWithList(arglist_1),
Aviv Keshete43bccf2013-08-14 14:11:59 -0700137 ContainsSublist(expected_args_sublist)),
138 stdout=subprocess.PIPE,
139 stderr=subprocess.STDOUT
Aviv Keshet30322f92013-07-18 13:21:52 -0700140 ).AndReturn(mock_process_1)
Aviv Keshete43bccf2013-08-14 14:11:59 -0700141 mock_process_1.stdout.readline().AndReturn(b'')
Aviv Keshet82683132013-06-18 10:44:41 -0700142 mock_process_1.wait()
Aviv Keshet30322f92013-07-18 13:21:52 -0700143
Fang Deng2db96762013-10-03 16:45:31 -0700144 os.makedirs(job2_results_dir)
145 utils.write_keyval(job2_results_dir, experimental_keyval)
Aviv Keshete43bccf2013-08-14 14:11:59 -0700146 arglist_2 = [autoserv_command, '-p', '-r', job2_results_dir,
Aviv Keshetad7e34e2013-08-19 18:09:27 -0700147 '-m', remote, '--no_console_prefix', '-l', 'sullivan',
148 '-s']
Aviv Keshet30322f92013-07-18 13:21:52 -0700149 subprocess.Popen(mox.And(StartsWithList(arglist_2),
Aviv Keshete43bccf2013-08-14 14:11:59 -0700150 ContainsSublist(expected_args_sublist)),
151 stdout=subprocess.PIPE,
152 stderr=subprocess.STDOUT
Aviv Keshet30322f92013-07-18 13:21:52 -0700153 ).AndReturn(mock_process_2)
Aviv Keshete43bccf2013-08-14 14:11:59 -0700154 mock_process_2.stdout.readline().AndReturn(b'')
Aviv Keshet82683132013-06-18 10:44:41 -0700155 mock_process_2.wait()
Aviv Keshetd4a04302013-04-30 15:48:30 -0700156
157 # Test run_job.
158 self.mox.ReplayAll()
Christopher Wileyf6b5aae2013-07-09 10:14:02 -0700159 job_res = test_that.run_job(job1, remote, autotest_path, results_dir,
Aviv Keshet6a704f72013-09-04 14:57:43 -0700160 fast_mode, id_digits, 0, None, args)
Aviv Keshetc8824402013-06-29 20:37:30 -0700161 self.assertEqual(job_res, job1_results_dir)
Christopher Wileyf6b5aae2013-07-09 10:14:02 -0700162 job_res = test_that.run_job(job2, remote, autotest_path, results_dir,
Aviv Keshet6a704f72013-09-04 14:57:43 -0700163 fast_mode, id_digits, 0, None, args)
Aviv Keshet30322f92013-07-18 13:21:52 -0700164
Aviv Keshetc8824402013-06-29 20:37:30 -0700165 self.assertEqual(job_res, job2_results_dir)
Aviv Keshetd4a04302013-04-30 15:48:30 -0700166 self.mox.UnsetStubs()
167 self.mox.VerifyAll()
168 self.mox.ResetAll()
169
170
171 def test_perform_local_run(self):
172 afe = test_that.setup_local_afe()
173 autotest_path = 'ottotest_path'
174 suite_name = 'sweet_name'
175 remote = 'remoat'
Aviv Keshet10711962013-06-24 12:20:33 -0700176 build = 'bild'
177 board = 'bored'
Christopher Wileyf6b5aae2013-07-09 10:14:02 -0700178 fast_mode = False
Fang Deng9a841232013-09-17 16:29:14 -0700179 suite_control_files = ['c1', 'c2', 'c3', 'c4']
Aviv Keshetc8824402013-06-29 20:37:30 -0700180 results_dir = '/tmp/test_that_results_fake'
Aviv Keshet5e33c172013-07-16 05:00:49 -0700181 id_digits = 1
Fang Deng9a841232013-09-17 16:29:14 -0700182 ssh_verbosity = 2
Aviv Keshet6a704f72013-09-04 14:57:43 -0700183 ssh_options = '-F /dev/null -i /dev/null'
Aviv Keshet30322f92013-07-18 13:21:52 -0700184 args = 'matey'
Fang Dengb1da8302013-09-24 13:57:54 -0700185 ignore_deps = False
Aviv Keshetd4a04302013-04-30 15:48:30 -0700186
Aviv Keshet20b20c72014-04-02 11:54:28 -0700187 # Fake suite objects that will be returned by fetch_local_suite
188 class fake_suite(object):
189 def __init__(self, suite_control_files, hosts):
190 self._suite_control_files = suite_control_files
191 self._hosts = hosts
192
193 def schedule(self, *args, **kwargs):
194 for control_file in self._suite_control_files:
195 afe.create_job(control_file, hosts=self._hosts)
Aviv Keshetd4a04302013-04-30 15:48:30 -0700196
197 # Mock out scheduling of suite and running of jobs.
198 self.mox = mox.Mox()
Aviv Keshet1de5bc62013-08-19 14:19:26 -0700199
Aviv Keshet20b20c72014-04-02 11:54:28 -0700200 self.mox.StubOutWithMock(test_that, 'fetch_local_suite')
201 test_that.fetch_local_suite(autotest_path, mox.IgnoreArg(),
Fang Dengb1da8302013-09-24 13:57:54 -0700202 afe, remote=remote, build=build,
Aviv Keshete9170d92013-07-19 11:20:45 -0700203 board=board, results_directory=results_dir,
Aviv Keshet20b20c72014-04-02 11:54:28 -0700204 no_experimental=False,
205 ignore_deps=ignore_deps
206 ).AndReturn(fake_suite(suite_control_files, [remote]))
Aviv Keshetd4a04302013-04-30 15:48:30 -0700207 self.mox.StubOutWithMock(test_that, 'run_job')
Fang Deng9a841232013-09-17 16:29:14 -0700208 self.mox.StubOutWithMock(test_that, 'run_provisioning_job')
Fang Dengb1da8302013-09-24 13:57:54 -0700209 self.mox.StubOutWithMock(test_that, '_auto_detect_labels')
Aviv Keshetd4a04302013-04-30 15:48:30 -0700210
Fang Denga6d597a2013-10-10 13:58:42 -0700211 test_that._auto_detect_labels(afe, remote)
Fang Dengb1da8302013-09-24 13:57:54 -0700212 # Test perform_local_run. Enforce that run_provisioning_job,
213 # run_job and _auto_detect_labels are called correctly.
Fang Deng9a841232013-09-17 16:29:14 -0700214 test_that.run_provisioning_job(
215 'cros-version:' + build, remote, autotest_path,
216 results_dir, fast_mode,
217 ssh_verbosity, ssh_options,
218 False, False)
219
Aviv Keshetd4a04302013-04-30 15:48:30 -0700220 for control_file in suite_control_files:
221 test_that.run_job(mox.ContainsAttributeValue('control_file',
Christopher Wileyf6b5aae2013-07-09 10:14:02 -0700222 control_file),
Aviv Keshet5e33c172013-07-16 05:00:49 -0700223 remote, autotest_path, results_dir, fast_mode,
Aviv Keshet6a704f72013-09-04 14:57:43 -0700224 id_digits, ssh_verbosity, ssh_options,
225 args, False, False)
Aviv Keshetd4a04302013-04-30 15:48:30 -0700226 self.mox.ReplayAll()
227 test_that.perform_local_run(afe, autotest_path, ['suite:'+suite_name],
Aviv Keshet30322f92013-07-18 13:21:52 -0700228 remote, fast_mode, build=build, board=board,
Fang Denga6d597a2013-10-10 13:58:42 -0700229 ignore_deps=False,
Aviv Keshet6a704f72013-09-04 14:57:43 -0700230 ssh_verbosity=ssh_verbosity,
231 ssh_options=ssh_options,
232 args=args,
Aviv Keshete43bccf2013-08-14 14:11:59 -0700233 results_directory=results_dir)
Aviv Keshetd4a04302013-04-30 15:48:30 -0700234 self.mox.UnsetStubs()
235 self.mox.VerifyAll()
236
237
238if __name__ == '__main__':
239 unittest.main()