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