def test_hostinfo_register_suse(self, cit_mock, hvlc_mock, hvrc_mock, eac_mock, cf_mock, jp_mock, cls_mock, cu_mock, gir_mock, gipbr_mock, gipbn_mock, gpd_mock, aip_mock, aap_mock, whcf_mock, os_umask_mock, get_os_type_mock): cit_mock.return_value = True hvlc_mock.return_value = 1 hvrc_mock.return_value = 1 gipbr_mock.return_value = ["pkg1"] gipbn_mock.return_value = ["pkg2"] gpd_mock.return_value = ["pkg1", "pkg2"] get_os_type_mock.return_value = "suse" hostInfo = HostInfo() dict = {} hostInfo.register(dict, False, False) self.assertTrue(cit_mock.called) self.assertTrue(gir_mock.called) self.assertTrue(gpd_mock.called) self.assertTrue(aip_mock.called) self.assertTrue(aap_mock.called) self.assertTrue(os_umask_mock.called) self.assertTrue(whcf_mock.called) self.assertTrue(0 < len(dict['installedPackages'])) self.assertTrue('agentTimeStampAtReporting' in dict['hostHealth'])
def build(self, id='-1', add_state=False, componentsMapped=False): global clusterId, clusterDefinitionRevision, firstContact timestamp = int(time.time()*1000) queueResult = self.actionQueue.result() recovery_timestamp = self.actionQueue.controller.recovery_manager.recovery_timestamp nodeStatus = { "status" : "HEALTHY", "cause" : "NONE" } heartbeat = { 'responseId' : int(id), 'timestamp' : timestamp, 'hostname' : hostname(self.config), 'nodeStatus' : nodeStatus, 'recoveryTimestamp' : recovery_timestamp } rec_status = self.actionQueue.controller.recovery_manager.get_recovery_status() heartbeat['recoveryReport'] = rec_status commandsInProgress = False if not self.actionQueue.commandQueue.empty(): commandsInProgress = True if len(queueResult) != 0: heartbeat['reports'] = queueResult['reports'] heartbeat['componentStatus'] = queueResult['componentStatus'] if len(heartbeat['reports']) > 0: # There may be IN_PROGRESS tasks commandsInProgress = True pass # For first request/heartbeat assume no components are mapped if int(id) == 0: componentsMapped = False logger.debug("Building Heartbeat: {responseId = %s, timestamp = %s, " "commandsInProgress = %s, componentsMapped = %s," "recoveryTimestamp = %s}", str(id), str(timestamp), repr(commandsInProgress), repr(componentsMapped), str(recovery_timestamp)) logger.debug("Heartbeat: %s", pformat(heartbeat)) hostInfo = HostInfo(self.config) if add_state: logger.info("Adding host info/state to heartbeat message.") nodeInfo = { } # for now, just do the same work as registration # this must be the last step before returning heartbeat hostInfo.register(nodeInfo, componentsMapped, commandsInProgress) heartbeat['agentEnv'] = nodeInfo mounts = Hardware(config=self.config, cache_info=False).osdisks() heartbeat['mounts'] = mounts logger.debug("agentEnv: %s", str(nodeInfo)) logger.debug("mounts: %s", str(mounts)) if self.collector is not None: heartbeat['alerts'] = self.collector.alerts() return heartbeat
def test_checkFolders(self, path_mock): path_mock.return_value = True hostInfo = HostInfo() results = [] existingUsers = [{ 'name': 'a1', 'homeDir': os.path.join('home', 'a1') }, { 'name': 'b1', 'homeDir': os.path.join('home', 'b1') }] hostInfo.checkFolders( [os.path.join("etc", "conf"), os.path.join("var", "lib"), "home"], ["a1", "b1"], ["c", "d"], existingUsers, results) print results self.assertEqual(6, len(results)) names = [i['name'] for i in results] for item in [ os.path.join('etc', 'conf', 'a1'), os.path.join('var', 'lib', 'a1'), os.path.join('etc', 'conf', 'b1'), os.path.join('var', 'lib', 'b1') ]: self.assertTrue(item in names)
def test_hostinfo_register_suse(self, hvlc_mock, hvrc_mock, eac_mock, cf_mock, jp_mock, cls_mock, cu_mock, gir_mock, gipbr_mock, gipbn_mock, gpd_mock, aip_mock, aap_mock, whcf_mock, odas_mock, os_umask_mock, get_os_type_mock): hvlc_mock.return_value = 1 hvrc_mock.return_value = 1 gipbr_mock.return_value = ["pkg1"] gipbn_mock.return_value = ["pkg2"] gpd_mock.return_value = ["pkg1", "pkg2"] odas_mock.return_value = [{'name':'name1'}] get_os_type_mock.return_value = "suse" hostInfo = HostInfo() dict = {} hostInfo.register(dict, False, False) self.assertFalse(gir_mock.called) self.assertFalse(gpd_mock.called) self.assertFalse(aip_mock.called) self.assertFalse(aap_mock.called) self.assertTrue(odas_mock.called) self.assertTrue(os_umask_mock.called) self.assertFalse(whcf_mock.called) self.assertTrue(0 == len(dict['installedPackages'])) self.assertTrue('agentTimeStampAtReporting' in dict['hostHealth'])
class HostStatusReporter(threading.Thread): """ The thread reports host status to server if it changed from previous report every 'host_status_report_interval' seconds. """ def __init__(self, initializer_module): self.initializer_module = initializer_module self.report_interval = initializer_module.config.host_status_report_interval self.stop_event = initializer_module.stop_event self.config = initializer_module.config self.host_info = HostInfo(initializer_module.config) self.last_report = {} self.server_responses_listener = initializer_module.server_responses_listener self.hardware = Hardware(config=initializer_module.config, cache_info=False) threading.Thread.__init__(self) def run(self): while not self.stop_event.is_set(): try: if self.initializer_module.is_registered: report = self.get_report() if self.initializer_module.is_registered and not Utils.are_dicts_equal( report, self.last_report, keys_to_skip=["agentTimeStampAtReporting"]): correlation_id = self.initializer_module.connection.send( message=report, destination=Constants.HOST_STATUS_REPORTS_ENDPOINT) self.server_responses_listener.listener_functions_on_success[ correlation_id] = lambda headers, message: self.save_last_report( report) except ConnectionIsAlreadyClosed: # server and agent disconnected during sending data. Not an issue pass except: logger.exception( "Exception in HostStatusReporter. Re-running it") self.stop_event.wait(self.report_interval) logger.info("HostStatusReporter has successfully finished") def save_last_report(self, report): self.last_report = report def get_report(self): host_info_dict = {} self.host_info.register(host_info_dict) report = { 'agentEnv': host_info_dict, 'mounts': self.hardware.osdisks(), } return report def clean_cache(self): self.last_report = {}
def execute_last_agent_env_check(self): print "Last Agent Env check started." hostInfo = HostInfo() last_agent_env_check_structured_output = { } hostInfo.register(last_agent_env_check_structured_output) print "Last Agent Env check completed successfully." return last_agent_env_check_structured_output
def execute_last_agent_env_check(self): Logger.info("Last Agent Env check started.") hostInfo = HostInfo() last_agent_env_check_structured_output = {} hostInfo.register(last_agent_env_check_structured_output, False, False) Logger.info("Last Agent Env check completed successfully.") return last_agent_env_check_structured_output
def execute_last_agent_env_check(self): Logger.info("Last Agent Env check started.") hostInfo = HostInfo() last_agent_env_check_structured_output = { } hostInfo.register(last_agent_env_check_structured_output, runExpensiveChecks=False, checkJavaProcs=True) Logger.info("Last Agent Env check completed successfully.") return last_agent_env_check_structured_output
def build(self, id='-1', state_interval=-1, componentsMapped=False): global clusterId, clusterDefinitionRevision, firstContact timestamp = int(time.time()*1000) queueResult = self.actionQueue.result() nodeStatus = { "status" : "HEALTHY", "cause" : "NONE" } heartbeat = { 'responseId' : int(id), 'timestamp' : timestamp, 'hostname' : hostname(self.config), 'nodeStatus' : nodeStatus } rec_status = self.actionQueue.controller.recovery_manager.get_recovery_status() heartbeat['recoveryReport'] = rec_status commandsInProgress = False if not self.actionQueue.commandQueue.empty(): commandsInProgress = True if len(queueResult) != 0: heartbeat['reports'] = queueResult['reports'] heartbeat['componentStatus'] = queueResult['componentStatus'] if len(heartbeat['reports']) > 0: # There may be IN_PROGRESS tasks commandsInProgress = True pass # For first request/heartbeat assume no components are mapped if int(id) == 0: componentsMapped = False logger.info("Building Heartbeat: {responseId = %s, timestamp = %s, commandsInProgress = %s, componentsMapped = %s}", str(id), str(timestamp), repr(commandsInProgress), repr(componentsMapped)) if logger.isEnabledFor(logging.DEBUG): logger.debug("Heartbeat: %s", pformat(heartbeat)) hostInfo = HostInfo(self.config) if (int(id) >= 0) and state_interval > 0 and (int(id) % state_interval) == 0: nodeInfo = { } # for now, just do the same work as registration # this must be the last step before returning heartbeat hostInfo.register(nodeInfo, componentsMapped, commandsInProgress) heartbeat['agentEnv'] = nodeInfo mounts = Hardware.osdisks(self.config) heartbeat['mounts'] = mounts if logger.isEnabledFor(logging.DEBUG): logger.debug("agentEnv: %s", str(nodeInfo)) logger.debug("mounts: %s", str(mounts)) if self.collector is not None: heartbeat['alerts'] = self.collector.alerts() return heartbeat
def build(self, id='-1', state_interval=-1, componentsMapped=False): global clusterId, clusterDefinitionRevision, firstContact timestamp = int(time.time() * 1000) queueResult = self.actionQueue.result() nodeStatus = {"status": "HEALTHY", "cause": "NONE"} heartbeat = { 'responseId': int(id), 'timestamp': timestamp, 'hostname': hostname(self.config), 'nodeStatus': nodeStatus } commandsInProgress = False if not self.actionQueue.commandQueue.empty(): commandsInProgress = True if len(queueResult) != 0: heartbeat['reports'] = queueResult['reports'] heartbeat['componentStatus'] = queueResult['componentStatus'] if len(heartbeat['reports']) > 0: # There may be IN_PROGRESS tasks commandsInProgress = True pass # For first request/heartbeat assume no components are mapped if int(id) == 0: componentsMapped = False logger.info( "Building Heartbeat: {responseId = %s, timestamp = %s, commandsInProgress = %s, componentsMapped = %s}", str(id), str(timestamp), repr(commandsInProgress), repr(componentsMapped)) if logger.isEnabledFor(logging.DEBUG): logger.debug("Heartbeat: %s", pformat(heartbeat)) hostInfo = HostInfo(self.config) if (int(id) >= 0) and state_interval > 0 and (int(id) % state_interval) == 0: nodeInfo = {} # for now, just do the same work as registration # this must be the last step before returning heartbeat hostInfo.register(nodeInfo, componentsMapped, commandsInProgress) heartbeat['agentEnv'] = nodeInfo mounts = Hardware.osdisks() heartbeat['mounts'] = mounts if logger.isEnabledFor(logging.DEBUG): logger.debug("agentEnv: %s", str(nodeInfo)) logger.debug("mounts: %s", str(mounts)) if self.collector is not None: heartbeat['alerts'] = self.collector.alerts() return heartbeat
def test_hostinfo_register( self, get_transparentHuge_page_mock, cit_mock, hvlc_mock, hvrc_mock, eac_mock, cf_mock, jp_mock, cls_mock, cu_mock, gir_mock, gipbr_mock, gipbn_mock, gpd_mock, aip_mock, aap_mock, whcf_mock, os_umask_mock, get_os_type_mock, ): cit_mock.return_value = True hvlc_mock.return_value = 1 hvrc_mock.return_value = 1 gipbr_mock.return_value = ["pkg1"] gipbn_mock.return_value = ["pkg2"] gpd_mock.return_value = ["pkg1", "pkg2"] get_os_type_mock.return_value = "redhat" hostInfo = HostInfo() dict = {} hostInfo.register(dict, True, True) self.verifyReturnedValues(dict) hostInfo.register(dict, True, False) self.verifyReturnedValues(dict) hostInfo.register(dict, False, True) self.verifyReturnedValues(dict) self.assertTrue(os_umask_mock.call_count == 2) cit_mock.reset_mock() hostInfo = HostInfo() dict = {} hostInfo.register(dict, False, False) self.assertTrue(gir_mock.called) self.assertTrue(gpd_mock.called) self.assertTrue(aip_mock.called) self.assertTrue(cit_mock.called) self.assertEqual(1, cit_mock.call_count) for existingPkg in ["pkg1", "pkg2"]: self.assertTrue(existingPkg in dict["installedPackages"]) args, kwargs = gpd_mock.call_args_list[0] for existingPkg in ["pkg1", "pkg2"]: self.assertTrue(existingPkg in args[1])
def test_checkFolders(self, path_mock): path_mock.return_value = True hostInfo = HostInfo() results = [] existingUsers = [{'name':'a1', 'homeDir':'/home/a1'}, {'name':'b1', 'homeDir':'/home/b1'}] hostInfo.checkFolders(["/etc/conf", "/var/lib", "/home/"], ["a1", "b1"], existingUsers, results) self.assertEqual(4, len(results)) names = [i['name'] for i in results] for item in ['/etc/conf/a1', '/var/lib/a1', '/etc/conf/b1', '/var/lib/b1']: self.assertTrue(item in names)
def __init__(self, initializer_module): self.initializer_module = initializer_module self.report_interval = initializer_module.config.host_status_report_interval self.stop_event = initializer_module.stop_event self.config = initializer_module.config self.host_info = HostInfo(initializer_module.config) self.last_report = {} self.hardware = Hardware(config=initializer_module.config, cache_info=False) threading.Thread.__init__(self)
def test_checkFolders(self, path_mock): path_mock.return_value = True hostInfo = HostInfo() results = [] existingUsers = [{"name": "a1", "homeDir": "/home/a1"}, {"name": "b1", "homeDir": "/home/b1"}] hostInfo.checkFolders(["/etc/conf", "/var/lib", "/home/"], ["a1", "b1"], existingUsers, results) self.assertEqual(4, len(results)) names = [i["name"] for i in results] for item in ["/etc/conf/a1", "/var/lib/a1", "/etc/conf/b1", "/var/lib/b1"]: self.assertTrue(item in names)
def test_hadoopVarRunCount(self, glob_glob_mock, os_path_exists_mock): hostInfo = HostInfo() os_path_exists_mock.return_value = True glob_glob_mock.return_value = ['pid1','pid2','pid3'] result = hostInfo.hadoopVarRunCount() self.assertEquals(result, 3) os_path_exists_mock.return_value = False result = hostInfo.hadoopVarRunCount() self.assertEquals(result, 0)
def test_hadoopVarLogCount(self, glob_glob_mock, os_path_exists_mock): hostInfo = HostInfo() os_path_exists_mock.return_value = True glob_glob_mock.return_value = ['log1','log2'] result = hostInfo.hadoopVarLogCount() self.assertEquals(result, 2) os_path_exists_mock.return_value = False result = hostInfo.hadoopVarLogCount() self.assertEquals(result, 0)
def test_hadoopVarLogCount(self, glob_glob_mock, os_path_exists_mock): hostInfo = HostInfo() os_path_exists_mock.return_value = True glob_glob_mock.return_value = ['log1', 'log2'] result = hostInfo.hadoopVarLogCount() self.assertEquals(result, 2) os_path_exists_mock.return_value = False result = hostInfo.hadoopVarLogCount() self.assertEquals(result, 0)
def test_hadoopVarRunCount(self, glob_glob_mock, os_path_exists_mock): hostInfo = HostInfo() os_path_exists_mock.return_value = True glob_glob_mock.return_value = ['pid1', 'pid2', 'pid3'] result = hostInfo.hadoopVarRunCount() self.assertEquals(result, 3) os_path_exists_mock.return_value = False result = hostInfo.hadoopVarRunCount() self.assertEquals(result, 0)
def test_checkIptables(self, subproc_popen_mock): hostInfo = HostInfo() p = MagicMock() subproc_popen_mock.return_value = p result = hostInfo.checkIptables() self.assertTrue(result == True) result = hostInfo.checkIptables() self.assertFalse(result == False)
def test_checkFolders(self, path_mock): path_mock.return_value = True hostInfo = HostInfo() results = [] existingUsers = [{'name':'a1', 'homeDir':os.path.join('home', 'a1')}, {'name':'b1', 'homeDir':os.path.join('home', 'b1')}] hostInfo.checkFolders([os.path.join("etc", "conf"), os.path.join("var", "lib"), "home"], ["a1", "b1"], ["c","d"], existingUsers, results) print results self.assertEqual(6, len(results)) names = [i['name'] for i in results] for item in [os.path.join('etc','conf','a1'), os.path.join('var','lib','a1'), os.path.join('etc','conf','b1'), os.path.join('var','lib','b1')]: self.assertTrue(item in names)
def test_getReposToRemove(self): l1 = ["Hortonworks Data Platform Utils Version - HDP-UTILS-1.1.0.15", "Ambari 1.x", "HDP"] l2 = ["Ambari", "HDP-UTIL"] hostInfo = HostInfo() l3 = hostInfo.getReposToRemove(l1, l2) self.assertTrue(1, len(l3)) self.assertEqual(l3[0], "HDP") l1 = ["AMBARI.dev-1.x", "HDP-1.3.0"] l3 = hostInfo.getReposToRemove(l1, l2) self.assertTrue(1, len(l3)) self.assertEqual(l3[0], "HDP-1.3.0")
def test_createAlerts(self, osdiskAvailableSpace_mock): hostInfo = HostInfo() osdiskAvailableSpace_mock.return_value = { 'size': '100', 'used': '50', 'available': '50', 'percent': '50%', 'mountpoint': '/testmount', 'type': 'ext4', 'device': 'device' } result = hostInfo.createAlerts([]) self.assertEquals(1, len(result))
def test_createAlerts(self, osdiskAvailableSpace_mock): hostInfo = HostInfo() osdiskAvailableSpace_mock.return_value = { "size": "100", "used": "50", "available": "50", "percent": "50%", "mountpoint": "/testmount", "type": "ext4", "device": "device", } result = hostInfo.createAlerts([]) self.assertEquals(1, len(result))
def test_osdiskAvailableSpace(self, extract_mount_info_mock, subproc_popen_mock): hostInfo = HostInfo() p = MagicMock() p.communicate.return_value = ['some'] subproc_popen_mock.return_value = p extract_mount_info_mock.return_value = {'info' : 'info'} result = hostInfo.osdiskAvailableSpace('') self.assertTrue(result['info'], 'info') p.communicate.return_value = '' result = hostInfo.osdiskAvailableSpace('') self.assertEquals(result, {})
def test_osdiskAvailableSpace(self, extract_mount_info_mock, subproc_popen_mock): hostInfo = HostInfo() p = MagicMock() p.communicate.return_value = ["some"] subproc_popen_mock.return_value = p extract_mount_info_mock.return_value = {"info": "info"} result = hostInfo.osdiskAvailableSpace("") self.assertTrue(result["info"], "info") p.communicate.return_value = "" result = hostInfo.osdiskAvailableSpace("") self.assertEquals(result, {})
def test_javaProcs(self, pwd_getpwuid_mock, buitin_open_mock, os_listdir_mock): hostInfo = HostInfo() openRead = MagicMock() openRead.read.return_value = "/java/;/hadoop/" buitin_open_mock.side_effect = [openRead, ["Uid: 22"]] pwuid = MagicMock() pwd_getpwuid_mock.return_value = pwuid pwuid.pw_name = "user" os_listdir_mock.return_value = ["1"] list = [] hostInfo.javaProcs(list) self.assertEquals(list[0]["command"], "/java/;/hadoop/") self.assertEquals(list[0]["pid"], 1) self.assertTrue(list[0]["hadoop"]) self.assertEquals(list[0]["user"], "user")
def test_etcAlternativesConf(self, os_path_realpath_mock, os_path_islink_mock, os_listdir_mock, os_path_exists_mock): hostInfo = HostInfo() os_path_exists_mock.return_value = False result = hostInfo.etcAlternativesConf('',[]) self.assertEquals(result, []) os_path_exists_mock.return_value = True os_listdir_mock.return_value = ['config1'] os_path_islink_mock.return_value = True os_path_realpath_mock.return_value = 'real_path_to_conf' result = [] hostInfo.etcAlternativesConf('project',result) self.assertEquals(result[0]['name'], 'config1') self.assertEquals(result[0]['target'], 'real_path_to_conf')
def test_javaProcs(self, pwd_getpwuid_mock, buitin_open_mock, os_listdir_mock): hostInfo = HostInfo() openRead = MagicMock() openRead.read.return_value = '/java/;/hadoop/' buitin_open_mock.side_effect = [openRead, ['Uid: 22']] pwuid = MagicMock() pwd_getpwuid_mock.return_value = pwuid pwuid.pw_name = 'user' os_listdir_mock.return_value = ['1'] list = [] hostInfo.javaProcs(list) self.assertEquals(list[0]['command'], '/java/;/hadoop/') self.assertEquals(list[0]['pid'], 1) self.assertTrue(list[0]['hadoop']) self.assertEquals(list[0]['user'], 'user')
def test_checkUsers(self, builtins_open_mock, path_mock): builtins_open_mock.return_value = [ "hdfs:x:493:502:Hadoop HDFS:/usr/lib/hadoop:/bin/bash", "zookeeper:x:492:502:ZooKeeper:/var/run/zookeeper:/bin/bash"] path_mock.side_effect = [True, False] hostInfo = HostInfo() results = [] hostInfo.checkUsers(["zookeeper", "hdfs"], results) self.assertEqual(2, len(results)) newlist = sorted(results, key=lambda k: k['name']) self.assertTrue(newlist[0]['name'], "hdfs") self.assertTrue(newlist[1]['name'], "zookeeper") self.assertTrue(newlist[0]['homeDir'], "/usr/lib/hadoop") self.assertTrue(newlist[1]['homeDir'], "/var/run/zookeeper") self.assertTrue(newlist[0]['status'], "Available") self.assertTrue(newlist[1]['status'], "Invalid home directory")
def verifyReturnedValues(self, dict): hostInfo = HostInfo() self.assertEqual(dict['alternatives'], []) self.assertEqual(dict['stackFoldersAndFiles'], []) self.assertEqual(dict['existingUsers'], []) self.assertEqual(dict['existingRepos'][0], hostInfo.RESULT_UNAVAILABLE) self.assertEqual(dict['installedPackages'], []) self.assertTrue(dict['iptablesIsRunning'])
def test_dirType(self, os_path_isfile_mock, os_path_isdir_mock, os_path_islink_mock, os_path_exists_mock): host = HostInfo() os_path_exists_mock.return_value = False result = host.dirType("/home") self.assertEquals(result, 'not_exist') os_path_exists_mock.return_value = True os_path_islink_mock.return_value = True result = host.dirType("/home") self.assertEquals(result, 'sym_link') os_path_exists_mock.return_value = True os_path_islink_mock.return_value = False os_path_isdir_mock.return_value = True result = host.dirType("/home") self.assertEquals(result, 'directory') os_path_exists_mock.return_value = True os_path_islink_mock.return_value = False os_path_isdir_mock.return_value = False os_path_isfile_mock.return_value = True result = host.dirType("/home") self.assertEquals(result, 'file') os_path_exists_mock.return_value = True os_path_islink_mock.return_value = False os_path_isdir_mock.return_value = False os_path_isfile_mock.return_value = False result = host.dirType("/home") self.assertEquals(result, 'unknown')
def test_checkIptables(self, subproc_popen_mock): hostInfo = HostInfo() p = MagicMock() p.communicate.return_value = ['Table: filter'] subproc_popen_mock.return_value = p result = hostInfo.checkIptables() self.assertTrue(result) p.communicate.return_value = [''] result = hostInfo.checkIptables() self.assertFalse(result) p.communicate.return_value = ['iptables: Firewall is not running.'] result = hostInfo.checkIptables() self.assertFalse(result)
def test_etcAlternativesConf( self, os_path_realpath_mock, os_path_islink_mock, os_listdir_mock, os_path_exists_mock ): hostInfo = HostInfo() os_path_exists_mock.return_value = False result = hostInfo.etcAlternativesConf("", []) self.assertEquals(result, []) os_path_exists_mock.return_value = True os_listdir_mock.return_value = ["config1"] os_path_islink_mock.return_value = True os_path_realpath_mock.return_value = "real_path_to_conf" result = [] hostInfo.etcAlternativesConf("project", result) self.assertEquals(result[0]["name"], "config1") self.assertEquals(result[0]["target"], "real_path_to_conf")
def test_transparent_huge_page(self, open_mock, os_path_isfile_mock): context_manager_mock = MagicMock() open_mock.return_value = context_manager_mock file_mock = MagicMock() file_mock.read.return_value = "[never] always" enter_mock = MagicMock() enter_mock.return_value = file_mock exit_mock = MagicMock() setattr(context_manager_mock, "__enter__", enter_mock) setattr(context_manager_mock, "__exit__", exit_mock) hostInfo = HostInfo() os_path_isfile_mock.return_value = True self.assertEqual("never", hostInfo.getTransparentHugePage()) os_path_isfile_mock.return_value = False self.assertEqual("", hostInfo.getTransparentHugePage())
def test_transparent_huge_page(self, open_mock, os_path_isfile_mock): context_manager_mock = MagicMock() open_mock.return_value = context_manager_mock file_mock = MagicMock() file_mock.read.return_value = "[never] always" enter_mock = MagicMock() enter_mock.return_value = file_mock exit_mock = MagicMock() setattr(context_manager_mock, '__enter__', enter_mock) setattr(context_manager_mock, '__exit__', exit_mock) hostInfo = HostInfo() os_path_isfile_mock.return_value = True self.assertEqual("never", hostInfo.getTransparentHugePage()) os_path_isfile_mock.return_value = False self.assertEqual("", hostInfo.getTransparentHugePage())
def test_checkLiveServices(self, subproc_popen, get_os_type_method): hostInfo = HostInfo() p = MagicMock() p.returncode = 0 p.communicate.return_value = ('', 'err') subproc_popen.return_value = p result = [] get_os_type_method.return_value = 'redhat' hostInfo.checkLiveServices(['service1'], result) self.assertEquals(result[0]['status'], 'Healthy') self.assertEquals(result[0]['name'], 'service1') self.assertEquals(result[0]['desc'], '') self.assertEquals( str(subproc_popen.call_args_list), "[call(['/sbin/service', 'service1', 'status'], stderr=-1, stdout=-1)]" ) p.returncode = 1 p.communicate.return_value = ('out', 'err') result = [] hostInfo.checkLiveServices(['service1'], result) self.assertEquals(result[0]['status'], 'Unhealthy') self.assertEquals(result[0]['name'], 'service1') self.assertEquals(result[0]['desc'], 'out') p.communicate.return_value = ('', 'err') result = [] hostInfo.checkLiveServices(['service1'], result) self.assertEquals(result[0]['status'], 'Unhealthy') self.assertEquals(result[0]['name'], 'service1') self.assertEquals(result[0]['desc'], 'err') p.communicate.return_value = ('', 'err', '') result = [] hostInfo.checkLiveServices(['service1'], result) self.assertEquals(result[0]['status'], 'Unhealthy') self.assertEquals(result[0]['name'], 'service1') self.assertTrue(len(result[0]['desc']) > 0)
def test_checkReverseLookup(self, gethostname_mock, gethostbyname_mock, getfqdn_mock): gethostname_mock.return_value = "test" gethostbyname_mock.side_effect = ["123.123.123.123", "123.123.123.123"] getfqdn_mock.return_value = "test.example.com" hostInfo = HostInfo() self.assertTrue(hostInfo.checkReverseLookup()) gethostbyname_mock.assert_any_call("test.example.com") gethostbyname_mock.assert_any_call("test") self.assertEqual(2, gethostbyname_mock.call_count) gethostbyname_mock.side_effect = ["123.123.123.123", "231.231.231.231"] self.assertFalse(hostInfo.checkReverseLookup()) gethostbyname_mock.side_effect = ["123.123.123.123", "123.123.123.123"] getfqdn_mock.side_effect = socket.error() self.assertFalse(hostInfo.checkReverseLookup())
def test_hostinfo_register_suse( self, cit_mock, hvlc_mock, hvrc_mock, eac_mock, cf_mock, jp_mock, cls_mock, cu_mock, gir_mock, gipbr_mock, gipbn_mock, gpd_mock, aip_mock, aap_mock, whcf_mock, os_umask_mock, get_os_type_mock, ): cit_mock.return_value = True hvlc_mock.return_value = 1 hvrc_mock.return_value = 1 gipbr_mock.return_value = ["pkg1"] gipbn_mock.return_value = ["pkg2"] gpd_mock.return_value = ["pkg1", "pkg2"] get_os_type_mock.return_value = "suse" hostInfo = HostInfo() dict = {} hostInfo.register(dict, False, False) self.assertTrue(cit_mock.called) self.assertTrue(gir_mock.called) self.assertTrue(gpd_mock.called) self.assertTrue(aip_mock.called) self.assertTrue(aap_mock.called) self.assertTrue(os_umask_mock.called) self.assertTrue(whcf_mock.called) self.assertTrue(0 < len(dict["installedPackages"])) self.assertTrue("agentTimeStampAtReporting" in dict["hostHealth"])
def test_run_os_command_exception(self, popen_mock): def base_test(): return "base test" def sub_test(): return "output 1", "error 1" base_test.communicate = sub_test base_test.returncode = 0 hostInfo = HostInfo() for firewallType in hostInfo.getFirewallObjectTypes(): firewall = firewallType() popen_mock.side_effect = None popen_mock.return_value = base_test self.assertTrue(firewall.check_iptables()) popen_mock.side_effect = OSError('File not found') popen_mock.return_value = None self.assertFalse(firewall.check_iptables())
def build(self, response_id='-1'): timestamp = int(time.time() * 1000) hostInfo = HostInfo(self.config) agentEnv = {} hostInfo.register(agentEnv, runExpensiveChecks=True) current_ping_port = self.config.get('agent', 'ping_port') register = { 'id': int(response_id), 'timestamp': timestamp, 'hostname': hostname.hostname(self.config), 'currentPingPort': int(current_ping_port), 'publicHostname': hostname.public_hostname(self.config), 'hardwareProfile': self.hardware.get(), 'agentEnv': agentEnv, 'agentVersion': Utils.read_agent_version(self.config), 'prefix': self.config.get('agent', 'prefix') } return register
def test_dirType(self, os_path_isfile_mock, os_path_isdir_mock, os_path_islink_mock, os_path_exists_mock): host = HostInfo() os_path_exists_mock.return_value = False result = host.dirType("/home") self.assertEquals(result, "not_exist") os_path_exists_mock.return_value = True os_path_islink_mock.return_value = True result = host.dirType("/home") self.assertEquals(result, "sym_link") os_path_exists_mock.return_value = True os_path_islink_mock.return_value = False os_path_isdir_mock.return_value = True result = host.dirType("/home") self.assertEquals(result, "directory") os_path_exists_mock.return_value = True os_path_islink_mock.return_value = False os_path_isdir_mock.return_value = False os_path_isfile_mock.return_value = True result = host.dirType("/home") self.assertEquals(result, "file") os_path_exists_mock.return_value = True os_path_islink_mock.return_value = False os_path_isdir_mock.return_value = False os_path_isfile_mock.return_value = False result = host.dirType("/home") self.assertEquals(result, "unknown")
def test_checkLiveServices(self, subproc_popen, get_os_type_method): hostInfo = HostInfo() p = MagicMock() p.returncode = 0 p.communicate.return_value = ("", "err") subproc_popen.return_value = p result = [] get_os_type_method.return_value = "redhat" hostInfo.checkLiveServices(["service1"], result) self.assertEquals(result[0]["status"], "Healthy") self.assertEquals(result[0]["name"], "service1") self.assertEquals(result[0]["desc"], "") self.assertEquals( str(subproc_popen.call_args_list), "[call(['service', 'service1', 'status'], stderr=-1, stdout=-1)]" ) p.returncode = 1 p.communicate.return_value = ("out", "err") result = [] hostInfo.checkLiveServices(["service1"], result) self.assertEquals(result[0]["status"], "Unhealthy") self.assertEquals(result[0]["name"], "service1") self.assertEquals(result[0]["desc"], "out") p.communicate.return_value = ("", "err") result = [] hostInfo.checkLiveServices(["service1"], result) self.assertEquals(result[0]["status"], "Unhealthy") self.assertEquals(result[0]["name"], "service1") self.assertEquals(result[0]["desc"], "err") p.communicate.return_value = ("", "err", "") result = [] hostInfo.checkLiveServices(["service1"], result) self.assertEquals(result[0]["status"], "Unhealthy") self.assertEquals(result[0]["name"], "service1") self.assertTrue(len(result[0]["desc"]) > 0)
def test_checkLiveServices(self, subproc_popen, get_os_type_method): hostInfo = HostInfo() p = MagicMock() p.returncode = 0 p.communicate.return_value = ('', 'err') subproc_popen.return_value = p result = [] get_os_type_method.return_value = 'redhat' hostInfo.checkLiveServices(['service1'], result) self.assertEquals(result[0]['status'], 'Healthy') self.assertEquals(result[0]['name'], 'service1') self.assertEquals(result[0]['desc'], '') self.assertEquals(str(subproc_popen.call_args_list), "[call(['/sbin/service', 'service1', 'status'], stderr=-1, stdout=-1)]") p.returncode = 1 p.communicate.return_value = ('out', 'err') result = [] hostInfo.checkLiveServices(['service1'], result) self.assertEquals(result[0]['status'], 'Unhealthy') self.assertEquals(result[0]['name'], 'service1') self.assertEquals(result[0]['desc'], 'out') p.communicate.return_value = ('', 'err') result = [] hostInfo.checkLiveServices(['service1'], result) self.assertEquals(result[0]['status'], 'Unhealthy') self.assertEquals(result[0]['name'], 'service1') self.assertEquals(result[0]['desc'], 'err') p.communicate.return_value = ('', 'err', '') result = [] hostInfo.checkLiveServices(['service1'], result) self.assertEquals(result[0]['status'], 'Unhealthy') self.assertEquals(result[0]['name'], 'service1') self.assertTrue(len(result[0]['desc']) > 0)
def test_IpTablesRunning(self, run_os_command_mock): hostInfo = HostInfo() for firewallType in hostInfo.getFirewallObjectTypes(): firewall = firewallType() run_os_command_mock.return_value = firewall.get_running_result() self.assertTrue(firewall.check_iptables())
def test_IpTablesStopped(self, run_os_command_mock): hostInfo = HostInfo() for firewallType in hostInfo.getFirewallObjectTypes(): firewall = firewallType() run_os_command_mock.return_value = firewall.get_stopped_result() self.assertFalse(firewall.check_iptables())
def test_hostinfo_register(self, cit_mock, hvlc_mock, hvrc_mock, eac_mock, cf_mock, jp_mock, cls_mock, cu_mock, gir_mock, gipbr_mock, gipbn_mock, gpd_mock, aip_mock, aap_mock, whcf_mock, os_umask_mock, get_os_type_mock): cit_mock.return_value = True hvlc_mock.return_value = 1 hvrc_mock.return_value = 1 gipbr_mock.return_value = ["pkg1"] gipbn_mock.return_value = ["pkg2"] gpd_mock.return_value = ["pkg1", "pkg2"] get_os_type_mock.return_value = "redhat" hostInfo = HostInfo() dict = {} hostInfo.register(dict, True, True) self.verifyReturnedValues(dict) hostInfo.register(dict, True, False) self.verifyReturnedValues(dict) hostInfo.register(dict, False, True) self.verifyReturnedValues(dict) self.assertTrue(os_umask_mock.call_count == 2) hostInfo = HostInfo() dict = {} hostInfo.register(dict, False, False) self.assertTrue(gir_mock.called) self.assertTrue(gpd_mock.called) self.assertTrue(aip_mock.called) self.assertTrue(cit_mock.called) for existingPkg in ["pkg1", "pkg2"]: self.assertTrue(existingPkg in dict['installedPackages']) args, kwargs = gpd_mock.call_args_list[0] for existingPkg in ["pkg1", "pkg2"]: self.assertTrue(existingPkg in args[1])