http_static: fix reply data leak
[vpp.git] / extras / hs-test / vcl_test.go
1 package main
2
3 import (
4         . "fd.io/hs-test/infra"
5         "fmt"
6         "time"
7 )
8
9 func init() {
10         RegisterVethTests(XEchoVclClientUdpTest, XEchoVclClientTcpTest, XEchoVclServerUdpTest,
11                 XEchoVclServerTcpTest, VclEchoTcpTest, VclEchoUdpTest, VclRetryAttachTest)
12 }
13
14 func getVclConfig(c *Container, ns_id_optional ...string) string {
15         var s Stanza
16         ns_id := "default"
17         if len(ns_id_optional) > 0 {
18                 ns_id = ns_id_optional[0]
19         }
20         s.NewStanza("vcl").
21                 Append(fmt.Sprintf("app-socket-api %[1]s/var/run/app_ns_sockets/%[2]s", c.GetContainerWorkDir(), ns_id)).
22                 Append("app-scope-global").
23                 Append("app-scope-local").
24                 Append("use-mq-eventfd")
25         if len(ns_id_optional) > 0 {
26                 s.Append(fmt.Sprintf("namespace-id %[1]s", ns_id)).
27                         Append(fmt.Sprintf("namespace-secret %[1]s", ns_id))
28         }
29         return s.Close().ToString()
30 }
31
32 func XEchoVclClientUdpTest(s *VethsSuite) {
33         testXEchoVclClient(s, "udp")
34 }
35
36 func XEchoVclClientTcpTest(s *VethsSuite) {
37         testXEchoVclClient(s, "tcp")
38 }
39
40 func testXEchoVclClient(s *VethsSuite, proto string) {
41         port := "12345"
42         serverVpp := s.GetContainerByName("server-vpp").VppInstance
43
44         serverVeth := s.GetInterfaceByName(ServerInterfaceName)
45         serverVpp.Vppctl("test echo server uri %s://%s/%s fifo-size 64k", proto, serverVeth.Ip4AddressString(), port)
46
47         echoClnContainer := s.GetTransientContainerByName("client-app")
48         echoClnContainer.CreateFile("/vcl.conf", getVclConfig(echoClnContainer))
49
50         testClientCommand := "vcl_test_client -N 100 -p " + proto + " " + serverVeth.Ip4AddressString() + " " + port
51         s.Log(testClientCommand)
52         echoClnContainer.AddEnvVar("VCL_CONFIG", "/vcl.conf")
53         o := echoClnContainer.Exec(testClientCommand)
54         s.Log(o)
55         s.AssertContains(o, "CLIENT RESULTS")
56 }
57
58 func XEchoVclServerUdpTest(s *VethsSuite) {
59         testXEchoVclServer(s, "udp")
60 }
61
62 func XEchoVclServerTcpTest(s *VethsSuite) {
63         testXEchoVclServer(s, "tcp")
64 }
65
66 func testXEchoVclServer(s *VethsSuite, proto string) {
67         port := "12345"
68         srvVppCont := s.GetContainerByName("server-vpp")
69         srvAppCont := s.GetContainerByName("server-app")
70
71         srvAppCont.CreateFile("/vcl.conf", getVclConfig(srvVppCont))
72         srvAppCont.AddEnvVar("VCL_CONFIG", "/vcl.conf")
73         vclSrvCmd := fmt.Sprintf("vcl_test_server -p %s %s", proto, port)
74         srvAppCont.ExecServer(vclSrvCmd)
75
76         serverVeth := s.GetInterfaceByName(ServerInterfaceName)
77         serverVethAddress := serverVeth.Ip4AddressString()
78
79         clientVpp := s.GetContainerByName("client-vpp").VppInstance
80         o := clientVpp.Vppctl("test echo client uri %s://%s/%s fifo-size 64k verbose mbytes 2", proto, serverVethAddress, port)
81         s.Log(o)
82         s.AssertContains(o, "Test finished at")
83 }
84
85 func testVclEcho(s *VethsSuite, proto string) {
86         port := "12345"
87         srvVppCont := s.GetContainerByName("server-vpp")
88         srvAppCont := s.GetContainerByName("server-app")
89
90         srvAppCont.CreateFile("/vcl.conf", getVclConfig(srvVppCont))
91         srvAppCont.AddEnvVar("VCL_CONFIG", "/vcl.conf")
92         srvAppCont.ExecServer("vcl_test_server " + port)
93
94         serverVeth := s.GetInterfaceByName(ServerInterfaceName)
95         serverVethAddress := serverVeth.Ip4AddressString()
96
97         echoClnContainer := s.GetTransientContainerByName("client-app")
98         echoClnContainer.CreateFile("/vcl.conf", getVclConfig(echoClnContainer))
99
100         testClientCommand := "vcl_test_client -p " + proto + " " + serverVethAddress + " " + port
101         echoClnContainer.AddEnvVar("VCL_CONFIG", "/vcl.conf")
102         o := echoClnContainer.Exec(testClientCommand)
103         s.Log(o)
104 }
105
106 func VclEchoTcpTest(s *VethsSuite) {
107         testVclEcho(s, "tcp")
108 }
109
110 func VclEchoUdpTest(s *VethsSuite) {
111         testVclEcho(s, "udp")
112 }
113
114 func VclRetryAttachTest(s *VethsSuite) {
115         testRetryAttach(s, "tcp")
116 }
117
118 func testRetryAttach(s *VethsSuite, proto string) {
119         srvVppContainer := s.GetTransientContainerByName("server-vpp")
120
121         echoSrvContainer := s.GetContainerByName("server-app")
122
123         echoSrvContainer.CreateFile("/vcl.conf", getVclConfig(echoSrvContainer))
124
125         echoSrvContainer.AddEnvVar("VCL_CONFIG", "/vcl.conf")
126         echoSrvContainer.ExecServer("vcl_test_server -p " + proto + " 12346")
127
128         s.Log("This whole test case can take around 3 minutes to run. Please be patient.")
129         s.Log("... Running first echo client test, before disconnect.")
130
131         serverVeth := s.GetInterfaceByName(ServerInterfaceName)
132         serverVethAddress := serverVeth.Ip4AddressString()
133
134         echoClnContainer := s.GetTransientContainerByName("client-app")
135         echoClnContainer.CreateFile("/vcl.conf", getVclConfig(echoClnContainer))
136
137         testClientCommand := "vcl_test_client -U -p " + proto + " " + serverVethAddress + " 12346"
138         echoClnContainer.AddEnvVar("VCL_CONFIG", "/vcl.conf")
139         o := echoClnContainer.Exec(testClientCommand)
140         s.Log(o)
141         s.Log("... First test ended. Stopping VPP server now.")
142
143         // Stop server-vpp-instance, start it again and then run vcl-test-client once more
144         srvVppContainer.VppInstance.Disconnect()
145         stopVppCommand := "/bin/bash -c 'ps -C vpp_main -o pid= | xargs kill -9'"
146         srvVppContainer.Exec(stopVppCommand)
147
148         s.SetupServerVpp()
149
150         s.Log("... VPP server is starting again, so waiting for a bit.")
151         time.Sleep(30 * time.Second) // Wait a moment for the re-attachment to happen
152
153         s.Log("... Running second echo client test, after disconnect and re-attachment.")
154         o = echoClnContainer.Exec(testClientCommand)
155         s.Log(o)
156         s.Log("Done.")
157 }