blob: 748e48b0344a1a073ef25d37f52f293bbd7e4694 [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"
adrianvillinfbf5f2b2024-02-13 03:26:25 -050012 const srcFile = "httpTestFile"
13 const fileSize = "10M"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000014 stopServer := make(chan struct{}, 1)
15 serverRunning := make(chan struct{}, 1)
adrianvillinfbf5f2b2024-02-13 03:26:25 -050016 serverNetns := "srv"
17 clientNetns := "cln"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000018
Filip Tehlar229f5fc2022-08-09 14:44:47 +000019 // create test file
adrianvillinfbf5f2b2024-02-13 03:26:25 -050020 err := exechelper.Run(fmt.Sprintf("ip netns exec %s truncate -s %s %s", serverNetns, fileSize, srcFile))
adrianvillin7c675472024-02-12 02:44:53 -050021 s.assertNil(err, "failed to run truncate command: " + fmt.Sprint(err))
Filip Tehlar229f5fc2022-08-09 14:44:47 +000022 defer func() { os.Remove(srcFile) }()
23
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010024 s.log("test file created...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000025
adrianvillinfbf5f2b2024-02-13 03:26:25 -050026 go s.startHttpServer(serverRunning, stopServer, ":666", serverNetns)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000027 // TODO better error handling and recovery
28 <-serverRunning
29
30 defer func(chan struct{}) {
31 stopServer <- struct{}{}
32 }(stopServer)
33
Maros Ondrejicka87531802022-12-19 20:35:27 +010034 s.log("http server started...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000035
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010036 clientVeth := s.netInterfaces[clientInterface]
adrianvillinfbf5f2b2024-02-13 03:26:25 -050037 c := fmt.Sprintf("ip netns exec %s wget --no-proxy --retry-connrefused"+
38 " --retry-on-http-error=503 --tries=10 -O %s ", clientNetns, outputFile)
Filip Tehlar3336eef2023-11-29 07:40:18 +010039 if proto == "tls" {
40 c += " --secure-protocol=TLSv1_3 --no-check-certificate https://"
41 }
42 c += fmt.Sprintf("%s:555/%s", clientVeth.ip4AddressString(), srcFile)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010043 s.log(c)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000044 _, err = exechelper.CombinedOutput(c)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000045
46 defer func() { os.Remove(outputFile) }()
47
adrianvillinfbf5f2b2024-02-13 03:26:25 -050048 s.assertNil(err, "failed to run wget: '%s', cmd: %s", err, c)
49 stopServer <- struct{}{}
50
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010051 s.assertNil(assertFileSize(outputFile, srcFile))
52 return nil
53}
54
Filip Tehlar3336eef2023-11-29 07:40:18 +010055func configureVppProxy(s *NsSuite, proto string) {
Maros Ondrejicka40cba402023-02-23 13:19:15 +010056 serverVeth := s.netInterfaces[serverInterface]
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010057 clientVeth := s.netInterfaces[clientInterface]
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010058
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010059 testVppProxy := s.getContainerByName("vpp").vppInstance
60 output := testVppProxy.vppctl(
Filip Tehlar3336eef2023-11-29 07:40:18 +010061 "test proxy server server-uri %s://%s/555 client-uri tcp://%s/666",
62 proto,
Maros Ondrejickae7625d02023-02-28 16:55:01 +010063 clientVeth.ip4AddressString(),
64 serverVeth.peer.ip4AddressString(),
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010065 )
66 s.log("proxy configured...", output)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000067}
68
69func (s *NsSuite) TestVppProxyHttpTcp() {
Filip Tehlar3336eef2023-11-29 07:40:18 +010070 proto := "tcp"
71 configureVppProxy(s, proto)
72 err := testProxyHttpTcp(s, proto)
adrianvillin7c675472024-02-12 02:44:53 -050073 s.assertNil(err, err)
Filip Tehlar3336eef2023-11-29 07:40:18 +010074}
75
76func (s *NsSuite) TestVppProxyHttpTls() {
77 proto := "tls"
78 configureVppProxy(s, proto)
79 err := testProxyHttpTcp(s, proto)
adrianvillin7c675472024-02-12 02:44:53 -050080 s.assertNil(err, err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000081}
82
Maros Ondrejicka85396a52023-02-28 12:49:43 +010083func configureEnvoyProxy(s *NsSuite) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010084 envoyContainer := s.getContainerByName("envoy")
adrianvillin7c675472024-02-12 02:44:53 -050085 err := envoyContainer.create()
86 s.assertNil(err, "Error creating envoy container: %s", err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +010087
88 serverVeth := s.netInterfaces[serverInterface]
89 address := struct {
90 Server string
91 }{
Maros Ondrejickae7625d02023-02-28 16:55:01 +010092 Server: serverVeth.peer.ip4AddressString(),
Maros Ondrejicka85396a52023-02-28 12:49:43 +010093 }
94 envoyContainer.createConfig(
95 "/etc/envoy/envoy.yaml",
96 "resources/envoy/proxy.yaml",
97 address,
98 )
Filip Tehlar9abba112023-03-07 10:13:19 +010099 s.assertNil(envoyContainer.start())
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100100}
101
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000102func (s *NsSuite) TestEnvoyProxyHttpTcp() {
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100103 configureEnvoyProxy(s)
Filip Tehlar3336eef2023-11-29 07:40:18 +0100104 err := testProxyHttpTcp(s, "tcp")
adrianvillin7c675472024-02-12 02:44:53 -0500105 s.assertNil(err, err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000106}