[checkmk-commits] added tests form load_data_to_file/save_data_from_file

Lars Michelsen lm at mathias-kettner.de
Thu Sep 29 12:12:55 CEST 2016


Module: check_mk
Branch: master
Commit: 2ce54cb6aa82fe08c6a514356ddf58f105ba9751
URL:    http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2ce54cb6aa82fe08c6a514356ddf58f105ba9751

Author: Lars Michelsen <lm at mathias-kettner.de>
Date:   Thu Sep 29 08:04:24 2016 +0200

added tests form load_data_to_file/save_data_from_file

---

 lib/store.py            | 26 ++++++++++++----------
 tests/lib/test_store.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 12 deletions(-)

diff --git a/lib/store.py b/lib/store.py
index bc92d2b..8101918 100644
--- a/lib/store.py
+++ b/lib/store.py
@@ -152,13 +152,14 @@ def load_mk_file(path, default=None, lock=False):
         aquire_lock(path)
 
     try:
-        execfile(path, globals(), default)
-        return default
-    except IOError, e:
-        if e.errno == 2: # IOError: [Errno 2] No such file or directory
+        try:
+            execfile(path, globals(), default)
             return default
-        else:
-            raise
+        except IOError, e:
+            if e.errno == 2: # IOError: [Errno 2] No such file or directory
+                return default
+            else:
+                raise
 
     except Exception, e:
         # TODO: How to handle debug mode or logging?
@@ -186,12 +187,13 @@ def load_data_from_file(path, default=None, lock=False):
         aquire_lock(path)
 
     try:
-        return ast.literal_eval(file(path).read())
-    except IOError, e:
-        if e.errno == 2: # IOError: [Errno 2] No such file or directory
-            return default
-        else:
-            raise
+        try:
+            return ast.literal_eval(file(path).read())
+        except IOError, e:
+            if e.errno == 2: # IOError: [Errno 2] No such file or directory
+                return default
+            else:
+                raise
 
     except Exception, e:
         # TODO: How to handle debug mode or logging?
diff --git a/tests/lib/test_store.py b/tests/lib/test_store.py
index 03d658c..e5ba7ea 100644
--- a/tests/lib/test_store.py
+++ b/tests/lib/test_store.py
@@ -1,8 +1,67 @@
+# encoding: utf-8
+
 import cmk.store as store
+from cmk.exceptions import MKGeneralException
 import imp
 import sys
 import threading
 import time
+import os
+import pytest
+
+def test_load_data_from_file_not_existing(tmpdir):
+    data = store.load_data_from_file("%s/x" % tmpdir)
+    assert data == None
+
+    data = store.load_data_from_file("%s/x" % tmpdir, "DEFAULT")
+    assert data == "DEFAULT"
+
+
+def test_load_data_not_locked(tmpdir):
+    locked_file = tmpdir.join("locked_file")
+    locked_file.write("[1, 2]")
+
+    data = store.load_data_from_file("%s" % locked_file)
+    assert store.have_lock("%s" % locked_file) == False
+
+
+def test_load_data_from_file_locking(tmpdir):
+    locked_file = tmpdir.join("locked_file")
+    locked_file.write("[1, 2]")
+
+    data = store.load_data_from_file("%s" % locked_file, lock=True)
+    assert data == [1, 2]
+    assert store.have_lock("%s" % locked_file) == True
+
+
+def test_load_data_from_not_permitted_file(tmpdir):
+    locked_file = tmpdir.join("test")
+    locked_file.write("[1, 2]")
+    os.chmod("%s" % locked_file, 0200)
+
+    with pytest.raises(MKGeneralException) as e:
+        store.load_data_from_file("%s" % locked_file)
+    assert "%s" % locked_file in "%s" % e
+    assert "Permission denied" in "%s" % e
+
+
+def test_load_data_from_file_dict(tmpdir):
+    locked_file = tmpdir.join("test")
+    locked_file.write(repr({"1": 2, "ä": u"ß"}))
+
+    data = store.load_data_from_file("%s" % locked_file)
+    assert type(data) == dict
+    assert data["1"] == 2
+    assert type(data["ä"]) == unicode
+    assert data["ä"] == u"ß"
+
+
+def test_save_data_to_file(tmpdir):
+    f = tmpdir.join("test")
+    path = "%s" % f
+
+    store.save_data_to_file(path, [2, 3])
+    assert store.load_data_from_file(path) == [2, 3]
 
 
 def test_acquire_lock_not_existing(tmpdir):



More information about the checkmk-commits mailing list