def setUp(self): from ots.worker.conductor.hardware import Hardware from ots.worker.conductor.executor import TestRunData as TestRunData self.config = _conductor_config_simple() self.testrun = TestRunData(Options(), config=self.config) self.mock_hw = Mock_Hardware(self.testrun) self.real_hw = Hardware( self.testrun) #for testing methods stubbed out in mock_hw
def setUp(self): from ots.worker.conductor.hardware import Hardware from ots.worker.conductor.executor import TestRunData as TestRunData self.config = _conductor_config_simple() self.testrun = TestRunData(Options(), config=self.config) self.mock_hw = Mock_Hardware(self.testrun) self.real_hw = Hardware(self.testrun) # for testing methods stubbed out in mock_hw
class TestHardware(unittest.TestCase): def setUp(self): from ots.worker.conductor.hardware import Hardware from ots.worker.conductor.executor import TestRunData as TestRunData self.config = _conductor_config_simple() self.testrun = TestRunData(Options(), config=self.config) self.mock_hw = Mock_Hardware(self.testrun) self.real_hw = Hardware(self.testrun) # for testing methods stubbed out in mock_hw def test_str(self): self.assertEquals(str(self.mock_hw), "Hardware") def test_get_commands_to_show_test_environment(self): zipped = self.mock_hw.get_commands_to_show_test_environment() self.assertTrue(type(zipped) == type(list())) def test_get_command_to_copy_file(self): cmd = self.mock_hw.get_command_to_copy_file("xxx", "yyy") self.assertTrue(cmd.find("xxx") != -1 and cmd.find("yyy") != -1) def test_get_command_to_copy_testdef(self): cmd = self.mock_hw.get_command_to_copy_testdef() self.assertTrue(cmd) def test_get_command_to_copy_results(self): cmd = self.mock_hw.get_command_to_copy_results() self.assertTrue(cmd) def test_get_command_to_find_test_packages(self): cmd = self.mock_hw.get_command_to_find_test_packages() self.assertTrue(cmd.find("dpkg") != -1) def test_get_command_to_list_installed_packages(self): cmd = self.mock_hw.get_command_to_list_installed_packages() self.assertTrue(cmd.find("dpkg") != -1) def test_fetch_file(self): url = "http://my.fake.server.com/path/filename" path = self.mock_hw._fetch_file(url) # urllib.urlretrieve and delete_file are stubbed out self.assertTrue(path.find("/tmp/tmp") == 0) self.assertTrue(path.find("/filename") > 0) def test_fetch_flasher(self): self.testrun.flasher_url = "http://my.fake.server.com/path/flasher" self.mock_hw._fetch_flasher() def test_fetch_release(self): self.testrun.image_url = "http://my.fake.server.com/path/image.bin" # we get OSError because file does not exist self.assertRaises(OSError, self.mock_hw._fetch_release) def test_fetch_content_image(self): self.testrun.content_image_url = "http://my.fake.server.com/path/image.bin" expected_path = os.path.join(self.config["tmp_path"], "image.bin") # we get OSError because file does not exist self.assertRaises(OSError, self.mock_hw._fetch_content_image) def test_cleanup(self): self.mock_hw.cleanup() def test_raise_download_error(self): self.assertRaises(ConductorError, self.mock_hw._raise_download_error, "") def test_flash_with_dontflash_option(self): self.testrun.dontflash = True self.mock_hw.prepare() # returns doing nothing def test_flash(self): # we get OSError because first image file does not exist. # TODO: improve when file size checks removed self.assertRaises(OSError, self.mock_hw.prepare) def test__flash(self): # testing private method self.mock_hw._flash() # all stubbed out, so expecting no exceptions # Below tests use real Hardware class instead of mock: (Add here all you can!) def test_read_file(self): content = self.real_hw._read_file(PATH_TO_TEST_CONF_FILE) expected_content = open(PATH_TO_TEST_CONF_FILE, "r").read() self.assertEquals(content, expected_content) def test_delete_file_eats_exceptions(self): self.real_hw._delete_file("non/existing/file") # shouldn't raise exception self.real_hw._delete_file("/") # existing dir, shouldn't raise exception self.real_hw._delete_file(None) # deleting not attempted self.real_hw._delete_file("") # deleting not attempted def test_md5_digest(self): digest = self.real_hw._md5_digest("any string") self.assertEquals(len(digest), 16) def test_md5_valid(self): # MD5 valid: self.assertEquals(self.real_hw._md5_valid(EMPTY_FILE, MD5_FOR_EMPTY_STRING), True) # MD5 invalid: # mock returns "" as file content but md5 for "" is a 16-byte string self.assertEquals(self.mock_hw._md5_valid("ununsed", "unused"), False) def test_add_execute_privileges(self): self.assertTrue(self.real_hw._add_execute_privileges(NON_EXISTING_FILE) != 0) def test_parse_installed_packages(self): lines = "ii mypackage-tests 1.2.3 My very special tests\n" + "ii someotherpackage 0.0.1 Something\n" self.assertEquals(self.real_hw.parse_installed_packages(lines), ["mypackage-tests"]) def test_parse_packages_with_file(self): lines = ( "somepackage: /usr/share/somepackage/tests.xml\n" + "mypackage-tests: /usr/share/mypackage-tests/tests.xml\n" ) self.assertEquals(self.real_hw.parse_packages_with_file(lines), ["mypackage-tests"]) def _mock_flash_using_software_updater(self, error): def _mocked_flasher_module(flasher="", device_n="", host_ip="", device_ip="", custom_options=dict()): return Mock_Flasher(error) self.real_hw.testrun.flasher_module = _mocked_flasher_module self.real_hw.flasher_path = None def _assert_message_raised_by_flash(self, message): try: self.real_hw._flash() except ConductorError, conductor_error: self.assertEqual(conductor_error.error_info, message) else:
class TestHardware(unittest.TestCase): def setUp(self): from ots.worker.conductor.hardware import Hardware from ots.worker.conductor.executor import TestRunData as TestRunData self.config = _conductor_config_simple() self.testrun = TestRunData(Options(), config=self.config) self.mock_hw = Mock_Hardware(self.testrun) self.real_hw = Hardware( self.testrun) #for testing methods stubbed out in mock_hw def test_str(self): self.assertEquals(str(self.mock_hw), "Hardware") def test_get_commands_to_show_test_environment(self): zipped = self.mock_hw.get_commands_to_show_test_environment() self.assertTrue(type(zipped) == type(list())) def test_get_command_to_copy_file(self): cmd = self.mock_hw.get_command_to_copy_file("xxx", "yyy") self.assertTrue(cmd.find("xxx") != -1 and cmd.find("yyy") != -1) def test_get_command_to_copy_testdef(self): cmd = self.mock_hw.get_command_to_copy_testdef() self.assertTrue(cmd) def test_get_command_to_copy_results(self): cmd = self.mock_hw.get_command_to_copy_results() self.assertTrue(cmd) def test_get_command_to_find_test_packages(self): cmd = self.mock_hw.get_command_to_find_test_packages() self.assertTrue(cmd.find('dpkg') != -1) def test_get_command_to_list_installed_packages(self): cmd = self.mock_hw.get_command_to_list_installed_packages() self.assertTrue(cmd.find('dpkg') != -1) def test_fetch_file(self): url = "http://my.fake.server.com/path/filename" path = self.mock_hw._fetch_file( url) #urllib.urlretrieve and delete_file are stubbed out self.assertTrue(path.find("/tmp/tmp") == 0) self.assertTrue(path.find("/filename") > 0) def test_fetch_flasher(self): self.testrun.flasher_url = "http://my.fake.server.com/path/flasher" self.mock_hw._fetch_flasher() def test_fetch_release(self): self.testrun.image_url = "http://my.fake.server.com/path/image.bin" #we get OSError because file does not exist self.assertRaises(OSError, self.mock_hw._fetch_release) def test_fetch_content_image(self): self.testrun.content_image_url = "http://my.fake.server.com/path/image.bin" expected_path = os.path.join(self.config['tmp_path'], "image.bin") #we get OSError because file does not exist self.assertRaises(OSError, self.mock_hw._fetch_content_image) def test_cleanup(self): self.mock_hw.cleanup() def test_raise_download_error(self): self.assertRaises(ConductorError, self.mock_hw._raise_download_error, "") def test_flash_with_dontflash_option(self): self.testrun.dontflash = True self.mock_hw.prepare() #returns doing nothing def test_flash(self): #we get OSError because first image file does not exist. #TODO: improve when file size checks removed self.assertRaises(OSError, self.mock_hw.prepare) def test__flash(self): #testing private method self.mock_hw._flash() #all stubbed out, so expecting no exceptions # Below tests use real Hardware class instead of mock: (Add here all you can!) def test_read_file(self): content = self.real_hw._read_file(PATH_TO_TEST_CONF_FILE) expected_content = open(PATH_TO_TEST_CONF_FILE, "r").read() self.assertEquals(content, expected_content) def test_delete_file_eats_exceptions(self): self.real_hw._delete_file( "non/existing/file") #shouldn't raise exception self.real_hw._delete_file( "/") #existing dir, shouldn't raise exception self.real_hw._delete_file(None) #deleting not attempted self.real_hw._delete_file("") #deleting not attempted def test_md5_digest(self): digest = self.real_hw._md5_digest("any string") self.assertEquals(len(digest), 16) def test_md5_valid(self): #MD5 valid: self.assertEquals( self.real_hw._md5_valid(EMPTY_FILE, MD5_FOR_EMPTY_STRING), True) #MD5 invalid: #mock returns "" as file content but md5 for "" is a 16-byte string self.assertEquals(self.mock_hw._md5_valid("ununsed", "unused"), False) def test_add_execute_privileges(self): self.assertTrue( self.real_hw._add_execute_privileges(NON_EXISTING_FILE) != 0) def test_parse_installed_packages(self): lines = "ii mypackage-tests 1.2.3 My very special tests\n"+\ "ii someotherpackage 0.0.1 Something\n" self.assertEquals(self.real_hw.parse_installed_packages(lines), ["mypackage-tests"]) def test_parse_packages_with_file(self): lines = "somepackage: /usr/share/somepackage/tests.xml\n"+\ "mypackage-tests: /usr/share/mypackage-tests/tests.xml\n" self.assertEquals(self.real_hw.parse_packages_with_file(lines), ["mypackage-tests"]) def _mock_flash_using_software_updater(self, error): def _mocked_flasher_module(flasher="", device_n="", host_ip="", device_ip="", custom_options=dict()): return Mock_Flasher(error) self.real_hw.testrun.flasher_module = _mocked_flasher_module self.real_hw.flasher_path = None def _assert_message_raised_by_flash(self, message): try: self.real_hw._flash() except ConductorError, conductor_error: self.assertEqual(conductor_error.error_info, message) else: