hs-test: add tests repeat option
[vpp.git] / extras / hs-test / proxy_test.go
1 package main
2
3 import (
4         "fmt"
5         "os"
6
7         "github.com/edwarnicke/exechelper"
8         . "github.com/onsi/ginkgo/v2"
9 )
10
11 func init() {
12         registerNsTests(VppProxyHttpTcpTest, VppProxyHttpTlsTest, EnvoyProxyHttpTcpTest)
13 }
14
15 func testProxyHttpTcp(s *NsSuite, proto string) error {
16         var outputFile string = "test" + s.pid + ".data"
17         var srcFilePid string = "httpTestFile" + s.pid
18         const srcFileNoPid = "httpTestFile"
19         const fileSize string = "10M"
20         stopServer := make(chan struct{}, 1)
21         serverRunning := make(chan struct{}, 1)
22         serverNetns := s.getNetNamespaceByName("srv")
23         clientNetns := s.getNetNamespaceByName("cln")
24
25         // create test file
26         err := exechelper.Run(fmt.Sprintf("ip netns exec %s truncate -s %s %s", serverNetns, fileSize, srcFilePid))
27         s.assertNil(err, "failed to run truncate command: "+fmt.Sprint(err))
28         defer func() { os.Remove(srcFilePid) }()
29
30         s.log("test file created...")
31
32         go func() {
33                 defer GinkgoRecover()
34                 s.startHttpServer(serverRunning, stopServer, ":666", serverNetns)
35         }()
36         // TODO better error handling and recovery
37         <-serverRunning
38
39         defer func(chan struct{}) {
40                 stopServer <- struct{}{}
41         }(stopServer)
42
43         s.log("http server started...")
44
45         clientVeth := s.getInterfaceByName(clientInterface)
46         c := fmt.Sprintf("ip netns exec %s wget --no-proxy --retry-connrefused"+
47                 " --retry-on-http-error=503 --tries=10 -O %s ", clientNetns, outputFile)
48         if proto == "tls" {
49                 c += " --secure-protocol=TLSv1_3 --no-check-certificate https://"
50         }
51         c += fmt.Sprintf("%s:555/%s", clientVeth.ip4AddressString(), srcFileNoPid)
52         s.log(c)
53         _, err = exechelper.CombinedOutput(c)
54
55         defer func() { os.Remove(outputFile) }()
56
57         s.assertNil(err, "failed to run wget: '%s', cmd: %s", err, c)
58         stopServer <- struct{}{}
59
60         s.assertNil(assertFileSize(outputFile, srcFilePid))
61         return nil
62 }
63
64 func configureVppProxy(s *NsSuite, proto string) {
65         serverVeth := s.getInterfaceByName(serverInterface)
66         clientVeth := s.getInterfaceByName(clientInterface)
67
68         testVppProxy := s.getContainerByName("vpp").vppInstance
69         output := testVppProxy.vppctl(
70                 "test proxy server server-uri %s://%s/555 client-uri tcp://%s/666",
71                 proto,
72                 clientVeth.ip4AddressString(),
73                 serverVeth.peer.ip4AddressString(),
74         )
75         s.log("proxy configured: " + output)
76 }
77
78 func VppProxyHttpTcpTest(s *NsSuite) {
79         proto := "tcp"
80         configureVppProxy(s, proto)
81         err := testProxyHttpTcp(s, proto)
82         s.assertNil(err, fmt.Sprint(err))
83 }
84
85 func VppProxyHttpTlsTest(s *NsSuite) {
86         proto := "tls"
87         configureVppProxy(s, proto)
88         err := testProxyHttpTcp(s, proto)
89         s.assertNil(err, fmt.Sprint(err))
90 }
91
92 func configureEnvoyProxy(s *NsSuite) {
93         envoyContainer := s.getContainerByName("envoy")
94         err := envoyContainer.create()
95         s.assertNil(err, "Error creating envoy container: %s", err)
96
97         serverVeth := s.getInterfaceByName(serverInterface)
98         address := struct {
99                 Server string
100         }{
101                 Server: serverVeth.peer.ip4AddressString(),
102         }
103         envoyContainer.createConfig(
104                 "/etc/envoy/envoy.yaml",
105                 "resources/envoy/proxy.yaml",
106                 address,
107         )
108         s.assertNil(envoyContainer.start())
109 }
110
111 func EnvoyProxyHttpTcpTest(s *NsSuite) {
112         configureEnvoyProxy(s)
113         err := testProxyHttpTcp(s, "tcp")
114         s.assertNil(err, fmt.Sprint(err))
115 }