diff options
author | adrianvillin <avillin@cisco.com> | 2023-10-24 12:53:10 +0200 |
---|---|---|
committer | Dave Wallace <dwallacelf@gmail.com> | 2023-10-30 21:17:56 +0000 |
commit | 30f2600dcf143baaff845570086b8498c01e29ee (patch) | |
tree | 541f9abd13da741afab5e5e6e884cdca87a247cb /test | |
parent | c78eeaba49cba55a5e4c18aad1bd41e4c9b3aa33 (diff) |
tests: Added http static server tests.
Coverage increased from 21% to 80%
Type: test
Change-Id: Ic8ecc620cef738d7dbe4c259f58a373ac155a588
Signed-off-by: adrianvillin <avillin@cisco.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/asf/test_http_static.py | 155 | ||||
-rw-r--r-- | test/vpp_qemu_utils.py | 5 |
2 files changed, 157 insertions, 3 deletions
diff --git a/test/asf/test_http_static.py b/test/asf/test_http_static.py new file mode 100644 index 00000000000..17eed9c9c7b --- /dev/null +++ b/test/asf/test_http_static.py @@ -0,0 +1,155 @@ +from config import config +from asfframework import VppTestCase, VppTestRunner +import unittest +import subprocess +import tempfile +from vpp_qemu_utils import ( + create_host_interface, + delete_host_interfaces, + can_create_namespaces, + create_namespace, + delete_namespace, +) + + +@unittest.skipIf( + "http_static" in config.excluded_plugins, "Exclude HTTP Static Server plugin tests" +) +class TestHttpStaticVapi(VppTestCase): + """enable the http static server [VAPI]""" + + @classmethod + def setUpClass(cls): + super(TestHttpStaticVapi, cls).setUpClass() + # 2 temp files to improve coverage of http_cache.c + cls.temp = tempfile.NamedTemporaryFile() + cls.temp.write(b"Hello world") + + cls.temp2 = tempfile.NamedTemporaryFile() + cls.temp2.write(b"Hello world2") + + if not can_create_namespaces(): + raise Exception("Unable to create namespace") + create_namespace("HttpStatic") + + create_host_interface("vppHost", "vppOut", "HttpStatic", "10.10.1.1/24") + cls.vapi.cli("create host-interface name vppOut") + cls.vapi.cli("set int state host-vppOut up") + cls.vapi.cli("set int ip address host-vppOut 10.10.1.2/24") + + @classmethod + def tearDownClass(cls): + delete_namespace(["HttpStatic"]) + delete_host_interfaces("vppHost") + cls.temp.close() + cls.temp2.close() + super(TestHttpStaticVapi, cls).tearDownClass() + + def test_http_static_vapi(self): + self.vapi.http_static_enable( + www_root="/tmp", + uri="tcp://0.0.0.0/80", + ) + # move file pointer to the beginning + self.temp.seek(0) + process = subprocess.run( + [ + "ip", + "netns", + "exec", + "HttpStatic", + "curl", + f"10.10.1.2/{self.temp.name[5:]}", + ], + capture_output=True, + ) + self.assertIn(b"Hello world", process.stdout) + + self.temp2.seek(0) + process = subprocess.run( + [ + "ip", + "netns", + "exec", + "HttpStatic", + "curl", + f"10.10.1.2/{self.temp2.name[5:]}", + ], + capture_output=True, + ) + self.assertIn(b"Hello world2", process.stdout) + + +@unittest.skipIf( + "http_static" in config.excluded_plugins, "Exclude HTTP Static Server plugin tests" +) +class TestHttpStaticCli(VppTestCase): + """enable the static http server [CLI]""" + + @classmethod + def setUpClass(cls): + super(TestHttpStaticCli, cls).setUpClass() + # 2 temp files to improve coverage of http_cache.c + cls.temp = tempfile.NamedTemporaryFile() + cls.temp.write(b"Hello world") + + cls.temp2 = tempfile.NamedTemporaryFile() + cls.temp2.write(b"Hello world2") + + if not can_create_namespaces("vpp_chk_4212_2"): + raise Exception("Unable to create namespace") + create_namespace("HttpStatic2") + + create_host_interface("vppHost2", "vppOut2", "HttpStatic2", "10.10.1.1/24") + cls.vapi.cli("create host-interface name vppOut2") + cls.vapi.cli("set int state host-vppOut2 up") + cls.vapi.cli("set int ip address host-vppOut2 10.10.1.2/24") + + @classmethod + def tearDownClass(cls): + delete_namespace(["HttpStatic2"]) + delete_host_interfaces("vppHost2") + cls.temp.close() + cls.temp2.close() + super(TestHttpStaticCli, cls).tearDownClass() + + def test_http_static_cli(self): + self.vapi.cli( + "http static server www-root /tmp uri tcp://0.0.0.0/80 cache-size 2m" + ) + # move file pointer to the beginning + self.temp.seek(0) + process = subprocess.run( + [ + "ip", + "netns", + "exec", + "HttpStatic2", + "curl", + f"10.10.1.2/{self.temp.name[5:]}", + ], + capture_output=True, + ) + self.assertIn(b"Hello world", process.stdout) + + self.temp2.seek(0) + process = subprocess.run( + [ + "ip", + "netns", + "exec", + "HttpStatic2", + "curl", + f"10.10.1.2/{self.temp2.name[5:]}", + ], + capture_output=True, + ) + self.assertIn(b"Hello world2", process.stdout) + + self.vapi.cli("show http static server cache") + self.vapi.cli("clear http static cache") + self.vapi.cli("show http static server sessions") + + +if __name__ == "__main__": + unittest.main(testRunner=VppTestRunner) diff --git a/test/vpp_qemu_utils.py b/test/vpp_qemu_utils.py index cb1a0dd9f4c..e7e21a01b73 100644 --- a/test/vpp_qemu_utils.py +++ b/test/vpp_qemu_utils.py @@ -6,11 +6,10 @@ import subprocess import sys -def can_create_namespaces(): +def can_create_namespaces(namespace="vpp_chk_4212"): """Check if the environment allows creating the namespaces""" try: - namespace = "vpp_chk_4212" result = subprocess.run(["ip", "netns", "add", namespace], capture_output=True) if result.returncode != 0: return False @@ -18,7 +17,7 @@ def can_create_namespaces(): if result.returncode != 0: return False return True - except: + except Exception: return False |