blob: c57b927846e2864a1fae7030f6e7a7499c59e2db [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"
Adrian Villincee15aa2024-03-14 11:42:55 -04008 . "github.com/onsi/ginkgo/v2"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00009)
10
Adrian Villincee15aa2024-03-14 11:42:55 -040011func init() {
12 registerNsTests(VppProxyHttpTcpTest, VppProxyHttpTlsTest, EnvoyProxyHttpTcpTest)
13}
14
Filip Tehlar3336eef2023-11-29 07:40:18 +010015func testProxyHttpTcp(s *NsSuite, proto string) error {
Adrian Villineaa7d912024-06-06 04:26:30 -040016 var outputFile string = s.processIndex + "test" + s.ppid + ".data"
17 var srcFilePpid string = s.processIndex + "httpTestFile" + s.ppid
18 const srcFileNoPpid = "httpTestFile"
adrianvillin28bd8f02024-02-13 06:00:02 -050019 const fileSize string = "10M"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000020 stopServer := make(chan struct{}, 1)
21 serverRunning := make(chan struct{}, 1)
adrianvillin28bd8f02024-02-13 06:00:02 -050022 serverNetns := s.getNetNamespaceByName("srv")
23 clientNetns := s.getNetNamespaceByName("cln")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000024
Filip Tehlar229f5fc2022-08-09 14:44:47 +000025 // create test file
Adrian Villineaa7d912024-06-06 04:26:30 -040026 err := exechelper.Run(fmt.Sprintf("ip netns exec %s truncate -s %s %s", serverNetns, fileSize, srcFilePpid))
Adrian Villincee15aa2024-03-14 11:42:55 -040027 s.assertNil(err, "failed to run truncate command: "+fmt.Sprint(err))
Adrian Villineaa7d912024-06-06 04:26:30 -040028 defer func() { os.Remove(srcFilePpid) }()
Filip Tehlar229f5fc2022-08-09 14:44:47 +000029
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010030 s.log("test file created...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000031
Adrian Villincee15aa2024-03-14 11:42:55 -040032 go func() {
33 defer GinkgoRecover()
34 s.startHttpServer(serverRunning, stopServer, ":666", serverNetns)
35 }()
Filip Tehlar229f5fc2022-08-09 14:44:47 +000036 // TODO better error handling and recovery
37 <-serverRunning
38
39 defer func(chan struct{}) {
40 stopServer <- struct{}{}
41 }(stopServer)
42
Maros Ondrejicka87531802022-12-19 20:35:27 +010043 s.log("http server started...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000044
adrianvillin28bd8f02024-02-13 06:00:02 -050045 clientVeth := s.getInterfaceByName(clientInterface)
adrianvillinfbf5f2b2024-02-13 03:26:25 -050046 c := fmt.Sprintf("ip netns exec %s wget --no-proxy --retry-connrefused"+
47 " --retry-on-http-error=503 --tries=10 -O %s ", clientNetns, outputFile)
Filip Tehlar3336eef2023-11-29 07:40:18 +010048 if proto == "tls" {
49 c += " --secure-protocol=TLSv1_3 --no-check-certificate https://"
50 }
Adrian Villineaa7d912024-06-06 04:26:30 -040051 c += fmt.Sprintf("%s:555/%s", clientVeth.ip4AddressString(), srcFileNoPpid)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010052 s.log(c)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000053 _, err = exechelper.CombinedOutput(c)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000054
55 defer func() { os.Remove(outputFile) }()
56
adrianvillinfbf5f2b2024-02-13 03:26:25 -050057 s.assertNil(err, "failed to run wget: '%s', cmd: %s", err, c)
58 stopServer <- struct{}{}
59
Adrian Villineaa7d912024-06-06 04:26:30 -040060 s.assertNil(assertFileSize(outputFile, srcFilePpid))
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010061 return nil
62}
63
Filip Tehlar3336eef2023-11-29 07:40:18 +010064func configureVppProxy(s *NsSuite, proto string) {
adrianvillin28bd8f02024-02-13 06:00:02 -050065 serverVeth := s.getInterfaceByName(serverInterface)
66 clientVeth := s.getInterfaceByName(clientInterface)
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010067
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010068 testVppProxy := s.getContainerByName("vpp").vppInstance
69 output := testVppProxy.vppctl(
Filip Tehlar3336eef2023-11-29 07:40:18 +010070 "test proxy server server-uri %s://%s/555 client-uri tcp://%s/666",
71 proto,
Maros Ondrejickae7625d02023-02-28 16:55:01 +010072 clientVeth.ip4AddressString(),
73 serverVeth.peer.ip4AddressString(),
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010074 )
Adrian Villincee15aa2024-03-14 11:42:55 -040075 s.log("proxy configured: " + output)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000076}
77
Adrian Villincee15aa2024-03-14 11:42:55 -040078func VppProxyHttpTcpTest(s *NsSuite) {
Filip Tehlar3336eef2023-11-29 07:40:18 +010079 proto := "tcp"
80 configureVppProxy(s, proto)
81 err := testProxyHttpTcp(s, proto)
Adrian Villincee15aa2024-03-14 11:42:55 -040082 s.assertNil(err, fmt.Sprint(err))
Filip Tehlar3336eef2023-11-29 07:40:18 +010083}
84
Adrian Villincee15aa2024-03-14 11:42:55 -040085func VppProxyHttpTlsTest(s *NsSuite) {
Filip Tehlar3336eef2023-11-29 07:40:18 +010086 proto := "tls"
87 configureVppProxy(s, proto)
88 err := testProxyHttpTcp(s, proto)
Adrian Villincee15aa2024-03-14 11:42:55 -040089 s.assertNil(err, fmt.Sprint(err))
Filip Tehlar229f5fc2022-08-09 14:44:47 +000090}
91
Maros Ondrejicka85396a52023-02-28 12:49:43 +010092func configureEnvoyProxy(s *NsSuite) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010093 envoyContainer := s.getContainerByName("envoy")
adrianvillin7c675472024-02-12 02:44:53 -050094 err := envoyContainer.create()
95 s.assertNil(err, "Error creating envoy container: %s", err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +010096
adrianvillin28bd8f02024-02-13 06:00:02 -050097 serverVeth := s.getInterfaceByName(serverInterface)
Maros Ondrejicka85396a52023-02-28 12:49:43 +010098 address := struct {
99 Server string
100 }{
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100101 Server: serverVeth.peer.ip4AddressString(),
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100102 }
103 envoyContainer.createConfig(
104 "/etc/envoy/envoy.yaml",
105 "resources/envoy/proxy.yaml",
106 address,
107 )
Filip Tehlar9abba112023-03-07 10:13:19 +0100108 s.assertNil(envoyContainer.start())
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100109}
110
Adrian Villincee15aa2024-03-14 11:42:55 -0400111func EnvoyProxyHttpTcpTest(s *NsSuite) {
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100112 configureEnvoyProxy(s)
Filip Tehlar3336eef2023-11-29 07:40:18 +0100113 err := testProxyHttpTcp(s, "tcp")
Adrian Villincee15aa2024-03-14 11:42:55 -0400114 s.assertNil(err, fmt.Sprint(err))
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000115}