1#!/usr/bin/python 2 3import unittest 4import common 5 6from autotest_lib.client.common_lib.test_utils import mock 7from autotest_lib.client.common_lib import boottool 8 9 10class test_boottool(unittest.TestCase): 11 def setUp(self): 12 self.god = mock.mock_god() 13 # creates a bootloader with _run_boottool mocked out 14 self.bt_mock = boottool.boottool() 15 self.god.stub_function(self.bt_mock, '_run_boottool') 16 17 18 def tearDown(self): 19 self.god.unstub_all() 20 21 22 def expect_run_boottool(self, args, result=''): 23 self.bt_mock._run_boottool.expect_call(*args).and_return(result) 24 25 26 def test_get_type(self): 27 # set up the recording 28 self.expect_run_boottool(('--bootloader-probe',), 'lilo\n') 29 # run the test 30 self.assertEquals(self.bt_mock.get_type(), 'lilo') 31 self.god.check_playback() 32 33 34 def test_get_arch(self): 35 # set up the recording 36 self.expect_run_boottool(('--arch-probe',), 'x86_64\n') 37 # run the test 38 self.assertEquals(self.bt_mock.get_architecture(), 'x86_64') 39 self.god.check_playback() 40 41 42 def test_get_default(self): 43 # set up the recording 44 self.expect_run_boottool(('--default',), '0\n') 45 # run the test 46 self.assertEquals(self.bt_mock.get_default(), 0) 47 self.god.check_playback() 48 49 50 def test_get_titles(self): 51 # set up the recording 52 self.expect_run_boottool( 53 ('--info=all',), '\nindex\t: 0\ntitle\t: title #1\n' 54 '\nindex\t: 1\ntitle\t: title #2\n') 55 # run the test 56 self.assertEquals(self.bt_mock.get_titles(), 57 ['title #1', 'title #2']) 58 self.god.check_playback() 59 60 61 def test_get_entry(self): 62 RESULT = ( 63 'index\t: 5\n' 64 'args\t: ro single\n' 65 'boot\t: (hd0,0)\n' 66 'initrd\t: /boot/initrd.img-2.6.15-23-386\n' 67 'kernel\t: /boot/vmlinuz-2.6.15-23-386\n' 68 'root\t: UUID=07D7-0714\n' 69 'savedefault\t: \n' 70 'title\t: Distro, kernel 2.6.15-23-386\n' 71 ) 72 # set up the recording 73 self.expect_run_boottool(('--info=5',), RESULT) 74 # run the test 75 info = self.bt_mock.get_entry(5) 76 self.god.check_playback() 77 expected_info = {'index': 5, 'args': 'ro single', 78 'boot': '(hd0,0)', 79 'initrd': '/boot/initrd.img-2.6.15-23-386', 80 'kernel': '/boot/vmlinuz-2.6.15-23-386', 81 'root': 'UUID=07D7-0714', 'savedefault': '', 82 'title': 'Distro, kernel 2.6.15-23-386'} 83 self.assertEquals(expected_info, info) 84 85 86 def test_get_entry_missing_result(self): 87 # set up the recording 88 self.expect_run_boottool(('--info=4',), '') 89 # run the test 90 info = self.bt_mock.get_entry(4) 91 self.god.check_playback() 92 self.assertEquals({}, info) 93 94 95 def test_get_entries(self): 96 RESULT = ( 97 'index\t: 5\n' 98 'args\t: ro single\n' 99 'boot\t: (hd0,0)\n' 100 'initrd\t: /boot/initrd.img-2.6.15-23-386\n' 101 'kernel\t: /boot/vmlinuz-2.6.15-23-386\n' 102 'root\t: UUID=07D7-0714\n' 103 'savedefault\t: \n' 104 'title\t: Distro, kernel 2.6.15-23-386\n' 105 '\n' 106 'index\t: 7\n' 107 'args\t: ro single\n' 108 'boot\t: (hd0,0)\n' 109 'initrd\t: /boot/initrd.img-2.6.15-23-686\n' 110 'kernel\t: /boot/vmlinuz-2.6.15-23-686\n' 111 'root\t: UUID=07D7-0714\n' 112 'savedefault\t: \n' 113 'title\t: Distro, kernel 2.6.15-23-686\n' 114 ) 115 # set up the recording 116 self.expect_run_boottool(('--info=all',), RESULT) 117 # run the test 118 info = self.bt_mock.get_entries() 119 self.god.check_playback() 120 expected_info = { 121 5: {'index': 5, 'args': 'ro single', 'boot': '(hd0,0)', 122 'initrd': '/boot/initrd.img-2.6.15-23-386', 123 'kernel': '/boot/vmlinuz-2.6.15-23-386', 124 'root': 'UUID=07D7-0714', 'savedefault': '', 125 'title': 'Distro, kernel 2.6.15-23-386'}, 126 7: {'index': 7, 'args': 'ro single', 'boot': '(hd0,0)', 127 'initrd': '/boot/initrd.img-2.6.15-23-686', 128 'kernel': '/boot/vmlinuz-2.6.15-23-686', 129 'root': 'UUID=07D7-0714', 'savedefault': '', 130 'title': 'Distro, kernel 2.6.15-23-686'}} 131 self.assertEquals(expected_info, info) 132 133 134 def test_set_default(self): 135 # set up the recording 136 self.expect_run_boottool(('--set-default=41',)) 137 # run the test 138 self.bt_mock.set_default(41) 139 self.god.check_playback() 140 141 142 def test_add_args(self): 143 # set up the recording 144 self.expect_run_boottool( 145 ('--update-kernel=10', '--args=some kernel args')) 146 # run the test 147 self.bt_mock.add_args(10, 'some kernel args') 148 self.god.check_playback() 149 150 151 def test_remove_args(self): 152 # set up the recording 153 self.expect_run_boottool( 154 ('--update-kernel=12', '--remove-args=some kernel args')) 155 # run the test 156 self.bt_mock.remove_args(12, 'some kernel args') 157 self.god.check_playback() 158 159 160 def setup_add_kernel(self, oldtitle, path, title, root=None, args=None, 161 initrd=None, default=False, position='end', 162 xen_hypervisor=None): 163 self.bt_mock.get_titles = self.god.create_mock_function('get_titles') 164 # set up the recording 165 self.bt_mock.get_titles.expect_call().and_return([oldtitle]) 166 if oldtitle == title: 167 self.expect_run_boottool(('--remove-kernel=%s' % oldtitle,)) 168 169 parameters = ['--add-kernel=%s' % path, '--title=%s' % title] 170 if root: 171 parameters.append('--root=%s' % root) 172 if args: 173 parameters.append('--args=%s' % args) 174 if initrd: 175 parameters.append('--initrd=%s' % initrd) 176 if default: 177 parameters.append('--make-default') 178 if position: 179 parameters.append('--position=%s' % position) 180 if self.bt_mock.get_xen_mode(): 181 parameters.append('--xen') 182 if xen_hypervisor: 183 parameters.append('--xenhyper=%s' % xen_hypervisor) 184 self.expect_run_boottool(parameters) 185 186 187 def test_add_kernel_basic(self): 188 # set up the recording 189 self.setup_add_kernel( 190 'notmylabel', '/unittest/kernels/vmlinuz', 'mylabel') 191 # run the test 192 self.bt_mock.add_kernel('/unittest/kernels/vmlinuz', 193 'mylabel') 194 self.god.check_playback() 195 196 197 def test_add_kernel_removes_old(self): 198 # set up the recording 199 self.setup_add_kernel( 200 'mylabel', '/unittest/kernels/vmlinuz', 'mylabel') 201 # run the test 202 self.bt_mock.add_kernel('/unittest/kernels/vmlinuz', 'mylabel') 203 self.god.check_playback() 204 205 206 def test_add_kernel_adds_root(self): 207 # set up the recording 208 self.setup_add_kernel( 209 'notmylabel', '/unittest/kernels/vmlinuz', 'mylabel', 210 root='/unittest/root') 211 # run the test 212 self.bt_mock.add_kernel('/unittest/kernels/vmlinuz', 213 'mylabel', root='/unittest/root') 214 self.god.check_playback() 215 216 217 def test_add_kernel_adds_args(self): 218 # set up the recording 219 self.setup_add_kernel( 220 'notmylabel', '/unittest/kernels/vmlinuz', 'mylabel', 221 args='my kernel args') 222 # run the test 223 self.bt_mock.add_kernel('/unittest/kernels/vmlinuz', 224 'mylabel', args='my kernel args') 225 self.god.check_playback() 226 227 228 def test_add_kernel_args_remove_duplicates(self): 229 # set up the recording 230 self.setup_add_kernel( 231 'notmylabel', '/unittest/kernels/vmlinuz', 'mylabel', 232 args='param2 param1') 233 # run the test 234 self.bt_mock.add_kernel('/unittest/kernels/vmlinuz', 235 'mylabel', args='param1 param2 param1') 236 self.god.check_playback() 237 238 239 def test_add_kernel_adds_initrd(self): 240 # set up the recording 241 self.setup_add_kernel( 242 'notmylabel', '/unittest/kernels/vmlinuz', 'mylabel', 243 initrd='/unittest/initrd') 244 # run the test 245 self.bt_mock.add_kernel('/unittest/kernels/vmlinuz', 246 'mylabel', initrd='/unittest/initrd') 247 self.god.check_playback() 248 249 250 def test_add_kernel_enables_make_default(self): 251 # set up the recording 252 self.setup_add_kernel( 253 'notmylabel', '/unittest/kernels/vmlinuz', 'mylabel', 254 default=True) 255 # run the test 256 self.bt_mock.add_kernel('/unittest/kernels/vmlinuz', 257 'mylabel', default=True) 258 self.god.check_playback() 259 260 261 def test_add_kernel_position(self): 262 # set up the recording 263 self.setup_add_kernel( 264 'notmylabel', '/unittest/kernels/vmlinuz', 'mylabel', 265 position=5) 266 # run the test 267 self.bt_mock.add_kernel('/unittest/kernels/vmlinuz', 268 'mylabel', position=5) 269 self.god.check_playback() 270 271 272 def test_remove_kernel(self): 273 # set up the recording 274 self.expect_run_boottool(('--remove-kernel=14',)) 275 # run the test 276 self.bt_mock.remove_kernel(14) 277 self.god.check_playback() 278 279 280 def test_boot_once(self): 281 # set up the recording 282 self.expect_run_boottool(('--boot-once', '--title=autotest')) 283 # run the test 284 self.bt_mock.boot_once('autotest') 285 self.god.check_playback() 286 287 288 def test_enable_xen(self): 289 self.bt_mock.enable_xen_mode() 290 self.assertTrue(self.bt_mock.get_xen_mode()) 291 292 293 def test_disable_xen(self): 294 self.bt_mock.disable_xen_mode() 295 self.assertFalse(self.bt_mock.get_xen_mode()) 296 297 298 def test_add_kernel_xen(self): 299 # set up the recording 300 self.bt_mock.enable_xen_mode() 301 self.setup_add_kernel( 302 'notmylabel', '/unittest/kernels/vmlinuz', 'mylabel', 303 xen_hypervisor='xen_image') 304 # run the test 305 self.bt_mock.add_kernel('/unittest/kernels/vmlinuz', 306 'mylabel', xen_hypervisor='xen_image') 307 self.god.check_playback() 308 309 310if __name__ == '__main__': 311 unittest.main() 312