blob: 9161a3ece5887a4977c26c9d39145e61974c8f0d [file] [log] [blame]
jadmanski71d17502008-06-05 16:11:13 +00001#!/usr/bin/python
2
3import unittest, os
4import common
5
jadmanski3d161b02008-06-06 15:43:36 +00006from autotest_lib.client.common_lib.test_utils import mock
jadmanski71d17502008-06-05 16:11:13 +00007from autotest_lib.client.common_lib import error, utils as common_utils
8from autotest_lib.server import utils
9from autotest_lib.server.hosts import bootloader, ssh_host
10
11
12class test_bootloader_install(unittest.TestCase):
13 def setUp(self):
14 self.god = mock.mock_god()
15
16 # mock out get_server_dir
17 self.god.stub_function(utils, "get_server_dir")
18
19
20 def tearDown(self):
21 self.god.unstub_all()
22
23
24 def create_mock_sshhost(self):
25 # useful for building disposable SSHHost mocks
26 return self.god.create_mock_class(ssh_host.SSHHost, "SSHHost")
27
28
29 def create_install_boottool_mock(self, loader, dst_dir):
30 mock_install_boottool = \
31 self.god.create_mock_function("install_boottool")
32 def install_boottool():
33 loader._boottool_path = dst_dir
34 mock_install_boottool()
35 loader.install_boottool = install_boottool
36 return mock_install_boottool
37
38
39 def test_install_fails_without_host(self):
40 host = self.create_mock_sshhost()
41 loader = bootloader.Bootloader(host)
42 del host
43 self.assertRaises(error.AutoservError, loader.install_boottool)
44
45
46 def test_installs_to_tmpdir(self):
47 TMPDIR = "/unittest/tmp"
48 SERVERDIR = "/unittest/server"
49 BOOTTOOL_SRC = os.path.join(SERVERDIR, bootloader.BOOTTOOL_SRC)
50 BOOTTOOL_SRC = os.path.abspath(BOOTTOOL_SRC)
51 BOOTTOOL_DST = os.path.join(TMPDIR, "boottool")
52 # set up the recording
53 host = self.create_mock_sshhost()
54 host.get_tmp_dir.expect_call().and_return(TMPDIR)
55 utils.get_server_dir.expect_call().and_return(SERVERDIR)
56 host.send_file.expect_call(BOOTTOOL_SRC, TMPDIR)
57 # run the test
58 loader = bootloader.Bootloader(host)
59 loader.install_boottool()
60 # assert the playback is correct
61 self.god.check_playback()
62 # assert the final dest is correct
63 self.assertEquals(loader.boottool_path, BOOTTOOL_DST)
64
65
66 def test_get_path_automatically_installs(self):
67 BOOTTOOL_DST = "/unittest/tmp/boottool"
68 host = self.create_mock_sshhost()
69 loader = bootloader.Bootloader(host)
70 # mock out loader.install_boottool
71 mock_install = \
72 self.create_install_boottool_mock(loader, BOOTTOOL_DST)
73 # set up the recording
74 mock_install.expect_call()
75 # run the test
76 self.assertEquals(loader.boottool_path, BOOTTOOL_DST)
77 self.god.check_playback()
78
79
80 def test_install_is_only_called_once(self):
81 BOOTTOOL_DST = "/unittest/tmp/boottool"
82 host = self.create_mock_sshhost()
83 loader = bootloader.Bootloader(host)
84 # mock out loader.install_boottool
85 mock_install = \
86 self.create_install_boottool_mock(loader, BOOTTOOL_DST)
87 # set up the recording
88 mock_install.expect_call()
89 # run the test
90 self.assertEquals(loader.boottool_path, BOOTTOOL_DST)
91 self.god.check_playback()
92 self.assertEquals(loader.boottool_path, BOOTTOOL_DST)
93 self.god.check_playback()
94
95
96class test_bootloader_methods(unittest.TestCase):
97 def setUp(self):
98 self.god = mock.mock_god()
99 self.host = self.god.create_mock_class(ssh_host.SSHHost,
100 "SSHHost")
101 # creates a bootloader with _run_boottool mocked out
102 self.loader = bootloader.Bootloader(self.host)
103 self.god.stub_function(self.loader, "_run_boottool")
104
105
106 def tearDown(self):
107 self.god.unstub_all()
108
109
110 def expect_run_boottool(self, arg, result):
111 result = common_utils.CmdResult(stdout=result, exit_status=0)
112 self.loader._run_boottool.expect_call(arg).and_return(result)
113
114
115 def test_get_type(self):
116 # set up the recording
117 self.expect_run_boottool("--bootloader-probe", "lilo\n")
118 # run the test
119 self.assertEquals(self.loader.get_type(), "lilo")
120 self.god.check_playback()
121
122
123 def test_get_arch(self):
124 # set up the recording
125 self.expect_run_boottool("--arch-probe", "x86_64\n")
126 # run the test
127 self.assertEquals(self.loader.get_architecture(), "x86_64")
128 self.god.check_playback()
129
130
131 def test_get_default(self):
132 # set up the recording
133 self.expect_run_boottool("--default", "0\n")
134 # run the test
135 self.assertEquals(self.loader.get_default(), "0")
136 self.god.check_playback()
137
138
139 def test_get_titles(self):
140 # set up the recording
141 self.expect_run_boottool(mock.regex_comparator(
142 r"^--info all \|"), "title #1\ntitle #2\n")
143 # run the test
144 self.assertEquals(self.loader.get_titles(),
145 ["title #1", "title #2"])
146 self.god.check_playback()
147
148
149 def test_get_info_single_result(self):
150 RESULT = (
151 "index\t: 5\n"
152 "args\t: ro single\n"
153 "boot\t: (hd0,0)\n"
154 "initrd\t: /boot/initrd.img-2.6.15-23-386\n"
155 "kernel\t: /boot/vmlinuz-2.6.15-23-386\n"
156 "root\t: UUID=07D7-0714\n"
157 "savedefault\t: \n"
158 "title\t: Distro, kernel 2.6.15-23-386\n"
159 )
160 # set up the recording
161 self.expect_run_boottool("--info=5", RESULT)
162 # run the test
163 info = self.loader.get_info(5)
164 self.god.check_playback()
165 expected_info = {"index": "5", "args": "ro single",
166 "boot": "(hd0,0)",
167 "initrd": "/boot/initrd.img-2.6.15-23-386",
168 "kernel": "/boot/vmlinuz-2.6.15-23-386",
169 "root": "UUID=07D7-0714", "savedefault": "",
170 "title": "Distro, kernel 2.6.15-23-386"}
171 self.assertEquals(expected_info, info)
172
173
174 def test_get_info_missing_result(self):
175 # set up the recording
176 self.expect_run_boottool("--info=4", "")
177 # run the test
178 info = self.loader.get_info(4)
179 self.god.check_playback()
180 self.assertEquals({}, info)
181
182
183 def test_get_info_multiple_results(self):
184 RESULT = (
185 "index\t: 5\n"
186 "args\t: ro single\n"
187 "boot\t: (hd0,0)\n"
188 "initrd\t: /boot/initrd.img-2.6.15-23-386\n"
189 "kernel\t: /boot/vmlinuz-2.6.15-23-386\n"
190 "root\t: UUID=07D7-0714\n"
191 "savedefault\t: \n"
192 "title\t: Distro, kernel 2.6.15-23-386\n"
193 "\n"
194 "index\t: 7\n"
195 "args\t: ro single\n"
196 "boot\t: (hd0,0)\n"
197 "initrd\t: /boot/initrd.img-2.6.15-23-686\n"
198 "kernel\t: /boot/vmlinuz-2.6.15-23-686\n"
199 "root\t: UUID=07D7-0714\n"
200 "savedefault\t: \n"
201 "title\t: Distro, kernel 2.6.15-23-686\n"
202 )
203 # set up the recording
204 self.expect_run_boottool("--info=all", RESULT)
205 # run the test
206 info = self.loader.get_all_info()
207 self.god.check_playback()
208 expected_info = [{"index": "5", "args": "ro single",
209 "boot": "(hd0,0)",
210 "initrd": "/boot/initrd.img-2.6.15-23-386",
211 "kernel": "/boot/vmlinuz-2.6.15-23-386",
212 "root": "UUID=07D7-0714", "savedefault": "",
213 "title": "Distro, kernel 2.6.15-23-386"},
214 {"index": "7", "args": "ro single",
215 "boot": "(hd0,0)",
216 "initrd": "/boot/initrd.img-2.6.15-23-686",
217 "kernel": "/boot/vmlinuz-2.6.15-23-686",
218 "root": "UUID=07D7-0714", "savedefault": "",
219 "title": "Distro, kernel 2.6.15-23-686"}]
220 self.assertEquals(expected_info, info)
221
222
223 def test_set_default(self):
224 # set up the recording
225 self.loader._run_boottool.expect_call("--set-default=41")
226 # run the test
227 self.loader.set_default(41)
228 self.god.check_playback()
229
230
231 def test_add_args(self):
232 # set up the recording
233 self.loader._run_boottool.expect_call(
234 "--update-kernel=10 --args=\"some kernel args\"")
235 # run the test
236 self.loader.add_args(10, "some kernel args")
237 self.god.check_playback()
238
239
240 def test_remove_args(self):
241 # set up the recording
242 self.loader._run_boottool.expect_call(
243 "--update-kernel=12 --remove-args=\"some kernel args\"")
244 # run the test
245 self.loader.remove_args(12, "some kernel args")
246 self.god.check_playback()
247
248
249 def test_add_kernel_basic(self):
250 self.loader.get_titles = self.god.create_mock_function(
251 "get_titles")
252 # set up the recording
253 self.loader.get_titles.expect_call().and_return(["notmylabel"])
254 self.loader._run_boottool.expect_call(
255 "--add-kernel \"/unittest/kernels/vmlinuz\" "
256 "--title \"mylabel\" --make-default")
257 # run the test
258 self.loader.add_kernel("/unittest/kernels/vmlinuz",
259 "mylabel")
260 self.god.check_playback()
261
262
263 def test_add_kernel_adds_root(self):
264 self.loader.get_titles = self.god.create_mock_function(
265 "get_titles")
266 # set up the recording
267 self.loader.get_titles.expect_call().and_return(["notmylabel"])
268 self.loader._run_boottool.expect_call(
269 "--add-kernel \"/unittest/kernels/vmlinuz\" "
270 "--title \"mylabel\" --root \"/unittest/root\" "
271 "--make-default")
272 # run the test
273 self.loader.add_kernel("/unittest/kernels/vmlinuz",
274 "mylabel", root="/unittest/root")
275 self.god.check_playback()
276
277
278 def test_add_kernel_adds_args(self):
279 self.loader.get_titles = self.god.create_mock_function(
280 "get_titles")
281 # set up the recording
282 self.loader.get_titles.expect_call().and_return(["notmylabel"])
283 self.loader._run_boottool.expect_call(
284 "--add-kernel \"/unittest/kernels/vmlinuz\" "
285 "--title \"mylabel\" --args \"my kernel args\" "
286 "--make-default")
287 # run the test
288 self.loader.add_kernel("/unittest/kernels/vmlinuz",
289 "mylabel", args="my kernel args")
290 self.god.check_playback()
291
292
293 def test_add_kernel_adds_initrd(self):
294 self.loader.get_titles = self.god.create_mock_function(
295 "get_titles")
296 # set up the recording
297 self.loader.get_titles.expect_call().and_return(["notmylabel"])
298 self.loader._run_boottool.expect_call(
299 "--add-kernel \"/unittest/kernels/vmlinuz\" "
300 "--title \"mylabel\" --initrd \"/unittest/initrd\" "
301 "--make-default")
302 # run the test
303 self.loader.add_kernel("/unittest/kernels/vmlinuz",
304 "mylabel", initrd="/unittest/initrd")
305 self.god.check_playback()
306
307
308 def test_add_kernel_disables_make_default(self):
309 self.loader.get_titles = self.god.create_mock_function(
310 "get_titles")
311 # set up the recording
312 self.loader.get_titles.expect_call().and_return(["notmylabel"])
313 self.loader._run_boottool.expect_call(
314 "--add-kernel \"/unittest/kernels/vmlinuz\" "
315 "--title \"mylabel\"")
316 # run the test
317 self.loader.add_kernel("/unittest/kernels/vmlinuz",
318 "mylabel", default=False)
319 self.god.check_playback()
320
321
322 def test_add_kernel_removes_old(self):
323 self.loader.get_titles = self.god.create_mock_function(
324 "get_titles")
325 # set up the recording
326 self.loader.get_titles.expect_call().and_return(["mylabel"])
327 self.loader._run_boottool.expect_call(
328 "--remove-kernel \"mylabel\"")
329 self.loader._run_boottool.expect_call(
330 "--add-kernel \"/unittest/kernels/vmlinuz\" "
331 "--title \"mylabel\" --make-default")
332 # run the test
333 self.loader.add_kernel("/unittest/kernels/vmlinuz",
334 "mylabel")
335 self.god.check_playback()
336
337
338 def test_remove_kernel(self):
339 # set up the recording
340 self.loader._run_boottool.expect_call("--remove-kernel=14")
341 # run the test
342 self.loader.remove_kernel(14)
343 self.god.check_playback()
344
345
346 def test_boot_once(self):
347 # set up the recording
348 self.loader._run_boottool.expect_call(
349 "--boot-once --title=autotest")
350 # run the test
351 self.loader.boot_once("autotest")
352 self.god.check_playback()
353
354
355if __name__ == "__main__":
356 unittest.main()