Ejemplo n.º 1
0
  def test_extractMountInfo(self):
    outputLine = "device type size used available percent mountpoint"
    result = Hardware.extractMountInfo(outputLine)

    self.assertEquals(result['device'], 'device')
    self.assertEquals(result['type'], 'type')
    self.assertEquals(result['size'], 'size')
    self.assertEquals(result['used'], 'used')
    self.assertEquals(result['available'], 'available')
    self.assertEquals(result['percent'], 'percent')
    self.assertEquals(result['mountpoint'], 'mountpoint')

    outputLine = ""
    result = Hardware.extractMountInfo(outputLine)

    self.assertEquals(result, None)

    outputLine = "device type size used available percent"
    result = Hardware.extractMountInfo(outputLine)

    self.assertEquals(result, None)

    outputLine = "device type size used available percent mountpoint info"
    result = Hardware.extractMountInfo(outputLine)

    self.assertEquals(result, None)
Ejemplo n.º 2
0
  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))
Ejemplo n.º 3
0
    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))
Ejemplo n.º 4
0
    def test_extractMountInfo(self):
        outputLine = "device type size used available percent mountpoint"
        result = Hardware.extractMountInfo(outputLine)

        self.assertEquals(result['device'], 'device')
        self.assertEquals(result['type'], 'type')
        self.assertEquals(result['size'], 'size')
        self.assertEquals(result['used'], 'used')
        self.assertEquals(result['available'], 'available')
        self.assertEquals(result['percent'], 'percent')
        self.assertEquals(result['mountpoint'], 'mountpoint')

        outputLine = ""
        result = Hardware.extractMountInfo(outputLine)

        self.assertEquals(result, None)

        outputLine = "device type size used available percent"
        result = Hardware.extractMountInfo(outputLine)

        self.assertEquals(result, None)

        outputLine = "device type size used available percent mountpoint info"
        result = Hardware.extractMountInfo(outputLine)

        self.assertEquals(result, None)
Ejemplo n.º 5
0
    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 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")
Ejemplo n.º 7
0
 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_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")
Ejemplo n.º 9
0
    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")
Ejemplo n.º 10
0
    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.osdisks()

        self.assertEquals(1, len(result))

        expected_mounts_left = ["/"]
        mounts_left = [item["mountpoint"] for item in result]

        self.assertEquals(expected_mounts_left, mounts_left)
Ejemplo n.º 11
0
    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:
            result = Hardware._parse_df_line(sample["sample"])
            self.assertEquals(
                result, sample["expected"],
                "Failed with sample: '{0}', expected: {1}, got: {2}".format(
                    sample["sample"], sample["expected"], result))
Ejemplo n.º 12
0
class Register:
    """ Registering with the server. Get the hardware profile and
  declare success for now """
    def __init__(self, config):
        self.config = config
        self.hardware = Hardware(self.config)
        self.init_time_ms = int(1000 * time.time())

    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'),
            'agentStartTime': self.init_time_ms
        }
        return register
Ejemplo n.º 13
0
  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
Ejemplo n.º 14
0
    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))
Ejemplo n.º 15
0
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 = {}
Ejemplo n.º 16
0
    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.osdisks(conf)

        self.assertEquals(1, len(result))

        expected_mounts_left = ["/"]
        mounts_left = [item["mountpoint"] for item in result]

        self.assertEquals(expected_mounts_left, mounts_left)
Ejemplo n.º 17
0
  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
Ejemplo n.º 18
0
    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
Ejemplo n.º 19
0
 def osdiskAvailableSpace(self, path):
   diskInfo = {}
   try:
     df = subprocess.Popen(["df", "-kPT", path], stdout=subprocess.PIPE)
     dfdata = df.communicate()[0]
     return Hardware.extractMountInfo(dfdata.splitlines()[-1])
   except:
     pass
   return diskInfo
Ejemplo n.º 20
0
 def osdiskAvailableSpace(self, path):
   diskInfo = {}
   try:
     df = subprocess.Popen(["df", "-kPT", path], stdout=subprocess.PIPE)
     dfdata = df.communicate()[0]
     return Hardware.extractMountInfo(dfdata.splitlines()[-1])
   except:
     pass
   return diskInfo
  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, {})
Ejemplo n.º 22
0
    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_build(self):
    hardware = Hardware(AmbariConfig().getConfig())
    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 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, {})
Ejemplo n.º 25
0
    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)
Ejemplo n.º 26
0
 def test_osdisks_remote(self, communicate_mock, popen_mock,
                         get_os_version_mock, get_os_type_mock):
   get_os_type_mock.return_value = "suse"
   get_os_version_mock.return_value = "11"
   Hardware.osdisks()
   popen_mock.assert_called_with(['timeout', '10', "df","-kPT"], stdout=-1)
   config = AmbariConfig()
   Hardware.osdisks(config)
   popen_mock.assert_called_with(['timeout', '10', "df","-kPT"], stdout=-1)
   config.add_section(AmbariConfig.AMBARI_PROPERTIES_CATEGORY)
   config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY, Hardware.CHECK_REMOTE_MOUNTS_KEY, "true")
   Hardware.osdisks(config)
   popen_mock.assert_called_with(['timeout', '10', "df","-kPT"], stdout=-1)
   config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY, Hardware.CHECK_REMOTE_MOUNTS_KEY, "false")
   Hardware.osdisks(config)
   popen_mock.assert_called_with(['timeout', '10', "df","-kPT", "-l"], stdout=-1)
   config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY, Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, "0")
   Hardware.osdisks(config)
   popen_mock.assert_called_with(['timeout', '10', "df","-kPT","-l"], stdout=-1)
   config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY, Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, "1")
   Hardware.osdisks(config)
   popen_mock.assert_called_with(["timeout","1","df","-kPT","-l"], stdout=-1)
   config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY, Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, "2")
   Hardware.osdisks(config)
   popen_mock.assert_called_with(["timeout","2","df","-kPT","-l"], stdout=-1)
