def test_write_hash_sum(self): NEW_HASH = "new_hash" resource_files_keeper = ResourceFilesKeeper( self.TEST_RESOURCES_DIR, self.DUMMY_UNCHANGEABLE_PACKAGE) resource_files_keeper.write_hash_sum(self.DUMMY_UNCHANGEABLE_PACKAGE, NEW_HASH) hash_sum = resource_files_keeper.read_hash_sum( self.DUMMY_UNCHANGEABLE_PACKAGE) self.assertEquals(hash_sum, NEW_HASH) # Revert to previous value resource_files_keeper.write_hash_sum(self.DUMMY_UNCHANGEABLE_PACKAGE, self.DUMMY_HASH) hash_sum = resource_files_keeper.read_hash_sum( self.DUMMY_UNCHANGEABLE_PACKAGE) self.assertEquals(hash_sum, self.DUMMY_HASH) # Test exception handling with patch("__builtin__.open") as open_mock: open_mock.side_effect = self.exc_side_effect try: resource_files_keeper.write_hash_sum("path-to-directory", self.DUMMY_HASH) self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_update_directory_archieves(self, abspath_mock, list_active_stacks_mock, list_common_services_mock, update_directory_archive_mock): list_active_stacks_mock.return_value = [self.DUMMY_UNCHANGEABLE_STACK, self.DUMMY_UNCHANGEABLE_STACK, self.DUMMY_UNCHANGEABLE_STACK] list_common_services_mock.return_value = [self.DUMMY_UNCHANGEABLE_COMMON_SERVICES, self.DUMMY_UNCHANGEABLE_COMMON_SERVICES] abspath_mock.side_effect = lambda s : s resource_files_keeper = ResourceFilesKeeper(self.TEST_RESOURCES_DIR, self.TEST_STACKS_DIR) resource_files_keeper.update_directory_archieves() self.assertEquals(pprint.pformat( update_directory_archive_mock.call_args_list), "[call('../resources/TestAmbaryServer.samples/" "dummy_stack/HIVE/package'),\n " "call('../resources/TestAmbaryServer.samples/" "dummy_stack/HIVE/package'),\n " "call('../resources/TestAmbaryServer.samples/" "dummy_stack/HIVE/package'),\n " "call('../resources/TestAmbaryServer.samples/" "dummy_common_services/HIVE/0.11.0.2.0.5.0/package'),\n " "call('../resources/TestAmbaryServer.samples/" "dummy_common_services/HIVE/0.11.0.2.0.5.0/package'),\n " "call('../resources/custom_actions'),\n " "call('../resources/host_scripts')]") pass
def test_update_directory_archieves(self, abspath_mock, list_active_stacks_mock, list_common_services_mock, update_directory_archive_mock): list_active_stacks_mock.return_value = [ self.DUMMY_UNCHANGEABLE_STACK, self.DUMMY_UNCHANGEABLE_STACK, self.DUMMY_UNCHANGEABLE_STACK ] list_common_services_mock.return_value = [ self.DUMMY_UNCHANGEABLE_COMMON_SERVICES, self.DUMMY_UNCHANGEABLE_COMMON_SERVICES ] abspath_mock.side_effect = lambda s: s resource_files_keeper = ResourceFilesKeeper(self.TEST_RESOURCES_DIR, self.TEST_STACKS_DIR) resource_files_keeper.update_directory_archieves() self.assertEquals( pprint.pformat(update_directory_archive_mock.call_args_list), "[call('../resources/TestAmbaryServer.samples/" "dummy_stack/HIVE/package'),\n " "call('../resources/TestAmbaryServer.samples/" "dummy_stack/HIVE/package'),\n " "call('../resources/TestAmbaryServer.samples/" "dummy_stack/HIVE/package'),\n " "call('../resources/TestAmbaryServer.samples/" "dummy_common_services/HIVE/0.11.0.2.0.5.0/package'),\n " "call('../resources/TestAmbaryServer.samples/" "dummy_common_services/HIVE/0.11.0.2.0.5.0/package'),\n " "call('../resources/custom_actions'),\n " "call('../resources/host_scripts')]") pass
def test_update_directory_archive(self, write_hash_sum_mock, zip_directory_mock, read_hash_sum_mock, count_hash_sum_mock, os_listdir_mock): os_listdir_mock.return_value = ['file1', 'dir1'] # Test situation when there is no saved directory hash read_hash_sum_mock.return_value = None count_hash_sum_mock.return_value = self.YA_HASH resource_files_keeper = ResourceFilesKeeper(self.TEST_RESOURCES_DIR, self.SOME_PATH) resource_files_keeper.update_directory_archive(self.SOME_PATH) self.assertTrue(read_hash_sum_mock.called) self.assertTrue(count_hash_sum_mock.called) self.assertTrue(zip_directory_mock.called) self.assertTrue(write_hash_sum_mock.called) read_hash_sum_mock.reset_mock() count_hash_sum_mock.reset_mock() zip_directory_mock.reset_mock() write_hash_sum_mock.reset_mock() # Test situation when saved directory hash == current hash read_hash_sum_mock.return_value = self.DUMMY_HASH count_hash_sum_mock.return_value = self.YA_HASH resource_files_keeper.update_directory_archive(self.SOME_PATH) self.assertTrue(read_hash_sum_mock.called) self.assertTrue(count_hash_sum_mock.called) self.assertTrue(zip_directory_mock.called) self.assertTrue(write_hash_sum_mock.called) read_hash_sum_mock.reset_mock() count_hash_sum_mock.reset_mock() zip_directory_mock.reset_mock() write_hash_sum_mock.reset_mock() # Test situation when saved directory hash == current hash read_hash_sum_mock.return_value = self.DUMMY_HASH count_hash_sum_mock.return_value = self.DUMMY_HASH resource_files_keeper.update_directory_archive(self.SOME_PATH) self.assertTrue(read_hash_sum_mock.called) self.assertTrue(count_hash_sum_mock.called) self.assertFalse(zip_directory_mock.called) self.assertFalse(write_hash_sum_mock.called) read_hash_sum_mock.reset_mock() count_hash_sum_mock.reset_mock() zip_directory_mock.reset_mock() write_hash_sum_mock.reset_mock() # Check that no saved hash file is created when zipping failed zip_directory_mock.side_effect = self.keeper_exc_side_effect read_hash_sum_mock.return_value = self.DUMMY_HASH count_hash_sum_mock.return_value = self.YA_HASH try: resource_files_keeper.update_directory_archive(self.SOME_PATH) self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_update_directory_archive(self, write_hash_sum_mock, zip_directory_mock, read_hash_sum_mock, count_hash_sum_mock): # Test situation when there is no saved directory hash read_hash_sum_mock.return_value = None count_hash_sum_mock.return_value = self.YA_HASH resource_files_keeper = ResourceFilesKeeper(self.SOME_PATH) resource_files_keeper.update_directory_archive(self.SOME_PATH) self.assertTrue(read_hash_sum_mock.called) self.assertTrue(count_hash_sum_mock.called) self.assertTrue(zip_directory_mock.called) self.assertTrue(write_hash_sum_mock.called) read_hash_sum_mock.reset_mock() count_hash_sum_mock.reset_mock() zip_directory_mock.reset_mock() write_hash_sum_mock.reset_mock() # Test situation when saved directory hash == current hash read_hash_sum_mock.return_value = self.DUMMY_HASH count_hash_sum_mock.return_value = self.YA_HASH resource_files_keeper.update_directory_archive(self.SOME_PATH) self.assertTrue(read_hash_sum_mock.called) self.assertTrue(count_hash_sum_mock.called) self.assertTrue(zip_directory_mock.called) self.assertTrue(write_hash_sum_mock.called) read_hash_sum_mock.reset_mock() count_hash_sum_mock.reset_mock() zip_directory_mock.reset_mock() write_hash_sum_mock.reset_mock() # Test situation when saved directory hash == current hash read_hash_sum_mock.return_value = self.DUMMY_HASH count_hash_sum_mock.return_value = self.DUMMY_HASH resource_files_keeper.update_directory_archive(self.SOME_PATH) self.assertTrue(read_hash_sum_mock.called) self.assertTrue(count_hash_sum_mock.called) self.assertFalse(zip_directory_mock.called) self.assertFalse(write_hash_sum_mock.called) read_hash_sum_mock.reset_mock() count_hash_sum_mock.reset_mock() zip_directory_mock.reset_mock() write_hash_sum_mock.reset_mock() # Check that no saved hash file is created when zipping failed zip_directory_mock.side_effect = self.keeper_exc_side_effect read_hash_sum_mock.return_value = self.DUMMY_HASH count_hash_sum_mock.return_value = self.YA_HASH try: resource_files_keeper.update_directory_archive(self.SOME_PATH) self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def refresh_stack_hash(properties): resources_location = get_resources_location(properties) stacks_location = get_stack_location(properties) resource_files_keeper = ResourceFilesKeeper(resources_location, stacks_location) try: print "Organizing resource files at {0}...".format(resources_location, verbose=get_verbose()) resource_files_keeper.perform_housekeeping() except KeeperException, ex: msg = "Can not organize resource files at {0}: {1}".format( resources_location, str(ex)) raise FatalException(-1, msg)
def refresh_stack_hash(properties): stack_location = get_stack_location(properties) # Hack: we determine resource dir as a parent dir for stack_location resources_location = os.path.dirname(stack_location) resource_files_keeper = ResourceFilesKeeper(resources_location) try: # print "Organizing resource files at {0}...".format(resources_location, verbose=get_verbose()) print "Organizing resource files..." resource_files_keeper.perform_housekeeping() except KeeperException, ex: msg = "Can not organize resource files at {0}: {1}".format( resources_location, str(ex)) raise FatalException(-1, msg)
def refresh_stack_hash(properties): stack_location = get_stack_location(properties) # Hack: we determine resource dir as a parent dir for stack_location resources_location = os.path.dirname(stack_location) resource_files_keeper = ResourceFilesKeeper(resources_location) try: print "Organizing resource files at {0}...".format( resources_location, verbose=get_verbose()) resource_files_keeper.perform_housekeeping() except KeeperException, ex: msg = "Can not organize resource files at {0}: {1}".format( resources_location, str(ex)) raise FatalException(-1, msg)
def test_update_directory_archieves(self, abspath_mock, list_active_stacks_mock, list_common_services_mock, update_directory_archive_mock): list_active_stacks_mock.return_value = [self.DUMMY_UNCHANGEABLE_STACK, self.DUMMY_UNCHANGEABLE_STACK, self.DUMMY_UNCHANGEABLE_STACK] list_common_services_mock.return_value = [self.DUMMY_UNCHANGEABLE_COMMON_SERVICES, self.DUMMY_UNCHANGEABLE_COMMON_SERVICES] abspath_mock.side_effect = lambda s : s resource_files_keeper = ResourceFilesKeeper(self.TEST_RESOURCES_DIR, self.TEST_STACKS_DIR) resource_files_keeper.update_directory_archieves() self.assertEquals(pprint.pformat( update_directory_archive_mock.call_args_list), self.UPDATE_DIRECTORY_ARCHIVE_CALL_LIST) pass
def test_list_common_services(self, exists_mock, glob_mock): resource_files_keeper = ResourceFilesKeeper(self.TEST_RESOURCES_DIR, self.SOME_PATH) # Test normal execution flow glob_mock.return_value = ["common_service1", "common_service2", "common_service3"] exists_mock.side_effect = [True, False, True] res = resource_files_keeper.list_common_services(self.SOME_PATH) self.assertEquals(pprint.pformat(res), "['common_service1', 'common_service3']") # Test exception handling glob_mock.side_effect = self.keeper_exc_side_effect try: resource_files_keeper.list_common_services(self.SOME_PATH) self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_count_hash_sum(self): # Test normal flow resource_files_keeper = ResourceFilesKeeper(self.TEST_RESOURCES_DIR, self.DUMMY_UNCHANGEABLE_PACKAGE) test_dir = os.path.join(self.DUMMY_UNCHANGEABLE_PACKAGE) hash_sum = resource_files_keeper.count_hash_sum(test_dir) self.assertEquals(hash_sum, self.DUMMY_UNCHANGEABLE_PACKAGE_HASH) # Test exception handling with patch("__builtin__.open") as open_mock: open_mock.side_effect = self.exc_side_effect try: resource_files_keeper.count_hash_sum(test_dir) self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_count_hash_sum(self): # Test normal flow resource_files_keeper = ResourceFilesKeeper(self.DUMMY_UNCHANGEABLE_PACKAGE) test_dir = os.path.join(self.DUMMY_UNCHANGEABLE_PACKAGE) hash_sum = resource_files_keeper.count_hash_sum(test_dir) self.assertEquals(hash_sum, self.DUMMY_UNCHANGEABLE_PACKAGE_HASH) # Test exception handling with patch("__builtin__.open") as open_mock: open_mock.side_effect = self.exc_side_effect try: resource_files_keeper.count_hash_sum(test_dir) self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_list_stacks(self, exists_mock, glob_mock): resource_files_keeper = ResourceFilesKeeper(self.SOME_PATH) # Test normal execution flow glob_mock.return_value = ["stack1", "stack2", "stack3"] exists_mock.side_effect = [True, False, True] res = resource_files_keeper.list_stacks(self.SOME_PATH) self.assertEquals(pprint.pformat(res), "['stack1', 'stack3']") # Test exception handling glob_mock.side_effect = self.keeper_exc_side_effect try: resource_files_keeper.list_stacks(self.SOME_PATH) self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_zip_directory(self): # Test normal flow resource_files_keeper = ResourceFilesKeeper( self.TEST_RESOURCES_DIR, self.DUMMY_UNCHANGEABLE_PACKAGE) resource_files_keeper.zip_directory(self.DUMMY_UNCHANGEABLE_PACKAGE) arc_file = os.path.join(self.DUMMY_UNCHANGEABLE_PACKAGE, ResourceFilesKeeper.ARCHIVE_NAME) # Arc file should not be empty arc_size = os.path.getsize(arc_file) self.assertTrue(40000 < arc_size < 50000) # After creating zip, count hash sum of dir (should not change) hash_val = resource_files_keeper.count_hash_sum( self.DUMMY_UNCHANGEABLE_PACKAGE) self.assertEquals(hash_val, self.DUMMY_UNCHANGEABLE_PACKAGE_HASH) # Remove arc file os.unlink(arc_file) # Test exception handling with patch("os.path.join") as join_mock: join_mock.side_effect = self.exc_side_effect try: resource_files_keeper.zip_directory("path-to-directory") self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_is_ignored(self): resource_files_keeper = ResourceFilesKeeper(self.DUMMY_UNCHANGEABLE_PACKAGE) self.assertTrue(resource_files_keeper.is_ignored(".hash")) self.assertTrue(resource_files_keeper.is_ignored("archive.zip")) self.assertTrue(resource_files_keeper.is_ignored("dummy.pyc")) self.assertFalse(resource_files_keeper.is_ignored("dummy.py")) self.assertFalse(resource_files_keeper.is_ignored("1.sh"))
def test_read_hash_sum(self): resource_files_keeper = ResourceFilesKeeper(self.TEST_RESOURCES_DIR, self.DUMMY_UNCHANGEABLE_PACKAGE) hash_sum = resource_files_keeper.read_hash_sum(self.DUMMY_UNCHANGEABLE_PACKAGE) self.assertEquals(hash_sum, "dummy_hash") # Test exception handling # If file exists, should rethrow exception with patch("os.path.isfile") as isfile_mock: isfile_mock.return_value = True with patch("__builtin__.open") as open_mock: open_mock.side_effect = self.exc_side_effect try: resource_files_keeper.read_hash_sum("path-to-directory") self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_read_hash_sum(self): resource_files_keeper = ResourceFilesKeeper(self.DUMMY_UNCHANGEABLE_PACKAGE) hash_sum = resource_files_keeper.read_hash_sum(self.DUMMY_UNCHANGEABLE_PACKAGE) self.assertEquals(hash_sum, "dummy_hash") # Test exception handling # If file exists, should rethrow exception with patch("os.path.isfile") as isfile_mock: isfile_mock.return_value = True with patch("__builtin__.open") as open_mock: open_mock.side_effect = self.exc_side_effect try: resource_files_keeper.read_hash_sum("path-to-directory") self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_list_active_stacks(self, is_active_stack_mock, exists_mock, glob_mock): resource_files_keeper = ResourceFilesKeeper(self.SOME_PATH) # Test normal execution flow glob_mock.return_value = ["stack1", "stack2", "stack3", "stack4", "stack5"] exists_mock.side_effect = [True, True, False, True, True] is_active_stack_mock.side_effect = [True, False, False, True] res = resource_files_keeper.list_active_stacks(self.SOME_PATH) self.assertEquals(pprint.pformat(res), "['stack1', 'stack5']") # Test exception handling glob_mock.side_effect = self.keeper_exc_side_effect try: resource_files_keeper.list_active_stacks(self.SOME_PATH) self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_zip_directory(self): # Test normal flow resource_files_keeper = ResourceFilesKeeper(self.DUMMY_UNCHANGEABLE_PACKAGE) resource_files_keeper.zip_directory(self.DUMMY_UNCHANGEABLE_PACKAGE) arc_file = os.path.join(self.DUMMY_UNCHANGEABLE_PACKAGE, ResourceFilesKeeper.ARCHIVE_NAME) # Arc file should not be empty arc_size=os.path.getsize(arc_file) self.assertTrue(40000 < arc_size < 50000) # After creating zip, count hash sum of dir (should not change) hash_val = resource_files_keeper.count_hash_sum(self.DUMMY_UNCHANGEABLE_PACKAGE) self.assertEquals(hash_val, self.DUMMY_UNCHANGEABLE_PACKAGE_HASH) # Remove arc file os.unlink(arc_file) # Test exception handling with patch("os.path.join") as join_mock: join_mock.side_effect = self.exc_side_effect try: resource_files_keeper.zip_directory("path-to-directory") self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_write_hash_sum(self): NEW_HASH = "new_hash" resource_files_keeper = ResourceFilesKeeper(self.DUMMY_UNCHANGEABLE_PACKAGE) resource_files_keeper.write_hash_sum( self.DUMMY_UNCHANGEABLE_PACKAGE, NEW_HASH) hash_sum = resource_files_keeper.read_hash_sum(self.DUMMY_UNCHANGEABLE_PACKAGE) self.assertEquals(hash_sum, NEW_HASH) # Revert to previous value resource_files_keeper.write_hash_sum( self.DUMMY_UNCHANGEABLE_PACKAGE, self.DUMMY_HASH) hash_sum = resource_files_keeper.read_hash_sum(self.DUMMY_UNCHANGEABLE_PACKAGE) self.assertEquals(hash_sum, self.DUMMY_HASH) # Test exception handling with patch("__builtin__.open") as open_mock: open_mock.side_effect = self.exc_side_effect try: resource_files_keeper.write_hash_sum("path-to-directory", self.DUMMY_HASH) self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
def test_is_active_stack(self): # Test normal flow resource_files_keeper = ResourceFilesKeeper(self.DUMMY_UNCHANGEABLE_PACKAGE) self.assertTrue( resource_files_keeper.is_active_stack( os.path.join(self.DUMMY_ACTIVE_STACK, ResourceFilesKeeper.METAINFO_XML))) self.assertFalse( resource_files_keeper.is_active_stack( os.path.join(self.DUMMY_INACTIVE_STACK, ResourceFilesKeeper.METAINFO_XML))) # Test exception handling with patch("xml.dom.minidom.parse") as parse_mock: parse_mock.side_effect = self.exc_side_effect try: resource_files_keeper.is_active_stack("path-to-xml") self.fail('KeeperException not thrown') except KeeperException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))
self.fail('Unexpected exception thrown:' + str(e)) self.assertTrue(read_hash_sum_mock.called) self.assertTrue(count_hash_sum_mock.called) self.assertTrue(zip_directory_mock.called) self.assertFalse(write_hash_sum_mock.called) read_hash_sum_mock.reset_mock() count_hash_sum_mock.reset_mock() zip_directory_mock.reset_mock() write_hash_sum_mock.reset_mock() # Test nozip option read_hash_sum_mock.return_value = None count_hash_sum_mock.return_value = self.YA_HASH resource_files_keeper = ResourceFilesKeeper(self.TEST_RESOURCES_DIR, self.SOME_PATH, nozip=True) resource_files_keeper.update_directory_archive(self.SOME_PATH) self.assertTrue(read_hash_sum_mock.called) self.assertTrue(count_hash_sum_mock.called) self.assertFalse(zip_directory_mock.called) self.assertTrue(write_hash_sum_mock.called) pass def test_count_hash_sum(self): # Test normal flow resource_files_keeper = ResourceFilesKeeper( self.TEST_RESOURCES_DIR, self.DUMMY_UNCHANGEABLE_PACKAGE) test_dir = os.path.join(self.DUMMY_UNCHANGEABLE_PACKAGE) hash_sum = resource_files_keeper.count_hash_sum(test_dir) self.assertEquals(hash_sum, self.DUMMY_UNCHANGEABLE_PACKAGE_HASH)
def test_perform_housekeeping(self, update_directory_archieves_mock): resource_files_keeper = ResourceFilesKeeper("/dummy-resources", "/dummy-path") resource_files_keeper.perform_housekeeping() update_directory_archieves_mock.assertCalled() pass
def test_perform_housekeeping(self, update_directory_archieves_mock): resource_files_keeper = ResourceFilesKeeper("/dummy-path") resource_files_keeper.perform_housekeeping() update_directory_archieves_mock.assertCalled()
except Exception, e: self.fail('Unexpected exception thrown:' + str(e)) self.assertTrue(read_hash_sum_mock.called) self.assertTrue(count_hash_sum_mock.called) self.assertTrue(zip_directory_mock.called) self.assertFalse(write_hash_sum_mock.called) read_hash_sum_mock.reset_mock() count_hash_sum_mock.reset_mock() zip_directory_mock.reset_mock() write_hash_sum_mock.reset_mock() # Test nozip option read_hash_sum_mock.return_value = None count_hash_sum_mock.return_value = self.YA_HASH resource_files_keeper = ResourceFilesKeeper(self.SOME_PATH, nozip=True) resource_files_keeper.update_directory_archive(self.SOME_PATH) self.assertTrue(read_hash_sum_mock.called) self.assertTrue(count_hash_sum_mock.called) self.assertFalse(zip_directory_mock.called) self.assertTrue(write_hash_sum_mock.called) def test_count_hash_sum(self): # Test normal flow resource_files_keeper = ResourceFilesKeeper(self.DUMMY_UNCHANGEABLE_PACKAGE) test_dir = os.path.join(self.DUMMY_UNCHANGEABLE_PACKAGE) hash_sum = resource_files_keeper.count_hash_sum(test_dir) self.assertEquals(hash_sum, self.DUMMY_UNCHANGEABLE_PACKAGE_HASH) # Test exception handling