Signed-off-by: Waldemar Kozaczuk <[email protected]>
---
 modules/httpserver-api/tests/basetest.py      | 11 +++-
 .../tests/monitoring-api/testenv.py           | 19 +++++++
 .../tests/monitoring-api/testfile.py          | 54 +++++++++++++++++++
 .../tests/monitoring-api/testfs.py            | 18 +++++++
 .../tests/monitoring-api/testnetwork.py       | 30 +++++++++++
 .../tests/monitoring-api/testos.py            | 52 ++++++++++++++++++
 .../tests/testhttpserver-monitoring-api.py    | 41 ++++++++++++++
 7 files changed, 223 insertions(+), 2 deletions(-)
 create mode 100755 modules/httpserver-api/tests/monitoring-api/testenv.py
 create mode 100755 modules/httpserver-api/tests/monitoring-api/testfile.py
 create mode 100755 modules/httpserver-api/tests/monitoring-api/testfs.py
 create mode 100755 modules/httpserver-api/tests/monitoring-api/testnetwork.py
 create mode 100755 modules/httpserver-api/tests/monitoring-api/testos.py
 create mode 100755 
modules/httpserver-api/tests/testhttpserver-monitoring-api.py

diff --git a/modules/httpserver-api/tests/basetest.py 
b/modules/httpserver-api/tests/basetest.py
index 40ca64ce..271550c9 100755
--- a/modules/httpserver-api/tests/basetest.py
+++ b/modules/httpserver-api/tests/basetest.py
@@ -85,9 +85,9 @@ class Basetest(unittest.TestCase):
         path = self.path_by_nick(api_definition, nickname)
         self.assertRegex(self.curl(path), expr)
 
-    def assertHttpError(self, url, code=404):
+    def assertHttpError(self, url, code=404, method='GET', data=None):
         try:
-            self.curl(url)
+            self.curl(url, method, data)
         except HttpError as e:
             if e.code != code:
                 raise Exception('Expected error code %d but got %d' % (code, 
e.code))
@@ -159,6 +159,13 @@ class Basetest(unittest.TestCase):
                 raise Exception("Fail to shutdown server")
             time.sleep(1)
 
+    @classmethod
+    def hard_shutdown(cls):
+        child_pid = subprocess.call(['pgrep', "-P", str(cls.os_process.pid)])
+        subprocess.call(['kill', '-9', str(child_pid)])
+        cls.os_process.kill()
+        cls.os_process.wait()
+
     @classmethod
     def start_image(cls):
         if cls.config.check_jvm:
