53e6ec22ac32513d1c43ec47d7f28e4fcbfe7112
[vpp.git] / extras / hs-test / http_test.go
1 package main
2
3 import (
4         "fmt"
5         "os"
6         "strings"
7 )
8
9 func (s *NsSuite) TestHttpTps() {
10         iface := s.netInterfaces[clientInterface]
11         client_ip := iface.ip4AddressString()
12         port := "8080"
13         finished := make(chan error, 1)
14
15         container := s.getContainerByName("vpp")
16
17         // configure vpp in the container
18         container.vppInstance.vppctl("http tps uri tcp://0.0.0.0/8080")
19
20         go s.startWget(finished, client_ip, port, "test_file_10M", "client")
21         // wait for client
22         err := <-finished
23         s.assertNil(err)
24 }
25
26 func (s *VethsSuite) TestHttpCli() {
27         serverContainer := s.getContainerByName("server-vpp")
28         clientContainer := s.getContainerByName("client-vpp")
29
30         serverVeth := s.netInterfaces[serverInterfaceName]
31
32         serverContainer.vppInstance.vppctl("http cli server")
33
34         uri := "http://" + serverVeth.ip4AddressString() + "/80"
35
36         o := clientContainer.vppInstance.vppctl("http cli client" +
37                 " uri " + uri + " query /show/version")
38
39         s.log(o)
40         s.assertContains(o, "<html>", "<html> not found in the result!")
41 }
42
43 func (s *NoTopoSuite) TestNginxHttp3() {
44         s.SkipUnlessExtendedTestsBuilt()
45
46         query := "index.html"
47         nginxCont := s.getContainerByName("nginx-http3")
48         s.assertNil(nginxCont.run())
49
50         vpp := s.getContainerByName("vpp").vppInstance
51         vpp.waitForApp("nginx-", 5)
52         serverAddress := s.netInterfaces[tapInterfaceName].peer.ip4AddressString()
53
54         defer func() { os.Remove(query) }()
55         curlCont := s.getContainerByName("curl")
56         args := fmt.Sprintf("curl --noproxy '*' --http3-only -k https://%s:8443/%s", serverAddress, query)
57         curlCont.extraRunningArgs = args
58         o, err := curlCont.combinedOutput()
59         s.assertNil(err)
60         s.assertContains(o, "<http>", "<http> not found in the result!")
61 }
62
63 func (s *NoTopoSuite) TestHttpStaticProm() {
64         finished := make(chan error, 1)
65         query := "stats.prom"
66         vpp := s.getContainerByName("vpp").vppInstance
67         serverAddress := s.netInterfaces[tapInterfaceName].peer.ip4AddressString()
68         s.log(vpp.vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
69         s.log(vpp.vppctl("prom enable"))
70         go s.startWget(finished, serverAddress, "80", query, "")
71         err := <-finished
72         s.assertNil(err)
73 }
74
75 func (s *NoTopoSuite) TestNginxAsServer() {
76         query := "return_ok"
77         finished := make(chan error, 1)
78
79         nginxCont := s.getContainerByName("nginx")
80         s.assertNil(nginxCont.run())
81
82         vpp := s.getContainerByName("vpp").vppInstance
83         vpp.waitForApp("nginx-", 5)
84
85         serverAddress := s.netInterfaces[tapInterfaceName].peer.ip4AddressString()
86
87         defer func() { os.Remove(query) }()
88         go s.startWget(finished, serverAddress, "80", query, "")
89         s.assertNil(<-finished)
90 }
91
92 func parseString(s, pattern string) string {
93         temp := strings.Split(s, "\n")
94         for _, item := range temp {
95                 if strings.Contains(item, pattern) {
96                         return item
97                 }
98         }
99         return ""
100 }
101
102 func runNginxPerf(s *NoTopoSuite, mode, ab_or_wrk string) error {
103         nRequests := 1000000
104         nClients := 1000
105
106         serverAddress := s.netInterfaces[tapInterfaceName].peer.ip4AddressString()
107
108         vpp := s.getContainerByName("vpp").vppInstance
109
110         nginxCont := s.getContainerByName("nginx")
111         s.assertNil(nginxCont.run())
112         vpp.waitForApp("nginx-", 5)
113
114         if ab_or_wrk == "ab" {
115                 abCont := s.getContainerByName("ab")
116                 args := fmt.Sprintf("-n %d -c %d", nRequests, nClients)
117                 if mode == "rps" {
118                         args += " -k"
119                 } else if mode != "cps" {
120                         return fmt.Errorf("invalid mode %s; expected cps/rps", mode)
121                 }
122                 // don't exit on socket receive errors
123                 args += " -r"
124                 args += " http://" + serverAddress + ":80/64B.json"
125                 abCont.extraRunningArgs = args
126                 o, err := abCont.combinedOutput()
127                 rps := parseString(o, "Requests per second:")
128                 s.log(rps, err)
129                 s.assertNil(err)
130         } else {
131                 wrkCont := s.getContainerByName("wrk")
132                 args := fmt.Sprintf("-c %d -t 2 -d 30 http://%s:80/64B.json", nClients,
133                         serverAddress)
134                 wrkCont.extraRunningArgs = args
135                 o, err := wrkCont.combinedOutput()
136                 rps := parseString(o, "requests")
137                 s.log(rps, err)
138                 s.assertNil(err)
139         }
140         return nil
141 }
142
143 func (s *NoTopoSuite) TestNginxPerfCps() {
144         s.assertNil(runNginxPerf(s, "cps", "ab"))
145 }
146
147 func (s *NoTopoSuite) TestNginxPerfRps() {
148         s.assertNil(runNginxPerf(s, "rps", "ab"))
149 }
150
151 func (s *NoTopoSuite) TestNginxPerfWrk() {
152         s.assertNil(runNginxPerf(s, "", "wrk"))
153 }