1 #!/usr/bin/python 2 3 import unittest, os 4 import common 5 6 from autotest_lib.client.common_lib.test_utils import mock 7 from autotest_lib.client.common_lib import error 8 from autotest_lib.server import utils, hosts 9 from autotest_lib.server.hosts import bootloader 10 11 12 class test_bootloader(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_host(self): 25 # useful for building disposable RemoteHost mocks 26 return self.god.create_mock_class(hosts.RemoteHost, "host") 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_host() 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_host() 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_host() 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._get_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_host() 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._get_boottool_path(), BOOTTOOL_DST) 91 self.god.check_playback() 92 self.assertEquals(loader._get_boottool_path(), BOOTTOOL_DST) 93 self.god.check_playback() 94 95 96 if __name__ == "__main__": 97 unittest.main() 98