summaryrefslogtreecommitdiffstats
path: root/extras/hs-test/http_test.go
blob: f52d2816930598f670fc7ced9219c63851641d3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main

import (
	"fmt"
	"net/http"
	"os"
	"strings"
	"time"

	. "github.com/onsi/ginkgo/v2"
)

func init() {
	registerNsTests(HttpTpsTest)
	registerVethTests(HttpCliTest, HttpCliConnectErrorTest)
	registerNoTopoTests(NginxHttp3Test, NginxAsServerTest,
		NginxPerfCpsTest, NginxPerfRpsTest, NginxPerfWrkTest, HeaderServerTest,
		HttpStaticMovedTest, HttpStaticNotFoundTest, HttpCliMethodNotAllowedTest,
		HttpCliBadRequestTest)
	registerNoTopoSoloTests(HttpStaticPromTest)
}

func HttpTpsTest(s *NsSuite) {
	iface := s.getInterfaceByName(clientInterface)
	client_ip := iface.ip4AddressString()
	port := "8080"
	finished := make(chan error, 1)
	clientNetns := s.getNetNamespaceByName("cln")

	container := s.getContainerByName("vpp")

	// configure vpp in the container
	container.vppInstance.vppctl("http tps uri tcp://0.0.0.0/8080")

	go func() {
		defer GinkgoRecover()
		s.startWget(finished, client_ip, port, "test_file_10M", clientNetns)
	}()
	// wait for client
	err := <-finished
	s.assertNil(err, fmt.Sprint(err))
}

func HttpCliTest(s *VethsSuite) {
	serverContainer := s.getContainerByName("server-vpp")
	clientContainer := s.getContainerByName("client-vpp")

	serverVeth := s.getInterfaceByName(serverInterfaceName)

	serverContainer.vppInstance.vppctl("http cli server")

	uri := "http://" + serverVeth.ip4AddressString() + "/80"

	o := clientContainer.vppInstance.vppctl("http cli client" +
		" uri " + uri + " query /show/vlib/graph")

	s.log(o)
	s.assertContains(o, "<html>", "<html> not found in the result!")
}

func HttpCliConnectErrorTest(s *VethsSuite) {
	clientContainer := s.getContainerByName("client-vpp")

	serverVeth := s.getInterfaceByName(serverInterfaceName)

	uri := "http://" + serverVeth.ip4AddressString() + "/80"

	o := clientContainer.vppInstance.vppctl("http cli client" +
		" uri " + uri + " query /show/vlib/graph")

	s.log(o)
	s.assertContains(o, "failed to connect")
}

func NginxHttp3Test(s *NoTopoSuite) {
	s.SkipUnlessExtendedTestsBuilt()

	query := "index.html"
	nginxCont := s.getContainerByName("nginx-http3")
	s.assertNil(nginxCont.run())

	vpp := s.getContainerByName("vpp").vppInstance
	vpp.waitForApp("nginx-", 5)
	serverAddress := s.getInterfaceByName(tapInterfaceName).peer.ip4AddressString()

	defer func() { os.Remove(query) }()
	curlCont := s.getContainerByName("curl")
	args := fmt.Sprintf("curl --noproxy '*' --local-port 55444 --http3-only -k https://%s:8443/%s", serverAddress, query)
	curlCont.extraRunningArgs = args
	o, err := curlCont.combinedOutput()
	s.assertNil(err, fmt.Sprint(err))
	s.assertContains(o, "<http>", "<http> not found in the result!")
}

func HttpStaticPromTest(s *NoTopoSuite) {
	finished := make(chan error, 1)
	query := "stats.prom"
	vpp := s.getContainerByName("vpp").vppInstance
	serverAddress := s.getInterfaceByName(tapInterfaceName).peer.ip4AddressString()
	s.log(vpp.vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
	s.log(vpp.vppctl("prom enable"))
	time.Sleep(time.Second * 5)
	go func() {
		defer GinkgoRecover()
		s.startWget(finished, serverAddress, "80", query, "")
	}()
	err := <-finished
	s.assertNil(err)
}

func HttpStaticMovedTest(s *NoTopoSuite) {
	vpp := s.getContainerByName("vpp").vppInstance
	vpp.container.exec("mkdir -p /tmp/tmp.aaa")
	vpp.container.createFile("/tmp/tmp.aaa/index.html", "<http><body><p>Hello</p></body></http>")
	serverAddress := s.getInterfaceByName(tapInterfaceName).peer.ip4AddressString()
	s.log(vpp.vppctl("http static server www-root /tmp uri tcp://" + serverAddress + "/80 debug"))

	client := &http.Client{
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			return http.ErrUseLastResponse
		},
	}
	req, err := http.NewRequest("GET", "http://"+serverAddress+":80/tmp.aaa", nil)
	s.assertNil(err, fmt.Sprint(err))
	resp, err := client.Do(req)
	s.assertNil(err, fmt.Sprint(err))
	defer resp.Body.Close()
	s.assertEqual(301, resp.StatusCode)
	s.assertNotEqual("", resp.Header.Get("Location"))
}

