Esempio n. 1
0
 def test_init(self):
     """Tests ZHMCUsageCollector.__init__."""
     cred_dict = {"hmc": "192.168.0.0", "userid": "user", "password": "******"}
     session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc",
                                            "2.13.1", "1.8")
     yaml_metric_groups = {"metric-group": {"prefix": "pre", "fetch": True}}
     context = (zhmc_prometheus_exporter.create_metrics_context(
         session, yaml_metric_groups, "filename"))
     yaml_metrics = {
         "metric-group": {
             "metric": {
                 "percent": True,
                 "exporter_name": "metric",
                 "exporter_desc": "metric"
             }
         }
     }
     my_zhmc_usage_collector = (zhmc_prometheus_exporter.ZHMCUsageCollector(
         cred_dict, session, context, yaml_metric_groups, yaml_metrics,
         "filename", "filename"))
     self.assertEqual(my_zhmc_usage_collector.yaml_creds, cred_dict)
     self.assertEqual(my_zhmc_usage_collector.session, session)
     self.assertEqual(my_zhmc_usage_collector.context, context)
     self.assertEqual(my_zhmc_usage_collector.yaml_metric_groups,
                      yaml_metric_groups)
     self.assertEqual(my_zhmc_usage_collector.yaml_metrics, yaml_metrics)
     self.assertEqual(my_zhmc_usage_collector.filename_metrics, "filename")
     self.assertEqual(my_zhmc_usage_collector.filename_creds, "filename")
Esempio n. 2
0
 def test_collect(self):
     """Test ZHMCUsageCollector.collect"""
     session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc",
                                            "2.13.1", "1.8")
     yaml_metric_groups = {"metric-group": {"prefix": "pre", "fetch": True}}
     context = (zhmc_prometheus_exporter.create_metrics_context(
         session, yaml_metric_groups, "filename"))
     yaml_metrics = {
         "metric-group": {
             "metric": {
                 "percent": True,
                 "exporter_name": "metric",
                 "exporter_desc": "metric"
             }
         }
     }
     my_zhmc_usage_collector = (zhmc_prometheus_exporter.ZHMCUsageCollector(
         context, yaml_metric_groups, yaml_metrics, "filename"))
     collected = list(my_zhmc_usage_collector.collect())
     self.assertEqual(len(collected), 1)
     self.assertEqual(type(collected[0]),
                      prometheus_client.core.GaugeMetricFamily)
     self.assertEqual(collected[0].name, "zhmc_pre_metric")
     self.assertEqual(collected[0].documentation, "metric")
     self.assertEqual(collected[0].type, "gauge")
     self.assertEqual(collected[0].samples, [])
     self.assertEqual(collected[0]._labelnames, ("resource", ))
def setup_faked_session():
    """Create a faked session."""

    session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc", "2.13.1",
                                           "1.8")
    session.hmc.add_resources({
        "cpcs": [{
            "properties": {
                "name": "cpc_1",
                "object-id": "cpc_1",
                "object-uri": "/api/cpcs/cpc_1",
            }
        }]
    })
    session.hmc.add_metric_values(
        zhmcclient_mock.FakedMetricObjectValues(
            group_name="dpm-system-usage-overview",
            resource_uri="/api/cpcs/cpc_1",
            timestamp=datetime.datetime.now(),
            values=[
                ("processor-usage", 1),
                ("network-usage", 2),
                ("storage-usage", 3),
                ("accelerator-usage", 4),
                ("crypto-usage", 5),
                ("power-consumption-watts", 100),
                ("temperature-celsius", 10),
                ("cp-shared-processor-usage", 1),
                ("cp-dedicated-processor-usage", 2),
                ("ifl-shared-processor-usage", 3),
                ("ifl-dedicated-processor-usage", 4),
            ]))
    return session
Esempio n. 4
0
 def test_delete_context(self):
     """Tests normal input, just needs to know no errors happen."""
     session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc",
                                            "2.13.1", "1.8")
     client = zhmcclient.Client(session)
     context = client.metrics_contexts.create({
         "anticipated-frequency-seconds":
         15,
         "metric-groups": ["metric-group"]
     })
     zhmc_prometheus_exporter.delete_metrics_context(session, context)
