Module: check_mk
Branch: master
Commit: c0b144eb9a045390808202a2c871ae981d5f8499
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=c0b144eb9a0453…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Thu May 18 08:35:41 2017 +0200
New tests for get_host/get_all_hosts API calls
- Ensure effective_attributes work
- Check whether or not host tags in hosts.mk are written correctly
Change-Id: I3a91b9ca2b00f405d49edc16ee6cc56ff49fa694
---
tests/testlib/__init__.py | 9 ++---
tests/web/test_webapi.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 90 insertions(+), 4 deletions(-)
diff --git a/tests/testlib/__init__.py b/tests/testlib/__init__.py
index 36aa4a2..fa7480e 100644
--- a/tests/testlib/__init__.py
+++ b/tests/testlib/__init__.py
@@ -931,10 +931,11 @@ class CMKWebSession(WebSession):
assert host["attributes"] == attributes
- def get_host(self, hostname):
+ def get_host(self, hostname, effective_attributes=False):
result = self._api_request("webapi.py?action=get_host", {
"request": json.dumps({
- "hostname" : hostname,
+ "hostname" : hostname,
+ "effective_attributes" : effective_attributes,
}),
})
@@ -946,7 +947,7 @@ class CMKWebSession(WebSession):
return result
- def get_all_hosts(self, effective_attributes=0):
+ def get_all_hosts(self, effective_attributes=False):
result = self._api_request("webapi.py?action=get_all_hosts", {
"request": json.dumps({
"effective_attributes": effective_attributes,
@@ -966,7 +967,7 @@ class CMKWebSession(WebSession):
assert result == None
- hosts = self.get_all_hosts(hostname)
+ hosts = self.get_all_hosts()
assert hostname not in hosts
diff --git a/tests/web/test_webapi.py b/tests/web/test_webapi.py
index 709daa6..7b67637 100644
--- a/tests/web/test_webapi.py
+++ b/tests/web/test_webapi.py
@@ -43,6 +43,91 @@ def test_delete_host(web):
web.delete_host("test-host-delete")
+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_get_all_hosts_effective_attributes(web):
+ try:
+ web.add_host("test-host", attributes={
+ "ipaddress": "127.0.0.1",
+ })
+
+ hosts = web.get_all_hosts(effective_attributes=False)
+ host = hosts["test-host"]
+ assert "tag_networking" not in host["attributes"]
+
+ hosts = web.get_all_hosts(effective_attributes=True)
+ host = hosts["test-host"]
+ assert "tag_networking" in host["attributes"]
+ assert host["attributes"]["tag_networking"] ==
"lan"
+ finally:
+ web.delete_host("test-host")
+
+
+def test_write_host_tags(web, site):
+ try:
+ web.add_host("test-host-dmz", attributes={
+ "ipaddress": "127.0.0.1",
+ "tag_networking": "dmz",
+ })
+
+ web.add_host("test-host-lan", attributes={
+ "ipaddress": "127.0.0.1",
+ "tag_networking": "lan",
+ })
+
+ web.add_host("test-host-lan2", attributes={
+ "ipaddress": "127.0.0.1",
+ })
+
+ hosts = web.get_all_hosts(effective_attributes=True)
+ assert
hosts["test-host-dmz"]["attributes"]["tag_networking"] ==
"dmz"
+ assert
hosts["test-host-lan"]["attributes"]["tag_networking"] ==
"lan"
+ assert
hosts["test-host-lan2"]["attributes"]["tag_networking"] ==
"lan"
+
+ cfg = {
+ "FOLDER_PATH": "/",
+ "all_hosts": [],
+ "ipaddresses": {},
+ "host_attributes": {},
+ }
+
+ exec(site.read_file("etc/check_mk/conf.d/wato/hosts.mk"), cfg, cfg)
+
+ tags_by_host = {}
+ for entry in cfg["all_hosts"]:
+ hostname, tag_txt = entry.split("|", 1)
+ tags_by_host[hostname] = tag_txt.split("|")
+
+
+ assert "dmz" in tags_by_host["test-host-dmz"]
+ assert "lan" not in tags_by_host["test-host-dmz"]
+
+ assert "dmz" not in tags_by_host["test-host-lan"]
+ assert "lan" in tags_by_host["test-host-lan"]
+
+ assert "dmz" not in tags_by_host["test-host-lan2"]
+ assert "lan" in tags_by_host["test-host-lan2"]
+
+ finally:
+ web.delete_host("test-host-lan2")
+ web.delete_host("test-host-lan")
+ web.delete_host("test-host-dmz")
+
+
@pytest.mark.parametrize(("group_type"), [ "contact",
"host", "service" ])
def test_add_group(web, group_type):
group_id = "%s_testgroup_id" % group_type