Module: check_mk
Branch: master
Commit: e5c504dfcb31e31ba7cf99ce9cee88f8c8777394
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e5c504dfcb31e3…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue Oct 11 16:45:45 2016 +0200
Added some helper functions to detect the current Check_MK environment
---
lib/__init__.py | 30 ++++++++++++++++++++++++++++
tests/lib/test_base.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+)
diff --git a/lib/__init__.py b/lib/__init__.py
index dad38dd..950c2d6 100644
--- a/lib/__init__.py
+++ b/lib/__init__.py
@@ -30,3 +30,33 @@ This library is currently handled as internal module of Check_MK and
does not offer stable APIs. The code may change at any time."""
__version__ = "1.4.0i2"
+
+import os
+
+import cmk.paths
+
+
+def omd_version():
+ return os.path.basename(os.readlink(cmk.paths.omd_root + "/version"))
+
+
+def edition_short():
+ """Can currently either return \"cre\" or
\"cee\"."""
+ parts = omd_version().split(".")
+ if parts[-1] == "demo":
+ return parts[-2]
+ else:
+ return parts[-1]
+
+
+def is_enterprise_edition():
+ return edition_short() == "cee"
+
+
+def is_raw_edition():
+ return edition_short() == "cre"
+
+
+def is_demo():
+ parts = omd_version().split(".")
+ return parts[-1] == "demo"
diff --git a/tests/lib/test_base.py b/tests/lib/test_base.py
new file mode 100644
index 0000000..62c8ba4
--- /dev/null
+++ b/tests/lib/test_base.py
@@ -0,0 +1,54 @@
+import pytest
+import os
+
+import cmk
+
+def test_version():
+ assert type(cmk.__version__) == str
+
+
+def test_omd_version(tmpdir, monkeypatch):
+ link_path = "%s" % tmpdir.dirpath("version")
+
+ monkeypatch.setattr(cmk.paths, 'omd_root', os.path.dirname(link_path))
+
+ os.symlink("/omd/versions/2016.09.12.cee", link_path)
+ assert cmk.omd_version() == "2016.09.12.cee"
+ os.unlink(link_path)
+
+ os.symlink("/omd/versions/2016.09.12.cee.demo", link_path)
+ assert cmk.omd_version() == "2016.09.12.cee.demo"
+ os.unlink(link_path)
+
+
+def test_is_enterprise_edition(monkeypatch):
+ monkeypatch.setattr(cmk, "omd_version", lambda: "1.4.0i1.cre")
+ assert cmk.is_enterprise_edition() == False
+ monkeypatch.setattr(cmk, "omd_version", lambda: "1.4.0i1.cee")
+ assert cmk.is_enterprise_edition() == True
+ monkeypatch.setattr(cmk, "omd_version", lambda:
"2016.09.22.cee")
+ assert cmk.is_enterprise_edition() == True
+ monkeypatch.setattr(cmk, "omd_version", lambda:
"2016.09.22.cee.demo")
+ assert cmk.is_enterprise_edition() == True
+
+
+def test_is_raw_edition(monkeypatch):
+ monkeypatch.setattr(cmk, "omd_version", lambda: "1.4.0i1.cre")
+ assert cmk.is_raw_edition() == True
+ monkeypatch.setattr(cmk, "omd_version", lambda: "1.4.0i1.cee")
+ assert cmk.is_raw_edition() == False
+ monkeypatch.setattr(cmk, "omd_version", lambda:
"2016.09.22.cee")
+ assert cmk.is_raw_edition() == False
+ monkeypatch.setattr(cmk, "omd_version", lambda:
"2016.09.22.cee.demo")
+ assert cmk.is_raw_edition() == False
+
+
+def test_is_demo(monkeypatch):
+ monkeypatch.setattr(cmk, "omd_version", lambda: "1.4.0i1.cre")
+ assert cmk.is_demo() == False
+ monkeypatch.setattr(cmk, "omd_version", lambda: "1.4.0i1.cee")
+ assert cmk.is_demo() == False
+ monkeypatch.setattr(cmk, "omd_version", lambda:
"2016.09.22.cee")
+ assert cmk.is_demo() == False
+ monkeypatch.setattr(cmk, "omd_version", lambda:
"2016.09.22.cee.demo")
+ assert cmk.is_demo() == True