Esempio n. 5
0
 def test_normal_input(self):
     """Tests normal input with a generic metric group."""
     session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc",
                                            "2.13.1", "1.8")
     context = (zhmc_prometheus_exporter.create_metrics_context(
         session, {"metric-group": {
             "prefix": "pre",
             "fetch": True
         }}, "filename"))
     self.assertEqual(type(context), zhmcclient._metrics.MetricsContext)
     context.delete()
     session.logoff()
 def test_clecnup(self):
     # pylint: disable=no-self-use
     """Tests normal input, just needs to know no errors happen."""
     session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc",
                                            "2.13.1", "1.8")
     client = zhmcclient.Client(session)
     context = client.metrics_contexts.create({
         "anticipated-frequency-seconds":
         15,
         "metric-groups": ["dpm-system-usage-overview"]
     })
     zhmc_prometheus_exporter.cleanup(session, context, None)
 def test_normal_input(self):
     """Tests normal input with a generic metric group."""
     session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc",
                                            "2.13.1", "1.8")
     context, _ = zhmc_prometheus_exporter.create_metrics_context(
         session,
         {"dpm-system-usage-overview": {
             "prefix": "pre",
             "fetch": True
         }}, '2.14')
     # pylint: disable=protected-access
     self.assertEqual(type(context), zhmcclient._metrics.MetricsContext)
     context.delete()
     session.logoff()
Esempio n. 8
0
 def create_session_2():
     """
     Demonstrate how to populate a faked session with resources one by one.
     """
     session = zhmcclient_mock.FakedSession('fake-host', 'fake-hmc',
                                            '2.13.1', '1.8')
     cpc1 = session.hmc.cpcs.add({
         # object-id is auto-generated
         # object-uri is auto-generated
         'name': 'cpc_1',
         'dpm-enabled': False,
         'description': 'CPC #1',
     })
     cpc1.lpars.add({
         # object-id is auto-generated
         # object-uri is auto-generated
         'name': 'lpar_1',
         'description': 'LPAR #1 in CPC #1',
     })
     cpc2 = session.hmc.cpcs.add({
         # object-id is auto-generated
         # object-uri is auto-generated
         'name': 'cpc_2',
         'dpm-enabled': True,
         'description': 'CPC #2',
     })
     cpc2.partitions.add({
         # object-id is auto-generated
         # object-uri is auto-generated
         'name': 'partition_1',
         'description': 'Partition #1 in CPC #2',
     })
     adapter1 = cpc2.adapters.add({
         # object-id is auto-generated
         # object-uri is auto-generated
         'name': 'osa_1',
         'description': 'OSA #1 in CPC #2',
         'type': 'osd',
     })
     adapter1.ports.add({
         # element-id is auto-generated
         # element-uri is auto-generated
         'name': 'osa_1_port_1',
         'description': 'Port #1 of OSA #1',
     })
     return session
Esempio n. 9
0
 def test_retrieve_metrics(self):
     """Tests metrics retrieval with a fake CPC and fake metrics."""
     session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc",
                                            "2.13.1", "1.8")
     session.hmc.add_resources({
         "cpcs": [{
             "properties": {
                 "name": "cpc_1",
                 "object-uri": "cpc_1"
             }
         }]
     })
     session.hmc.metrics_contexts.add_metric_group_definition(
         zhmcclient_mock.FakedMetricGroupDefinition(
             name="dpm-system-usage-overview",
             types=[("metric", "integer-metric")]))
     session.hmc.metrics_contexts.add_metric_values(
         zhmcclient_mock.FakedMetricObjectValues(
             group_name="dpm-system-usage-overview",
             resource_uri="cpc_1",
             timestamp=datetime.datetime.now(),
             values=[("metric", 1)]))
     client = zhmcclient.Client(session)
     context = client.metrics_contexts.create({
         "anticipated-frequency-seconds":
         15,
         "metric-groups": ["dpm-system-usage-overview"]
     })
     expected_output = {
         "dpm-system-usage-"
         "overview": {
             "cpc_1": {
                 "metric": 1
             }
         }
     }
     actual_output = zhmc_prometheus_exporter.retrieve_metrics(context)
     self.assertEqual(expected_output, actual_output)
     context.delete()
     session.logoff()
