Exemple #1
0
def test_discovered_host_labels_add():
    labels_1 = DiscoveredHostLabels()
    labels_1.add_label(HostLabel(u"äbc", u"123", "plugin_1"))

    labels_2 = DiscoveredHostLabels()
    labels_2.add_label(HostLabel(u"xyz", u"blä", "plugin_2"))

    new_labels = labels_1 + labels_2
    assert new_labels.to_dict() == {
        u"äbc": {
            "value": u"123",
            "plugin_name": "plugin_1",
        },
        u"xyz": {
            "value": u"blä",
            "plugin_name": "plugin_2",
        },
    }

    labels_1 += labels_2
    assert labels_1.to_dict() == {
        u"äbc": {
            "value": u"123",
            "plugin_name": "plugin_1",
        },
        u"xyz": {
            "value": u"blä",
            "plugin_name": "plugin_2",
        },
    }
def test_discovered_host_labels_repr():
    labels = DiscoveredHostLabels()
    labels.add_label(HostLabel(u"äbc", u"123", "plugin_1"))
    labels.add_label(HostLabel(u"ccc", u"ddd", "plugin_2"))
    assert repr(
        labels
    ) == "DiscoveredHostLabels(HostLabel('ccc', 'ddd', plugin_name='plugin_2'), HostLabel('äbc', '123', plugin_name='plugin_1'))"
def test_discovered_host_labels_repr():
    labels = DiscoveredHostLabels()
    labels.add_label(HostLabel(u"äbc", u"123", SectionName("plugin_1")))
    labels.add_label(HostLabel(u"ccc", u"ddd", SectionName("plugin_2")))
    assert repr(labels) == (
        "DiscoveredHostLabels("  #
        "HostLabel('ccc', 'ddd', plugin_name=SectionName('plugin_2')), "  #
        "HostLabel('äbc', '123', plugin_name=SectionName('plugin_1')))")
Exemple #4
0
def test_discovered_host_labels_to_list():
    labels = DiscoveredHostLabels()
    assert labels.to_list() == []

    labels.add_label(HostLabel(u"äbc", u"123", "plugin_1"))
    labels.add_label(HostLabel(u"xyz", u"blä", "plugin_2"))

    assert labels.to_list() == [
        HostLabel(u"xyz", u"blä", "plugin_2"),
        HostLabel(u"äbc", u"123", "plugin_1")
    ]
def test_discovered_host_labels_to_list():
    labels = DiscoveredHostLabels()
    assert labels.to_list() == []

    labels.add_label(HostLabel("äbc", "123", SectionName("plugin_1")))
    labels.add_label(HostLabel("xyz", "blä", SectionName("plugin_2")))

    assert labels.to_list() == [
        HostLabel("xyz", "blä", SectionName("plugin_2")),
        HostLabel("äbc", "123", SectionName("plugin_1")),
    ]
Exemple #6
0
class DiscoveryResult(object):  # pylint: disable=useless-object-inheritance
    """
    The result of the discovery as a whole.

    Much like in the case of the check result, this also makes sure
    that yield-based discovery functions run, and that no exceptions
    get lost in the laziness.
    """

    # TODO: Add some more consistency checks here.
    def __init__(self, result=()):
        self.entries = []
        self.labels = DiscoveredHostLabels()
        if not result:
            # discovering nothing is valid!
            return
        for entry in result:
            if isinstance(entry, DiscoveredHostLabels):
                self.labels += entry
            elif isinstance(entry, HostLabel):
                self.labels.add_label(entry)
            # preparation for ServiceLabel Discovery
            #elif isinstance(entry, Service):
            #
            else:
                self.entries.append(DiscoveryEntry(entry))
        self.entries.sort(key=repr)

    def __eq__(self, other):
        return self.entries == other.entries and self.labels == other.labels

    # TODO: Very questionable __repr__ conversion, leading to even more
    # interesting typing Kung Fu...
    def __repr__(self):
        # type: () -> str
        entries = [o for o in self.entries
                   if isinstance(o, object)]  # type: List[object]
        host_labels = [
            HostLabel(six.text_type(k), six.text_type(self.labels[k]))
            for k in self.labels
        ]  # type: List[object]
        return "DiscoveryResult(%r)" % (entries + host_labels, )

    # TODO: Very obscure and inconsistent __str__ conversion...
    def __str__(self):
        return "%s%s" % (map(
            tuple, self.entries), [self.labels[k].label for k in self.labels])
Exemple #7
0
def test_discovered_host_labels_to_dict():
    labels = DiscoveredHostLabels()
    assert labels.to_dict() == {}

    labels.add_label(HostLabel(u"äbc", u"123", "plugin_1"))
    labels.add_label(HostLabel(u"xyz", u"blä", "plugin_2"))

    assert labels.to_dict() == {
        u"äbc": {
            "value": u"123",
            "plugin_name": "plugin_1",
        },
        u"xyz": {
            "value": u"blä",
            "plugin_name": "plugin_2",
        },
    }
def test_discovered_host_labels_to_dict():
    labels = DiscoveredHostLabels()
    assert labels.to_dict() == {}

    labels.add_label(HostLabel("äbc", "123", SectionName("plugin_1")))
    labels.add_label(HostLabel("xyz", "blä", SectionName("plugin_2")))

    assert labels.to_dict() == {
        "äbc": {
            "value": "123",
            "plugin_name": "plugin_1",
        },
        "xyz": {
            "value": "blä",
            "plugin_name": "plugin_2",
        },
    }
Exemple #9
0
class DiscoveryResult(object):
    """
    The result of the discovery as a whole.

    Much like in the case of the check result, this also makes sure
    that yield-based discovery functions run, and that no exceptions
    get lost in the laziness.
    """

    # TODO: Add some more consistency checks here.
    def __init__(self, result=()):
        self.entries = []
        self.labels = DiscoveredHostLabels()
        if not result:
            # discovering nothing is valid!
            return
        for entry in result:
            if isinstance(entry, DiscoveredHostLabels):
                self.labels += entry
            elif isinstance(entry, HostLabel):
                self.labels.add_label(entry)
            # preparation for ServiceLabel Discovery
            #elif isinstance(entry, Service):
            #
            else:
                self.entries.append(DiscoveryEntry(entry))
        self.entries.sort(key=repr)

    def __eq__(self, other):
        return self.entries == other.entries and self.labels == other.labels

    def __repr__(self):
        args = self.entries + [
            HostLabel(six.text_type(k), six.text_type(self.labels[k]))
            for k in self.labels
        ]
        return "DiscoveryResult(%r)" % (args, )

    def __str__(self):
        return "%s%s" % (map(
            tuple, self.entries), [self.labels[k].label for k in self.labels])
def test_discovered_host_labels_sub():
    labels_1 = DiscoveredHostLabels()
    labels_1.add_label(HostLabel("foo", "bär", SectionName("plugin_1")))
    labels_1.add_label(HostLabel("foo2", "bär2", SectionName("plugin_2")))

    labels_2 = DiscoveredHostLabels()
    labels_2.add_label(HostLabel("foo", "bär", SectionName("plugin_1")))

    assert (labels_1 - labels_2).to_dict() == {
        "foo2": {
            "value": "bär2",
            "plugin_name": "plugin_2",
        },
    }

    assert (labels_2 - labels_1).to_dict() == {}