def test_write_and_read_host_attributes(tmp_path, attributes, monkeypatch):
    folder_path = str(tmp_path)
    # Write/Read operations always require a valid user
    monkeypatch.setattr(config, "user", config.LoggedInSuperUser())

    # Used to write the data
    write_data_folder = watolib.Folder("testfolder",
                                       folder_path=folder_path,
                                       parent_folder=None)

    # Used to read the previously written data
    read_data_folder = watolib.Folder("testfolder",
                                      folder_path=folder_path,
                                      parent_folder=None)

    environ = dict(create_environ(), REQUEST_URI='')
    with AppContext(DummyApplication(environ, None)), \
            RequestContext(htmllib.html(Request(environ))):
        # Write data
        # Note: The create_hosts function modifies the attributes dict, adding a meta_data key inplace
        write_data_folder.create_hosts([("testhost", attributes, [])])
        write_folder_hosts = write_data_folder.hosts()
        assert len(write_folder_hosts) == 1

        # Read data back
        read_folder_hosts = read_data_folder.hosts()
        assert len(read_folder_hosts) == 1
        for _, host in read_folder_hosts.items():
            assert host.attributes() == attributes
Example #2
0
def test_create_nested_folders(request_context):
    with in_chdir("/"):
        root = watolib.Folder.root_folder()

        folder1 = watolib.Folder("folder1", parent_folder=root)
        folder1.persist_instance()

        folder2 = watolib.Folder("folder2", parent_folder=folder1)
        folder2.persist_instance()

        shutil.rmtree(os.path.dirname(folder1.wato_info_path()))
def test_folder_times(register_builtin_html):
    root = watolib.Folder.root_folder()

    with freezegun.freeze_time(datetime.datetime(2020, 2, 2, 2, 2, 2)):
        current = time.time()
        folder = watolib.Folder('test', parent_folder=root)
        folder.save()

    folder = watolib.Folder("test", "")
    meta_data = folder.attributes()['meta_data']
    assert int(meta_data['created_at']) == int(current)
    assert int(meta_data['updated_at']) == int(current)

    folder.persist_instance()
    assert int(meta_data['updated_at']) > int(current)
Example #4
0
def test_folder_times(request_context):
    root = watolib.Folder.root_folder()

    with freezegun.freeze_time(datetime.datetime(2020, 2, 2, 2, 2, 2)):
        current = time.time()
        folder = watolib.Folder("test", parent_folder=root)
        folder.save()

    folder = watolib.Folder("test", "")
    meta_data = folder.attributes()["meta_data"]
    assert int(meta_data["created_at"]) == int(current)
    assert int(meta_data["updated_at"]) == int(current)

    folder.persist_instance()
    assert int(meta_data["updated_at"]) > int(current)
Example #5
0
def test_next_network_scan_at(allowed, last_end, next_time):
    folder = watolib.Folder(name="bla",
                            title="Bla",
                            attributes={
                                "network_scan": {
                                    'exclude_ranges': [],
                                    'ip_ranges':
                                    [('ip_range', ('10.3.1.1', '10.3.1.100'))],
                                    'run_as':
                                    u'cmkadmin',
                                    'scan_interval':
                                    300,
                                    'set_ipaddress':
                                    True,
                                    'tag_criticality':
                                    'offline',
                                    'time_allowed':
                                    allowed,
                                },
                                "network_scan_result": {
                                    "end": last_end,
                                }
                            })

    assert folder.next_network_scan_at() == next_time
Example #6
0
def test_eq_operation(request_context):
    with in_chdir("/"):
        root = watolib.Folder.root_folder()
        folder1 = watolib.Folder("folder1", parent_folder=root)
        folder1.persist_instance()

        folder1_new = watolib.Folder("folder1")
        folder1_new.load_instance()

        assert folder1 == folder1_new
        assert id(folder1) != id(folder1_new)
        assert folder1 in [folder1_new]

        folder2 = watolib.Folder("folder2", parent_folder=folder1)
        folder2.persist_instance()

        assert folder1 not in [folder2]
Example #7
0
def test_write_and_read_host_attributes(tmp_path, attributes):
    folder_path = str(tmp_path)
    # Used to write the data
    write_data_folder = watolib.Folder("testfolder",
                                       folder_path=folder_path,
                                       parent_folder=None)

    # Used to read the previously written data
    read_data_folder = watolib.Folder("testfolder",
                                      folder_path=folder_path,
                                      parent_folder=None)

    # Write data
    # Note: The create_hosts function modifies the attributes dict, adding a meta_data key inplace
    write_data_folder.create_hosts([("testhost", attributes, [])])
    write_folder_hosts = write_data_folder.hosts()
    assert len(write_folder_hosts) == 1

    # Read data back
    read_folder_hosts = read_data_folder.hosts()
    assert len(read_folder_hosts) == 1
    for _, host in read_folder_hosts.items():
        assert host.attributes() == attributes
Example #8
0
def test_next_network_scan_at(allowed, last_end, next_time):
    folder = watolib.Folder(
        name="bla",
        title="Bla",
        attributes={
            "network_scan": {
                "exclude_ranges": [],
                "ip_ranges": [("ip_range", ("10.3.1.1", "10.3.1.100"))],
                "run_as": "cmkadmin",
                "scan_interval": 300,
                "set_ipaddress": True,
                "tag_criticality": "offline",
                "time_allowed": allowed,
            },
            "network_scan_result": {
                "end": last_end,
            },
        },
    )

    with on_time("2018-01-10 02:00:00", "CET"):
        assert folder.next_network_scan_at() == next_time
Example #9
0
 def _init_folder(self):
     # TODO: Needed to make pylint know the correct type of the return value.
     # Will be cleaned up in future when typing is established
     return watolib.Folder(name=None)
Example #10
0
 def _init_folder(self):
     return watolib.Folder(name=None)