def setup_faked_session():
    """Create a faked session."""

    session = zhmcclient_mock.FakedSession("fake-host", "fake-hmc", "2.13.1",
                                           "1.8")
    session.hmc.add_resources(
        {"cpcs": [{
            "properties": {
                "name": "cpc_1",
                "object-uri": "cpc_1"
            }
        }]})
    session.hmc.metrics_contexts.add_metric_group_definition(
        zhmcclient_mock.FakedMetricGroupDefinition(
            name="dpm-system-usage-overview",
            types=[("metric-1", "integer-metric")]))
    session.hmc.metrics_contexts.add_metric_values(
        zhmcclient_mock.FakedMetricObjectValues(
            group_name="dpm-system-usage-overview",
            resource_uri="cpc_1",
            timestamp=datetime.datetime.now(),
            values=[("metric-1", 1)]))
    return session
Esempio n. 11
0
def hmc_session(request, hmc_definition):
    """
    Pytest fixture representing the set of `zhmcclient.Session` objects to use
    for the end2end tests.

    Because the `hmc_definition` parameter of this fixture is again a fixture,
    `hmc_definition` needs to be imported as well when this fixture is used.

    Returns a `zhmcclient.Session` object that is logged on to the HMC.

    Upon teardown, the `zhmcclient.Session` object is logged off.
    """
    hd = hmc_definition

    # We use the cached skip reason from previous attempts
    skip_msg = getattr(hd, 'skip_msg', None)
    if skip_msg:
        pytest.skip("Skip reason from earlier attempt: {0}".format(skip_msg))

    if hd.faked_hmc_file:
        # A faked HMC

        # Read the faked HMC file
        filepath = os.path.join(os.path.dirname(hd.hmc_filepath),
                                hd.faked_hmc_file)
        try:
            with open(filepath) as fp:
                try:
                    data = yaml.load(fp, Loader=yamlordereddictloader.Loader)
                except (yaml.parser.ParserError,
                        yaml.scanner.ScannerError) as exc:
                    raise FakedHMCFileError(
                        "Invalid YAML syntax in faked HMC file {0!r}: {1} {2}".
                        format(filepath, exc.__class__.__name__, exc))
        except IOError as exc:
            if exc.errno == errno.ENOENT:
                raise FakedHMCFileError(
                    "The faked HMC file {0!r} was not found".format(filepath))
            else:
                raise

        client = data['faked_client']
        session = zhmcclient_mock.FakedSession(client['hmc_host'],
                                               client['hmc_name'],
                                               client['hmc_version'],
                                               client['api_version'])
        for cpc in client['cpcs']:
            session.hmc.cpcs.add(cpc['properties'])

    else:
        # A real HMC

        # Creating a session does not interact with the HMC (logon is deferred)
        session = zhmcclient.Session(hd.hmc_host, hd.hmc_userid,
                                     hd.hmc_password)

        # Check access to the HMC
        try:
            session.logon()
        except zhmcclient.Error as exc:
            msg = "Cannot log on to HMC {0} at {1} due to {2}: {3}". \
                format(hd.nickname, hd.hmc_host, exc.__class__.__name__, exc)
            hd.skip_msg = msg
            pytest.skip(msg)

    hd.skip_msg = None
    session.hmc_definition = hd

    yield session

    session.logoff()
