• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python
2#pylint: disable-msg=C0111
3__author__ = "raphtee@google.com (Travis Miller)"
4
5import unittest, os, tempfile, logging
6
7import common
8from autotest_lib.server import autotest, utils, hosts, server_job, profilers
9from autotest_lib.client.bin import sysinfo
10from autotest_lib.client.common_lib import packages
11from autotest_lib.client.common_lib import error
12from autotest_lib.client.common_lib.test_utils import mock
13
14
15class TestAutotest(unittest.TestCase):
16    def setUp(self):
17        # create god
18        self.god = mock.mock_god()
19
20        # create mock host object
21        self.host = self.god.create_mock_class(hosts.RemoteHost, "host")
22        self.host.hostname = "hostname"
23        self.host.job = self.god.create_mock_class(server_job.server_job,
24                                                   "job")
25        self.host.job.run_test_cleanup = True
26        self.host.job.sysinfo = self.god.create_mock_class(
27            sysinfo.sysinfo, "sysinfo")
28        self.host.job.profilers = self.god.create_mock_class(
29            profilers.profilers, "profilers")
30        self.host.job.profilers.add_log = {}
31        self.host.job.tmpdir = "/job/tmp"
32        self.host.job.default_profile_only = False
33        self.host.job.args = []
34        self.host.job.record = lambda *args: None
35
36        # stubs
37        self.god.stub_function(utils, "get_server_dir")
38        self.god.stub_function(utils, "run")
39        self.god.stub_function(utils, "get")
40        self.god.stub_function(utils, "read_keyval")
41        self.god.stub_function(utils, "write_keyval")
42        self.god.stub_function(utils, "system")
43        self.god.stub_function(tempfile, "mkstemp")
44        self.god.stub_function(tempfile, "mktemp")
45        self.god.stub_function(os, "getcwd")
46        self.god.stub_function(os, "system")
47        self.god.stub_function(os, "chdir")
48        self.god.stub_function(os, "makedirs")
49        self.god.stub_function(os, "remove")
50        self.god.stub_function(os, "fdopen")
51        self.god.stub_function(os.path, "exists")
52        self.god.stub_function(autotest, "open")
53        self.god.stub_function(autotest.global_config.global_config,
54                               "get_config_value")
55        self.god.stub_function(logging, "exception")
56        self.god.stub_class(autotest, "_Run")
57        self.god.stub_class(autotest, "log_collector")
58
59
60    def tearDown(self):
61        self.god.unstub_all()
62
63
64    def construct(self):
65        # setup
66        self.serverdir = "serverdir"
67
68        # record
69        utils.get_server_dir.expect_call().and_return(self.serverdir)
70
71        # create the autotest object
72        self.autotest = autotest.Autotest(self.host)
73        self.autotest.job = self.host.job
74        self.god.stub_function(self.autotest, "_install_using_send_file")
75
76        # stub out abspath
77        self.god.stub_function(os.path, "abspath")
78
79        # check
80        self.god.check_playback()
81
82
83    def record_install_prologue(self):
84        self.construct()
85
86        # setup
87        self.god.stub_class(packages, "PackageManager")
88        self.autotest.got = False
89        location = os.path.join(self.serverdir, '../client')
90        location = os.path.abspath.expect_call(location).and_return(location)
91
92        # record
93        utils.get.expect_call(os.path.join(self.serverdir,
94            '../client')).and_return('source_material')
95
96        self.host.wait_up.expect_call(timeout=30)
97        self.host.setup.expect_call()
98        self.host.get_autodir.expect_call().and_return("autodir")
99        self.host.set_autodir.expect_call("autodir")
100        self.host.run.expect_call('mkdir -p autodir')
101        self.host.run.expect_call('rm -rf autodir/results/*',
102                                  ignore_status=True)
103
104
105    def test_constructor(self):
106        self.construct()
107
108        # we should check the calls
109        self.god.check_playback()
110
111
112    def test_full_client_install(self):
113        self.record_install_prologue()
114
115        self.host.run.expect_call('rm -f "autodir/packages.checksum"')
116        c = autotest.global_config.global_config
117        c.get_config_value.expect_call('PACKAGES',
118                                       'serve_packages_from_autoserv',
119                                       type=bool).and_return(False)
120        self.host.send_file.expect_call('source_material', 'autodir',
121                                        delete_dest=True)
122        self.god.stub_function(autotest.Autotest, "_send_shadow_config")
123        autotest.Autotest._send_shadow_config.expect_call()
124
125        # run and check
126        self.autotest.install_full_client()
127        self.god.check_playback()
128
129
130    def test_autoserv_install(self):
131        self.record_install_prologue()
132
133        c = autotest.global_config.global_config
134        c.get_config_value.expect_call('PACKAGES',
135            'fetch_location', type=list, default=[]).and_return([])
136
137        os.path.exists.expect_call('/etc/cros_chroot_version').and_return(True)
138        c.get_config_value.expect_call('PACKAGES',
139                                       'serve_packages_from_autoserv',
140                                       type=bool).and_return(True)
141        self.autotest._install_using_send_file.expect_call(self.host,
142                                                           'autodir')
143        self.god.stub_function(autotest.Autotest, "_send_shadow_config")
144        autotest.Autotest._send_shadow_config.expect_call()
145        # run and check
146        self.autotest.install()
147        self.god.check_playback()
148
149
150    def test_packaging_install(self):
151        self.record_install_prologue()
152
153        c = autotest.global_config.global_config
154        c.get_config_value.expect_call('PACKAGES',
155            'fetch_location', type=list, default=[]).and_return(['repo'])
156        os.path.exists.expect_call('/etc/cros_chroot_version').and_return(True)
157        pkgmgr = packages.PackageManager.expect_new('autodir',
158            repo_urls=['repo'], hostname='hostname', do_locking=False,
159            run_function=self.host.run, run_function_dargs=dict(timeout=600))
160        pkg_dir = os.path.join('autodir', 'packages')
161        cmd = ('cd autodir && ls | grep -v "^packages$" | '
162               'grep -v "^result_tools$" | '
163               'xargs rm -rf && rm -rf .[!.]*')
164        self.host.run.expect_call(cmd)
165        pkgmgr.install_pkg.expect_call('autotest', 'client', pkg_dir,
166                                       'autodir', preserve_install_dir=True)
167
168        # run and check
169        self.autotest.install()
170        self.god.check_playback()
171
172
173    def test_run(self):
174        self.construct()
175
176        # setup
177        control = "control"
178
179        # stub out install
180        self.god.stub_function(self.autotest, "install")
181
182        # record
183        self.autotest.install.expect_call(self.host, use_packaging=True)
184        self.host.wait_up.expect_call(timeout=30)
185        os.path.abspath.expect_call('.').and_return('.')
186        run_obj = autotest._Run.expect_new(self.host, '.', None, False, False)
187        tag = None
188        run_obj.manual_control_file = os.path.join('autodir',
189                                                   'control.%s' % tag)
190        run_obj.remote_control_file = os.path.join('autodir',
191                                                   'control.%s.autoserv' % tag)
192        run_obj.tag = tag
193        run_obj.autodir = 'autodir'
194        run_obj.verify_machine.expect_call()
195        run_obj.background = False
196        debug = os.path.join('.', 'debug')
197        os.makedirs.expect_call(debug)
198        delete_file_list = [run_obj.remote_control_file,
199                            run_obj.remote_control_file + '.state',
200                            run_obj.manual_control_file,
201                            run_obj.manual_control_file + '.state']
202        cmd = ';'.join('rm -f ' + control for control in delete_file_list)
203        self.host.run.expect_call(cmd, ignore_status=True)
204
205        utils.get.expect_call(control, local_copy=True).and_return("temp")
206
207        c = autotest.global_config.global_config
208        c.get_config_value.expect_call("PACKAGES",
209            'fetch_location', type=list, default=[]).and_return(['repo'])
210
211        cfile = self.god.create_mock_class(file, "file")
212        cfile_orig = "original control file"
213        cfile_new = "args = []\njob.add_repository(['repo'])\n"
214        cfile_new += cfile_orig
215
216        os.path.exists.expect_call('/etc/cros_chroot_version').and_return(True)
217        autotest.open.expect_call("temp").and_return(cfile)
218        cfile.read.expect_call().and_return(cfile_orig)
219        autotest.open.expect_call("temp", 'w').and_return(cfile)
220        cfile.write.expect_call(cfile_new)
221
222        self.host.job.preprocess_client_state.expect_call().and_return(
223            '/job/tmp/file1')
224        self.host.send_file.expect_call(
225            "/job/tmp/file1", "autodir/control.None.autoserv.init.state")
226        os.remove.expect_call("/job/tmp/file1")
227
228        self.host.send_file.expect_call("temp", run_obj.remote_control_file)
229        os.path.abspath.expect_call('temp').and_return('control_file')
230        os.path.abspath.expect_call('control').and_return('control')
231        os.remove.expect_call("temp")
232
233        run_obj.execute_control.expect_call(timeout=30,
234                                            client_disconnect_timeout=240)
235
236        # run and check output
237        self.autotest.run(control, timeout=30)
238        self.god.check_playback()
239
240
241    def _stub_get_client_autodir_paths(self):
242        def mock_get_client_autodir_paths(cls, host):
243            return ['/some/path', '/another/path']
244        self.god.stub_with(autotest.Autotest, 'get_client_autodir_paths',
245                           classmethod(mock_get_client_autodir_paths))
246
247
248    def _expect_failed_run(self, command):
249        (self.host.run.expect_call(command)
250         .and_raises(error.AutoservRunError('dummy', object())))
251
252
253    def test_get_installed_autodir(self):
254        self._stub_get_client_autodir_paths()
255        self.host.get_autodir.expect_call().and_return(None)
256        self._expect_failed_run('test -x /some/path/bin/autotest')
257        self.host.run.expect_call('test -x /another/path/bin/autotest')
258        self.host.run.expect_call('test -w /another/path')
259
260        autodir = autotest.Autotest.get_installed_autodir(self.host)
261        self.assertEquals(autodir, '/another/path')
262
263
264    def test_get_install_dir(self):
265        self._stub_get_client_autodir_paths()
266        self.host.get_autodir.expect_call().and_return(None)
267        self._expect_failed_run('test -x /some/path/bin/autotest')
268        self._expect_failed_run('test -x /another/path/bin/autotest')
269        self._expect_failed_run('mkdir -p /some/path')
270        self.host.run.expect_call('mkdir -p /another/path')
271        self.host.run.expect_call('test -w /another/path')
272
273        install_dir = autotest.Autotest.get_install_dir(self.host)
274        self.assertEquals(install_dir, '/another/path')
275
276
277    def test_client_logger_process_line_log_copy_collection_failure(self):
278        collector = autotest.log_collector.expect_new(self.host, '', '')
279        logger = autotest.client_logger(self.host, '', '')
280        collector.collect_client_job_results.expect_call().and_raises(
281                Exception('log copy failure'))
282        logging.exception.expect_call(mock.is_string_comparator())
283        logger._process_line('AUTOTEST_TEST_COMPLETE:/autotest/fifo1')
284
285
286    def test_client_logger_process_line_log_copy_fifo_failure(self):
287        collector = autotest.log_collector.expect_new(self.host, '', '')
288        logger = autotest.client_logger(self.host, '', '')
289        collector.collect_client_job_results.expect_call()
290        self.host.run.expect_call('echo A > /autotest/fifo2').and_raises(
291                Exception('fifo failure'))
292        logging.exception.expect_call(mock.is_string_comparator())
293        logger._process_line('AUTOTEST_TEST_COMPLETE:/autotest/fifo2')
294
295
296    def test_client_logger_process_line_package_install_fifo_failure(self):
297        collector = autotest.log_collector.expect_new(self.host, '', '')
298        logger = autotest.client_logger(self.host, '', '')
299        self.god.stub_function(logger, '_send_tarball')
300
301        c = autotest.global_config.global_config
302        c.get_config_value.expect_call('PACKAGES',
303                                       'serve_packages_from_autoserv',
304                                       type=bool).and_return(True)
305        c.get_config_value.expect_call('PACKAGES',
306                                       'serve_packages_from_autoserv',
307                                       type=bool).and_return(True)
308        logger._send_tarball.expect_call('pkgname.tar.bz2', '/autotest/dest/')
309
310        self.host.run.expect_call('echo B > /autotest/fifo3').and_raises(
311                Exception('fifo failure'))
312        logging.exception.expect_call(mock.is_string_comparator())
313        logger._process_line('AUTOTEST_FETCH_PACKAGE:pkgname.tar.bz2:'
314                             '/autotest/dest/:/autotest/fifo3')
315
316
317if __name__ == "__main__":
318    unittest.main()
319