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
def test_load_group_information_empty(tmp_path): environ = dict(create_environ(), REQUEST_URI='') with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(Request(environ))): assert groups.load_contact_group_information() == {} assert gui_groups.load_host_group_information() == {} assert gui_groups.load_service_group_information() == {}
def module_wide_request_context(): # This one is kind of an hack because some other test-fixtures touch the user object AFTER the # request context has already ended. If we increase our scope this won't matter, but it is of # course wrong. These other fixtures have to be fixed. environ = create_environ() with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(Request(environ))): yield
def fixture_current_cookie(with_user, session_id): user_id = with_user[0] cookie_name = login.auth_cookie_name() cookie_value = login._auth_cookie_value(user_id, session_id) environ = dict(create_environ(), HTTP_COOKIE=f"{cookie_name}={cookie_value}".encode("utf-8")) with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(http.Request(environ))): yield cookie_name
def fixture_pre_20_cookie(): environ = dict( create_environ(), HTTP_COOKIE= u"xyz=123; auth_stable=lärs:1534272374.61:1f59cac3fcd5bcc389e4f8397bed315b; abc=123".encode( "utf-8")) with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(http.Request(environ))): yield "auth_stable"
def test_web_server_auth_session(user_id): environ = dict(create_environ(), REMOTE_USER=str(user_id)) with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(http.Request(environ))): assert user.id is None with login.authenticate(request) as authenticated: assert authenticated is True assert user.id == user_id assert session.user_id == user.id assert user.id is None
def test_flash(user_id): environ = create_environ() # Execute the first request flash some message with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(http.Request(environ))) as request, \ login.UserContext(user_id): session_id = on_succeeded_login(user_id) # Create and activate session assert request.session is not None flash("abc") assert session.session_info.flashes == ["abc"] # Now create the second request to get the previously flashed message with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(http.Request(environ))), \ login.UserContext(user_id): on_access(user_id, session_id) assert request.session is not None assert session.session_info.flashes == ["abc"] # Get the flashed messages removes the messages from the session # and subsequent calls to get_flashed_messages return the messages # over and over. assert get_flashed_messages() == [HTML("abc")] assert get_flashed_messages() == [HTML("abc")] assert session.session_info.flashes == [] # Now create the third request that should not have access to the flashed messages since the # second one consumed them. with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(http.Request(environ))), \ login.UserContext(user_id): on_access(user_id, session_id) assert request.session is not None assert session.session_info.flashes == [] assert get_flashed_messages() == []
def register_builtin_html(): """This fixture registers a global htmllib.html() instance just like the regular GUI""" environ = create_environ() with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(Request(environ))): yield
def test_load_group_information(tmp_path): with open(cmk.utils.paths.check_mk_config_dir + "/wato/groups.mk", "w") as f: f.write("""# encoding: utf-8 if type(define_contactgroups) != dict: define_contactgroups = {} define_contactgroups.update({'all': u'Everything'}) if type(define_hostgroups) != dict: define_hostgroups = {} define_hostgroups.update({'all_hosts': u'All hosts :-)'}) if type(define_servicegroups) != dict: define_servicegroups = {} define_servicegroups.update({'all_services': u'All särvices'}) """) with open(cmk.utils.paths.default_config_dir + "/multisite.d/wato/groups.mk", "w") as f: f.write("""# encoding: utf-8 multisite_hostgroups = { "all_hosts": { "ding": "dong", }, } multisite_servicegroups = { "all_services": { "d1ng": "dong", }, } multisite_contactgroups = { "all": { "d!ng": "dong", }, } """) environ = dict(create_environ(), REQUEST_URI='') with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(Request(environ))): assert groups.load_group_information() == { 'contact': { 'all': { 'alias': u'Everything', "d!ng": "dong", } }, 'host': { 'all_hosts': { 'alias': u'All hosts :-)', "ding": "dong", } }, 'service': { 'all_services': { 'alias': u'All s\xe4rvices', "d1ng": "dong", } }, } assert groups.load_contact_group_information() == { 'all': { 'alias': u'Everything', "d!ng": "dong", } } assert gui_groups.load_host_group_information() == { 'all_hosts': { 'alias': u'All hosts :-)', "ding": "dong", } } assert gui_groups.load_service_group_information() == { 'all_services': { 'alias': u'All s\xe4rvices', "d1ng": "dong", } }
def test_apply_sync_snapshot(edition_short, snapshot_manager_class, monkeypatch, tmp_path, with_user): user_id = with_user[0] monkeypatch.setattr(cmk_version, "edition_short", lambda: edition_short) environ = dict(create_environ(), REQUEST_URI='') with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(Request(environ))): login.login(user_id) _create_test_sync_config(monkeypatch) snapshot_settings = _create_sync_snapshot(snapshot_manager_class, monkeypatch, tmp_path) # Change unpack target directory from "unit test site" paths to a test specific path unpack_dir = tmp_path / "snapshot_unpack" components = [ activate_changes._replace_omd_root(str(unpack_dir), p) for p in activate_changes.get_replication_paths() ] with open(snapshot_settings.snapshot_path, "rb") as f: activate_changes.apply_sync_snapshot("unit_remote_1", f.read(), components) expected_paths = [ 'etc', 'var', 'etc/check_mk', 'etc/check_mk/conf.d', 'etc/check_mk/mkeventd.d', 'etc/check_mk/multisite.d', 'etc/check_mk/conf.d/wato', 'etc/check_mk/conf.d/wato/hosts.mk', 'etc/check_mk/conf.d/wato/contacts.mk', 'etc/check_mk/mkeventd.d/mkp', 'etc/check_mk/mkeventd.d/wato', 'etc/check_mk/mkeventd.d/mkp/rule_packs', 'etc/check_mk/multisite.d/wato', 'etc/check_mk/multisite.d/wato/global.mk', 'var/check_mk', 'var/check_mk/web', "etc/htpasswd", "etc/auth.serials", "etc/check_mk/multisite.d/wato/users.mk", six.ensure_str('var/check_mk/web/%s' % user_id), six.ensure_str('var/check_mk/web/%s/cached_profile.mk' % user_id), six.ensure_str('var/check_mk/web/%s/enforce_pw_change.mk' % user_id), six.ensure_str('var/check_mk/web/%s/last_pw_change.mk' % user_id), six.ensure_str('var/check_mk/web/%s/num_failed_logins.mk' % user_id), six.ensure_str('var/check_mk/web/%s/serial.mk' % user_id), ] # The paths are registered once the enterprise plugins are available, independent of the # cmk_version.edition_short() value. if is_enterprise_repo(): expected_paths += [ 'etc/check_mk/dcd.d', 'etc/check_mk/mknotifyd.d', 'etc/check_mk/mknotifyd.d/wato', 'etc/check_mk/dcd.d/wato', ] # TODO: Shouldn't we clean up these subtle differences? if cmk_version.is_managed_edition(): expected_paths += [ "etc/check_mk/conf.d/wato/groups.mk", "etc/check_mk/mkeventd.d/wato/rules.mk", "etc/check_mk/multisite.d/wato/bi.mk", "etc/check_mk/multisite.d/wato/customers.mk", "etc/check_mk/multisite.d/wato/groups.mk", "etc/check_mk/multisite.d/wato/user_connections.mk", #"etc/check_mk/conf.d/wato/sitespecific.mk", #"etc/check_mk/dcd.d/wato/distributed.mk", #"etc/check_mk/dcd.d/wato/sitespecific.mk", #"etc/check_mk/mkeventd.d/wato/sitespecific.mk", #"etc/check_mk/mknotifyd.d/wato/sitespecific.mk", #"etc/check_mk/multisite.d/wato/sitespecific.mk", ] expected_paths.remove("etc/check_mk/conf.d/wato/hosts.mk") if not cmk_version.is_raw_edition(): expected_paths += [ 'etc/check_mk/liveproxyd.d', 'etc/check_mk/liveproxyd.d/wato', #'etc/check_mk/multisite.d/wato/ca-certificates_sitespecific.mk', ] paths = [str(p.relative_to(unpack_dir)) for p in unpack_dir.glob("**/*")] assert sorted(paths) == sorted(expected_paths)
def test_generate_snapshot(edition_short, snapshot_manager_class, monkeypatch, tmp_path, with_user): user_id = with_user[0] monkeypatch.setattr(cmk_version, "edition_short", lambda: edition_short) environ = dict(create_environ(), REQUEST_URI='') with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(Request(environ))): login.login(user_id) _create_test_sync_config(monkeypatch) snapshot_settings = _create_sync_snapshot(snapshot_manager_class, monkeypatch, tmp_path) # And now check the resulting snapshot contents unpack_dir = tmp_path / "snapshot_unpack" with tarfile.open(snapshot_settings.snapshot_path, "r") as t: t.extractall(str(unpack_dir)) expected_subtars = [ "auth.secret.tar", "auth.serials.tar", "check_mk.tar", "diskspace.tar", "htpasswd.tar", "mkeventd_mkp.tar", "mkeventd.tar", "multisite.tar", "sitespecific.tar", "usersettings.tar", ] if is_enterprise_repo(): expected_subtars += [ "dcd.tar", "mknotify.tar", ] if not cmk_version.is_raw_edition(): expected_subtars.append("liveproxyd.tar") assert sorted(f.name for f in unpack_dir.iterdir()) == sorted(expected_subtars) expected_files = { 'mkeventd_mkp.tar': [], 'multisite.tar': ["global.mk", "users.mk"], 'usersettings.tar': [user_id], 'mkeventd.tar': [], 'check_mk.tar': ["hosts.mk", "contacts.mk"], 'htpasswd.tar': ["htpasswd"], 'liveproxyd.tar': [], 'sitespecific.tar': ['sitespecific.mk'], 'auth.secret.tar': [], 'dcd.tar': [], 'auth.serials.tar': ["auth.serials"], 'mknotify.tar': [], 'diskspace.tar': [], } # TODO: Shouldn't we clean up these subtle differences? if cmk_version.is_managed_edition(): expected_files.update({ "mkeventd.tar": ["rules.mk"], 'check_mk.tar': ["groups.mk", "contacts.mk"], 'multisite.tar': [ "bi.mk", "customers.mk", "global.mk", "groups.mk", "user_connections.mk", "users.mk", ], }) if not cmk_version.is_raw_edition(): expected_files['liveproxyd.tar'] = [] # And now check the subtar contents for subtar in unpack_dir.iterdir(): subtar_unpack_dir = unpack_dir / subtar.stem subtar_unpack_dir.mkdir(parents=True, exist_ok=True) with tarfile.open(str(subtar), "r") as s: s.extractall(str(subtar_unpack_dir)) files = sorted(str(f.relative_to(subtar_unpack_dir)) for f in subtar_unpack_dir.iterdir()) assert sorted(expected_files[subtar.name]) == files, \ "Subtar %s has wrong files" % subtar.name
def register_builtin_html(): """This fixture registers a global htmllib.html() instance just like the regular GUI""" environ = dict(create_environ(), REQUEST_URI='') with AppContext(DummyApplication(environ, None)), \ RequestContext(htmllib.html(Request(environ), Response(is_secure=False))): yield