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