blob: 73a95e992dafaa3704c5aa8f4c030d6820dde417 [file] [log] [blame]
adrianvillin30f26002023-10-24 12:53:10 +02001from config import config
Dave Wallace8800f732023-08-31 00:47:44 -04002from asfframework import VppAsfTestCase, VppTestRunner
adrianvillin30f26002023-10-24 12:53:10 +02003import unittest
4import subprocess
5import tempfile
6from vpp_qemu_utils import (
7 create_host_interface,
8 delete_host_interfaces,
adrianvillin30f26002023-10-24 12:53:10 +02009 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)
adrianvilline908fe72023-10-31 13:15:53 +010017@unittest.skipIf(config.skip_netns_tests, "netns not available or disabled from cli")
Dave Wallace8800f732023-08-31 00:47:44 -040018class TestHttpStaticVapi(VppAsfTestCase):
adrianvilline908fe72023-10-31 13:15:53 +010019 """enable the http static server and send requests [VAPI]"""
adrianvillin30f26002023-10-24 12:53:10 +020020
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
adrianvillin098ee3a2023-11-08 15:17:14 +010031 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
adrianvillin30f26002023-10-24 12:53:10 +020038 create_host_interface("vppHost", "vppOut", "HttpStatic", "10.10.1.1/24")
adrianvilline908fe72023-10-31 13:15:53 +010039
adrianvillin30f26002023-10-24 12:53:10 +020040 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):
adrianvillin098ee3a2023-11-08 15:17:14 +010046 delete_namespace("HttpStatic")
adrianvillin30f26002023-10-24 12:53:10 +020047 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 Villin86fa9432024-08-08 08:56:34 +020066 "-v",
adrianvillin30f26002023-10-24 12:53:10 +020067 f"10.10.1.2/{self.temp.name[5:]}",
68 ],
69 capture_output=True,
70 )
71 self.assertIn(b"Hello world", process.stdout)
Adrian Villin86fa9432024-08-08 08:56:34 +020072 self.assertIn(b"max-age=600", process.stderr)
adrianvillin30f26002023-10-24 12:53:10 +020073
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)
adrianvilline908fe72023-10-31 13:15:53 +010092@unittest.skipIf(config.skip_netns_tests, "netns not available or disabled from cli")
Dave Wallace8800f732023-08-31 00:47:44 -040093class TestHttpStaticCli(VppAsfTestCase):
adrianvilline908fe72023-10-31 13:15:53 +010094 """enable the static http server and send requests [CLI]"""
adrianvillin30f26002023-10-24 12:53:10 +020095
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
adrianvillin098ee3a2023-11-08 15:17:14 +0100106 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
adrianvillin30f26002023-10-24 12:53:10 +0200113 create_host_interface("vppHost2", "vppOut2", "HttpStatic2", "10.10.1.1/24")
adrianvilline908fe72023-10-31 13:15:53 +0100114
adrianvillin30f26002023-10-24 12:53:10 +0200115 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):
adrianvillin098ee3a2023-11-08 15:17:14 +0100121 delete_namespace("HttpStatic2")
adrianvillin30f26002023-10-24 12:53:10 +0200122 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
165if __name__ == "__main__":
166 unittest.main(testRunner=VppTestRunner)