diff --git a/modules/httpserver-api/tests/monitoring-api/testenv.py 
b/modules/httpserver-api/tests/monitoring-api/testenv.py
new file mode 100755
index 00000000..1b92d76c
--- /dev/null
+++ b/modules/httpserver-api/tests/monitoring-api/testenv.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+import basetest
+
+class testenv(basetest.Basetest):
+    def test_env(self):
+        param = "test-param"
+        get_path = self.path_by_nick(self.env_api, "list_env") + param
+        set_path = get_path + "?val=TEST"
+        self.assertHttpError(set_path, 404, method='POST')
+
+        lst = self.curl(self.path_by_nick(self.env_api, "list_env"))
+        if not "OSV_CPUS=4" in lst:
+            raise Exception('environment variable OSV_CPUS not found in list')
+
+        self.assertHttpError(get_path, 404, method="DELETE")
+
+    @classmethod
+    def setUpClass(cls):
+        cls.env_api = cls.get_json_api("env.json")
diff --git a/modules/httpserver-api/tests/monitoring-api/testfile.py 
b/modules/httpserver-api/tests/monitoring-api/testfile.py
new file mode 100755
index 00000000..c03c1146
--- /dev/null
+++ b/modules/httpserver-api/tests/monitoring-api/testfile.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+import os
+import urllib.request, urllib.parse, urllib.error
+import basetest
+import subprocess
+
+class testfile(basetest.Basetest):
+    def build_curl_cmd(self, args):
+        if not self._client.is_ssl():
+            return 'curl ' + args
+        return 'curl --cacert %s --cert %s --key %s %s' % (
+            self.get_ca_cert_path(), self.get_client_cert_path(), 
self.get_client_key_path(), args)
+
+    def build_wget_cmd(self, args):
+        if not self._client.is_ssl():
+            return 'wget ' + args
+        return 'wget --ca-certificate=%s --certificate=%s --private-key=%s %s' 
% (
+            self.get_ca_cert_path(), self.get_client_cert_path(), 
self.get_client_key_path(), args)
+
+    def test_list_file_cmd(self):
+        path = "/file"
+        lst = self.curl(path + "/etc?op=LISTSTATUS")
+        hosts = next((item for item in lst if item["pathSuffix"] == "hosts"), 
None)
+        self.assertEqual(hosts["owner"], "osv")
+
+    def test_list_astrik_file_cmd(self):
+        path = "/file"
+        lst = self.curl(path + "/et%3F/hos*?op=LISTSTATUS")
+        hosts = next((item for item in lst if item["pathSuffix"] == "hosts"), 
None)
+        self.assertEqual(hosts["owner"], "osv")
+
+    def test_file_status_cmd(self):
+        path = "/file"
+        hosts = self.curl(path + "/etc/hosts?op=GETFILESTATUS")
+        self.assertEqual(hosts["type"], "FILE")
+        self.assert_between("accessTime", 1300000000, 2000000000, 
hosts["accessTime"])
+        self.assertEqual(hosts["blockSize"], 512)
+        self.assertEqual(hosts["group"], "osv")
+        self.assert_between("length", 20, 40, hosts["length"])
+        self.assert_between("modificationTime", 1300000000, 2000000000, 
hosts["modificationTime"])
+        self.assertEqual(hosts["owner"], "osv")
+        self.assertEqual(hosts["pathSuffix"], "hosts")
+        self.assertEqual(hosts["permission"], "664")
+        self.assertEqual(hosts["replication"], 1)
+
+    def test_put_file_cmd(self):
+        path = "/file"
+        self.assertHttpError(path + 
"/etc/hosts?op=COPY&destination="+urllib.parse.quote("/etc/hosts1"), 404, 
method='PUT')
+        self.assertHttpError(path + 
"/etc/hosts1?op=RENAME&destination="+urllib.parse.quote("/etc/hosts2"), 404, 
method='PUT')
+        self.assertHttpError(path + "/etc/hosts2?op=DELETE", 404, 
method='DELETE')
+
+    @classmethod
+    def setUpClass(cls):
+        cls.file_api = cls.get_json_api("file.json")
diff --git a/modules/httpserver-api/tests/monitoring-api/testfs.py 
b/modules/httpserver-api/tests/monitoring-api/testfs.py
new file mode 100755
index 00000000..b0924747
--- /dev/null
+++ b/modules/httpserver-api/tests/monitoring-api/testfs.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python3
+import basetest
+
+class testfs(basetest.Basetest):
+    def test_getfs(self):
+        get_path = "/fs/df/"
+        ls = self.curl(get_path)
+        val = ls[0]
+        self.assertEqual(val["mount"], "/")
+        self.assertGreaterEqual(val["ffree"], 200000)
+        self.assertGreaterEqual(val["ftotal"], 200000)
+        self.assertGreaterEqual(val["bfree"], 200000)
+        self.assertGreaterEqual(val["btotal"], 200000)
+        self.assertEqual(val["filesystem"], "/dev/vblk0.1")
+
+    @classmethod
+    def setUpClass(cls):
+        cls.fs_api = cls.get_json_api("fs.json")
diff --git a/modules/httpserver-api/tests/monitoring-api/testnetwork.py 
b/modules/httpserver-api/tests/monitoring-api/testnetwork.py
new file mode 100755
index 00000000..62b4fc83
--- /dev/null
+++ b/modules/httpserver-api/tests/monitoring-api/testnetwork.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+import time
+import basetest
+
+class testnetwork(basetest.Basetest):
+    def test_ifconfig(self):
+        path = self.path_by_nick(self.network_api, "listIfconfig")
+        intf = self.curl(path + 'lo0')
+        self.assert_key_in("data", intf)
+        self.assert_key_in("config", intf)
+        conf = intf["config"]
+        self.assert_key_in("phys_addr", conf)
+        self.assert_key_in("mask", conf)
+        self.assertEqual(conf["addr"], "127.0.0.1")
+        lst = self.curl(path)
+        self.assertNotEqual(lst, [])
+
+    def test_get_routes(self):
+        path = self.path_by_nick(self.network_api, "getRoute")
+        routes = self.curl(path)
+        route = routes[0]
+        self.assert_key_in("destination", route)
+        self.assert_key_in("gateway", route)
+        self.assert_key_in("flags", route)
+        self.assert_key_in("netif", route)
+        self.assert_key_in("ipv6", route)
+
+    @classmethod
+    def setUpClass(cls):
+        cls.network_api = cls.get_json_api("network.json")
diff --git a/modules/httpserver-api/tests/monitoring-api/testos.py 
b/modules/httpserver-api/tests/monitoring-api/testos.py
new file mode 100755
index 00000000..73ef6423
--- /dev/null
+++ b/modules/httpserver-api/tests/monitoring-api/testos.py
@@ -0,0 +1,52 @@
+#!/usr/bin/env python3
+import time
+import basetest
+
+class testos(basetest.Basetest):
+    def test_os_version(self):
+        path = self.path_by_nick(self.os_api, "os_version")
+        self.assertRegex(self.curl(path), r"v0\.\d+(-rc\d+)?(-\d+-[0-9a-z]+)?" 
, path)
+
+    def test_vendor(self):
+        self.validate_path(self.os_api, "os_vendor", "Cloudius Systems")
+
+    def test_os_uptime(self):
+        path = self.path_by_nick(self.os_api, "os_uptime")
+        up_time = self.curl(path)
+        time.sleep(2)
+        self.assert_between(path, up_time + 1, up_time + 3, self.curl(path))
+
+    def test_os_date(self):
+        path = self.path_by_nick(self.os_api, "os_date")
+        val = self.curl(path)
+        self.assertRegex(val, 
"...\\s+...\\s+\\d+\\s+\\d\\d:\\d\\d:\\d\\d\\s+UTC\\s+20..", path)
+
+    def test_os_total_memory(self):
+        path = self.path_by_nick(self.os_api, "os_memory_total")
+        val = self.curl(path)
+        self.assertGreater(val, 1024 * 1024 * 256, msg="Memory should be 
greater than 256Mb")
+
+    def test_os_free_memory(self):
+        path = self.path_by_nick(self.os_api, "os_memory_free")
+        val = self.curl(path)
+        self.assertGreater(val, 1024 * 1024 * 256, msg="Free memory should be 
greater than 256Mb")
+
+    def test_os_threads(self):
+        path = self.path_by_nick(self.os_api, "os_threads")
+        val = self.curl(path)
+        self.assert_key_in("time_ms", val)
+        ctime = val["time_ms"]
+        idle_thread = next((item for item in val["list"] if item["name"] == 
"idle1"), None)
+        idle = idle_thread["cpu_ms"]
+        id = idle_thread["id"]
+        cpu = idle_thread["cpu"]
+        self.assertEqual(cpu,1)
+        time.sleep(2)
+        val = self.curl(path)
+        self.assert_key_in("time_ms", val)
+        self.assert_between(path, ctime + 1000, ctime + 3000, val["time_ms"])
+        idle_thread = next((item for item in val["list"] if item["name"] == 
"idle1"), None)
+        idle1 = idle_thread["cpu_ms"]
+        self.assert_between(path + " idle thread cputime was" + str(idle)+
+                            " new time=" + str(idle1), idle + 1000, idle + 
3000, idle1)
+        self.assertEqual(id, idle_thread["id"])
diff --git a/modules/httpserver-api/tests/testhttpserver-monitoring-api.py 
b/modules/httpserver-api/tests/testhttpserver-monitoring-api.py
new file mode 100755
index 00000000..264536ab
--- /dev/null
+++ b/modules/httpserver-api/tests/testhttpserver-monitoring-api.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+import sys
+import argparse
+import os
+import unittest
+import basetest
+
+from osv import client
+
+parser = argparse.ArgumentParser(description="""Testing the read-only 
Httpserver""")
+
+module_base = os.path.join(os.path.realpath(os.path.dirname(__file__)), '..')
+osv_base = os.path.join(module_base, '..', '..')
+
+parser.add_argument('--connect', help='Connect to an existing image', 
action='store_true')
+parser.add_argument('--run_script', help='path to the run image script', 
default=os.path.join(osv_base, 'scripts', 'run.py'))
+parser.add_argument('--cmd', help='the command to execute')
+parser.add_argument('--use_sudo', help='Use sudo with -n option instead of 
port forwarding', action='store_true')
+parser.add_argument('--jsondir', help='location of the json files', 
default=os.path.join(module_base, 'api-doc/listings/'))
+parser.add_argument('--test_image', help='the path to the test image')
+parser.add_argument('--hypervisor', action="store", default="qemu", 
help="choose hypervisor to run: qemu, firecracker")
+client.Client.add_arguments(parser)
+
+class test_httpserver(basetest.Basetest):
+    @classmethod
+    def setUpClass(cls):
+        basetest.Basetest.start_image()
+
+    @classmethod
+    def tearDownClass(cls):
+        basetest.Basetest.hard_shutdown()
+
+if __name__ == '__main__':
+    basetest.Basetest.set_config(parser)
+    basetest.Basetest.config.check_jvm = False
+    basetest.Basetest.start_image()
+    del sys.argv[1:]
+    api_tests = unittest.TestLoader().discover(os.path.join(module_base, 
'tests', 'monitoring-api'), pattern='*.py')
+    test_suite = unittest.TestSuite((api_tests))
+    unittest.TextTestRunner(verbosity=2).run(test_suite)
+    basetest.Basetest.hard_shutdown()
-- 
2.20.1

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/20200319204501.23817-3-jwkozaczuk%40gmail.com.

Reply via email to