func HttpStaticNotFoundTest(s *NoTopoSuite) {
	vpp := s.getContainerByName("vpp").vppInstance
	serverAddress := s.getInterfaceByName(tapInterfaceName).peer.ip4AddressString()
	s.log(vpp.vppctl("http static server www-root /tmp uri tcp://" + serverAddress + "/80 debug"))

	req, err := http.NewRequest("GET", "http://"+serverAddress+":80/notfound.html", nil)
	s.assertNil(err, fmt.Sprint(err))
	resp, err := http.DefaultClient.Do(req)
	s.assertNil(err, fmt.Sprint(err))
	defer resp.Body.Close()
	s.assertEqual(404, resp.StatusCode)
}

func HttpCliMethodNotAllowedTest(s *NoTopoSuite) {
	vpp := s.getContainerByName("vpp").vppInstance
	serverAddress := s.getInterfaceByName(tapInterfaceName).peer.ip4AddressString()
	vpp.vppctl("http cli server")

	req, err := http.NewRequest("POST", "http://"+serverAddress+":80/test", nil)
	s.assertNil(err, fmt.Sprint(err))
	resp, err := http.DefaultClient.Do(req)
	s.assertNil(err, fmt.Sprint(err))
	defer resp.Body.Close()
	s.assertEqual(405, resp.StatusCode)
	// TODO: need to be fixed in http code
	//s.assertNotEqual("", resp.Header.Get("Allow"))
}

func HttpCliBadRequestTest(s *NoTopoSuite) {
	vpp := s.getContainerByName("vpp").vppInstance
	serverAddress := s.getInterfaceByName(tapInterfaceName).peer.ip4AddressString()
	vpp.vppctl("http cli server")

	req, err := http.NewRequest("GET", "http://"+serverAddress+":80", nil)
	s.assertNil(err, fmt.Sprint(err))
	resp, err := http.DefaultClient.Do(req)
	s.assertNil(err, fmt.Sprint(err))
	defer resp.Body.Close()
	s.assertEqual(400, resp.StatusCode)
}

func HeaderServerTest(s *NoTopoSuite) {
	vpp := s.getContainerByName("vpp").vppInstance
	serverAddress := s.getInterfaceByName(tapInterfaceName).peer.ip4AddressString()
	vpp.vppctl("http cli server")

	req, err := http.NewRequest("GET", "http://"+serverAddress+":80/show/version", nil)
	s.assertNil(err, fmt.Sprint(err))
	resp, err := http.DefaultClient.Do(req)
	s.assertNil(err, fmt.Sprint(err))
	defer resp.Body.Close()
	s.assertEqual("http_cli_server", resp.Header.Get("Server"))
}

func NginxAsServerTest(s *NoTopoSuite) {
	query := "return_ok"
	finished := make(chan error, 1)

	nginxCont := s.getContainerByName("nginx")
	s.assertNil(nginxCont.run())

	vpp := s.getContainerByName("vpp").vppInstance
	vpp.waitForApp("nginx-", 5)

	serverAddress := s.getInterfaceByName(tapInterfaceName).peer.ip4AddressString()

	defer func() { os.Remove(query) }()
	go func() {
		defer GinkgoRecover()
		s.startWget(finished, serverAddress, "80", query, "")
	}()
	s.assertNil(<-finished)
}

func parseString(s, pattern string) string {
	temp := strings.Split(s, "\n")
	for _, item := range temp {
		if strings.Contains(item, pattern) {
			return item
		}
	}
	return ""
}

func runNginxPerf(s *NoTopoSuite, mode, ab_or_wrk string) error {
	nRequests := 1000000
	nClients := 1000

	serverAddress := s.getInterfaceByName(tapInterfaceName).peer.ip4AddressString()

	vpp := s.getContainerByName("vpp").vppInstance

	nginxCont := s.getContainerByName(singleTopoContainerNginx)
	s.assertNil(nginxCont.run())
	vpp.waitForApp("nginx-", 5)

	if ab_or_wrk == "ab" {
		abCont := s.getContainerByName("ab")
		args := fmt.Sprintf("-n %d -c %d", nRequests, nClients)
		if mode == "rps" {
			args += " -k"
		} else if mode != "cps" {
			return fmt.Errorf("invalid mode %s; expected cps/rps", mode)
		}
		// don't exit on socket receive errors
		args += " -r"
		args += " http://" + serverAddress + ":80/64B.json"
		abCont.extraRunningArgs = args
		time.Sleep(time.Second * 10)
		o, err := abCont.combinedOutput()
		rps := parseString(o, "Requests per second:")
		s.log(rps)
		s.log(err)
		s.assertNil(err, "err: '%s', output: '%s'", err, o)
	} else {
		wrkCont := s.getContainerByName("wrk")
		args := fmt.Sprintf("-c %d -t 2 -d 30 http://%s:80/64B.json", nClients,
			serverAddress)
		wrkCont.extraRunningArgs = args
		o, err := wrkCont.combinedOutput()
		rps := parseString(o, "requests")
		s.log(rps)
		s.log(err)
		s.assertNil(err, "err: '%s', output: '%s'", err, o)
	}
	return nil
}

func NginxPerfCpsTest(s *NoTopoSuite) {
	s.assertNil(runNginxPerf(s, "cps", "ab"))
}

func NginxPerfRpsTest(s *NoTopoSuite) {
	s.assertNil(runNginxPerf(s, "rps", "ab"))
}

func NginxPerfWrkTest(s *NoTopoSuite) {
	s.assertNil(runNginxPerf(s, "", "wrk"))
}