hs-test: shortened interface names to avoid character limit
[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 )
9
10 func testProxyHttpTcp(s *NsSuite, proto string) error {
11         const outputFile = "test.data"
12         const srcFile = "httpTestFile"
13         const fileSize = "10M"
14         stopServer := make(chan struct{}, 1)
15         serverRunning := make(chan struct{}, 1)
16         serverNetns := "srv"
17         clientNetns := "cln"
18
19         // create test file
20         err := exechelper.Run(fmt.Sprintf("ip netns exec %s truncate -s %s %s", serverNetns, fileSize, srcFile))
21         s.assertNil(err, "failed to run truncate command: " + fmt.Sprint(err))
22         defer func() { os.Remove(srcFile) }()
23
24         s.log("test file created...")
25
26         go s.startHttpServer(serverRunning, stopServer, ":666", serverNetns)
27         // TODO better error handling and recovery
28         <-serverRunning
29
30         defer func(chan struct{}) {
31                 stopServer <- struct{}{}
32         }(stopServer)
33
34         s.log("http server started...")
35
36         clientVeth := s.netInterfaces[clientInterface]
37         c := fmt.Sprintf("ip netns exec %s wget --no-proxy --retry-connrefused"+
38                 " --retry-on-http-error=503 --tries=10 -O %s ", clientNetns, outputFile)
39         if proto == "tls" {
40                 c += " --secure-protocol=TLSv1_3 --no-check-certificate https://"
41         }
42         c += fmt.Sprintf("%s:555/%s", clientVeth.ip4AddressString(), srcFile)
43         s.log(c)
44         _, err = exechelper.CombinedOutput(c)
45
46         defer func() { os.Remove(outputFile) }()
47
48         s.assertNil(err, "failed to run wget: '%s', cmd: %s", err, c)
49         stopServer <- struct{}{}
50
51         s.assertNil(assertFileSize(outputFile, srcFile))
52         return nil
53 }
54
55 func configureVppProxy(s *NsSuite, proto string) {
56         serverVeth := s.netInterfaces[serverInterface]
57         clientVeth := s.netInterfaces[clientInterface]
58
59         testVppProxy := s.getContainerByName("vpp").vppInstance
60         output := testVppProxy.vppctl(
61                 "test proxy server server-uri %s://%s/555 client-uri tcp://%s/666",
62                 proto,
63                 clientVeth.ip4AddressString(),
64                 serverVeth.peer.ip4AddressString(),
65         )
66         s.log("proxy configured...", output)
67 }
68
69 func (s *NsSuite) TestVppProxyHttpTcp() {
70         proto := "tcp"
71         configureVppProxy(s, proto)
72         err := testProxyHttpTcp(s, proto)
73         s.assertNil(err, err)
74 }
75
76 func (s *NsSuite) TestVppProxyHttpTls() {
77         proto := "tls"
78         configureVppProxy(s, proto)
79         err := testProxyHttpTcp(s, proto)
80         s.assertNil(err, err)
81 }
82
83 func configureEnvoyProxy(s *NsSuite) {
84         envoyContainer := s.getContainerByName("envoy")
85         err := envoyContainer.create()
86         s.assertNil(err, "Error creating envoy container: %s", err)
87
88         serverVeth := s.netInterfaces[serverInterface]
89         address := struct {
90                 Server string
91         }{
92                 Server: serverVeth.peer.ip4AddressString(),
93         }
94         envoyContainer.createConfig(
95                 "/etc/envoy/envoy.yaml",
96                 "resources/envoy/proxy.yaml",
97                 address,
98         )
99         s.assertNil(envoyContainer.start())
100 }
101
102 func (s *NsSuite) TestEnvoyProxyHttpTcp() {
103         configureEnvoyProxy(s)
104         err := testProxyHttpTcp(s, "tcp")
105         s.assertNil(err, err)
106 }