def test_dump_command_to_json(self): 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' } } } config = AmbariConfig().getConfig() tempdir = tempfile.gettempdir() config.set('agent', 'prefix', tempdir) orchestrator = CustomServiceOrchestrator(config) file = orchestrator.dump_command_to_json(command) self.assertTrue(os.path.exists(file)) self.assertTrue(os.path.getsize(file) > 0) self.assertEqual(oct(os.stat(file).st_mode & 0777), '0600') os.unlink(file)
def test_dump_command_to_json_with_retry(self, FileCache_mock, unlink_mock, isfile_mock, hostname_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_racks': [u'/default-rack:0'], 'ambari_server_host': 'a.b.c', 'all_ipv4_ips': [u'192.168.12.101:0'], 'all_hosts': ['h1.hortonworks.com', 'h2.hortonworks.com'], 'all_ping_ports': ['8670:0,1'] }, 'hostLevelParams': {} } 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")) os.unlink(json_file) # Test dumping STATUS_COMMAND json_file = orchestrator.dump_command_to_json(command, True) 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")) 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 test_add_reg_listener_to_controller(self, FileCache_mock): FileCache_mock.return_value = None dummy_controller = MagicMock() config = AmbariConfig().getConfig() tempdir = tempfile.gettempdir() config.set('agent', 'prefix', tempdir) CustomServiceOrchestrator(config, dummy_controller) self.assertTrue(dummy_controller.registration_listeners.append.called)
def test_add_reg_listener_to_controller(self, FileCache_mock): FileCache_mock.return_value = None dummy_controller = MagicMock() config = AmbariConfig().getConfig() tempdir = tempfile.gettempdir() config.set('agent', 'prefix', tempdir) CustomServiceOrchestrator(config, dummy_controller) self.assertTrue(dummy_controller.registration_listeners.append.called)
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)
def test_configureEnviron(self, hrdware_facterinfo_mock, subproc_popen, os_path_exists_mock): config = AmbariConfig().getConfig() config.set("puppet", "ruby_home", AmbariConfig().getConfig().get("stack", "installprefix")) hardware = Hardware(config) os_path_exists_mock.return_value = True result = hardware.configureEnviron({'PATH': ""}) self.assertEquals(result['PATH'], AmbariConfig().getConfig().get("stack", "installprefix") + "/bin:") self.assertEquals(result['MY_RUBY_HOME'], AmbariConfig().getConfig().get("stack", "installprefix")) config.remove_option("puppet", "ruby_home")
def test_facterInfo(self, os_path_exists_mock, hardware_facterLib_mock, subprocess_popen_mock): config = AmbariConfig().getConfig() config.set("puppet", "facter_home", AmbariConfig().getConfig().get("stack", "installprefix")) hardware = Hardware(config) facter = MagicMock() facter.communicate.return_value = [ "memoryfree => 1 GB\n memorysize => 25 MB\n memorytotal => 300 KB\n " + "physicalprocessorcount => 25\n is_virtual => true\n", "no errors" ] facter.returncode = 0 os.environ['RUBYLIB'] = AmbariConfig().getConfig().get( "stack", "installprefix") subprocess_popen_mock.return_value = facter os_path_exists_mock.return_value = True hardware_facterLib_mock.return_value = "bla bla bla" facterInfo = hardware.facterInfo() self.assertEquals(facterInfo['memoryfree'], 1048576L) self.assertEquals(facterInfo['memorysize'], 25600L) self.assertEquals(facterInfo['memorytotal'], 300L) self.assertEquals(facterInfo['physicalprocessorcount'], 25) self.assertTrue(facterInfo['is_virtual']) self.assertEquals( subprocess_popen_mock.call_args[1]['env']['RUBYLIB'], AmbariConfig().getConfig().get("stack", "installprefix") + ":" + "bla bla bla") facter.communicate.return_value = [ "memoryfree => 1 G\n memorysize => 25 M\n memorytotal => 300 K\n " + "someinfo => 12 Byte\n ssh_name_key => Aa06Fdd\n", "no errors" ] facterInfo = hardware.facterInfo() facter.returncode = 1 self.assertEquals(facterInfo['memoryfree'], 1048576L) self.assertEquals(facterInfo['memorysize'], 25600L) self.assertEquals(facterInfo['memorytotal'], 300L) self.assertEquals(facterInfo['someinfo'], '12 Byte') self.assertFalse(facterInfo.has_key('ssh_name_key')) facter.communicate.return_value = [ "memoryfree => 1024 M B\n memorytotal => 1024 Byte", "no errors" ] facterInfo = hardware.facterInfo() self.assertEquals(facterInfo['memoryfree'], 1L) self.assertEquals(facterInfo['memorytotal'], 1L) os_path_exists_mock.return_value = False facterInfo = hardware.facterInfo() self.assertEquals(facterInfo, {})
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']} } 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) 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) 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)
def test_configureEnviron(self, hrdware_facterinfo_mock, subproc_popen, os_path_exists_mock): config = AmbariConfig().getConfig() tmpdir = tempfile.gettempdir() config.set("puppet", "ruby_home", tmpdir) hardware = Hardware(config) os_path_exists_mock.return_value = True result = hardware.configureEnviron({'PATH': ""}) self.assertEquals(result['PATH'], tmpdir + "/bin:") self.assertEquals(result['MY_RUBY_HOME'], tmpdir) config.remove_option("puppet", "ruby_home")
def test_configure_environ(self, osPathExistsMock): config = AmbariConfig().getConfig() tmpdir = tempfile.gettempdir() puppetInstance = PuppetExecutor("/tmp", "/x", "/y", tmpdir, config) environ = puppetInstance.configureEnviron({}) self.assertEquals(environ, {}) config.set('puppet','ruby_home',"test/ruby_home") puppetInstance = PuppetExecutor("/tmp", "/x", "/y", tmpdir, config) osPathExistsMock.return_value = True environ = puppetInstance.configureEnviron({"PATH" : "test_path"}) self.assertEquals(environ["PATH"], "test/ruby_home/bin:test_path") self.assertEquals(environ["MY_RUBY_HOME"], "test/ruby_home")
def test_facterInfo(self, os_path_exists_mock, hardware_facterLib_mock, subprocess_popen_mock): config = AmbariConfig().getConfig() config.set("puppet", "facter_home", AmbariConfig().getConfig().get("stack", "installprefix")) hardware = Hardware(config) facter = MagicMock() facter.communicate.return_value = ["memoryfree => 1 GB\n memorysize => 25 MB\n memorytotal => 300 KB\n " + "physicalprocessorcount => 25\n is_virtual => true\n", "no errors"] facter.returncode = 0 os.environ['RUBYLIB'] = AmbariConfig().getConfig().get("stack", "installprefix"); subprocess_popen_mock.return_value = facter os_path_exists_mock.return_value = True hardware_facterLib_mock.return_value = "bla bla bla" facterInfo = hardware.facterInfo() self.assertEquals(facterInfo['memoryfree'], 1048576L) self.assertEquals(facterInfo['memorysize'], 25600L) self.assertEquals(facterInfo['memorytotal'], 300L) self.assertEquals(facterInfo['physicalprocessorcount'], 25) self.assertTrue(facterInfo['is_virtual']) self.assertEquals(subprocess_popen_mock.call_args[1]['env']['RUBYLIB'], AmbariConfig().getConfig().get("stack", "installprefix") + ":" + "bla bla bla") facter.communicate.return_value = ["memoryfree => 1 G\n memorysize => 25 M\n memorytotal => 300 K\n " + "someinfo => 12 Byte\n ssh_name_key => Aa06Fdd\n", "no errors"] facterInfo = hardware.facterInfo() facter.returncode = 1 self.assertEquals(facterInfo['memoryfree'], 1048576L) self.assertEquals(facterInfo['memorysize'], 25600L) self.assertEquals(facterInfo['memorytotal'], 300L) self.assertEquals(facterInfo['someinfo'], '12 Byte') self.assertFalse(facterInfo.has_key('ssh_name_key')) facter.communicate.return_value = ["memoryfree => 1024 M B\n memorytotal => 1024 Byte" , "no errors"] facterInfo = hardware.facterInfo() self.assertEquals(facterInfo['memoryfree'], 1L) self.assertEquals(facterInfo['memorytotal'], 1L) os_path_exists_mock.return_value = False facterInfo = hardware.facterInfo() self.assertEquals(facterInfo, {})
def test_cancel_backgound_command(self, read_stack_version_mock, resolve_hook_script_path_mock, resolve_script_path_mock, FileCache_mock, kill_process_with_children_mock): FileCache_mock.return_value = None FileCache_mock.cache_dir = MagicMock() resolve_hook_script_path_mock.return_value = None # shell.kill_process_with_children = MagicMock() dummy_controller = MagicMock() cfg = AmbariConfig().getConfig() cfg.set('agent', 'tolerate_download_failures', 'true') cfg.set('agent', 'prefix', '.') cfg.set('agent', 'cache_dir', 'background_tasks') actionQueue = ActionQueue(cfg, dummy_controller) dummy_controller.actionQueue = actionQueue orchestrator = CustomServiceOrchestrator(cfg, dummy_controller) orchestrator.file_cache = MagicMock() def f (a, b): return "" orchestrator.file_cache.get_service_base_dir = f actionQueue.customServiceOrchestrator = orchestrator import TestActionQueue import copy TestActionQueue.patch_output_file(orchestrator.python_executor) orchestrator.python_executor.prepare_process_result = MagicMock() orchestrator.dump_command_to_json = MagicMock() lock = threading.RLock() complete_done = threading.Condition(lock) complete_was_called = {} def command_complete_w(process_condenced_result, handle): with lock: complete_was_called['visited']= '' complete_done.wait(3) actionQueue.on_background_command_complete_callback = TestActionQueue.wraped(actionQueue.on_background_command_complete_callback, command_complete_w, None) execute_command = copy.deepcopy(TestActionQueue.TestActionQueue.background_command) actionQueue.put([execute_command]) actionQueue.processBackgroundQueueSafeEmpty() time.sleep(.1) orchestrator.cancel_command(19,'') self.assertTrue(kill_process_with_children_mock.called) kill_process_with_children_mock.assert_called_with(33) with lock: complete_done.notifyAll() with lock: self.assertTrue(complete_was_called.has_key('visited')) time.sleep(.1) runningCommand = actionQueue.commandStatuses.get_command_status(19) self.assertTrue(runningCommand is not None) self.assertEqual(runningCommand['status'], ActionQueue.FAILED_STATUS)
def test_watchdog_1(self, kill_process_with_children_mock): """ Tests whether watchdog works """ subproc_mock = self.Subprocess_mockup() config = AmbariConfig().getConfig() config.set('puppet','timeout_seconds',"0.1") executor_mock = self.PuppetExecutor_mock("/home/centos/ambari_repo_info/ambari-agent/src/main/puppet/", "/usr/", "/root/workspace/puppet-install/facter-1.6.10/", "/tmp", config, subproc_mock) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() result = { } puppetEnv = { "RUBYLIB" : ""} kill_process_with_children_mock.side_effect = lambda pid : subproc_mock.terminate() subproc_mock.returncode = None thread = Thread(target = executor_mock.runPuppetFile, args = ("fake_puppetFile", result, puppetEnv, tmpoutfile, tmperrfile)) thread.start() time.sleep(0.1) subproc_mock.finished_event.wait() self.assertEquals(subproc_mock.was_terminated, True, "Subprocess should be terminated due to timeout")
def main(): """ May be used for manual testing if needed """ config = AmbariConfig().getConfig() orchestrator = CustomServiceOrchestrator(config) config.set('agent', 'prefix', "/tmp") command = { "serviceName": "HBASE", "role": "HBASE_MASTER", "stackName": "HDP", "stackVersion": "1.2.0", "scriptType": "PYTHON", "script": "/tmp/1.py", "roleCommand": "START", "timeout": 600 } result = orchestrator.runCommand(command, "/tmp/out-1.txt", "/tmp/err-1.txt") pprint.pprint(result) pass
def test_watchdog_2(self): """ Tries to catch false positive watchdog invocations """ subproc_mock = self.Subprocess_mockup() config = AmbariConfig().getConfig() config.set('puppet','timeout_seconds',"5") executor_mock = self.PuppetExecutor_mock("/home/centos/ambari_repo_info/ambari-agent/src/main/puppet/", "/usr/", "/root/workspace/puppet-install/facter-1.6.10/", "/tmp", config, subproc_mock) _, tmpoutfile = tempfile.mkstemp() _, tmperrfile = tempfile.mkstemp() result = { } puppetEnv = { "RUBYLIB" : ""} subproc_mock.returncode = 0 thread = Thread(target = executor_mock.runPuppetFile, args = ("fake_puppetFile", result, puppetEnv, tmpoutfile, tmperrfile)) thread.start() time.sleep(0.1) subproc_mock.should_finish_event.set() subproc_mock.finished_event.wait() self.assertEquals(subproc_mock.was_terminated, False, "Subprocess should not be terminated before timeout") self.assertEquals(subproc_mock.returncode, 0, "Subprocess should not be terminated before timeout")
def test_cancel_backgound_command(self, read_stack_version_mock, resolve_hook_script_path_mock, resolve_script_path_mock, FileCache_mock, kill_process_with_children_mock, get_py_executor_mock): FileCache_mock.return_value = None FileCache_mock.cache_dir = MagicMock() resolve_hook_script_path_mock.return_value = None # shell.kill_process_with_children = MagicMock() dummy_controller = MagicMock() cfg = AmbariConfig() cfg.set('agent', 'tolerate_download_failures', 'true') cfg.set('agent', 'prefix', '.') cfg.set('agent', 'cache_dir', 'background_tasks') actionQueue = ActionQueue(cfg, dummy_controller) dummy_controller.actionQueue = actionQueue orchestrator = CustomServiceOrchestrator(cfg, dummy_controller) orchestrator.file_cache = MagicMock() def f(a, b): return "" orchestrator.file_cache.get_service_base_dir = f actionQueue.customServiceOrchestrator = orchestrator import TestActionQueue import copy pyex = PythonExecutor(actionQueue.customServiceOrchestrator.tmp_dir, actionQueue.customServiceOrchestrator.config) TestActionQueue.patch_output_file(pyex) pyex.prepare_process_result = MagicMock() get_py_executor_mock.return_value = pyex orchestrator.dump_command_to_json = MagicMock() lock = threading.RLock() complete_done = threading.Condition(lock) complete_was_called = {} def command_complete_w(process_condenced_result, handle): with lock: complete_was_called['visited'] = '' complete_done.wait(3) actionQueue.on_background_command_complete_callback = TestActionQueue.wraped( actionQueue.on_background_command_complete_callback, command_complete_w, None) execute_command = copy.deepcopy( TestActionQueue.TestActionQueue.background_command) actionQueue.put([execute_command]) actionQueue.processBackgroundQueueSafeEmpty() time.sleep(.1) orchestrator.cancel_command(19, '') self.assertTrue(kill_process_with_children_mock.called) kill_process_with_children_mock.assert_called_with(33) with lock: complete_done.notifyAll() with lock: self.assertTrue(complete_was_called.has_key('visited')) time.sleep(.1) runningCommand = actionQueue.commandStatuses.get_command_status(19) self.assertTrue(runningCommand is not None) self.assertEqual(runningCommand['status'], ActionQueue.FAILED_STATUS)