Esempio n. 1
0
def test_create_nagios_host_spec(hostname, result, monkeypatch):
    ts = Scenario().add_host("localhost")
    ts.add_host("host2")
    ts.add_cluster("cluster1")

    ts.add_cluster("cluster2", nodes=["node1", "node2"])
    ts.add_host("node1")
    ts.add_host("node2")
    ts.add_host("switch")
    ts.set_option("ipaddresses", {
        "node1": "127.0.0.1",
        "node2": "127.0.0.2",
    })

    ts.set_option("extra_host_conf", {
        "alias": [(u'lOCALhost', ['localhost']),],
    })

    ts.set_option(
        "extra_host_conf", {
            "alias": [
                (u'lOCALhost', ['host2']),
                (u'CLUSTer', ['cluster2']),
            ],
            "parents": [('switch', ['node1', 'node2']),],
        })

    outfile = io.StringIO()
    cfg = core_nagios.NagiosConfig(outfile, [hostname])

    config_cache = ts.apply(monkeypatch)
    host_attrs = core_config.get_host_attributes(hostname, config_cache)

    host_spec = core_nagios._create_nagios_host_spec(cfg, config_cache, hostname, host_attrs)
    assert host_spec == result
Esempio n. 2
0
def test_create_nagios_host_spec(hostname_str: str, result: Dict[str, str],
                                 monkeypatch: MonkeyPatch) -> None:
    if cmk_version.is_managed_edition():
        result = result.copy()
        result["_CUSTOMER"] = "provider"

    ts = Scenario()
    ts.add_host(HostName("localhost"))
    ts.add_host(HostName("host2"))
    ts.add_cluster(HostName("cluster1"))

    ts.add_cluster(HostName("cluster2"), nodes=["node1", "node2"])
    ts.add_host(HostName("node1"))
    ts.add_host(HostName("node2"))
    ts.add_host(HostName("switch"))
    ts.set_option(
        "ipaddresses",
        {
            HostName("node1"): "127.0.0.1",
            HostName("node2"): "127.0.0.2",
        },
    )

    ts.set_option(
        "extra_host_conf",
        {
            "alias": [
                ("lOCALhost", ["localhost"]),
            ],
        },
    )

    ts.set_option(
        "extra_host_conf",
        {
            "alias": [
                ("lOCALhost", ["host2"]),
                ("CLUSTer", ["cluster2"]),
            ],
            "parents": [
                ("switch", ["node1", "node2"]),
            ],
        },
    )

    hostname = HostName(hostname_str)
    outfile = io.StringIO()
    cfg = core_nagios.NagiosConfig(outfile, [hostname])

    config_cache = ts.apply(monkeypatch)
    host_attrs = core_config.get_host_attributes(hostname, config_cache)

    host_spec = core_nagios._create_nagios_host_spec(cfg, config_cache,
                                                     hostname, host_attrs)
    assert host_spec == result
Esempio n. 3
0
def test_create_nagios_servicedefs_active_check(
    active_checks: Tuple[str, Sequence[Mapping[str, str]]],
    active_check_info: Mapping[str, Mapping[str, str]],
    host_attrs: Dict[str, Any],
    expected_result: str,
    monkeypatch: MonkeyPatch,
) -> None:
    monkeypatch.setattr(config.HostConfig, "active_checks", active_checks)
    monkeypatch.setattr(config, "active_check_info", active_check_info)

    cache = config.get_config_cache()
    cache.initialize()

    hostname = HostName("my_host")
    outfile = io.StringIO()
    cfg = core_nagios.NagiosConfig(outfile, [hostname])
    core_nagios._create_nagios_servicedefs(cfg, cache, "my_host", host_attrs)

    assert outfile.getvalue() == expected_result
Esempio n. 4
0
def test_create_nagios_servicedefs_invalid_args(
    active_checks: Tuple[str, Sequence[Mapping[str, str]]],
    active_check_info: Mapping[str, Mapping[str, str]],
    host_attrs: Dict[str, Any],
    error_message: str,
    monkeypatch: MonkeyPatch,
) -> None:
    monkeypatch.setattr(config.HostConfig, "active_checks", active_checks)
    monkeypatch.setattr(config, "active_check_info", active_check_info)

    cache = config.get_config_cache()
    cache.initialize()

    hostname = HostName("my_host")
    outfile = io.StringIO()
    cfg = core_nagios.NagiosConfig(outfile, [hostname])

    with pytest.raises(exceptions.MKGeneralException, match=error_message):
        core_nagios._create_nagios_servicedefs(cfg, cache, "my_host",
                                               host_attrs)