def test_delete_host(web): # noqa: F811 # pylint: disable=redefined-outer-name try: web.add_host("test-host-delete", attributes={ "ipaddress": "127.0.0.1", }) finally: web.delete_host("test-host-delete")
def test_config(web, site): users = { "hh": { "alias": "Harry Hirsch", "password": "******", "email": u"%s@localhost" % web.site.id, 'contactgroups': ['all'], }, } expected_users = set(["cmkadmin", "automation"] + users.keys()) web.add_htpasswd_users(users) all_users = web.get_all_users() assert not expected_users - set(all_users.keys()) site.live.command("[%d] STOP_EXECUTING_HOST_CHECKS" % time.time()) site.live.command("[%d] STOP_EXECUTING_SVC_CHECKS" % time.time()) web.add_host("notify-test", attributes={ "ipaddress": "127.0.0.1", }) web.activate_changes() yield site.live.command("[%d] START_EXECUTING_HOST_CHECKS" % time.time()) site.live.command("[%d] START_EXECUTING_SVC_CHECKS" % time.time()) web.delete_host("notify-test") web.delete_htpasswd_users(users.keys()) web.activate_changes()
def test_mgmt_config_ruleset_order(web): web.set_ruleset( "management_board_config", { "ruleset": { "": [ # "" -> folder { 'condition': {}, 'options': {}, 'value': ("snmp", "RULESET1"), }, { 'condition': {}, 'options': {}, 'value': ("snmp", "RULESET2"), }, ], } }) web.add_folder("folder1") web.add_host("mgmt-host", folder="folder1", attributes={ "ipaddress": "127.0.0.1", "management_protocol": "snmp", }) config_cache = reload_config() host_config = config_cache.get_host_config("mgmt-host") assert host_config.has_management_board assert host_config.management_protocol == "snmp" assert host_config.management_address == "127.0.0.1" assert host_config.management_credentials == "RULESET1"
def test_write_host_labels(web, site): try: web.add_host("test-host-lan", attributes={ "ipaddress": "127.0.0.1", 'labels': { 'blä': 'blüb' } }, verify_set_attributes=False) hosts = web.get_all_hosts(effective_attributes=True) assert hosts["test-host-lan"]["attributes"]["labels"] == {u'blä': u'blüb'} cfg = { "FOLDER_PATH": "/", "all_hosts": [], "host_tags": {}, "host_labels": {}, "ipaddresses": {}, "host_attributes": {}, } # type: Dict[str, Any] exec(site.read_file("etc/check_mk/conf.d/wato/hosts.mk"), cfg, cfg) assert cfg["host_labels"]["test-host-lan"] == { u"blä": u"blüb", } for label_id, label_value in cfg["host_labels"]["test-host-lan"].items(): assert isinstance(label_id, six.text_type) assert isinstance(label_value, six.text_type) finally: web.delete_hosts(["test-host-lan"])
def test_mgmt_config_ruleset(web, protocol, cred_attribute, credentials, ruleset_credentials): web.set_ruleset( "management_board_config", { "ruleset": { "": [ # "" -> folder { 'conditions': { 'host_specs': ['@all'], 'host_tags': [] }, 'options': {}, 'value': (protocol, ruleset_credentials), }, ], } }) web.add_folder("folder1") web.add_host( "mgmt-host", folder="folder1", attributes={ "ipaddress": "127.0.0.1", "management_protocol": protocol, }) config_cache = reload_config() host_config = config_cache.get_host_config("mgmt-host") assert host_config.has_management_board assert host_config.management_protocol == protocol assert host_config.management_address == "127.0.0.1" assert host_config.management_credentials == ruleset_credentials
def graph_test_config(web, site): # No graph yet... with pytest.raises(APIError) as exc_info: web.get_regular_graph("test-host-get-graph", "Check_MK", 0, expect_error=True) assert "Cannot calculate graph recipes" in "%s" % exc_info try: # Now add the host web.add_host("test-host-get-graph", attributes={ "ipaddress": "127.0.0.1", }) site.write_file( "etc/check_mk/conf.d/test-host-get-graph.mk", "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ['test-host-get-graph']))\n" ) site.makedirs("var/check_mk/agent_output/") site.write_file( "var/check_mk/agent_output/test-host-get-graph", open( "%s/tests/integration/cmk/base/test-files/linux-agent-output" % repo_path()).read()) web.discover_services("test-host-get-graph") web.activate_changes() site.schedule_check("test-host-get-graph", "Check_MK", 0) # Wait for RRD file creation. Isn't this a bug that the graph is not instantly available? rrd_path = site.path( "var/check_mk/rrd/test-host-get-graph/Check_MK.rrd") for attempt in xrange(50): time.sleep(0.1) proc = subprocess.Popen([ site.path("bin/unixcat"), site.path("tmp/run/rrdcached.sock") ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = proc.communicate("FLUSH %s\n" % rrd_path) if os.path.exists(rrd_path): break sys.stdout.write("waiting for %s (attempt %d)%s%s\n" % ( rrd_path, attempt + 1, # ", stdout: %s" % out if out else "", ", stderr: %s" % err if err else "")) else: assert False, "RRD file %s missing" % rrd_path yield finally: web.delete_host("test-host-get-graph") site.delete_file("etc/check_mk/conf.d/test-host-get-graph.mk") web.activate_changes()
def test_log(request, web, site, fake_sendmail): core, log = request.param site.set_config("CORE", core, with_restart=True) users = { "hh": { "alias": "Harry Hirsch", "password": "******", "email": u"%s@localhost" % web.site.id, 'contactgroups': ['all'], }, } expected_users = set(["cmkadmin", "automation"] + users.keys()) web.add_htpasswd_users(users) all_users = web.get_all_users() assert not expected_users - set(all_users.keys()) site.live.command("[%d] STOP_EXECUTING_HOST_CHECKS" % time.time()) site.live.command("[%d] STOP_EXECUTING_SVC_CHECKS" % time.time()) web.add_host("notify-test", attributes={ "ipaddress": "127.0.0.1", }) web.activate_changes() with WatchLog(site, log, default_timeout=20) as l: yield l site.live.command("[%d] START_EXECUTING_HOST_CHECKS" % time.time()) site.live.command("[%d] START_EXECUTING_SVC_CHECKS" % time.time()) web.delete_host("notify-test") web.delete_htpasswd_users(users.keys()) web.activate_changes()
def test_automation_update_dns_cache(test_cfg, site, web): cache_path = 'var/check_mk/ipaddresses.cache' if site.file_exists(cache_path): site.delete_file(cache_path) try: web.add_host("update-dns-cache-host") web.add_host("localhost") site.write_file(cache_path, "{('bla', 4): '127.0.0.1'}") data = _execute_automation(site, "update-dns-cache") assert isinstance(data, tuple) assert len(data) == 2 assert data[0] > 0 assert data[1] == ["update-dns-cache-host"] assert site.file_exists(cache_path) cache = eval(site.read_file(cache_path)) assert isinstance(cache, dict) assert cache[("localhost", 4)] == "127.0.0.1" assert ("bla", 4) not in cache finally: web.delete_host("localhost") web.delete_host("update-dns-cache-host")
def test_delete_host(web): try: web.add_host("test-host-delete", attributes={ "ipaddress": "127.0.0.1", }) finally: web.delete_host("test-host-delete")
def test_mgmt_config_ruleset_overidden_by_explicit_setting(web, protocol, cred_attribute, folder_credentials, ruleset_credentials): web.set_ruleset( "management_board_config", { "ruleset": { "": [ # "" -> folder { 'condition': {}, 'options': {}, 'value': (protocol, ruleset_credentials), }, ], } }) web.add_folder("folder1", attributes={ cred_attribute: folder_credentials, }) web.add_host("mgmt-host", folder="folder1", attributes={ "ipaddress": "127.0.0.1", "management_protocol": protocol, }) config_cache = reload_config() host_config = config_cache.get_host_config("mgmt-host") assert host_config.has_management_board assert host_config.management_protocol == protocol assert host_config.management_address == "127.0.0.1" assert host_config.management_credentials == folder_credentials
def test_get_inventory(web): host_name = "test-host" inventory_dir = "var/check_mk/inventory" try: web.add_host(host_name, attributes={"ipaddress": "127.0.0.1"}) # NOTE: Deleting the host deletes the file, too. web.site.makedirs(inventory_dir) web.site.write_file(os.path.join(inventory_dir, host_name), "{'hardware': {'memory': {'ram': 10000, 'foo': 1}, 'blubb': 42}}") inv = web.get_inventory([host_name]) assert inv[host_name] == { u'hardware': { u'memory': { u'foo': 1, u'ram': 10000 }, u'blubb': 42 } } inv = web.get_inventory([host_name], paths=['.hardware.memory.']) assert inv[host_name] == {u'hardware': {u'memory': {u'foo': 1, u'ram': 10000}}} inv = web.get_inventory([host_name], paths=['.hardware.mumpf.']) assert inv[host_name] == {} finally: web.delete_host(host_name)
def test_add_host(web): try: # Also tests get_host web.add_host("test-host", attributes={ "ipaddress": "127.0.0.1", }) finally: web.delete_host("test-host")
def test_discover_services(web): try: web.add_host("test-host-discovery", attributes={ "ipaddress": "127.0.0.1", }) web.discover_services("test-host-discovery") finally: web.delete_host("test-host-discovery")
def test_edit_host(web): try: web.add_host("test-edit-host", attributes={ "ipaddress": "127.0.0.1", }) web.edit_host("test-edit-host", attributes={"ipaddress": "127.10.0.1"}) finally: web.delete_host("test-edit-host")
def test_discover_servics(web): try: web.add_host("test-host-discovery", attributes={ "ipaddress": "127.0.0.1", }) web.discover_services("test-host-discovery") finally: web.delete_host("test-host-discovery")
def test_discover_services(web): # noqa: F811 # pylint: disable=redefined-outer-name try: web.add_host("test-host-discovery", attributes={ "ipaddress": "127.0.0.1", }) web.discover_services("test-host-discovery") finally: web.delete_host("test-host-discovery")
def graph_test_config(web, site): # No graph yet... with pytest.raises(APIError) as e: web.get_regular_graph("test-host-get-graph", "Check_MK", 0, expect_error=True) assert "Cannot calculate graph recipes" in "%s" % e try: # Now add the host web.add_host("test-host-get-graph", attributes={ "ipaddress": "127.0.0.1", }) site.write_file( "etc/check_mk/conf.d/test-host-get-graph.mk", "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ['test-host-get-graph']))\n" ) site.makedirs("var/check_mk/agent_output/") site.write_file( "var/check_mk/agent_output/test-host-get-graph", file( "%s/tests/integration/cmk_base/test-files/linux-agent-output" % repo_path()).read()) web.discover_services("test-host-get-graph") web.activate_changes() site.schedule_check("test-host-get-graph", "Check_MK", 0) # Wait for RRD file creation # Isn't this a bug that the graph is not instantly available? timeout = 10 print "Checking for graph..." while timeout and not site.file_exists( "var/check_mk/rrd/test-host-get-graph/Check_MK.rrd"): try: web.get_regular_graph("test-host-get-graph", "Check_MK", 0, expect_error=True) except Exception: pass timeout -= 1 time.sleep(1) print "Checking for graph..." assert site.file_exists("var/check_mk/rrd/test-host-get-graph/Check_MK.rrd"), \ "RRD %s is still missing" % "var/check_mk/rrd/test-host-get-graph/Check_MK.rrd" yield finally: web.delete_host("test-host-get-graph") site.delete_file("etc/check_mk/conf.d/test-host-get-graph.mk") web.activate_changes()
def test_get_all_hosts_basic(web): try: web.add_host("test-host-list", attributes={ "ipaddress": "127.0.0.1", }) hosts = web.get_all_hosts() assert "test-host-list" in hosts finally: web.delete_host("test-host-list")
def test_delete_hosts(web): try: web.add_host("test-hosts-delete1", attributes={ "ipaddress": "127.0.0.1", }) web.add_host("test-hosts-delete2", attributes={ "ipaddress": "127.0.0.1", }) finally: web.delete_hosts(["test-hosts-delete1", "test-hosts-delete2"])
def test_get_all_hosts_basic(web): # noqa: F811 # pylint: disable=redefined-outer-name try: web.add_host("test-host-list", attributes={ "ipaddress": "127.0.0.1", }) hosts = web.get_all_hosts() assert "test-host-list" in hosts finally: web.delete_host("test-host-list")
def test_add_host_folder_create(web): web.add_host( "test-host", attributes={ "ipaddress": "127.0.0.1", }, create_folders=True, folder="asd/eee", ) web.delete_host("test-host")
def test_add_host_folder_create(web): # noqa: F811 # pylint: disable=redefined-outer-name try: web.add_host( "test-host", attributes={ "ipaddress": "127.0.0.1", }, create_folders=True, folder="asd/eee", ) finally: web.delete_host("test-host")
def test_activate_changes(web, site): try: web.add_host("test-host-activate", attributes={ "ipaddress": "127.0.0.1", }) web.activate_changes() result = site.live.query("GET hosts\nColumns: name\nFilter: name = test-host-activate\n") assert result == [["test-host-activate"]] finally: web.delete_host("test-host-activate") web.activate_changes()
def test_add_host_no_folder_create(web): with pytest.raises(APIError) as e: web.add_host( "test-host", attributes={ "ipaddress": "127.0.0.1", }, create_folders=False, folder="eins/zwei", expect_error=True, ) assert "Folder not existing" in "%s" % e
def test_get_host_effective_attributes(web): # noqa: F811 # pylint: disable=redefined-outer-name try: web.add_host("test-host", attributes={ "ipaddress": "127.0.0.1", }) host = web.get_host("test-host", effective_attributes=False) assert "tag_networking" not in host["attributes"] host = web.get_host("test-host", effective_attributes=True) assert "tag_networking" in host["attributes"] assert host["attributes"]["tag_networking"] == "lan" finally: web.delete_host("test-host")
def test_mgmt_explicit_address(web): web.add_host("mgmt-host", attributes={ "ipaddress": "127.0.0.1", "management_protocol": "snmp", "management_address": "127.0.0.2", }) config_cache = reload_config() host_config = config_cache.get_host_config("mgmt-host") assert host_config.has_management_board assert host_config.management_protocol == "snmp" assert host_config.management_address == "127.0.0.2" assert host_config.management_credentials == "public"
def test_mgmt_explicit_settings(web, protocol, cred_attribute, credentials): web.add_host("mgmt-host", attributes={ "ipaddress": "127.0.0.1", "management_protocol": protocol, cred_attribute: credentials, }) config_cache = reload_config() host_config = config_cache.get_host_config("mgmt-host") assert host_config.has_management_board assert host_config.management_protocol == protocol assert host_config.management_address == "127.0.0.1" assert host_config.management_credentials == credentials
def test_cfg(web, site): print("Applying default config") web.add_host("modes-test-host", attributes={ "ipaddress": "127.0.0.1", }) web.add_host("modes-test-host2", attributes={ "ipaddress": "127.0.0.1", "tag_criticality": "test", }) web.add_host("modes-test-host3", attributes={ "ipaddress": "127.0.0.1", "tag_criticality": "test", }) web.add_host("modes-test-host4", attributes={ "ipaddress": "127.0.0.1", "tag_criticality": "offline", }) site.write_file( "etc/check_mk/conf.d/modes-test-host.mk", "datasource_programs.append(('cat ~/var/check_mk/agent_output/<HOST>', [], ALL_HOSTS))\n" ) site.makedirs("var/check_mk/agent_output/") site.write_file( "var/check_mk/agent_output/modes-test-host", file("%s/tests/integration/cmk_base/test-files/linux-agent-output" % repo_path()).read()) site.write_file( "var/check_mk/agent_output/modes-test-host2", file("%s/tests/integration/cmk_base/test-files/linux-agent-output" % repo_path()).read()) site.write_file( "var/check_mk/agent_output/modes-test-host3", file("%s/tests/integration/cmk_base/test-files/linux-agent-output" % repo_path()).read()) web.discover_services("modes-test-host") web.discover_services("modes-test-host2") web.discover_services("modes-test-host3") web.activate_changes() yield None # # Cleanup code # print("Cleaning up test config") site.delete_dir("var/check_mk/agent_output") site.delete_file("etc/check_mk/conf.d/modes-test-host.mk") web.delete_host("modes-test-host") web.delete_host("modes-test-host2") web.delete_host("modes-test-host3") web.delete_host("modes-test-host4")
def test_get_host_effective_attributes(web): try: web.add_host("test-host", attributes={ "ipaddress": "127.0.0.1", }) host = web.get_host("test-host", effective_attributes=False) assert "tag_networking" not in host["attributes"] host = web.get_host("test-host", effective_attributes=True) assert "tag_networking" in host["attributes"] assert host["attributes"]["tag_networking"] == "lan" finally: web.delete_host("test-host")
def test_add_host_no_folder_create(web): with pytest.raises(APIError) as e: web.add_host( "test-host", attributes={ "ipaddress": "127.0.0.1", }, create_folders=False, folder="eins/zwei", expect_error=True, ) exc_msg = "%s" % e assert "Unable to create parent folder" in exc_msg
def test_get_graph(web, site): try: # No graph yet... with pytest.raises(APIError) as e: data = web.get_regular_graph("test-host-get-graph", "Check_MK", 0, expect_error=True) assert "Cannot calculate graph recipes" in "%s" % e # Now add the host web.add_host("test-host-get-graph", attributes={ "ipaddress": "127.0.0.1", }) web.discover_services("test-host-get-graph") web.activate_changes() # Issue a reschedule site.live.command( "SCHEDULE_FORCED_SERVICE_CHECK;test-host-get-graph;Check_MK;%d" % int(time.time())) # Wait for RRD file creation # Isn't this a bug that the graph is not instantly available? timeout = 10 print "Checking for graph..." while timeout and not site.file_exists( "var/check_mk/rrd/test-host-get-graph/Check_MK.rrd"): try: data = web.get_regular_graph("test-host-get-graph", "Check_MK", 0, expect_error=True) except Exception: pass timeout -= 1 time.sleep(1) print "Checking for graph..." assert site.file_exists("var/check_mk/rrd/test-host-get-graph/Check_MK.rrd"), \ "RRD %s is still missing" % "var/check_mk/rrd/test-host-get-graph/Check_MK.rrd" _test_get_graph_api(web) _test_get_graph_image(web) _test_get_graph_notification_image(web) finally: web.delete_host("test-host-get-graph") web.activate_changes()
def default_cfg(web): web.add_host("livestatus-test-host", attributes={ "ipaddress": "127.0.0.1", }) web.discover_services("livestatus-test-host") web.activate_changes() yield None # # Cleanup code # web.delete_host("livestatus-test-host")