blob: 7b7321eba6d05fbf6442e4216bcd6ed737dcc979 [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
Filip Tehlar229f5fc2022-08-09 14:44:47 +00004 "fmt"
5 "os"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00006
7 "github.com/edwarnicke/exechelper"
8)
9
Filip Tehlar3336eef2023-11-29 07:40:18 +010010func testProxyHttpTcp(s *NsSuite, proto string) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000011 const outputFile = "test.data"
12 const srcFile = "10M"
13 stopServer := make(chan struct{}, 1)
14 serverRunning := make(chan struct{}, 1)
15
Filip Tehlar229f5fc2022-08-09 14:44:47 +000016 // create test file
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010017 err := exechelper.Run(fmt.Sprintf("ip netns exec server truncate -s %s %s", srcFile, srcFile))
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010018 s.assertNil(err, "failed to run truncate command")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000019 defer func() { os.Remove(srcFile) }()
20
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010021 s.log("test file created...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000022
Filip Tehlar4b3598e2023-09-02 08:54:21 +020023 go s.startHttpServer(serverRunning, stopServer, ":666", "server")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000024 // TODO better error handling and recovery
25 <-serverRunning
26
27 defer func(chan struct{}) {
28 stopServer <- struct{}{}
29 }(stopServer)
30
Maros Ondrejicka87531802022-12-19 20:35:27 +010031 s.log("http server started...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000032
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010033 clientVeth := s.netInterfaces[clientInterface]
34 c := fmt.Sprintf("ip netns exec client wget --no-proxy --retry-connrefused"+
Filip Tehlar3336eef2023-11-29 07:40:18 +010035 " --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)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010040 s.log(c)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000041 _, err = exechelper.CombinedOutput(c)
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010042 s.assertNil(err, "failed to run wget")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000043 stopServer <- struct{}{}
44
45 defer func() { os.Remove(outputFile) }()
46
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010047 s.assertNil(assertFileSize(outputFile, srcFile))
48 return nil
49}
50
Filip Tehlar3336eef2023-11-29 07:40:18 +010051func configureVppProxy(s *NsSuite, proto string) {
Maros Ondrejicka40cba402023-02-23 13:19:15 +010052 serverVeth := s.netInterfaces[serverInterface]
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010053 clientVeth := s.netInterfaces[clientInterface]
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010054
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010055 testVppProxy := s.getContainerByName("vpp").vppInstance
56 output := testVppProxy.vppctl(
Filip Tehlar3336eef2023-11-29 07:40:18 +010057 "test proxy server server-uri %s://%s/555 client-uri tcp://%s/666",
58 proto,
Maros Ondrejickae7625d02023-02-28 16:55:01 +010059 clientVeth.ip4AddressString(),
60 serverVeth.peer.ip4AddressString(),
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010061 )
62 s.log("proxy configured...", output)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000063}
64
65func (s *NsSuite) TestVppProxyHttpTcp() {
Filip Tehlar3336eef2023-11-29 07:40:18 +010066 proto := "tcp"
67 configureVppProxy(s, proto)
68 err := testProxyHttpTcp(s, proto)
69 s.assertNil(err)
70}
71
72func (s *NsSuite) TestVppProxyHttpTls() {
73 proto := "tls"
74 configureVppProxy(s, proto)
75 err := testProxyHttpTcp(s, proto)
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010076 s.assertNil(err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000077}
78
Maros Ondrejicka85396a52023-02-28 12:49:43 +010079func configureEnvoyProxy(s *NsSuite) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010080 envoyContainer := s.getContainerByName("envoy")
Maros Ondrejicka85396a52023-02-28 12:49:43 +010081 envoyContainer.create()
82
83 serverVeth := s.netInterfaces[serverInterface]
84 address := struct {
85 Server string
86 }{
Maros Ondrejickae7625d02023-02-28 16:55:01 +010087 Server: serverVeth.peer.ip4AddressString(),
Maros Ondrejicka85396a52023-02-28 12:49:43 +010088 }
89 envoyContainer.createConfig(
90 "/etc/envoy/envoy.yaml",
91 "resources/envoy/proxy.yaml",
92 address,
93 )
Filip Tehlar9abba112023-03-07 10:13:19 +010094 s.assertNil(envoyContainer.start())
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010095}
96
Filip Tehlar229f5fc2022-08-09 14:44:47 +000097func (s *NsSuite) TestEnvoyProxyHttpTcp() {
Maros Ondrejicka85396a52023-02-28 12:49:43 +010098 configureEnvoyProxy(s)
Filip Tehlar3336eef2023-11-29 07:40:18 +010099 err := testProxyHttpTcp(s, "tcp")
Maros Ondrejicka98a91e82022-12-06 15:38:05 +0100100 s.assertNil(err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000101}