blob: 504ffa3523600fbc5fcb839134f2455082a92e33 [file] [log] [blame]
adrianvillin30f26002023-10-24 12:53:10 +02001from config import config
2from asfframework import VppTestCase, VppTestRunner
3import 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")
adrianvillin30f26002023-10-24 12:53:10 +020018class TestHttpStaticVapi(VppTestCase):
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
adrianvillin30f26002023-10-24 12:53:10 +020031 create_namespace("HttpStatic")
adrianvillin30f26002023-10-24 12:53:10 +020032 create_host_interface("vppHost", "vppOut", "HttpStatic", "10.10.1.1/24")
adrianvilline908fe72023-10-31 13:15:53 +010033
adrianvillin30f26002023-10-24 12:53:10 +020034 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)
adrianvilline908fe72023-10-31 13:15:53 +010084@unittest.skipIf(config.skip_netns_tests, "netns not available or disabled from cli")
adrianvillin30f26002023-10-24 12:53:10 +020085class TestHttpStaticCli(VppTestCase):
adrianvilline908fe72023-10-31 13:15:53 +010086 """enable the static http server and send requests [CLI]"""
adrianvillin30f26002023-10-24 12:53:10 +020087
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
adrianvillin30f26002023-10-24 12:53:10 +020098 create_namespace("HttpStatic2")
adrianvillin30f26002023-10-24 12:53:10 +020099 create_host_interface("vppHost2", "vppOut2", "HttpStatic2", "10.10.1.1/24")
adrianvilline908fe72023-10-31 13:15:53 +0100100
adrianvillin30f26002023-10-24 12:53:10 +0200101 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
151if __name__ == "__main__":
152 unittest.main(testRunner=VppTestRunner)