blob: 8dc18a9c162ea06a1031b74e0ac82e70149046ad [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
jadmanski8d631c92008-08-18 21:12:40 +00008from autotest_lib.server import utils, hosts
9from autotest_lib.server.hosts import bootloader
jadmanski71d17502008-06-05 16:11:13 +000010
11
12class test_bootloader_install(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +000013 def setUp(self):
14 self.god = mock.mock_god()
jadmanski71d17502008-06-05 16:11:13 +000015
jadmanski0afbb632008-06-06 21:10:57 +000016 # mock out get_server_dir
17 self.god.stub_function(utils, "get_server_dir")
jadmanski71d17502008-06-05 16:11:13 +000018
19
jadmanski0afbb632008-06-06 21:10:57 +000020 def tearDown(self):
21 self.god.unstub_all()
jadmanski71d17502008-06-05 16:11:13 +000022
23
jadmanski8d631c92008-08-18 21:12:40 +000024 def create_mock_host(self):
25 # useful for building disposable RemoteHost mocks
26 return self.god.create_mock_class(hosts.RemoteHost, "host")
jadmanski71d17502008-06-05 16:11:13 +000027
28
jadmanski0afbb632008-06-06 21:10:57 +000029 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
jadmanski71d17502008-06-05 16:11:13 +000037
38
jadmanski0afbb632008-06-06 21:10:57 +000039 def test_install_fails_without_host(self):
jadmanski8d631c92008-08-18 21:12:40 +000040 host = self.create_mock_host()
jadmanski0afbb632008-06-06 21:10:57 +000041 loader = bootloader.Bootloader(host)
42 del host
43 self.assertRaises(error.AutoservError, loader.install_boottool)
jadmanski71d17502008-06-05 16:11:13 +000044
45
jadmanski0afbb632008-06-06 21:10:57 +000046 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
jadmanski8d631c92008-08-18 21:12:40 +000053 host = self.create_mock_host()
jadmanski0afbb632008-06-06 21:10:57 +000054 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)
jadmanski71d17502008-06-05 16:11:13 +000064
65
jadmanski0afbb632008-06-06 21:10:57 +000066 def test_get_path_automatically_installs(self):
67 BOOTTOOL_DST = "/unittest/tmp/boottool"
jadmanski8d631c92008-08-18 21:12:40 +000068 host = self.create_mock_host()
jadmanski0afbb632008-06-06 21:10:57 +000069 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()
jadmanski71d17502008-06-05 16:11:13 +000078
79
jadmanski0afbb632008-06-06 21:10:57 +000080 def test_install_is_only_called_once(self):
81 BOOTTOOL_DST = "/unittest/tmp/boottool"
jadmanski8d631c92008-08-18 21:12:40 +000082 host = self.create_mock_host()
jadmanski0afbb632008-06-06 21:10:57 +000083 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()
jadmanski71d17502008-06-05 16:11:13 +000094
95
96class test_bootloader_methods(unittest.TestCase):
jadmanski0afbb632008-06-06 21:10:57 +000097 def setUp(self):
98 self.god = mock.mock_god()
jadmanski8d631c92008-08-18 21:12:40 +000099 self.host = self.god.create_mock_class(hosts.RemoteHost, "host")
jadmanski0afbb632008-06-06 21:10:57 +0000100 # creates a bootloader with _run_boottool mocked out
101 self.loader = bootloader.Bootloader(self.host)
102 self.god.stub_function(self.loader, "_run_boottool")
jadmanski71d17502008-06-05 16:11:13 +0000103
104
jadmanski0afbb632008-06-06 21:10:57 +0000105 def tearDown(self):
106 self.god.unstub_all()
jadmanski71d17502008-06-05 16:11:13 +0000107
108
jadmanski0afbb632008-06-06 21:10:57 +0000109 def expect_run_boottool(self, arg, result):
110 result = common_utils.CmdResult(stdout=result, exit_status=0)
111 self.loader._run_boottool.expect_call(arg).and_return(result)
jadmanski71d17502008-06-05 16:11:13 +0000112
113
jadmanski0afbb632008-06-06 21:10:57 +0000114 def test_get_type(self):
115 # set up the recording
116 self.expect_run_boottool("--bootloader-probe", "lilo\n")
117 # run the test
118 self.assertEquals(self.loader.get_type(), "lilo")
119 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000120
121
jadmanski0afbb632008-06-06 21:10:57 +0000122 def test_get_arch(self):
123 # set up the recording
124 self.expect_run_boottool("--arch-probe", "x86_64\n")
125 # run the test
126 self.assertEquals(self.loader.get_architecture(), "x86_64")
127 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000128
129
jadmanski0afbb632008-06-06 21:10:57 +0000130 def test_get_default(self):
131 # set up the recording
132 self.expect_run_boottool("--default", "0\n")
133 # run the test
134 self.assertEquals(self.loader.get_default(), "0")
135 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000136
137
jadmanski0afbb632008-06-06 21:10:57 +0000138 def test_get_titles(self):
139 # set up the recording
140 self.expect_run_boottool(mock.regex_comparator(
141 r"^--info all \|"), "title #1\ntitle #2\n")
142 # run the test
143 self.assertEquals(self.loader.get_titles(),
144 ["title #1", "title #2"])
145 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000146
147
jadmanski0afbb632008-06-06 21:10:57 +0000148 def test_get_info_single_result(self):
149 RESULT = (
150 "index\t: 5\n"
151 "args\t: ro single\n"
152 "boot\t: (hd0,0)\n"
153 "initrd\t: /boot/initrd.img-2.6.15-23-386\n"
154 "kernel\t: /boot/vmlinuz-2.6.15-23-386\n"
155 "root\t: UUID=07D7-0714\n"
156 "savedefault\t: \n"
157 "title\t: Distro, kernel 2.6.15-23-386\n"
158 )
159 # set up the recording
160 self.expect_run_boottool("--info=5", RESULT)
161 # run the test
162 info = self.loader.get_info(5)
163 self.god.check_playback()
164 expected_info = {"index": "5", "args": "ro single",
165 "boot": "(hd0,0)",
166 "initrd": "/boot/initrd.img-2.6.15-23-386",
167 "kernel": "/boot/vmlinuz-2.6.15-23-386",
168 "root": "UUID=07D7-0714", "savedefault": "",
169 "title": "Distro, kernel 2.6.15-23-386"}
170 self.assertEquals(expected_info, info)
jadmanski71d17502008-06-05 16:11:13 +0000171
172
jadmanski0afbb632008-06-06 21:10:57 +0000173 def test_get_info_missing_result(self):
174 # set up the recording
175 self.expect_run_boottool("--info=4", "")
176 # run the test
177 info = self.loader.get_info(4)
178 self.god.check_playback()
179 self.assertEquals({}, info)
jadmanski71d17502008-06-05 16:11:13 +0000180
181
jadmanski0afbb632008-06-06 21:10:57 +0000182 def test_get_info_multiple_results(self):
183 RESULT = (
184 "index\t: 5\n"
185 "args\t: ro single\n"
186 "boot\t: (hd0,0)\n"
187 "initrd\t: /boot/initrd.img-2.6.15-23-386\n"
188 "kernel\t: /boot/vmlinuz-2.6.15-23-386\n"
189 "root\t: UUID=07D7-0714\n"
190 "savedefault\t: \n"
191 "title\t: Distro, kernel 2.6.15-23-386\n"
192 "\n"
193 "index\t: 7\n"
194 "args\t: ro single\n"
195 "boot\t: (hd0,0)\n"
196 "initrd\t: /boot/initrd.img-2.6.15-23-686\n"
197 "kernel\t: /boot/vmlinuz-2.6.15-23-686\n"
198 "root\t: UUID=07D7-0714\n"
199 "savedefault\t: \n"
200 "title\t: Distro, kernel 2.6.15-23-686\n"
201 )
202 # set up the recording
203 self.expect_run_boottool("--info=all", RESULT)
204 # run the test
205 info = self.loader.get_all_info()
206 self.god.check_playback()
207 expected_info = [{"index": "5", "args": "ro single",
208 "boot": "(hd0,0)",
209 "initrd": "/boot/initrd.img-2.6.15-23-386",
210 "kernel": "/boot/vmlinuz-2.6.15-23-386",
211 "root": "UUID=07D7-0714", "savedefault": "",
212 "title": "Distro, kernel 2.6.15-23-386"},
213 {"index": "7", "args": "ro single",
214 "boot": "(hd0,0)",
215 "initrd": "/boot/initrd.img-2.6.15-23-686",
216 "kernel": "/boot/vmlinuz-2.6.15-23-686",
217 "root": "UUID=07D7-0714", "savedefault": "",
218 "title": "Distro, kernel 2.6.15-23-686"}]
219 self.assertEquals(expected_info, info)
jadmanski71d17502008-06-05 16:11:13 +0000220
221
jadmanski0afbb632008-06-06 21:10:57 +0000222 def test_set_default(self):
223 # set up the recording
224 self.loader._run_boottool.expect_call("--set-default=41")
225 # run the test
226 self.loader.set_default(41)
227 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000228
229
jadmanski0afbb632008-06-06 21:10:57 +0000230 def test_add_args(self):
231 # set up the recording
232 self.loader._run_boottool.expect_call(
233 "--update-kernel=10 --args=\"some kernel args\"")
234 # run the test
235 self.loader.add_args(10, "some kernel args")
236 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000237
238
jadmanski0afbb632008-06-06 21:10:57 +0000239 def test_remove_args(self):
240 # set up the recording
241 self.loader._run_boottool.expect_call(
242 "--update-kernel=12 --remove-args=\"some kernel args\"")
243 # run the test
244 self.loader.remove_args(12, "some kernel args")
245 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000246
247
jadmanski0afbb632008-06-06 21:10:57 +0000248 def test_add_kernel_basic(self):
249 self.loader.get_titles = self.god.create_mock_function(
250 "get_titles")
251 # set up the recording
252 self.loader.get_titles.expect_call().and_return(["notmylabel"])
253 self.loader._run_boottool.expect_call(
254 "--add-kernel \"/unittest/kernels/vmlinuz\" "
255 "--title \"mylabel\" --make-default")
256 # run the test
257 self.loader.add_kernel("/unittest/kernels/vmlinuz",
258 "mylabel")
259 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000260
261
jadmanski0afbb632008-06-06 21:10:57 +0000262 def test_add_kernel_adds_root(self):
263 self.loader.get_titles = self.god.create_mock_function(
264 "get_titles")
265 # set up the recording
266 self.loader.get_titles.expect_call().and_return(["notmylabel"])
267 self.loader._run_boottool.expect_call(
268 "--add-kernel \"/unittest/kernels/vmlinuz\" "
269 "--title \"mylabel\" --root \"/unittest/root\" "
270 "--make-default")
271 # run the test
272 self.loader.add_kernel("/unittest/kernels/vmlinuz",
273 "mylabel", root="/unittest/root")
274 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000275
276
jadmanski0afbb632008-06-06 21:10:57 +0000277 def test_add_kernel_adds_args(self):
278 self.loader.get_titles = self.god.create_mock_function(
279 "get_titles")
280 # set up the recording
281 self.loader.get_titles.expect_call().and_return(["notmylabel"])
282 self.loader._run_boottool.expect_call(
283 "--add-kernel \"/unittest/kernels/vmlinuz\" "
284 "--title \"mylabel\" --args \"my kernel args\" "
285 "--make-default")
286 # run the test
287 self.loader.add_kernel("/unittest/kernels/vmlinuz",
288 "mylabel", args="my kernel args")
289 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000290
291
jadmanski0afbb632008-06-06 21:10:57 +0000292 def test_add_kernel_adds_initrd(self):
293 self.loader.get_titles = self.god.create_mock_function(
294 "get_titles")
295 # set up the recording
296 self.loader.get_titles.expect_call().and_return(["notmylabel"])
297 self.loader._run_boottool.expect_call(
298 "--add-kernel \"/unittest/kernels/vmlinuz\" "
299 "--title \"mylabel\" --initrd \"/unittest/initrd\" "
300 "--make-default")
301 # run the test
302 self.loader.add_kernel("/unittest/kernels/vmlinuz",
303 "mylabel", initrd="/unittest/initrd")
304 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000305
306
jadmanski0afbb632008-06-06 21:10:57 +0000307 def test_add_kernel_disables_make_default(self):
308 self.loader.get_titles = self.god.create_mock_function(
309 "get_titles")
310 # set up the recording
311 self.loader.get_titles.expect_call().and_return(["notmylabel"])
312 self.loader._run_boottool.expect_call(
313 "--add-kernel \"/unittest/kernels/vmlinuz\" "
314 "--title \"mylabel\"")
315 # run the test
316 self.loader.add_kernel("/unittest/kernels/vmlinuz",
317 "mylabel", default=False)
318 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000319
320
jadmanski0afbb632008-06-06 21:10:57 +0000321 def test_add_kernel_removes_old(self):
322 self.loader.get_titles = self.god.create_mock_function(
323 "get_titles")
324 # set up the recording
325 self.loader.get_titles.expect_call().and_return(["mylabel"])
326 self.loader._run_boottool.expect_call(
327 "--remove-kernel \"mylabel\"")
328 self.loader._run_boottool.expect_call(
329 "--add-kernel \"/unittest/kernels/vmlinuz\" "
330 "--title \"mylabel\" --make-default")
331 # run the test
332 self.loader.add_kernel("/unittest/kernels/vmlinuz",
333 "mylabel")
334 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000335
336
jadmanski0afbb632008-06-06 21:10:57 +0000337 def test_remove_kernel(self):
338 # set up the recording
339 self.loader._run_boottool.expect_call("--remove-kernel=14")
340 # run the test
341 self.loader.remove_kernel(14)
342 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000343
344
jadmanski0afbb632008-06-06 21:10:57 +0000345 def test_boot_once(self):
346 # set up the recording
347 self.loader._run_boottool.expect_call(
348 "--boot-once --title=autotest")
349 # run the test
350 self.loader.boot_once("autotest")
351 self.god.check_playback()
jadmanski71d17502008-06-05 16:11:13 +0000352
353
354if __name__ == "__main__":
jadmanski0afbb632008-06-06 21:10:57 +0000355 unittest.main()