예제 #1
0
def test_uuid_link_manager_update_links_no_links_yet():
    uuid_link_manager = UUIDLinkManager(
        received_outputs_dir=received_outputs_dir,
        data_source_dir=data_source_push_agent_dir,
    )
    uuid_link_manager.update_links({})

    assert not received_outputs_dir.exists()
    assert not data_source_push_agent_dir.exists()
예제 #2
0
def test_uuid_link_manager_create_link_and_target_dir():
    hostname = "my-hostname"
    raw_uuid = "59e631e9-de89-40d6-9662-ba54569a24fb"

    uuid_link_manager = UUIDLinkManager(
        received_outputs_dir=received_outputs_dir,
        data_source_dir=data_source_push_agent_dir,
    )
    uuid_link_manager.create_link(hostname, UUID(raw_uuid), create_target_dir=True)

    assert len(list(received_outputs_dir.iterdir())) == 1
    assert len(list(data_source_push_agent_dir.iterdir())) == 1

    link = next(iter(uuid_link_manager))

    assert link.source == received_outputs_dir.joinpath(raw_uuid)
    assert link.target == data_source_push_agent_dir.joinpath(hostname)
예제 #3
0
def test_uuid_link_manager_create_link_to_different_uuid():
    hostname = "my-hostname"
    raw_uuid_old = "59e631e9-de89-40d6-9662-ba54569a24fb"
    raw_uuid_new = "db1ea77f-330e-4fb5-b59e-925f55290533"

    uuid_link_manager = UUIDLinkManager(
        received_outputs_dir=received_outputs_dir,
        data_source_dir=data_source_push_agent_dir,
    )
    uuid_link_manager.create_link(hostname, UUID(raw_uuid_old), create_target_dir=False)
    uuid_link_manager.create_link(hostname, UUID(raw_uuid_new), create_target_dir=False)

    assert len(list(received_outputs_dir.iterdir())) == 1
    assert not data_source_push_agent_dir.exists()

    link = next(iter(uuid_link_manager))

    assert link.source == received_outputs_dir.joinpath(raw_uuid_new)
    assert link.target == data_source_push_agent_dir.joinpath(hostname)
예제 #4
0
def test_openapi_host_link_uuid_204(
        aut_user_auth_wsgi_app: WebTestAppForCMK) -> None:
    uuid = UUID("1409ac78-6548-4138-9285-12484409ddf2")
    aut_user_auth_wsgi_app.call_method(
        "put",
        _URL_LINK_UUID,
        params=json.dumps({"uuid": str(uuid)}),
        status=204,
        headers={"Accept": "application/json"},
        content_type="application/json; charset=utf-8",
    )
    assert (UUIDLinkManager(
        received_outputs_dir=received_outputs_dir,
        data_source_dir=data_source_push_agent_dir,
    ).get_uuid("example.com") == uuid)
예제 #5
0
def test_uuid_link_manager_create_existing_link():
    hostname = "my-hostname"
    raw_uuid = "59e631e9-de89-40d6-9662-ba54569a24fb"

    uuid_link_manager = UUIDLinkManager(
        received_outputs_dir=received_outputs_dir,
        data_source_dir=data_source_push_agent_dir,
    )
    uuid_link_manager.create_link(hostname, UUID(raw_uuid), create_target_dir=False)
    # second time should be no-op, at least not fail
    uuid_link_manager.create_link(hostname, UUID(raw_uuid), create_target_dir=False)
예제 #6
0
    def _wrap_tls(self) -> ssl.SSLSocket:
        controller_uuid = UUIDLinkManager(
            received_outputs_dir=paths.received_outputs_dir,
            data_source_dir=paths.data_source_push_agent_dir,
        ).get_uuid(self.host_name)

        if controller_uuid is None:
            raise MKFetcherError("Agent controller not registered")

        self._logger.debug("Reading data from agent via TLS socket")
        try:
            ctx = ssl.create_default_context(cafile=paths.root_cert_file)
            ctx.load_cert_chain(certfile=paths.site_cert_file)
            return ctx.wrap_socket(self._socket,
                                   server_hostname=str(controller_uuid))
        except ssl.SSLError as e:
            raise MKFetcherError("Error establishing TLS connection") from e
