def test_parse_df_line(self): df_line_sample = "device type size used available percent mountpoint" samples = [{ "sample": df_line_sample, "expected": dict(zip(df_line_sample.split(), df_line_sample.split())) }, { "sample": "device type size used available percent", "expected": None, }, { "sample": "device type size used available percent mountpoint info", "expected": None, }, { "sample": "", "expected": None }] for sample in samples: try: result = Hardware(cache_info=False)._parse_df( [sample["sample"]]).next() except StopIteration: result = None self.assertEquals( result, sample["expected"], "Failed with sample: '{0}', expected: {1}, got: {2}".format( sample["sample"], sample["expected"], result))
def test_build(self, get_os_version_mock, get_os_type_mock): get_os_type_mock.return_value = "suse" get_os_version_mock.return_value = "11" hardware = Hardware() result = hardware.get() osdisks = hardware.osdisks() for dev_item in result['mounts']: self.assertTrue(dev_item['available'] >= 0) self.assertTrue(dev_item['used'] >= 0) self.assertTrue(dev_item['percent'] != None) self.assertTrue(dev_item['device'] != None) self.assertTrue(dev_item['mountpoint'] != None) self.assertTrue(dev_item['type'] != None) self.assertTrue(dev_item['size'] > 0) for os_disk_item in osdisks: self.assertTrue(os_disk_item['available'] >= 0) self.assertTrue(os_disk_item['used'] >= 0) self.assertTrue(os_disk_item['percent'] != None) self.assertTrue(os_disk_item['device'] != None) self.assertTrue(os_disk_item['mountpoint'] != None) self.assertTrue(os_disk_item['type'] != None) self.assertTrue(os_disk_item['size'] > 0) self.assertTrue(len(result['mounts']) == len(osdisks))
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_osdisks_parsing(self, shell_call_mock, isfile_mock, chk_writable_mount_mock): df_output =\ """Filesystem Type 1024-blocks Used Available Capacity Mounted on /dev/mapper/docker-253:0-4980899-d45c264d37ab18c8ed14f890f4d59ac2b81e1c52919eb36a79419787209515f3 xfs 31447040 1282384 30164656 5% / tmpfs tmpfs 32938336 4 32938332 1% /dev tmpfs tmpfs 32938336 0 32938336 0% /sys/fs/cgroup /dev/mapper/fedora-root ext4 224161316 12849696 199901804 7% /etc/resolv.conf /dev/mapper/fedora-root ext4 224161316 12849696 199901804 7% /etc/hostname /dev/mapper/fedora-root ext4 224161316 12849696 199901804 7% /etc/hosts shm tmpfs 65536 0 65536 0% /dev/shm /dev/mapper/fedora-root ext4 224161316 12849696 199901804 7% /run/secrets """ def isfile_side_effect(path): assume_files = ["/etc/resolv.conf", "/etc/hostname", "/etc/hosts"] return path in assume_files def chk_writable_mount_side_effect(path): assume_read_only = ["/run/secrets"] return path not in assume_read_only isfile_mock.side_effect = isfile_side_effect chk_writable_mount_mock.side_effect = chk_writable_mount_side_effect shell_call_mock.return_value = (0, df_output, '') result = Hardware(cache_info=False).osdisks() self.assertEquals(1, len(result)) expected_mounts_left = ["/"] mounts_left = [item["mountpoint"] for item in result] self.assertEquals(expected_mounts_left, mounts_left)
def test_osdisks_blacklist(self, shell_call_mock, isfile_mock, chk_writable_mount_mock): df_output = \ """Filesystem Type 1024-blocks Used Available Capacity Mounted on /dev/mapper/docker-253:0-4980899-d45c264d37ab18c8ed14f890f4d59ac2b81e1c52919eb36a79419787209515f3 xfs 31447040 1282384 30164656 5% / tmpfs tmpfs 32938336 4 32938332 1% /dev tmpfs tmpfs 32938336 0 32938336 0% /sys/fs/cgroup /dev/mapper/fedora-root ext4 224161316 12849696 199901804 7% /etc/resolv.conf /dev/mapper/fedora-root ext4 224161316 12849696 199901804 7% /etc/hostname /dev/mapper/fedora-root ext4 224161316 12849696 199901804 7% /etc/hosts shm tmpfs 65536 0 65536 0% /dev/shm /dev/mapper/fedora-root ext4 224161316 12849696 199901804 7% /run/secrets /dev/mapper/fedora-root ext4 224161316 12849696 199901804 7% /mnt/blacklisted_mount /dev/mapper/fedora-root ext4 224161316 12849696 199901804 7% /mnt/blacklisted_mount/sub-dir """ def isfile_side_effect(path): assume_files = ["/etc/resolv.conf", "/etc/hostname", "/etc/hosts"] return path in assume_files def chk_writable_mount_side_effect(path): assume_read_only = ["/run/secrets"] return path not in assume_read_only isfile_mock.side_effect = isfile_side_effect chk_writable_mount_mock.side_effect = chk_writable_mount_side_effect config_dict = { "agent": { "ignore_mount_points": "/mnt/blacklisted_mount" } } shell_call_mock.return_value = (0, df_output, '') def conf_get(section, key, default=""): if section in config_dict and key in config_dict[section]: return config_dict[section][key] return default def has_option(section, key): return section in config_dict and key in config_dict[section] conf = Mock() attr = { 'get.side_effect': conf_get, 'has_option.side_effect': has_option } conf.configure_mock(**attr) result = Hardware(config=conf, cache_info=False).osdisks() self.assertEquals(1, len(result)) expected_mounts_left = ["/"] mounts_left = [item["mountpoint"] for item in result] self.assertEquals(expected_mounts_left, mounts_left)
def test_facterBin(self, ps_path_exists_mock): hardware = Hardware(AmbariConfig().getConfig()) ps_path_exists_mock.return_value = False result = hardware.facterBin("bla bla bla") self.assertEquals(result, "facter") ps_path_exists_mock.return_value = True result = hardware.facterBin("bla bla bla") self.assertEquals(result, "bla bla bla/bin/facter")
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_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_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_build(self): hardware = Hardware() result = hardware.get() osdisks = hardware.osdisks() for dev_item in result['mounts']: self.assertTrue(dev_item['available'] >= 0) self.assertTrue(dev_item['used'] >= 0) self.assertTrue(dev_item['percent'] != None) self.assertTrue(dev_item['device'] != None) self.assertTrue(dev_item['mountpoint'] != None) self.assertTrue(dev_item['size'] > 0) for os_disk_item in osdisks: self.assertTrue(os_disk_item['available'] >= 0) self.assertTrue(os_disk_item['used'] >= 0) self.assertTrue(os_disk_item['percent'] != None) self.assertTrue(os_disk_item['device'] != None) self.assertTrue(os_disk_item['mountpoint'] != None) self.assertTrue(os_disk_item['size'] > 0) self.assertTrue(len(result['mounts']) == len(osdisks))
def test_osdisks_no_ignore_property(self, shell_call_mock, isfile_mock, chk_writable_mount_mock): df_output = \ """Filesystem Type 1024-blocks Used Available Capacity Mounted on /dev/mapper/docker-253:0-4980899-d45c264d37ab18c8ed14f890f4d59ac2b81e1c52919eb36a79419787209515f3 xfs 31447040 1282384 30164656 5% / """ isfile_mock.return_value = False chk_writable_mount_mock.return_value = True shell_call_mock.return_value = (0, df_output, '') config = AmbariConfig() # check, that config do not define ignore_mount_points property self.assertEquals( "test", config.get('agent', 'ignore_mount_points', default="test")) result = Hardware(config=config, cache_info=False).osdisks() self.assertEquals(1, len(result)) expected_mounts_left = ["/"] mounts_left = [item["mountpoint"] for item in result] self.assertEquals(expected_mounts_left, mounts_left)
def __init__(self, config): self.config = config self.hardware = Hardware(self.config) self.init_time_ms = int(1000 * time.time())
def __init__(self, config): self.config = config self.hardware = Hardware(self.config)
def test_facterLib(self): hardware = Hardware(AmbariConfig().getConfig()) facterLib = hardware.facterLib("/home") self.assertEquals(facterLib, "/home/lib/")
def test_osdisks_remote(self, shell_call_mock, get_os_version_mock, get_os_type_mock): get_os_type_mock.return_value = "suse" get_os_version_mock.return_value = "11" Hardware(cache_info=False).osdisks() timeout = 10 shell_call_mock.assert_called_with( ['timeout', str(timeout), "df", "-kPT"], stdout=subprocess32.PIPE, stderr=subprocess32.PIPE, timeout=timeout, quiet=True) config = AmbariConfig() Hardware(config=config, cache_info=False).osdisks() shell_call_mock.assert_called_with( ['timeout', str(timeout), "df", "-kPT"], stdout=subprocess32.PIPE, stderr=subprocess32.PIPE, timeout=timeout, quiet=True) config.add_section(AmbariConfig.AMBARI_PROPERTIES_CATEGORY) config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY, Hardware.CHECK_REMOTE_MOUNTS_KEY, "true") Hardware(config=config, cache_info=False).osdisks() shell_call_mock.assert_called_with( ['timeout', str(timeout), "df", "-kPT"], stdout=subprocess32.PIPE, stderr=subprocess32.PIPE, timeout=timeout, quiet=True) config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY, Hardware.CHECK_REMOTE_MOUNTS_KEY, "false") Hardware(config=config, cache_info=False).osdisks() shell_call_mock.assert_called_with( ['timeout', str(timeout), "df", "-kPT", "-l"], stdout=subprocess32.PIPE, stderr=subprocess32.PIPE, timeout=timeout, quiet=True) config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY, Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, "0") Hardware(config=config, cache_info=False).osdisks() shell_call_mock.assert_called_with( ['timeout', str(timeout), "df", "-kPT", "-l"], stdout=subprocess32.PIPE, stderr=subprocess32.PIPE, timeout=timeout, quiet=True) timeout = 1 config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY, Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, str(timeout)) Hardware(config=config, cache_info=False).osdisks() shell_call_mock.assert_called_with( ['timeout', str(timeout), "df", "-kPT", "-l"], stdout=subprocess32.PIPE, stderr=subprocess32.PIPE, timeout=timeout, quiet=True) timeout = 2 config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY, Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, str(timeout)) Hardware(config=config, cache_info=False).osdisks() shell_call_mock.assert_called_with( ['timeout', str(timeout), "df", "-kPT", "-l"], stdout=subprocess32.PIPE, stderr=subprocess32.PIPE, timeout=timeout, quiet=True)