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