summaryrefslogtreecommitdiff
path: root/server/autotest_unittest.py
blob: 14e5a145ef6688940d277d4f7c4781bdedc49137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/python

__author__ = "raphtee@google.com (Travis Miller)"

import unittest, os, tempfile
import common
from autotest_lib.server import autotest, utils, hosts, server_job
from autotest_lib.client.bin import sysinfo
from autotest_lib.client.common_lib import utils as client_utils, packages
from autotest_lib.client.common_lib.test_utils import mock


class TestBaseAutotest(unittest.TestCase):
    def setUp(self):
        # create god
        self.god = mock.mock_god()

        # create mock host object
        self.host = self.god.create_mock_class(hosts.RemoteHost, "host")
        self.host.hostname = "hostname"
        self.host.job = self.god.create_mock_class(server_job.server_job,
                                                   "job")
        self.host.job.run_test_cleanup = True
        self.host.job.last_boot_tag = 'Autotest'
        self.host.job.sysinfo = self.god.create_mock_class(
            sysinfo.sysinfo, "sysinfo")
        self.host.job.tmpdir = "/job/tmp"

        # stubs
        self.god.stub_function(utils, "get_server_dir")
        self.god.stub_function(utils, "run")
        self.god.stub_function(utils, "get")
        self.god.stub_function(utils, "read_keyval")
        self.god.stub_function(utils, "write_keyval")
        self.god.stub_function(tempfile, "mkstemp")
        self.god.stub_function(tempfile, "mktemp")
        self.god.stub_function(os, "getcwd")
        self.god.stub_function(os, "system")
        self.god.stub_function(os, "chdir")
        self.god.stub_function(os, "makedirs")
        self.god.stub_function(os, "remove")
        self.god.stub_function(os, "fdopen")
        self.god.stub_function(os.path, "exists")
        self.god.stub_function(utils, "sh_escape")
        self.god.stub_function(autotest, "open")
        self.god.stub_function(autotest.global_config.global_config,
                               "get_config_value")
        self.god.stub_class(autotest, "_Run")
        self.god.stub_class(autotest, "log_collector")


    def tearDown(self):
       self.god.unstub_all()


    def construct(self):
        # setup
        self.serverdir = "serverdir"

        # record
        utils.get_server_dir.expect_call().and_return(self.serverdir)

        # create the autotest object
        self.base_autotest = autotest.BaseAutotest(self.host)

        # stub out abspath
        self.god.stub_function(os.path, "abspath")

        # check
        self.god.check_playback()


    def test_constructor(self):
        self.construct()

        # we should check the calls
        self.god.check_playback()


    def test_install(self):
        self.construct()

        # setup
        self.god.stub_class(packages, "PackageManager")
        self.base_autotest.got = False
        location = os.path.join(self.serverdir, '../client')
        location = os.path.abspath.expect_call(location).and_return(location)

        # record
        os.getcwd.expect_call().and_return('cwd')
        os.chdir.expect_call(os.path.join(self.serverdir, '../client'))
        os.system.expect_call('tools/make_clean')
        os.chdir.expect_call('cwd')
        utils.get.expect_call(os.path.join(self.serverdir,
            '../client')).and_return('source_material')

        self.host.wait_up.expect_call(timeout=30)
        self.host.setup.expect_call()
        self.host.get_autodir.expect_call().and_return("autodir")
        self.host.set_autodir.expect_call("autodir")
        utils.sh_escape.expect_call("autodir").and_return("autodir")
        self.host.run.expect_call('mkdir -p "autodir"')
        c = autotest.global_config.global_config
        c.get_config_value.expect_call("PACKAGES",
            'fetch_location', type=list).and_return('repos')
        pkgmgr = packages.PackageManager.expect_new('autodir',
            repo_urls='repos', hostname='hostname', do_locking=False,
            run_function=self.host.run, run_function_dargs=dict(timeout=600))
        pkg_dir = os.path.join('autodir', 'packages')
        cmd = ('cd autodir && ls | grep -v "^packages$"'
               ' | xargs rm -rf && rm -rf .[^.]*')
        self.host.run.expect_call(cmd)
        pkgmgr.install_pkg.expect_call('autotest', 'client', pkg_dir,
                                       'autodir', preserve_install_dir=True)

        # run and check
        self.base_autotest.install()
        self.god.check_playback()


    def test_run(self):
        self.construct()

        # setup
        control = "control"

        # stub out install
        self.god.stub_function(self.base_autotest, "install")
        self.god.stub_class(packages, "PackageManager")

        # record
        self.base_autotest.install.expect_call(self.host)
        self.host.wait_up.expect_call(timeout=30)
        os.path.abspath.expect_call('.').and_return('.')
        run_obj = autotest._Run.expect_new(self.host, '.', None, False, False)
        tag = None
        run_obj.manual_control_file = os.path.join('autodir',
                                                   'control.%s' % tag)
        run_obj.remote_control_file = os.path.join('autodir',
                                                   'control.%s.autoserv' % tag)
        run_obj.tag = tag
        run_obj.autodir = 'autodir'
        run_obj.verify_machine.expect_call()
        run_obj.verify_machine.expect_call()
        run_obj.background = False
        debug = os.path.join('.', 'debug')
        os.makedirs.expect_call(debug)
        delete_file_list = [run_obj.remote_control_file,
                            run_obj.remote_control_file + '.state',
                            run_obj.manual_control_file,
                            run_obj.manual_control_file + '.state']
        cmd = ';'.join('rm -f ' + control for control in delete_file_list)
        self.host.run.expect_call(cmd, ignore_status=True)

        utils.get.expect_call(control).and_return("temp")

        c = autotest.global_config.global_config
        c.get_config_value.expect_call("PACKAGES",
            'fetch_location', type=list).and_return('repos')
        pkgmgr = packages.PackageManager.expect_new('autotest',
                                                     repo_urls='repos',
                                                     hostname='hostname')
        pkgmgr.repo_urls = 'repos'

        cfile = self.god.create_mock_class(file, "file")
        cfile_orig = "original control file"
        cfile_new = "job.default_boot_tag('Autotest')\n"
        cfile_new += "job.default_test_cleanup(True)\n"
        cfile_new += "job.add_repository(repos)\n"
        cfile_new += cfile_orig

        autotest.open.expect_call("temp").and_return(cfile)
        cfile.read.expect_call().and_return(cfile_orig)
        autotest.open.expect_call("temp", 'w').and_return(cfile)
        cfile.write.expect_call(cfile_new)

        self.host.job.sysinfo.serialize.expect_call().and_return(
            {"key1": 1, "key2": 2})
        tempfile.mkstemp.expect_call(dir="/job/tmp").and_return(
            (5, "/job/tmp/file1"))
        mock_temp = self.god.create_mock_class(file, "file1")
        mock_temp.write = lambda s: None
        mock_temp.close = lambda: None
        os.fdopen.expect_call(5, "w").and_return(mock_temp)
        self.host.send_file.expect_call("/job/tmp/file1",
                                        "autodir/control.None.autoserv.state")
        os.remove.expect_call("/job/tmp/file1")

        self.host.send_file.expect_call("temp", run_obj.remote_control_file)
        os.path.abspath.expect_call('temp').and_return('control_file')
        os.path.abspath.expect_call('control').and_return('control')
        os.remove.expect_call("temp")
        run_obj.execute_control.expect_call(timeout=30)
        collector = autotest.log_collector.expect_new(self.host, tag, '.')
        collector.collect_client_job_results.expect_call()

        autotest.open.expect_call('./control.None.autoserv.state').and_raises(
            Exception("File not found"))
        os.remove.expect_call('./control.None.autoserv.state').and_raises(
            Exception("File not found"))

        # run and check output
        self.base_autotest.run(control, timeout=30)
        self.god.check_playback()


