コード例 #1
0
 def test_get_request(
     self,
     monkeypatch: pytest.MonkeyPatch,
 ) -> None:
     request = Request({})
     request.set_var("site_id", "NO_SITE")
     request.set_var("to_delete", "['x/y/z.txt', 'abc.ending', '/ä/☃/☕']")
     request.set_var("config_generation", "123")
     request.files = werkzeug_datastructures.ImmutableMultiDict({
         "sync_archive":
         werkzeug_datastructures.FileStorage(
             stream=io.BytesIO(b"some data"),
             filename="sync_archive",
             name="sync_archive",
         )
     })
     monkeypatch.setattr(
         activate_changes,
         "_request",
         request,
     )
     assert (activate_changes.AutomationReceiveConfigSync().get_request() ==
             activate_changes.ReceiveConfigSyncRequest(
                 site_id=SiteId("NO_SITE"),
                 sync_archive=b"some data",
                 to_delete=["x/y/z.txt", "abc.ending", "/ä/☃/☕"],
                 config_generation=123,
             ))
コード例 #2
0
def test_automation_receive_config_sync(monkeypatch, tmp_path):
    remote_path = tmp_path / "remote"
    monkeypatch.setattr(cmk.utils.paths, "omd_root", remote_path)

    # Disable for the moment, because the unit test fake environment is not ready for this yet
    monkeypatch.setattr(
        cmk.gui.watolib.activate_changes, "_execute_post_config_sync_actions", lambda site_id: None
    )

    remote_path.mkdir(parents=True, exist_ok=True)

    dir_to_symlink_file = remote_path.joinpath("working-symlink/file")
    dir_to_symlink_file.parent.mkdir(parents=True, exist_ok=True)
    with dir_to_symlink_file.open("w", encoding="utf-8") as f:
        f.write("ig")

    dir_to_file = remote_path.joinpath("dir-to-file/file")
    dir_to_file.parent.mkdir(parents=True, exist_ok=True)
    with dir_to_file.open("w", encoding="utf-8") as f:
        f.write("fi")

    file_to_dir = remote_path.joinpath("file-to-dir")
    with file_to_dir.open("w", encoding="utf-8") as f:
        f.write("za")

    to_delete_path = remote_path.joinpath("to_delete")
    with to_delete_path.open("w", encoding="utf-8") as f:
        f.write("äää")

    assert to_delete_path.exists()
    assert not remote_path.joinpath("etc/abc").exists()
    assert not remote_path.joinpath("ding").exists()

    automation = activate_changes.AutomationReceiveConfigSync()
    automation.execute(
        activate_changes.ReceiveConfigSyncRequest(
            site_id="remote",
            sync_archive=_get_test_sync_archive(tmp_path.joinpath("central")),
            to_delete=[
                "to_delete",
                "working-symlink/file",
                "file-to-dir",
            ],
            config_generation=0,
        )
    )

    assert not to_delete_path.exists()
    assert remote_path.joinpath("etc/abc").exists()
    assert remote_path.joinpath("ding").exists()

    assert not dir_to_symlink_file.exists()
    assert dir_to_symlink_file.parent.is_symlink()

    assert not dir_to_file.parent.is_dir()
    assert dir_to_file.parent.exists()

    assert file_to_dir.is_dir()
    assert file_to_dir.joinpath("aaa").exists()