Esempio n. 12
0
 def create_session_1():
     """
     Demonstrate how to populate a faked session with resources defined
     in a resource dictionary.
     """
     session = zhmcclient_mock.FakedSession('fake-host', 'fake-hmc',
                                            '2.13.1', '1.8')
     session.hmc.add_resources({
         'cpcs': [
             {
                 'properties': {
                     # object-id is auto-generated
                     # object-uri is auto-generated
                     'name': 'cpc_1',
                     'dpm-enabled': False,
                     'description': 'CPC #1',
                 },
                 'lpars': [
                     {
                         'properties': {
                             # object-id is auto-generated
                             # object-uri is auto-generated
                             'name': 'lpar_1',
                             'description': 'LPAR #1 in CPC #1',
                         },
                     },
                 ],
             },
             {
                 'properties': {
                     # object-id is auto-generated
                     # object-uri is auto-generated
                     'name': 'cpc_2',
                     'dpm-enabled': True,
                     'description': 'CPC #2',
                 },
                 'partitions': [
                     {
                         'properties': {
                             # object-id is auto-generated
                             # object-uri is auto-generated
                             'name': 'partition_1',
                             'description': 'Partition #1 in CPC #2',
                         },
                     },
                 ],
                 'adapters': [
                     {
                         'properties': {
                             # object-id is auto-generated
                             # object-uri is auto-generated
                             'name': 'osa_1',
                             'description': 'OSA #1 in CPC #2',
                             'type': 'osd',
                         },
                         'ports': [
                             {
                                 'properties': {
                                     # element-id is auto-generated
                                     # element-uri is auto-generated
                                     'name': 'osa_1_port_1',
                                     'description': 'Port #1 of OSA #1',
                                 },
                             },
                         ],
                     },
                 ],
             },
         ],
     })
     return session
Esempio n. 13
0
def setup_cpc(capsys, hmc_creds, fake_data, rt_config=None):
    """
    Set up and return some objects for the CPC that is to be used for testing.

    This function uses the get_test_cpc() function to determine the CPC to be
    used for testing. If no CPC has been defined, this function sets up a faked
    CPC using the zhmcclient mock support.

    Parameters:

      capsys: The pytest 'capsys' fixture the testcase function must specify as
        an argument and pass to this functionhere.

      hmc_creds (HmcCredentials): HMC credentials with CPC data.

      fake_data (dict): Input data in case a mock environment needs to be set
        up. The dict has the following keys:

        * hmc_host (string): Hostname or IP address of the faked HMC.

        * hmc_name (string): HMC name of the faked HMC.

        * hmc_version (string): HMC version of the faked HMC.

        * api_version (string): API version of the faked HMC.

        * cpc_properties (dict): Properties for the faked CPC. 'name' must be
          set.

      rt_config (zhmcclient.RetryTimeoutConfig): Retry / timeout config
        to override the default values. The default values used by this
        function are the global defaults defined for the zhmcclient package,
        whereby connection retries, connection timeout, operation timeout, and
        status timeout have been shortened. The resulting default values should
        be good for most function testcases.

    Returns:

      tuple: Tuple with the objects thathave been set up:

        * cpc_name (string): Name of the CPC to be used (some fake name or
          the real name that has been set up).

        * session (zhmcclient.Session or zhmcclient_mock-FakedSession):
          zhmcclient session object (faked or real) to be used for accessing
          the HMC managing that CPC.

        * client (zhmcclient.Client):
          zhmcclient Client object to be used for accessing the HMC managing
          that CPC.

        * cpc (zhmcclient.Cpc): Cpc resource object representing the CPC to be
          used (faked or real).

        * faked_cpc (zhmcclient_mock.FakedCpc): FakedCpc object in case a
          mock environment was set up (so the caller can add resources to it),
          or otherwise `None`.
    """

    cpc_name = get_test_cpc()

    if cpc_name is None:

        # No test CPC defined in the environment -> use mock support and
        # add a faked CPC.

        cpc_properties = fake_data['cpc_properties']
        cpc_name = cpc_properties['name']

        info(capsys, "Testing with faked CPC %r", cpc_name)

        session = zhmcclient_mock.FakedSession(fake_data['hmc_host'],
                                               fake_data['hmc_name'],
                                               fake_data['hmc_version'],
                                               fake_data['api_version'])

        faked_cpc = session.hmc.cpcs.add(cpc_properties)

    else:
        # A test CPC is defined in the environment -> use it!

        info(capsys, "Testing with CPC %r", cpc_name)

        eff_rt_config = DEFAULT_RT_CONFIG
        if rt_config:
            eff_rt_config.override_with(rt_config)

        cpc_item = hmc_creds.get_cpc_item(cpc_name)

        assert cpc_item, "HMC credentials file not found: {!r}".\
            format(hmc_creds.filepath)

        session = zhmcclient.Session(cpc_item['hmc_host'],
                                     cpc_item['hmc_userid'],
                                     cpc_item['hmc_password'],
                                     retry_timeout_config=eff_rt_config)

        faked_cpc = None

    client = zhmcclient.Client(session)

    cpc = client.cpcs.find(name=cpc_name)

    return cpc_name, session, client, cpc, faked_cpc
