Browse Source

[5137] Python unit-tests written

Tomek Mrugalski 8 years ago
parent
commit
2939c088a7
2 changed files with 82 additions and 5 deletions
  1. 4 1
      src/bin/shell/tests/Makefile.am
  2. 78 4
      src/bin/shell/tests/kea-shell_unittest.py

+ 4 - 1
src/bin/shell/tests/Makefile.am

@@ -6,13 +6,16 @@ noinst_SCRIPTS = $(PYTESTS) shell_process_tests.sh
 EXTRA_DIST = testdata/plugins/testplugin.py
 
 # test using command-line arguments, so use check-local target instead of TESTS
-check-local:
+check-local: check-local-shell check-local-python
+
+check-local-python:
 	@for pytest in $(PYTESTS) ; do \
 	echo Running python test: $$pytest ; \
 	chmod +x $(abs_builddir)/$$pytest ; \
 	PYTHONPATH=$(PYTHONPATH):$(abs_top_builddir)/src/bin/shell python $(abs_builddir)/$$pytest || exit ; \
 	done
 
+check-local-shell:
 	@for shtest in $(SHTESTS) ; do \
 	echo Running shell test: $$shtest ; \
 	export KEA_LOCKFILE_DIR=$(abs_top_builddir); \

+ 78 - 4
src/bin/shell/tests/kea-shell_unittest.py

@@ -6,15 +6,89 @@
 
 import unittest
 
+from kea_conn import CARequest
+
 class CARequestUnitTest(unittest.TestCase):
+    """
+    This class is dedicated to testing CARequest class. That class
+    is responsible for generation of the body and headers.
+    """
+
     def setUp(self):
-        print ("Setting up")
+        """
+        This method is called before each test. Currently it does nothing.
+        """
+        pass
+
+    def test_bodyWithoutParams(self):
+        """
+        This test verifies if the CARequest object generates the request
+        content properly when there are no parameters.
+        """
+
+        x = CARequest()
+        x.command = "foo"
+        x.generateBody()
+        self.assertEqual(x.content, '{ "command": "foo" }')
+
+    def test_bodyWithParams(self):
+        """
+        This test verifies if the CARequest object generates the request
+        content properly when there are parameters.
+        """
+
+        x = CARequest()
+        x.command = "foo"
+        x.params = '"bar": "baz"'
+        x.generateBody()
+        self.assertEqual(x.content, '{ "command": "foo", "parameters": { "bar": "baz" } }')
+
+    def checkHeader(self, headers, header_name, value):
+        """
+        Checks if headers array contains an entry specified by header_name and that
+        its value matches specified value
+        """
+
+        if header_name in headers:
+            if (headers[header_name] == value):
+                return True
+            else:
+                print ("Expected value: " + value + " does not match actual value: "
+                       + headers[header_name])
+            return ()
+        else:
+            print ("Expected header: " + header_name + " missing")
+            return (false)
+
+    def test_headers(self):
+        """
+        This test checks if the headers are generated properly. Note that since
+        the content is not specified, it is 0. Therefore Content-Length is 0.
+        """
+        x = CARequest()
+        x.generateHeaders()
+
+        self.assertTrue(self.checkHeader(x.headers, 'Content-Type', 'application/json'))
+        self.assertTrue(self.checkHeader(x.headers, 'Accept', '*/*'))
+        self.assertTrue(self.checkHeader(x.headers, 'Content-Length', "0"))
+
+    def test_headerLength(self):
+        """
+        This test checks if the headers are generated properly. In this test there
+        is specific content of non-zero length, and its size should be reflected
+        in the header.
+        """
+        x = CARequest()
+        x.content = '{ "command": "foo" }'
+        x.generateHeaders()
 
-    def runTest(self):
-        print ("Run test")
+        self.assertTrue(self.checkHeader(x.headers, 'Content-Length', str(len(x.content))))
 
     def tearDown(self):
-        print ("Tearing down")
+        """
+        This method is called after each test. Currently it does nothing.
+        """
+        pass
 
 
 if __name__ == '__main__':