def test_dump_command_to_json(self, FileCache_mock, unlink_mock, isfile_mock, hostname_mock, decompress_cluster_host_info_mock): FileCache_mock.return_value = None hostname_mock.return_value = "test.hst" command = { 'commandType': 'EXECUTION_COMMAND', 'role': u'DATANODE', 'roleCommand': u'INSTALL', 'commandId': '1-1', 'taskId': 3, 'clusterName': u'cc', 'serviceName': u'HDFS', 'configurations':{'global' : {}}, 'configurationTags':{'global' : { 'tag': 'v1' }}, 'clusterHostInfo':{'namenode_host' : ['1'], 'slave_hosts' : ['0', '1'], 'all_hosts' : ['h1.hortonworks.com', 'h2.hortonworks.com'], 'all_ping_ports': ['8670:0,1']}, 'hostLevelParams':{} } decompress_cluster_host_info_mock.return_value = {'namenode_host' : ['h2.hortonworks.com'], 'slave_hosts' : ['h1.hortonworks.com', 'h2.hortonworks.com'], 'all_hosts' : ['h1.hortonworks.com', 'h2.hortonworks.com'], 'all_ping_ports': ['8670', '8670']} config = AmbariConfig() tempdir = tempfile.gettempdir() config.set('agent', 'prefix', tempdir) dummy_controller = MagicMock() orchestrator = CustomServiceOrchestrator(config, dummy_controller) isfile_mock.return_value = True # Test dumping EXECUTION_COMMAND json_file = orchestrator.dump_command_to_json(command) self.assertTrue(os.path.exists(json_file)) self.assertTrue(os.path.getsize(json_file) > 0) if get_platform() != PLATFORM_WINDOWS: self.assertEqual(oct(os.stat(json_file).st_mode & 0777), '0600') self.assertTrue(json_file.endswith("command-3.json")) self.assertTrue(decompress_cluster_host_info_mock.called) os.unlink(json_file) # Test dumping STATUS_COMMAND command['commandType']='STATUS_COMMAND' decompress_cluster_host_info_mock.reset_mock() json_file = orchestrator.dump_command_to_json(command) self.assertTrue(os.path.exists(json_file)) self.assertTrue(os.path.getsize(json_file) > 0) if get_platform() != PLATFORM_WINDOWS: self.assertEqual(oct(os.stat(json_file).st_mode & 0777), '0600') self.assertTrue(json_file.endswith("status_command.json")) self.assertFalse(decompress_cluster_host_info_mock.called) os.unlink(json_file) # Testing side effect of dump_command_to_json self.assertEquals(command['public_hostname'], "test.hst") self.assertEquals(command['agentConfigParams']['agent']['parallel_execution'], 0) self.assertTrue(unlink_mock.called)
def main(): test_mask = None if len(sys.argv) >= 2: test_mask = sys.argv[1] logger.info('------------------------------------------------------------------------') logger.info('PYTHON AGENT TESTS') logger.info('------------------------------------------------------------------------') runner = unittest.TextTestRunner(verbosity=2, stream=sys.stdout) suite = all_tests_suite(test_mask) status = runner.run(suite).wasSuccessful() if not status: logger.error('-----------------------------------------------------------------------') logger.error('Python unit tests failed') logger.error('Find detailed logs in ' + path) logger.error('-----------------------------------------------------------------------') if get_platform() == PLATFORM_WINDOWS: os._exit(1) else: exit(1) else: logger.info('------------------------------------------------------------------------') logger.info('Python unit tests finished successfully') logger.info('------------------------------------------------------------------------')
See the License for the specific language governing permissions and limitations under the License. ''' import os import tempfile import shutil from unittest import TestCase import ConfigParser import security from security import CertificateManager from ambari_agent import AmbariConfig from mock.mock import patch, MagicMock from ambari_commons import OSCheck from only_for_platform import only_for_platform, get_platform, PLATFORM_LINUX, PLATFORM_WINDOWS if get_platform() != PLATFORM_WINDOWS: os_distro_value = ('Suse','11','Final') else: os_distro_value = ('win2012serverr2','6.3','WindowsServer') class TestCertGeneration(TestCase): @patch.object(OSCheck, "os_distribution", new = MagicMock(return_value = os_distro_value)) def setUp(self): self.tmpdir = tempfile.mkdtemp() config = AmbariConfig.AmbariConfig() #config.add_section('server') config.set('server', 'hostname', 'example.com') config.set('server', 'url_port', '777') #config.add_section('security') config.set('security', 'keysdir', self.tmpdir) config.set('security', 'server_crt', 'ca.crt')
import install_packages from mock.mock import patch from mock.mock import MagicMock from stacks.utils.RMFTestCase import * from mock.mock import patch, MagicMock from resource_management.core.base import Resource from resource_management.core.exceptions import Fail OLD_VERSION_STUB = '2.1.0.0-400' VERSION_STUB_WITHOUT_BUILD_NUMBER = '2.2.0.1' VERSION_STUB = '2.2.0.1-885' from only_for_platform import get_platform, not_for_platform, only_for_platform, os_distro_value, PLATFORM_WINDOWS if get_platform() != PLATFORM_WINDOWS: import pty subproc_mock = MagicMock() subproc_mock.return_value = MagicMock() subproc_stdout = MagicMock() subproc_mock.return_value.stdout = subproc_stdout @not_for_platform(PLATFORM_WINDOWS) @patch.object(os, "read", new=MagicMock(return_value=None)) @patch.object(select, "select", new=MagicMock(return_value=([subproc_stdout], None, None))) @patch("pty.openpty", new = MagicMock(return_value=(1,5))) @patch.object(os, "close", new=MagicMock()) @patch.object(subprocess, "Popen", new=subproc_mock) class TestInstallPackages(RMFTestCase):
class TestResourceFilesKeeper(TestCase): TEST_RESOURCES_DIR = ".." + os.sep + "resources" TEST_STACKS_DIR = ".." + os.sep + "resources" + os.sep + "stacks" # Stack that is not expected to change DUMMY_UNCHANGEABLE_STACK = ".." + os.sep + "resources" + os.sep + "TestAmbaryServer.samples" + os.sep + \ "dummy_stack" + os.sep + "HIVE" DUMMY_ACTIVE_STACK = ".." + os.sep + "resources" + os.sep + "TestAmbaryServer.samples" + os.sep + \ "active_stack" DUMMY_INACTIVE_STACK = ".." + os.sep + "resources" + os.sep + "TestAmbaryServer.samples" + os.sep + \ "inactive_stack" DUMMY_UNCHANGEABLE_PACKAGE = os.path.join(DUMMY_UNCHANGEABLE_STACK, ResourceFilesKeeper.PACKAGE_DIR) if get_platform() != PLATFORM_WINDOWS: DUMMY_UNCHANGEABLE_PACKAGE_HASH = "11c9ed5f7987b41ce5d7adaedd6dd08c9cc9b418" else: DUMMY_UNCHANGEABLE_PACKAGE_HASH = "2e438f4f9862420ed8930a56b8809b8aca359e87" DUMMY_HASH = "dummy_hash" YA_HASH = "yet_another_hash" SOME_PATH = "some-path" DUMMY_UNCHANGEABLE_COMMON_SERVICES=".." + os.sep + "resources" + os.sep + "TestAmbaryServer.samples" + os.sep + \ "dummy_common_services" + os.sep + "HIVE" + os.sep + "0.11.0.2.0.5.0" DUMMY_UNCHANGEABLE_COMMON_SERVICES_PACKAGE = os.path.join( DUMMY_UNCHANGEABLE_COMMON_SERVICES, ResourceFilesKeeper.PACKAGE_DIR) if get_platform() != PLATFORM_WINDOWS: UPDATE_DIRECTORY_ARCHIVE_CALL_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')]" else: UPDATE_DIRECTORY_ARCHIVE_CALL_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')]" def setUp(self): logging.basicConfig(level=logging.ERROR) @patch.object(ResourceFilesKeeper, "update_directory_archieves") def test_perform_housekeeping(self, update_directory_archieves_mock): resource_files_keeper = ResourceFilesKeeper(os.sep + "dummy-resources", os.sep + "dummy-path") resource_files_keeper.perform_housekeeping() update_directory_archieves_mock.assertCalled() pass @patch.object(ResourceFilesKeeper, "update_directory_archive") @patch.object(ResourceFilesKeeper, "list_common_services") @patch.object(ResourceFilesKeeper, "list_stacks") @patch("os.path.abspath") 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 @patch("glob.glob") @patch("os.path.exists") def test_list_stacks(self, exists_mock, glob_mock): resource_files_keeper = ResourceFilesKeeper(self.TEST_RESOURCES_DIR, 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_dump_command_to_json(self, FileCache_mock, unlink_mock, isfile_mock, hostname_mock, decompress_cluster_host_info_mock): FileCache_mock.return_value = None hostname_mock.return_value = "test.hst" command = { 'commandType': 'EXECUTION_COMMAND', 'role': u'DATANODE', 'roleCommand': u'INSTALL', 'commandId': '1-1', 'taskId': 3, 'clusterName': u'cc', 'serviceName': u'HDFS', 'configurations': { 'global': {} }, 'configurationTags': { 'global': { 'tag': 'v1' } }, 'clusterHostInfo': { 'namenode_host': ['1'], 'slave_hosts': ['0', '1'], 'all_hosts': ['h1.hortonworks.com', 'h2.hortonworks.com'], 'all_ping_ports': ['8670:0,1'] }, 'hostLevelParams': {} } decompress_cluster_host_info_mock.return_value = { 'namenode_host': ['h2.hortonworks.com'], 'slave_hosts': ['h1.hortonworks.com', 'h2.hortonworks.com'], 'all_hosts': ['h1.hortonworks.com', 'h2.hortonworks.com'], 'all_ping_ports': ['8670', '8670'] } config = AmbariConfig().getConfig() tempdir = tempfile.gettempdir() config.set('agent', 'prefix', tempdir) dummy_controller = MagicMock() orchestrator = CustomServiceOrchestrator(config, dummy_controller) isfile_mock.return_value = True # Test dumping EXECUTION_COMMAND json_file = orchestrator.dump_command_to_json(command) self.assertTrue(os.path.exists(json_file)) self.assertTrue(os.path.getsize(json_file) > 0) if get_platform() != PLATFORM_WINDOWS: self.assertEqual(oct(os.stat(json_file).st_mode & 0777), '0600') self.assertTrue(json_file.endswith("command-3.json")) self.assertTrue(decompress_cluster_host_info_mock.called) os.unlink(json_file) # Test dumping STATUS_COMMAND command['commandType'] = 'STATUS_COMMAND' decompress_cluster_host_info_mock.reset_mock() json_file = orchestrator.dump_command_to_json(command) self.assertTrue(os.path.exists(json_file)) self.assertTrue(os.path.getsize(json_file) > 0) if get_platform() != PLATFORM_WINDOWS: self.assertEqual(oct(os.stat(json_file).st_mode & 0777), '0600') self.assertTrue(json_file.endswith("status_command.json")) self.assertFalse(decompress_cluster_host_info_mock.called) os.unlink(json_file) # Testing side effect of dump_command_to_json self.assertEquals(command['public_hostname'], "test.hst") self.assertTrue(unlink_mock.called)