adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 1 | from config import config |
| 2 | from asfframework import VppTestCase, VppTestRunner |
| 3 | import unittest |
| 4 | import subprocess |
| 5 | import tempfile |
| 6 | from vpp_qemu_utils import ( |
| 7 | create_host_interface, |
| 8 | delete_host_interfaces, |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 9 | create_namespace, |
| 10 | delete_namespace, |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | @unittest.skipIf( |
| 15 | "http_static" in config.excluded_plugins, "Exclude HTTP Static Server plugin tests" |
| 16 | ) |
adrianvillin | e908fe7 | 2023-10-31 13:15:53 +0100 | [diff] [blame] | 17 | @unittest.skipIf(config.skip_netns_tests, "netns not available or disabled from cli") |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 18 | class TestHttpStaticVapi(VppTestCase): |
adrianvillin | e908fe7 | 2023-10-31 13:15:53 +0100 | [diff] [blame] | 19 | """enable the http static server and send requests [VAPI]""" |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 20 | |
| 21 | @classmethod |
| 22 | def setUpClass(cls): |
| 23 | super(TestHttpStaticVapi, cls).setUpClass() |
| 24 | # 2 temp files to improve coverage of http_cache.c |
| 25 | cls.temp = tempfile.NamedTemporaryFile() |
| 26 | cls.temp.write(b"Hello world") |
| 27 | |
| 28 | cls.temp2 = tempfile.NamedTemporaryFile() |
| 29 | cls.temp2.write(b"Hello world2") |
| 30 | |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 31 | create_namespace("HttpStatic") |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 32 | create_host_interface("vppHost", "vppOut", "HttpStatic", "10.10.1.1/24") |
adrianvillin | e908fe7 | 2023-10-31 13:15:53 +0100 | [diff] [blame] | 33 | |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 34 | cls.vapi.cli("create host-interface name vppOut") |
| 35 | cls.vapi.cli("set int state host-vppOut up") |
| 36 | cls.vapi.cli("set int ip address host-vppOut 10.10.1.2/24") |
| 37 | |
| 38 | @classmethod |
| 39 | def tearDownClass(cls): |
| 40 | delete_namespace(["HttpStatic"]) |
| 41 | delete_host_interfaces("vppHost") |
| 42 | cls.temp.close() |
| 43 | cls.temp2.close() |
| 44 | super(TestHttpStaticVapi, cls).tearDownClass() |
| 45 | |
| 46 | def test_http_static_vapi(self): |
| 47 | self.vapi.http_static_enable( |
| 48 | www_root="/tmp", |
| 49 | uri="tcp://0.0.0.0/80", |
| 50 | ) |
| 51 | # move file pointer to the beginning |
| 52 | self.temp.seek(0) |
| 53 | process = subprocess.run( |
| 54 | [ |
| 55 | "ip", |
| 56 | "netns", |
| 57 | "exec", |
| 58 | "HttpStatic", |
| 59 | "curl", |
| 60 | f"10.10.1.2/{self.temp.name[5:]}", |
| 61 | ], |
| 62 | capture_output=True, |
| 63 | ) |
| 64 | self.assertIn(b"Hello world", process.stdout) |
| 65 | |
| 66 | self.temp2.seek(0) |
| 67 | process = subprocess.run( |
| 68 | [ |
| 69 | "ip", |
| 70 | "netns", |
| 71 | "exec", |
| 72 | "HttpStatic", |
| 73 | "curl", |
| 74 | f"10.10.1.2/{self.temp2.name[5:]}", |
| 75 | ], |
| 76 | capture_output=True, |
| 77 | ) |
| 78 | self.assertIn(b"Hello world2", process.stdout) |
| 79 | |
| 80 | |
| 81 | @unittest.skipIf( |
| 82 | "http_static" in config.excluded_plugins, "Exclude HTTP Static Server plugin tests" |
| 83 | ) |
adrianvillin | e908fe7 | 2023-10-31 13:15:53 +0100 | [diff] [blame] | 84 | @unittest.skipIf(config.skip_netns_tests, "netns not available or disabled from cli") |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 85 | class TestHttpStaticCli(VppTestCase): |
adrianvillin | e908fe7 | 2023-10-31 13:15:53 +0100 | [diff] [blame] | 86 | """enable the static http server and send requests [CLI]""" |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 87 | |
| 88 | @classmethod |
| 89 | def setUpClass(cls): |
| 90 | super(TestHttpStaticCli, cls).setUpClass() |
| 91 | # 2 temp files to improve coverage of http_cache.c |
| 92 | cls.temp = tempfile.NamedTemporaryFile() |
| 93 | cls.temp.write(b"Hello world") |
| 94 | |
| 95 | cls.temp2 = tempfile.NamedTemporaryFile() |
| 96 | cls.temp2.write(b"Hello world2") |
| 97 | |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 98 | create_namespace("HttpStatic2") |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 99 | create_host_interface("vppHost2", "vppOut2", "HttpStatic2", "10.10.1.1/24") |
adrianvillin | e908fe7 | 2023-10-31 13:15:53 +0100 | [diff] [blame] | 100 | |
adrianvillin | 30f2600 | 2023-10-24 12:53:10 +0200 | [diff] [blame] | 101 | cls.vapi.cli("create host-interface name vppOut2") |
| 102 | cls.vapi.cli("set int state host-vppOut2 up") |
| 103 | cls.vapi.cli("set int ip address host-vppOut2 10.10.1.2/24") |
| 104 | |
| 105 | @classmethod |
| 106 | def tearDownClass(cls): |
| 107 | delete_namespace(["HttpStatic2"]) |
| 108 | delete_host_interfaces("vppHost2") |
| 109 | cls.temp.close() |
| 110 | cls.temp2.close() |
| 111 | super(TestHttpStaticCli, cls).tearDownClass() |
| 112 | |
| 113 | def test_http_static_cli(self): |
| 114 | self.vapi.cli( |
| 115 | "http static server www-root /tmp uri tcp://0.0.0.0/80 cache-size 2m" |
| 116 | ) |
| 117 | # move file pointer to the beginning |
| 118 | self.temp.seek(0) |
| 119 | process = subprocess.run( |
| 120 | [ |
| 121 | "ip", |
| 122 | "netns", |
| 123 | "exec", |
| 124 | "HttpStatic2", |
| 125 | "curl", |
| 126 | f"10.10.1.2/{self.temp.name[5:]}", |
| 127 | ], |
| 128 | capture_output=True, |
| 129 | ) |
| 130 | self.assertIn(b"Hello world", process.stdout) |
| 131 | |
| 132 | self.temp2.seek(0) |
| 133 | process = subprocess.run( |
| 134 | [ |
| 135 | "ip", |
| 136 | "netns", |
| 137 | "exec", |
| 138 | "HttpStatic2", |
| 139 | "curl", |
| 140 | f"10.10.1.2/{self.temp2.name[5:]}", |
| 141 | ], |
| 142 | capture_output=True, |
| 143 | ) |
| 144 | self.assertIn(b"Hello world2", process.stdout) |
| 145 | |
| 146 | self.vapi.cli("show http static server cache") |
| 147 | self.vapi.cli("clear http static cache") |
| 148 | self.vapi.cli("show http static server sessions") |
| 149 | |
| 150 | |
| 151 | if __name__ == "__main__": |
| 152 | unittest.main(testRunner=VppTestRunner) |