Esempio n. 14
0
def create_session_1():
    session = zhmcclient_mock.FakedSession('fake-host', 'fake-hmc', '2.13.1',
                                           '1.8')
    session.hmc.add_resources({
        'cpcs': [
            {
                'properties': {
                    # object-id is auto-generated
                    # object-uri is auto-generated
                    'name': 'cpc_1',
                    'dpm-enabled': False,
                    'description': 'CPC #1',
                },
                'lpars': [
                    {
                        'properties': {
                            # object-id is auto-generated
                            # object-uri is auto-generated
                            'name': 'lpar_1',
                            'description': 'LPAR #1 in CPC #1',
                        },
                    },
                ],
            },
            {
                'properties': {
                    # object-id is auto-generated
                    # object-uri is auto-generated
                    'name': 'cpc_2',
                    'dpm-enabled': True,
                    'description': 'CPC #2',
                    'processor-count-ifl': 10,
                    'storage-customer': 2048,
                    'se-version': '2.13.1'
                },
                'partitions': [
                    {
                        'properties': {
                            # object-id is auto-generated
                            # object-uri is auto-generated
                            'name': 'OpenStack-fake-mini-1',
                            'description': 'Partition #1 in CPC #2',
                        },
                    },
                    {
                        'properties': PARTITION1
                    },
                    {
                        'properties': PARTITION2
                    },
                    {
                        'properties': PARTITION3
                    },
                ],
                'adapters': [
                    {
                        'properties': {
                            # object-id is auto-generated
                            # object-uri is auto-generated
                            'name': 'osa_1',
                            'description': 'OSA #1 in CPC #2',
                            'type': 'osd',
                        },
                        'ports': [
                            {
                                'properties': {
                                    # element-id is auto-generated
                                    # element-uri is auto-generated
                                    'name': 'osa_1_port_1',
                                    'description': 'Port #1 of OSA #1',
                                },
                            },
                        ],
                    },
                ],
            },
            {
                'properties': {
                    # object-id is auto-generated
                    # object-uri is auto-generated
                    'name': 'cpc_3',
                    'dpm-enabled': True,
                    'description': 'CPC #3',
                    'processor-count-ifl': 10,
                    'storage-customer': 1024,
                },
                'partitions': [
                    {
                        'properties': {
                            # object-id is auto-generated
                            # object-uri is auto-generated
                            'name': 'OpenStack-fakemini-38400000-'
                            '8cf0-11bd-b23e-10b96e4ef00d',
                            'description': 'Partition #1 in CPC #2',
                            'initial-memory': 512,
                            'ifl-processors': 1,
                        },
                        'hbas': [
                            {
                                'properties': {
                                    'name': 'hba1',
                                    'wwpn': '0x5565656565656565',
                                    'adapter-port-uri':
                                    '/api/cpcs/3/adapters/3',
                                    'hba-uris': 'test-hba',
                                }
                            },
                        ],
                    },
                ],
                'adapters': [
                    {
                        'properties': {
                            # object-id is auto-generated
                            # object-uri is auto-generated
                            'name': 'osa_1',
                            'description': 'OSA #1 in CPC #3',
                            'type': 'osd',
                        },
                        'ports': [
                            {
                                'properties': {
                                    # element-id is auto-generated
                                    # element-uri is auto-generated
                                    'name': 'osa_1_port_1',
                                    'description': 'Port #1 of OSA #1',
                                },
                            },
                        ],
                    },
                ],
            },
        ],
    })
    return session