예제 #7
0
def test_uuid_link_manager_update_links_no_host():
    hostname = "my-hostname"
    raw_uuid = "59e631e9-de89-40d6-9662-ba54569a24fb"

    uuid_link_manager = UUIDLinkManager(
        received_outputs_dir=received_outputs_dir,
        data_source_dir=data_source_push_agent_dir,
    )
    uuid_link_manager.create_link(hostname, UUID(raw_uuid), create_target_dir=False)
    uuid_link_manager.update_links({})

    assert list(received_outputs_dir.iterdir()) == []
    assert not data_source_push_agent_dir.exists()
예제 #8
0
def test_uuid_link_manager_update_links_host_no_push():
    hostname = "my-hostname"
    raw_uuid = "59e631e9-de89-40d6-9662-ba54569a24fb"

    uuid_link_manager = UUIDLinkManager(
        received_outputs_dir=received_outputs_dir,
        data_source_dir=data_source_push_agent_dir,
    )
    uuid_link_manager.create_link(hostname, UUID(raw_uuid), create_target_dir=False)
    uuid_link_manager.update_links({hostname: {}})

    assert len(list(received_outputs_dir.iterdir())) == 1
    assert not data_source_push_agent_dir.exists()

    link = next(iter(uuid_link_manager))

    assert link.source == received_outputs_dir.joinpath(raw_uuid)
    assert link.target == data_source_push_agent_dir.joinpath(hostname)
예제 #9
0
def test_uuid_link_manager_unlink_sources():
    hostname_1 = "my-hostname-1"
    raw_uuid_1 = "59e631e9-de89-40d6-9662-ba54569a24fb"
    hostname_2 = "my-hostname-2"
    raw_uuid_2 = "db1ea77f-330e-4fb5-b59e-925f55290533"

    uuid_link_manager = UUIDLinkManager(
        received_outputs_dir=received_outputs_dir,
        data_source_dir=data_source_push_agent_dir,
    )
    uuid_link_manager.create_link(hostname_1, UUID(raw_uuid_1), create_target_dir=False)
    uuid_link_manager.create_link(hostname_2, UUID(raw_uuid_2), create_target_dir=False)

    uuid_link_manager.unlink_sources([hostname_1])

    sources = list(received_outputs_dir.iterdir())
    assert len(sources) == 1
    assert sources[0].name == raw_uuid_2

    assert not data_source_push_agent_dir.exists()
예제 #10
0
def test_uuid_link_manager_update_links_host_push(create_target_dir: bool) -> None:
    hostname = "my-hostname"
    raw_uuid = "59e631e9-de89-40d6-9662-ba54569a24fb"

    uuid_link_manager = UUIDLinkManager(
        received_outputs_dir=received_outputs_dir,
        data_source_dir=data_source_push_agent_dir,
    )
    # During link creation the cmk_agent_connection could possibly not be calculated yet,
    # ie. push-agent or other.
    # During links update the target dirs are created for push hosts.
    uuid_link_manager.create_link(hostname, UUID(raw_uuid), create_target_dir=create_target_dir)
    uuid_link_manager.update_links({hostname: {"cmk_agent_connection": "push-agent"}})

    assert len(list(received_outputs_dir.iterdir())) == 1
    assert len(list(data_source_push_agent_dir.iterdir())) == 1

    link = next(iter(uuid_link_manager))

    assert link.source == received_outputs_dir.joinpath(raw_uuid)
    assert link.target == data_source_push_agent_dir.joinpath(hostname)
예제 #11
0
def test_uuid_link_manager_update_links_no_host_but_ready_or_discoverable(
    folder: Path,
    has_link: bool,
) -> None:
    hostname = "my-hostname"
    raw_uuid = "59e631e9-de89-40d6-9662-ba54569a24fb"

    folder.mkdir(parents=True, exist_ok=True)
    with get_r4r_filepath(folder, UUID(raw_uuid)).open("w") as f:
        f.write("")

    uuid_link_manager = UUIDLinkManager(
        received_outputs_dir=received_outputs_dir,
        data_source_dir=data_source_push_agent_dir,
    )
    uuid_link_manager.create_link(hostname, UUID(raw_uuid), create_target_dir=False)
    uuid_link_manager.update_links({})

    if has_link:
        assert len(list(received_outputs_dir.iterdir())) == 1
    else:
        assert list(received_outputs_dir.iterdir()) == []

    assert not data_source_push_agent_dir.exists()
예제 #12
0
def _rename_host_in_uuid_link_manager(
    uuid_link_manager: UUIDLinkManager,
    successful_renamings: Sequence[_Tuple[HostName, HostName]],
) -> Sequence[_Tuple[HostName, HostName]]:
    return uuid_link_manager.rename(successful_renamings)