blob: a0f44c65f3560bb9eccf937f52fa70794785190a [file] [log] [blame]
mbligh24f8f5b2008-06-12 19:56:22 +00001#!/usr/bin/python
mbligh5fe5c952008-05-27 20:14:43 +00002
3__author__ = "raphtee@google.com (Travis Miller)"
4
mbligh3c7a1502008-07-24 18:08:47 +00005import unittest, os, tempfile
mbligh5fe5c952008-05-27 20:14:43 +00006import common
jadmanskib6eb2f12008-09-12 16:39:36 +00007from autotest_lib.server import autotest, utils, hosts, server_job
mbligh3c7a1502008-07-24 18:08:47 +00008from autotest_lib.client.common_lib import utils as client_utils, packages
jadmanski3d161b02008-06-06 15:43:36 +00009from autotest_lib.client.common_lib.test_utils import mock
jadmanski8d631c92008-08-18 21:12:40 +000010
mbligh5fe5c952008-05-27 20:14:43 +000011
12class TestBaseAutotest(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +000013 def setUp(self):
14 # create god
15 self.god = mock.mock_god()
mbligh5fe5c952008-05-27 20:14:43 +000016
mbligh24f8f5b2008-06-12 19:56:22 +000017 # create mock host object
jadmanski8d631c92008-08-18 21:12:40 +000018 self.host = self.god.create_mock_class(hosts.RemoteHost, "host")
mbligh24f8f5b2008-06-12 19:56:22 +000019 self.host.hostname = "hostname"
jadmanskib6eb2f12008-09-12 16:39:36 +000020 self.host.job = self.god.create_mock_class(server_job.server_job,
21 "job")
mbligh5fe5c952008-05-27 20:14:43 +000022
mbligh24f8f5b2008-06-12 19:56:22 +000023 # stubs
mbligh3c7a1502008-07-24 18:08:47 +000024 self.god.stub_function(utils, "get_server_dir")
25 self.god.stub_function(utils, "run")
26 self.god.stub_function(utils, "get")
27 self.god.stub_function(utils, "read_keyval")
28 self.god.stub_function(utils, "write_keyval")
mbligh24f8f5b2008-06-12 19:56:22 +000029 self.god.stub_function(tempfile, "mkstemp")
30 self.god.stub_function(tempfile, "mktemp")
31 self.god.stub_function(os, "getcwd")
32 self.god.stub_function(os, "system")
33 self.god.stub_function(os, "chdir")
34 self.god.stub_function(os, "makedirs")
35 self.god.stub_function(os, "remove")
36 self.god.stub_function(os.path, "abspath")
37 self.god.stub_function(os.path, "exists")
mbligh3c7a1502008-07-24 18:08:47 +000038 self.god.stub_function(utils, "sh_escape")
39 self.god.stub_function(autotest, "open")
40 self.god.stub_function(autotest.global_config.global_config,
41 "get_config_value")
mbligh24f8f5b2008-06-12 19:56:22 +000042 self.god.stub_class(autotest, "_Run")
jadmanskia1f3c202008-09-15 19:17:16 +000043 self.god.stub_class(server_job, "log_collector")
mbligh5fe5c952008-05-27 20:14:43 +000044
mbligh5fe5c952008-05-27 20:14:43 +000045
mbligh24f8f5b2008-06-12 19:56:22 +000046 def tearDown(self):
47 self.god.unstub_all()
mbligh5fe5c952008-05-27 20:14:43 +000048
mbligh24f8f5b2008-06-12 19:56:22 +000049
50 def construct(self):
51 # setup
52 self.serverdir = "serverdir"
53
54 # record
mbligh3c7a1502008-07-24 18:08:47 +000055 utils.get_server_dir.expect_call().and_return(self.serverdir)
mbligh5fe5c952008-05-27 20:14:43 +000056
jadmanski0afbb632008-06-06 21:10:57 +000057 # create the autotest object
58 self.base_autotest = autotest.BaseAutotest(self.host)
mbligh5fe5c952008-05-27 20:14:43 +000059
mbligh24f8f5b2008-06-12 19:56:22 +000060 # check
61 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000062
63
jadmanski0afbb632008-06-06 21:10:57 +000064 def test_constructor(self):
mbligh24f8f5b2008-06-12 19:56:22 +000065 self.construct()
66
jadmanski0afbb632008-06-06 21:10:57 +000067 # we should check the calls
68 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000069
mbligh5fe5c952008-05-27 20:14:43 +000070
mbligh24f8f5b2008-06-12 19:56:22 +000071 def test_install(self):
72 self.construct()
73
74 # setup
mbligh3c7a1502008-07-24 18:08:47 +000075 self.god.stub_class(packages, "PackageManager")
76 self.base_autotest.got = False
77 location = os.path.join(self.serverdir, '../client')
78 location = os.path.abspath.expect_call(location).and_return(location)
jadmanski3d161b02008-06-06 15:43:36 +000079
mbligh24f8f5b2008-06-12 19:56:22 +000080 # record
mbligh3c7a1502008-07-24 18:08:47 +000081 os.getcwd.expect_call().and_return('cwd')
82 os.chdir.expect_call(os.path.join(self.serverdir, '../client'))
mbligh24f8f5b2008-06-12 19:56:22 +000083 os.system.expect_call('tools/make_clean')
mbligh3c7a1502008-07-24 18:08:47 +000084 os.chdir.expect_call('cwd')
85 utils.get.expect_call(os.path.join(self.serverdir,
86 '../client')).and_return('source_material')
87
jadmanski0afbb632008-06-06 21:10:57 +000088 self.host.wait_up.expect_call(timeout=30)
89 self.host.setup.expect_call()
mbligh24f8f5b2008-06-12 19:56:22 +000090 self.host.get_autodir.expect_call().and_return("autodir")
mbligh0562e652008-08-20 20:11:45 +000091 self.host.set_autodir.expect_call("autodir")
mbligh3c7a1502008-07-24 18:08:47 +000092 utils.sh_escape.expect_call("autodir").and_return("autodir")
mbligh24f8f5b2008-06-12 19:56:22 +000093 self.host.run.expect_call('mkdir -p "autodir"')
mbligh3c7a1502008-07-24 18:08:47 +000094 c = autotest.global_config.global_config
95 c.get_config_value.expect_call("PACKAGES",
96 'fetch_location', type=list).and_return('repos')
97 pkgmgr = packages.PackageManager.expect_new('autodir',
mblighc5ddfd12008-08-04 17:15:00 +000098 repo_urls='repos', do_locking=False, run_function=self.host.run,
mbligh3c7a1502008-07-24 18:08:47 +000099 run_function_dargs=dict(timeout=600))
100 pkg_dir = os.path.join('autodir', 'packages')
mblighc5ddfd12008-08-04 17:15:00 +0000101 cmd = ('cd autodir && ls | grep -v "^packages$"'
102 ' | xargs rm -rf && rm -rf .[^.]*')
103 self.host.run.expect_call(cmd)
mbligh3c7a1502008-07-24 18:08:47 +0000104 pkgmgr.install_pkg.expect_call('autotest', 'client', pkg_dir,
105 'autodir', preserve_install_dir=True)
mbligh5fe5c952008-05-27 20:14:43 +0000106
mbligh3c7a1502008-07-24 18:08:47 +0000107 # run and check
jadmanski0afbb632008-06-06 21:10:57 +0000108 self.base_autotest.install()
jadmanski0afbb632008-06-06 21:10:57 +0000109 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000110
mbligh5fe5c952008-05-27 20:14:43 +0000111
mbligh24f8f5b2008-06-12 19:56:22 +0000112 def test_run(self):
113 self.construct()
mbligh5fe5c952008-05-27 20:14:43 +0000114
jadmanski0afbb632008-06-06 21:10:57 +0000115 # setup
mbligh24f8f5b2008-06-12 19:56:22 +0000116 control = "control"
mbligh5fe5c952008-05-27 20:14:43 +0000117
mbligh24f8f5b2008-06-12 19:56:22 +0000118 # stub out install
119 self.god.stub_function(self.base_autotest, "install")
mbligh5fe5c952008-05-27 20:14:43 +0000120
mbligh24f8f5b2008-06-12 19:56:22 +0000121 # record
122 self.base_autotest.install.expect_call(self.host)
123 self.host.wait_up.expect_call(timeout=30)
124 os.path.abspath.expect_call('.').and_return('.')
125 run_obj = autotest._Run.expect_new(self.host, '.', None, False)
126 tag = None
127 run_obj.manual_control_file = os.path.join('autodir',
128 'control.%s' % tag)
129 run_obj.remote_control_file = os.path.join('autodir',
130 'control.%s.autoserv' % tag)
131 run_obj.tag = tag
132 run_obj.autodir = 'autodir'
133 run_obj.verify_machine.expect_call()
134 run_obj.verify_machine.expect_call()
135 debug = os.path.join('.', 'debug')
136 os.makedirs.expect_call(debug)
137 for control in [run_obj.remote_control_file,
138 run_obj.remote_control_file + '.state',
139 run_obj.manual_control_file,
140 run_obj.manual_control_file + '.state']:
141 self.host.run.expect_call('rm -f ' + control)
142
mbligh3c7a1502008-07-24 18:08:47 +0000143 utils.get.expect_call(control).and_return("temp")
mbligh24f8f5b2008-06-12 19:56:22 +0000144
mbligh3c7a1502008-07-24 18:08:47 +0000145 cfile = self.god.create_mock_class(file, "file")
146 autotest.open.expect_call("temp", 'r').and_return(cfile)
147 cfile_orig = "original control file"
148 cfile.read.expect_call().and_return(cfile_orig)
149 cfile.close.expect_call()
150 c = autotest.global_config.global_config
151 c.get_config_value.expect_call("PACKAGES",
152 'fetch_location', type=list).and_return('repos')
153 control_file_new = []
154 control_file_new.append('job.add_repository(repos)\n')
155 control_file_new.append(cfile_orig)
156 autotest.open.expect_call("temp", 'w').and_return(cfile)
157 cfile.write.expect_call('\n'.join(control_file_new))
158 cfile.close.expect_call()
159
160 self.host.send_file.expect_call("temp", run_obj.remote_control_file)
161 os.path.abspath.expect_call('temp').and_return('control_file')
162 os.path.abspath.expect_call('autodir/control.None.state').and_return(
163 'autodir/control.None.state')
164 os.remove.expect_call("temp")
165 run_obj.execute_control.expect_call(timeout=30)
jadmanskia1f3c202008-09-15 19:17:16 +0000166 collector = server_job.log_collector.expect_new(self.host, tag, '.')
167 collector.collect_client_job_results.expect_call()
mbligh24f8f5b2008-06-12 19:56:22 +0000168
169 # run and check output
mbligh3c7a1502008-07-24 18:08:47 +0000170 self.base_autotest.run(control, timeout=30)
jadmanski0afbb632008-06-06 21:10:57 +0000171 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000172
mbligh3c7a1502008-07-24 18:08:47 +0000173
mbligh5fe5c952008-05-27 20:14:43 +0000174if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000175 unittest.main()