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