Module: check_mk
Branch: master
Commit: 0425dc92a33e12a2e2272cc8c85778ef1d7a84b4
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=0425dc92a33e12…
Author: Andreas Boesl <ab(a)mathias-kettner.de>
Date: Fri Feb 10 17:13:48 2017 +0100
introduced a few more automated web api tests
Change-Id: Ied0155e7e1d4e9df8c1c03d3f8bc4cb503c3e8f0
---
tests/testlib/__init__.py | 69 ++++++++++++++++++++++++++++
tests/web/test_webapi.py | 115 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 184 insertions(+)
diff --git a/tests/testlib/__init__.py b/tests/testlib/__init__.py
index 918366c..9edf9c7 100644
--- a/tests/testlib/__init__.py
+++ b/tests/testlib/__init__.py
@@ -851,6 +851,75 @@ class CMKWebSession(WebSession):
assert hostname not in hosts
+ def get_all_groups(self, group_type):
+ result = self._api_request("webapi.py?action=get_all_%sgroups" %
group_type, {})
+ return result
+
+
+ def add_group(self, group_type, group_name, attributes, expect_error = False):
+ request_object = {
+ "groupname" : group_name
+ }
+ request_object.update(attributes)
+
+ result = self._api_request("webapi.py?action=add_%sgroup" % group_type,
{
+ "request": json.dumps(request_object)}, expect_error =
expect_error
+ )
+
+ assert result == None
+
+
+ def edit_group(self, group_type, group_name, attributes, expect_error = False):
+ request_object = {
+ "groupname" : group_name
+ }
+ request_object.update(attributes)
+
+ result = self._api_request("webapi.py?action=edit_%sgroup" %
group_type, {
+ "request": json.dumps(request_object)}, expect_error =
expect_error
+ )
+
+ assert result == None
+
+
+ def delete_group(self, group_type, group_name, expect_error = False):
+ result = self._api_request("webapi.py?action=delete_%sgroup" %
group_type, {
+ "request": json.dumps({
+ "groupname" : group_name,
+ })}, expect_error = expect_error
+ )
+
+ assert result == None
+
+
+ def get_all_users(self):
+ return self._api_request("webapi.py?action=get_all_users", {})
+
+
+ def add_htpasswd_users(self, users):
+ result = self._api_request("webapi.py?action=add_users", {
+ "request": json.dumps({"users": users})
+ })
+ assert result == None
+
+
+ def edit_htpasswd_users(self, users):
+ result = self._api_request("webapi.py?action=edit_users", {
+ "request": json.dumps({"users": users})
+ })
+
+ assert result == None
+
+
+ def delete_htpasswd_users(self, userlist):
+ result = self._api_request("webapi.py?action=delete_users", {
+ "request": json.dumps({
+ "users" : userlist
+ }),
+ })
+ assert result == None
+
+
def discover_services(self, hostname, mode=None):
request = {
"hostname" : hostname,
diff --git a/tests/web/test_webapi.py b/tests/web/test_webapi.py
index ad8efe8..06f5d6f 100644
--- a/tests/web/test_webapi.py
+++ b/tests/web/test_webapi.py
@@ -3,6 +3,7 @@
import pytest
import time
+import os
from testlib import web, APIError
@@ -42,6 +43,120 @@ def test_delete_host(web):
web.delete_host("test-host-delete")
+(a)pytest.mark.parametrize(("group_type")ype"), [ "contact",
"host", "service" ])
+def test_add_group(web, group_type):
+ group_id = "%s_testgroup_id" % group_type
+ group_alias = "%s_testgroup_alias" % group_type
+ try:
+ web.add_group(group_type, group_id, {"alias": group_alias})
+ all_groups = web.get_all_groups(group_type)
+
+ assert group_id in all_groups
+ assert group_alias == all_groups[group_id]["alias"]
+ finally:
+ web.delete_group(group_type, group_id)
+
+
+(a)pytest.mark.parametrize(("group_type")ype"), [ "contact",
"host", "service" ])
+def test_edit_group(web, group_type):
+ group_id = "%s_testgroup_id" % group_type
+ group_alias = "%s_testgroup_alias" % group_type
+ group_alias2 = "%s_testgroup_otheralias" % group_type
+ try:
+ web.add_group(group_type, group_id, {"alias": group_alias})
+ web.edit_group(group_type, group_id, {"alias": group_alias2})
+
+ all_groups = web.get_all_groups(group_type)
+ assert group_id in all_groups
+ assert group_alias2 in all_groups[group_id]["alias"]
+ finally:
+ web.delete_group(group_type, group_id)
+
+
+(a)pytest.mark.parametrize(("group_type")ype"), [ "contact",
"host", "service" ])
+def test_edit_group_missing(web, group_type):
+ group_id = "%s_testgroup_id" % group_type
+ group_alias = "%s_testgroup_alias" % group_type
+ group_alias2 = "%s_testgroup_otheralias" % group_type
+ try:
+ web.add_group(group_type, group_id, {"alias": group_alias})
+ try:
+ #web.edit_group(group_type, group_id, {"alias": group_alias2},
expect_error = True)
+ web.edit_group(group_type, "%s_missing" % group_id,
{"alias": group_alias2}, expect_error = True)
+ except APIError, e:
+ assert str(e) != str(None)
+ return
+
+ assert False
+ finally:
+ web.delete_group(group_type, group_id)
+
+
+def test_edit_cg_group_with_nagvis_maps(web, site):
+ dummy_map_filepath1 = "%s/etc/nagvis/maps/blabla.cfg" % site.root
+ dummy_map_filepath2 = "%s/etc/nagvis/maps/bloblo.cfg" % site.root
+ try:
+ file(dummy_map_filepath1, "w")
+ file(dummy_map_filepath2, "w")
+ web.add_group("contact", "nagvis_test", {"alias":
"nagvis_test_alias", "nagvis_maps": ["blabla"]})
+ web.edit_group("contact", "nagvis_test", {"alias":
"nagvis_test_alias", "nagvis_maps": ["bloblo"]})
+
+ all_groups = web.get_all_groups("contact")
+ assert "nagvis_test" in all_groups
+ assert "bloblo" in
all_groups["nagvis_test"]["nagvis_maps"]
+ finally:
+ web.delete_group("contact", "nagvis_test")
+ os.unlink(dummy_map_filepath1)
+ os.unlink(dummy_map_filepath2)
+
+
+
+(a)pytest.mark.parametrize(("group_type")ype"), [ "contact",
"host", "service" ])
+def test_delete_group(web, group_type):
+ group_id = "%s_testgroup_id" % group_type
+ group_alias = "%s_testgroup_alias" % group_type
+ try:
+ web.add_group(group_type, group_id, {"alias": group_alias})
+ finally:
+ web.delete_group(group_type, group_id)
+
+
+def test_get_all_users(web):
+ users = {"klaus": {"alias": "mr. klaus",
"pager": "99221199", "password": "1234"},
+ "monroe": {"alias": "mr. monroe"}}
+ expected_users = set(["omdadmin", "automation"] + users.keys())
+ try:
+ response = web.add_htpasswd_users(users)
+ all_users = web.get_all_users()
+ assert not expected_users - set(all_users.keys())
+ finally:
+ web.delete_htpasswd_users(users.keys())
+
+
+def test_add_htpasswd_users(web):
+ users = {"klaus": {"alias": "mr. klaus",
"pager": "99221199", "password": "1234"},
+ "monroe": {"alias": "mr. monroe"}}
+ try:
+ web.add_htpasswd_users(users)
+ finally:
+ web.delete_htpasswd_users(users.keys())
+
+
+def test_edit_htpasswd_users(web):
+ users = {"klaus": {"alias": "mr. klaus",
"pager": "99221199", "password": "1234"},
+ "monroe": {"alias": "mr. monroe"}}
+ try:
+ web.add_htpasswd_users(users)
+ web.edit_htpasswd_users({"monroe": {"set_attributes":
{"alias": "ms. monroe"}},
+ "klaus": {"unset_attributes":
["pager"]}})
+ all_users = web.get_all_users()
+ assert not "pager" in all_users["klaus"]
+ assert all_users["monroe"]["alias"] == "ms.
monroe"
+ finally:
+ web.delete_htpasswd_users(users.keys())
+ pass
+
+
def test_discover_servics(web):
try:
web.add_host("test-host-discovery", attributes={