blob: 573e19f1129eb86f4efb346351c88174d189e381 [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
mbligh3c7a1502008-07-24 18:08:47 +00007from autotest_lib.server import autotest, utils
mbligh5fe5c952008-05-27 20:14:43 +00008from autotest_lib.server.hosts import ssh_host
mbligh3c7a1502008-07-24 18:08:47 +00009from autotest_lib.client.common_lib import utils as client_utils, packages
jadmanski3d161b02008-06-06 15:43:36 +000010from autotest_lib.client.common_lib.test_utils import mock
mbligh24f8f5b2008-06-12 19:56:22 +000011import pdb
mbligh5fe5c952008-05-27 20:14:43 +000012
13class TestBaseAutotest(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +000014 def setUp(self):
15 # create god
16 self.god = mock.mock_god()
mbligh5fe5c952008-05-27 20:14:43 +000017
mbligh24f8f5b2008-06-12 19:56:22 +000018 # create mock host object
19 self.host = self.god.create_mock_class(ssh_host.SSHHost, "host")
20 self.host.hostname = "hostname"
mbligh5fe5c952008-05-27 20:14:43 +000021
mbligh24f8f5b2008-06-12 19:56:22 +000022 # stubs
mbligh3c7a1502008-07-24 18:08:47 +000023 self.god.stub_function(utils, "get_server_dir")
24 self.god.stub_function(utils, "run")
25 self.god.stub_function(utils, "get")
26 self.god.stub_function(utils, "read_keyval")
27 self.god.stub_function(utils, "write_keyval")
mbligh24f8f5b2008-06-12 19:56:22 +000028 self.god.stub_function(tempfile, "mkstemp")
29 self.god.stub_function(tempfile, "mktemp")
30 self.god.stub_function(os, "getcwd")
31 self.god.stub_function(os, "system")
32 self.god.stub_function(os, "chdir")
33 self.god.stub_function(os, "makedirs")
34 self.god.stub_function(os, "remove")
35 self.god.stub_function(os.path, "abspath")
36 self.god.stub_function(os.path, "exists")
mbligh3c7a1502008-07-24 18:08:47 +000037 self.god.stub_function(utils, "sh_escape")
38 self.god.stub_function(autotest, "open")
39 self.god.stub_function(autotest.global_config.global_config,
40 "get_config_value")
mbligh24f8f5b2008-06-12 19:56:22 +000041 self.god.stub_class(autotest, "_Run")
mbligh5fe5c952008-05-27 20:14:43 +000042
mbligh5fe5c952008-05-27 20:14:43 +000043
mbligh24f8f5b2008-06-12 19:56:22 +000044 def tearDown(self):
45 self.god.unstub_all()
mbligh5fe5c952008-05-27 20:14:43 +000046
mbligh24f8f5b2008-06-12 19:56:22 +000047
48 def construct(self):
49 # setup
50 self.serverdir = "serverdir"
51
52 # record
mbligh3c7a1502008-07-24 18:08:47 +000053 utils.get_server_dir.expect_call().and_return(self.serverdir)
mbligh5fe5c952008-05-27 20:14:43 +000054
jadmanski0afbb632008-06-06 21:10:57 +000055 # create the autotest object
56 self.base_autotest = autotest.BaseAutotest(self.host)
mbligh5fe5c952008-05-27 20:14:43 +000057
mbligh24f8f5b2008-06-12 19:56:22 +000058 # check
59 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000060
61
jadmanski0afbb632008-06-06 21:10:57 +000062 def test_constructor(self):
mbligh24f8f5b2008-06-12 19:56:22 +000063 self.construct()
64
jadmanski0afbb632008-06-06 21:10:57 +000065 # we should check the calls
66 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +000067
mbligh5fe5c952008-05-27 20:14:43 +000068
mbligh24f8f5b2008-06-12 19:56:22 +000069 def test_install(self):
70 self.construct()
71
72 # setup
mbligh3c7a1502008-07-24 18:08:47 +000073 self.god.stub_class(packages, "PackageManager")
74 self.base_autotest.got = False
75 location = os.path.join(self.serverdir, '../client')
76 location = os.path.abspath.expect_call(location).and_return(location)
jadmanski3d161b02008-06-06 15:43:36 +000077
mbligh24f8f5b2008-06-12 19:56:22 +000078 # record
mbligh3c7a1502008-07-24 18:08:47 +000079 os.getcwd.expect_call().and_return('cwd')
80 os.chdir.expect_call(os.path.join(self.serverdir, '../client'))
mbligh24f8f5b2008-06-12 19:56:22 +000081 os.system.expect_call('tools/make_clean')
mbligh3c7a1502008-07-24 18:08:47 +000082 os.chdir.expect_call('cwd')
83 utils.get.expect_call(os.path.join(self.serverdir,
84 '../client')).and_return('source_material')
85
jadmanski0afbb632008-06-06 21:10:57 +000086 self.host.wait_up.expect_call(timeout=30)
87 self.host.setup.expect_call()
mbligh24f8f5b2008-06-12 19:56:22 +000088 self.host.get_autodir.expect_call().and_return("autodir")
mbligh3c7a1502008-07-24 18:08:47 +000089 utils.sh_escape.expect_call("autodir").and_return("autodir")
mbligh24f8f5b2008-06-12 19:56:22 +000090 self.host.run.expect_call('mkdir -p "autodir"')
mbligh3c7a1502008-07-24 18:08:47 +000091 c = autotest.global_config.global_config
92 c.get_config_value.expect_call("PACKAGES",
93 'fetch_location', type=list).and_return('repos')
94 pkgmgr = packages.PackageManager.expect_new('autodir',
mblighc5ddfd12008-08-04 17:15:00 +000095 repo_urls='repos', do_locking=False, run_function=self.host.run,
mbligh3c7a1502008-07-24 18:08:47 +000096 run_function_dargs=dict(timeout=600))
97 pkg_dir = os.path.join('autodir', 'packages')
mblighc5ddfd12008-08-04 17:15:00 +000098 cmd = ('cd autodir && ls | grep -v "^packages$"'
99 ' | xargs rm -rf && rm -rf .[^.]*')
100 self.host.run.expect_call(cmd)
mbligh3c7a1502008-07-24 18:08:47 +0000101 pkgmgr.install_pkg.expect_call('autotest', 'client', pkg_dir,
102 'autodir', preserve_install_dir=True)
mbligh5fe5c952008-05-27 20:14:43 +0000103
mbligh3c7a1502008-07-24 18:08:47 +0000104 # run and check
jadmanski0afbb632008-06-06 21:10:57 +0000105 self.base_autotest.install()
jadmanski0afbb632008-06-06 21:10:57 +0000106 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000107
mbligh5fe5c952008-05-27 20:14:43 +0000108
mbligh24f8f5b2008-06-12 19:56:22 +0000109 def test_run(self):
110 self.construct()
mbligh5fe5c952008-05-27 20:14:43 +0000111
jadmanski0afbb632008-06-06 21:10:57 +0000112 # setup
mbligh24f8f5b2008-06-12 19:56:22 +0000113 control = "control"
mbligh5fe5c952008-05-27 20:14:43 +0000114
mbligh24f8f5b2008-06-12 19:56:22 +0000115 # stub out install
116 self.god.stub_function(self.base_autotest, "install")
117 self.god.stub_function(self.base_autotest, "prepare_for_copying_logs")
118 self.god.stub_function(self.base_autotest, "process_copied_logs")
119 self.god.stub_function(self.base_autotest, "postprocess_copied_logs")
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)
mbligh24f8f5b2008-06-12 19:56:22 +0000166 self.host.wait_up.expect_call(timeout=30)
mbligh3c7a1502008-07-24 18:08:47 +0000167
168 run_obj.autodir = 'autodir'
169 results = os.path.join(run_obj.autodir,
170 'results', 'default')
171 self.base_autotest.prepare_for_copying_logs.expect_call(
172 'autodir/results/default', '.', self.host).and_return('keyval_path')
173 self.host.get_file.expect_call('autodir/results/default/', '.')
174 self.base_autotest.process_copied_logs.expect_call('.',self.host,
175 'keyval_path')
mbligh24f8f5b2008-06-12 19:56:22 +0000176 self.base_autotest.postprocess_copied_logs.expect_call(results,
mbligh3c7a1502008-07-24 18:08:47 +0000177 self.host)
mbligh24f8f5b2008-06-12 19:56:22 +0000178
179 # run and check output
mbligh3c7a1502008-07-24 18:08:47 +0000180 self.base_autotest.run(control, timeout=30)
jadmanski0afbb632008-06-06 21:10:57 +0000181 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000182
mbligh3c7a1502008-07-24 18:08:47 +0000183
mbligh24f8f5b2008-06-12 19:56:22 +0000184 def test_prepare_for_copying_logs(self):
185 self.construct()
mbligh5fe5c952008-05-27 20:14:43 +0000186
mbligh24f8f5b2008-06-12 19:56:22 +0000187 # record
jadmanski0afbb632008-06-06 21:10:57 +0000188 src = "src"
189 dest = "dest"
190 keyval_path = ''
mbligh24f8f5b2008-06-12 19:56:22 +0000191 os.path.exists.expect_call(os.path.join(dest,
192 'keyval')).and_return(True)
193 tempfile.mkstemp.expect_call(
194 '.keyval_%s' % self.host.hostname).and_return((None, keyval_path))
195 self.host.get_file.expect_call(os.path.join(src, 'keyval'), keyval_path)
196 tempfile.mktemp.expect_call().and_return("temp_keyval")
197 self.host.run.expect_call('mv %s temp_keyval' %
198 os.path.join(src, 'keyval'))
mbligh5fe5c952008-05-27 20:14:43 +0000199
mbligh24f8f5b2008-06-12 19:56:22 +0000200 # run and check
201 self.base_autotest.prepare_for_copying_logs(src, dest, self.host)
jadmanski0afbb632008-06-06 21:10:57 +0000202 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000203
204
mbligh24f8f5b2008-06-12 19:56:22 +0000205 def test_process_copied_logs(self):
206 self.construct()
207
208 # record
jadmanski0afbb632008-06-06 21:10:57 +0000209 dest = "dest"
mbligh24f8f5b2008-06-12 19:56:22 +0000210 keyval_path = "keyval_path"
211 os.path.exists.expect_call(os.path.join(dest,
212 'keyval')).and_return(True)
213 old_keyval = {"version": 1, "author": "me"}
214 new_keyval = {"version": 1, "data": "foo"}
mbligh3c7a1502008-07-24 18:08:47 +0000215 utils.read_keyval.expect_call(
mbligh24f8f5b2008-06-12 19:56:22 +0000216 keyval_path).and_return(new_keyval)
mbligh3c7a1502008-07-24 18:08:47 +0000217 utils.read_keyval.expect_call(dest).and_return(old_keyval)
mbligh24f8f5b2008-06-12 19:56:22 +0000218 tmp_keyval = {}
219 for key, val in new_keyval.iteritems():
220 if key not in old_keyval:
221 tmp_keyval[key] = val
mbligh3c7a1502008-07-24 18:08:47 +0000222 utils.write_keyval.expect_call(dest, tmp_keyval)
mbligh24f8f5b2008-06-12 19:56:22 +0000223 os.remove.expect_call(keyval_path)
mbligh5fe5c952008-05-27 20:14:43 +0000224
jadmanski0afbb632008-06-06 21:10:57 +0000225 # run check
mbligh24f8f5b2008-06-12 19:56:22 +0000226 self.base_autotest.process_copied_logs(dest, self.host, keyval_path)
jadmanski0afbb632008-06-06 21:10:57 +0000227 self.god.check_playback()
mbligh5fe5c952008-05-27 20:14:43 +0000228
229
mbligh5fe5c952008-05-27 20:14:43 +0000230if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000231 unittest.main()