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