503f42a47c7d26d3f10230483acbbe9929fa3fe1
[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 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) TestNginxAsServer() {
64         query := "return_ok"
65         finished := make(chan error, 1)
66
67         nginxCont := s.getContainerByName("nginx")
68         s.assertNil(nginxCont.run())
69
70         vpp := s.getContainerByName("vpp").vppInstance
71         vpp.waitForApp("nginx-", 5)
72
73         serverAddress := s.netInterfaces[tapInterfaceName].peer.ip4AddressString()
74
75         defer func() { os.Remove(query) }()
76         go startWget(finished, serverAddress, "80", query, "")
77         s.assertNil(<-finished)
78 }
79
80 func parseString(s, pattern string) string {
81         temp := strings.Split(s, "\n")
82         for _, item := range temp {
83                 if strings.Contains(item, pattern) {
84                         return item
85                 }
86         }
87         return ""
88 }
89
90 func runNginxPerf(s *NoTopoSuite, mode, ab_or_wrk string) error {
91         nRequests := 1000000
92         nClients := 1000
93
94         serverAddress := s.netInterfaces[tapInterfaceName].peer.ip4AddressString()
95
96         vpp := s.getContainerByName("vpp").vppInstance
97
98         nginxCont := s.getContainerByName("nginx")
99         s.assertNil(nginxCont.run())
100         vpp.waitForApp("nginx-", 5)
101
102         if ab_or_wrk == "ab" {
103                 abCont := s.getContainerByName("ab")
104                 args := fmt.Sprintf("-n %d -c %d", nRequests, nClients)
105                 if mode == "rps" {
106                         args += " -k"
107                 } else if mode != "cps" {
108                         return fmt.Errorf("invalid mode %s; expected cps/rps", mode)
109                 }
110                 // don't exit on socket receive errors
111                 args += " -r"
112                 args += " http://" + serverAddress + ":80/64B.json"
113                 abCont.extraRunningArgs = args
114                 o, err := abCont.combinedOutput()
115                 rps := parseString(o, "Requests per second:")
116                 s.log(rps, err)
117                 s.assertNil(err)
118         } else {
119                 wrkCont := s.getContainerByName("wrk")
120                 args := fmt.Sprintf("-c %d -t 2 -d 30 http://%s:80/64B.json", nClients,
121                         serverAddress)
122                 wrkCont.extraRunningArgs = args
123                 o, err := wrkCont.combinedOutput()
124                 rps := parseString(o, "requests")
125                 s.log(rps, err)
126                 s.assertNil(err)
127         }
128         return nil
129 }
130
131 func (s *NoTopoSuite) TestNginxPerfCps() {
132         s.assertNil(runNginxPerf(s, "cps", "ab"))
133 }
134
135 func (s *NoTopoSuite) TestNginxPerfRps() {
136         s.assertNil(runNginxPerf(s, "rps", "ab"))
137 }
138
139 func (s *NoTopoSuite) TestNginxPerfWrk() {
140         s.assertNil(runNginxPerf(s, "", "wrk"))
141 }