Esempio n. 1
0
 def test_serialize_in_sync_with_deserialize(self):
     ty = ObjectRefType.Host
     ident = "node1"
     ref = ObjectRef.deserialize(ObjectRef(ty, ident).serialize())
     assert ref.object_type == ty
     assert ref.ident == ident
     assert ref.labels == {}
Esempio n. 2
0
 def test_serialize_in_sync_with_deserialize_with_labels(self):
     ty = ObjectRefType.Host
     ident = "node1"
     labels = {"abc": "123"}
     ref = ObjectRef.deserialize(ObjectRef(ty, ident, labels).serialize())
     assert ref.object_type == ty
     assert ref.ident == ident
     assert ref.labels == labels
Esempio n. 3
0
 def _deserialize(raw: object) -> ChangeSpec:
     if not isinstance(raw, dict):
         raise ValueError("expected a dictionary")
     # TODO: Parse raw's entries, too, below we have our traditional 'wishful typing'... :-P
     if isinstance(raw["object"], tuple):
         # Migrate the pre 2.0 change entries (Two element tuple: ("Folder/Host", "ident"))
         type_name, ident = raw["object"]
         if type_name in ("CMEHost", "CREHost"):
             type_name = "Host"
         elif type_name in ("CMEFolder", "CREFolder"):
             type_name = "Folder"
         raw["object"] = ObjectRef(ObjectRefType(type_name), ident)
     else:
         raw["object"] = ObjectRef.deserialize(
             raw["object"]) if raw["object"] else None
     return raw
Esempio n. 4
0
 def test_serialize(self):
     ty = ObjectRefType.Host
     ident = "node1"
     assert ObjectRef(ty, ident).serialize() == {
         "ident": "node1",
         "object_type": "Host"
     }
Esempio n. 5
0
 def _deserialize(raw: object) -> "AuditLogStore.Entry":
     if not isinstance(raw, dict):
         raise ValueError("expected a dictionary")
     # TODO: Parse raw's entries, too, below we have our traditional 'wishful typing'... :-P
     raw["text"] = HTML(raw["text"][1]) if raw["text"][0] == "html" else raw["text"][1]
     raw["object_ref"] = ObjectRef.deserialize(raw["object_ref"]) if raw["object_ref"] else None
     return AuditLogStore.Entry(**raw)
Esempio n. 6
0
 def test_serialization_with_labels(self):
     ty = ObjectRefType.Host
     ident = "node1"
     assert ObjectRef(ty, ident, {
         "a": "b"
     }).serialize() == {
         "ident": "node1",
         "object_type": "Host",
         "labels": {
             "a": "b"
         },
     }
Esempio n. 7
0
 def fixture_entry(self):
     return {
         "id": "d60ca3d4-7201-4a89-b66f-2f156192cad2",
         "action_name": "create-host",
         "text": "Created new host node1.",
         "object": ObjectRef(ObjectRefType.Host, "node1"),
         "user_id": "cmkadmin",
         "domains": ["check_mk"],
         "time": 1605461248.786142,
         "need_sync": True,
         "need_restart": True,
     }
Esempio n. 8
0
    def test_read_pre_20_host_change(self, store, old_type, ref_type):
        with store._path.open("wb") as f:
            f.write(
                repr({
                    "id": "d60ca3d4-7201-4a89-b66f-2f156192cad2",
                    "action_name": "create-host",
                    "text": "Created new host node1.",
                    "object": (old_type, "node1"),
                    "user_id": "cmkadmin",
                    "domains": ["check_mk"],
                    "time": 1605461248.786142,
                    "need_sync": True,
                    "need_restart": True,
                }).encode("utf-8") + b"\0")

        assert store.read()[0]["object"] == ObjectRef(ref_type, "node1")
Esempio n. 9
0
def test__migrate_pre_2_0_audit_log(
    uc: update_config.UpdateConfig,
    old_audit_log: Path,
    new_path: Path,
) -> None:
    assert not new_path.exists()
    assert old_audit_log.exists()

    uc._migrate_pre_2_0_audit_log()

    assert new_path.exists()
    assert not old_audit_log.exists()

    # Now try to parse the migrated log with the new logic
    log_store = AuditLogStore(new_path)
    assert log_store.read() == [
        AuditLogStore.Entry(
            time=1604991356,
            object_ref=None,
            user_id="cmkadmin",
            action="liveproxyd-activate",
            text="Activating changes of Livestatus Proxy configuration",
            diff_text=None,
        ),
        AuditLogStore.Entry(
            time=1604991356,
            object_ref=None,
            user_id="cmkadmin",
            action="liveproxyd-activate",
            text="Activating changes of Livestatus Proxy configuration",
            diff_text=None,
        ),
        AuditLogStore.Entry(
            time=1604992040,
            object_ref=ObjectRef(ObjectRefType.Host, "heute2"),
            user_id="cmkadmin",
            action="create-host",
            text="Created new host heute2.",
            diff_text=None,
        ),
        AuditLogStore.Entry(
            time=1604992159,
            object_ref=ObjectRef(ObjectRefType.Host, "heute2"),
            user_id="cmkadmin",
            action="delete-host",
            text="Deleted host heute2",
            diff_text=None,
        ),
        AuditLogStore.Entry(
            time=1604992163,
            object_ref=ObjectRef(ObjectRefType.Host, "heute1"),
            user_id="cmkadmin",
            action="create-host",
            text="Created new host heute1.",
            diff_text=None,
        ),
        AuditLogStore.Entry(
            time=1604992166,
            object_ref=ObjectRef(ObjectRefType.Host, "heute12"),
            user_id="cmkadmin",
            action="create-host",
            text="Created new host heute12.",
            diff_text=None,
        ),
    ]
Esempio n. 10
0
 def test_serialize_represented_as_native_types(self):
     serialized = ObjectRef(ObjectRefType.Host, "h1").serialize()
     as_text = repr(serialized)
     assert ast.literal_eval(as_text) == serialized
Esempio n. 11
0
def make_user_object_ref(user_id: UserId) -> ObjectRef:
    return ObjectRef(ObjectRefType.User, str(user_id))