Ejemplo n.º 27
0
 def test_build(self):
   hardware = Hardware()
   result = hardware.get()
   self.assertTrue(result['coreCount'] >= 1)
   self.assertTrue(result['netSpeed'] != None)
Ejemplo n.º 28
0
 def __init__(self, config):
     self.config = config
     self.hardware = Hardware(self.config)
     self.init_time_ms = int(1000 * time.time())
Ejemplo n.º 29
0
 def __init__(self, config):
     self.config = config
     self.hardware = Hardware(self.config)
Ejemplo n.º 30
0
    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.osdisks()
        timeout = 10
        shell_call_mock.assert_called_with(
            ['timeout', str(timeout), "df", "-kPT"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            timeout=timeout,
            quiet=True)

        config = AmbariConfig()
        Hardware.osdisks(config)
        shell_call_mock.assert_called_with(
            ['timeout', str(timeout), "df", "-kPT"],
            stdout=subprocess.PIPE,
            stderr=subprocess.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.osdisks(config)
        shell_call_mock.assert_called_with(
            ['timeout', str(timeout), "df", "-kPT"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            timeout=timeout,
            quiet=True)

        config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY,
                   Hardware.CHECK_REMOTE_MOUNTS_KEY, "false")
        Hardware.osdisks(config)
        shell_call_mock.assert_called_with(
            ['timeout', str(timeout), "df", "-kPT", "-l"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            timeout=timeout,
            quiet=True)

        config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY,
                   Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, "0")
        Hardware.osdisks(config)
        shell_call_mock.assert_called_with(
            ['timeout', str(timeout), "df", "-kPT", "-l"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            timeout=timeout,
            quiet=True)

        timeout = 1
        config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY,
                   Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, str(timeout))
        Hardware.osdisks(config)
        shell_call_mock.assert_called_with(
            ['timeout', str(timeout), "df", "-kPT", "-l"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            timeout=timeout,
            quiet=True)

        timeout = 2
        config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY,
                   Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, str(timeout))
        Hardware.osdisks(config)
        shell_call_mock.assert_called_with(
            ['timeout', str(timeout), "df", "-kPT", "-l"],
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            timeout=timeout,
            quiet=True)
 def test_facterLib(self):
   hardware = Hardware(AmbariConfig().getConfig())
   facterLib = hardware.facterLib("/home")
   self.assertEquals(facterLib, "/home/lib/")
Ejemplo n.º 32
0
 def test_facterLib(self):
     hardware = Hardware(AmbariConfig().getConfig())
     facterLib = hardware.facterLib("/home")
     self.assertEquals(facterLib, "/home/lib/")
Ejemplo n.º 33
0
    def test_osdisks_remote(self, communicate_mock, popen_mock,
                            get_os_version_mock, get_os_type_mock):
        get_os_type_mock.return_value = "suse"
        get_os_version_mock.return_value = "11"
        Hardware.osdisks()
        popen_mock.assert_called_with(['timeout', '10', "df", "-kPT"],
                                      stdout=-1)

        config = AmbariConfig()
        Hardware.osdisks(config)
        popen_mock.assert_called_with(['timeout', '10', "df", "-kPT"],
                                      stdout=-1)

        config.add_section(AmbariConfig.AMBARI_PROPERTIES_CATEGORY)
        config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY,
                   Hardware.CHECK_REMOTE_MOUNTS_KEY, "true")
        Hardware.osdisks(config)
        popen_mock.assert_called_with(['timeout', '10', "df", "-kPT"],
                                      stdout=-1)

        config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY,
                   Hardware.CHECK_REMOTE_MOUNTS_KEY, "false")
        Hardware.osdisks(config)
        popen_mock.assert_called_with(['timeout', '10', "df", "-kPT", "-l"],
                                      stdout=-1)

        config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY,
                   Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, "0")
        Hardware.osdisks(config)
        popen_mock.assert_called_with(['timeout', '10', "df", "-kPT", "-l"],
                                      stdout=-1)

        config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY,
                   Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, "1")
        Hardware.osdisks(config)
        popen_mock.assert_called_with(["timeout", "1", "df", "-kPT", "-l"],
                                      stdout=-1)

        config.set(AmbariConfig.AMBARI_PROPERTIES_CATEGORY,
                   Hardware.CHECK_REMOTE_MOUNTS_TIMEOUT_KEY, "2")
        Hardware.osdisks(config)
        popen_mock.assert_called_with(["timeout", "2", "df", "-kPT", "-l"],
                                      stdout=-1)