class CopyLogsTest(unittest.TestCase):
    def setUp(self):
        self.god = mock.mock_god()

        self.host = self.god.create_mock_class(hosts.RemoteHost, "host")
        self.host.hostname = "testhost"

        self.god.stub_function(os.path, "exists")
        self.god.stub_function(os, "close")
        self.god.stub_function(os, "remove")
        self.god.stub_function(tempfile, "mkstemp")
        self.god.stub_function(utils, "read_keyval")
        self.god.stub_function(utils, "write_keyval")


    def tearDown(self):
        self.god.unstub_all()


    def test_prepare_for_copying_logs(self):
        self.host.get_autodir.expect_call().and_return("/autodir")
        collector = autotest.log_collector(self.host, None, "/resultsdir")
        self.god.check_playback()

        os.path.exists.expect_call("/resultsdir/keyval").and_return(True)
        tempfile.mkstemp.expect_call(".keyval_testhost").and_return(
            (10, "tmp.keyval_testhost"))
        os.close.expect_call(10)
        self.host.get_file.expect_call("/autodir/results/default/keyval",
                                       "tmp.keyval_testhost")
        self.host.get_tmp_dir.expect_call().and_return("/autotmp")
        self.host.run.expect_call(
            "mv /autodir/results/default/keyval /autotmp/keyval")

        # run and check
        keyval = collector._prepare_for_copying_logs()
        self.assertEquals(keyval, "tmp.keyval_testhost")
        self.god.check_playback()


    def test_process_copied_logs(self):
        self.host.get_autodir.expect_call().and_return("/autodir")
        collector = autotest.log_collector(self.host, None, "/resultsdir")
        self.god.check_playback()

        utils.read_keyval.expect_call("tmp.keyval_testhost").and_return(
            {"field1": "new thing", "field3": "other new thing"})
        utils.read_keyval.expect_call("/resultsdir").and_return(
            {"field1": "thing", "field2": "otherthing"})
        utils.write_keyval.expect_call("/resultsdir",
                                       {"field3": "other new thing"})
        os.remove.expect_call("tmp.keyval_testhost")

        # run and check
        collector._process_copied_logs("tmp.keyval_testhost")
        self.god.check_playback()


if __name__ == "__main__":
    unittest.main()