Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 4 | "fmt" |
| 5 | "os" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 6 | |
| 7 | "github.com/edwarnicke/exechelper" |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 8 | . "github.com/onsi/ginkgo/v2" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 9 | ) |
| 10 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 11 | func init() { |
| 12 | registerNsTests(VppProxyHttpTcpTest, VppProxyHttpTlsTest, EnvoyProxyHttpTcpTest) |
| 13 | } |
| 14 | |
Filip Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 15 | func testProxyHttpTcp(s *NsSuite, proto string) error { |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 16 | var outputFile string = "test" + s.pid + ".data" |
| 17 | var srcFilePid string = "httpTestFile" + s.pid |
| 18 | const srcFileNoPid = "httpTestFile" |
| 19 | const fileSize string = "10M" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 20 | stopServer := make(chan struct{}, 1) |
| 21 | serverRunning := make(chan struct{}, 1) |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 22 | serverNetns := s.getNetNamespaceByName("srv") |
| 23 | clientNetns := s.getNetNamespaceByName("cln") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 24 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 25 | // create test file |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 26 | err := exechelper.Run(fmt.Sprintf("ip netns exec %s truncate -s %s %s", serverNetns, fileSize, srcFilePid)) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 27 | s.assertNil(err, "failed to run truncate command: "+fmt.Sprint(err)) |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 28 | defer func() { os.Remove(srcFilePid) }() |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 29 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 30 | s.log("test file created...") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 31 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 32 | go func() { |
| 33 | defer GinkgoRecover() |
| 34 | s.startHttpServer(serverRunning, stopServer, ":666", serverNetns) |
| 35 | }() |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 36 | // TODO better error handling and recovery |
| 37 | <-serverRunning |
| 38 | |
| 39 | defer func(chan struct{}) { |
| 40 | stopServer <- struct{}{} |
| 41 | }(stopServer) |
| 42 | |
Maros Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 43 | s.log("http server started...") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 44 | |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 45 | clientVeth := s.getInterfaceByName(clientInterface) |
adrianvillin | fbf5f2b | 2024-02-13 03:26:25 -0500 | [diff] [blame] | 46 | 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 Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 48 | if proto == "tls" { |
| 49 | c += " --secure-protocol=TLSv1_3 --no-check-certificate https://" |
| 50 | } |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 51 | c += fmt.Sprintf("%s:555/%s", clientVeth.ip4AddressString(), srcFileNoPid) |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 52 | s.log(c) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 53 | _, err = exechelper.CombinedOutput(c) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 54 | |
| 55 | defer func() { os.Remove(outputFile) }() |
| 56 | |
adrianvillin | fbf5f2b | 2024-02-13 03:26:25 -0500 | [diff] [blame] | 57 | s.assertNil(err, "failed to run wget: '%s', cmd: %s", err, c) |
| 58 | stopServer <- struct{}{} |
| 59 | |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 60 | s.assertNil(assertFileSize(outputFile, srcFilePid)) |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 61 | return nil |
| 62 | } |
| 63 | |
Filip Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 64 | func configureVppProxy(s *NsSuite, proto string) { |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 65 | serverVeth := s.getInterfaceByName(serverInterface) |
| 66 | clientVeth := s.getInterfaceByName(clientInterface) |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 67 | |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 68 | testVppProxy := s.getContainerByName("vpp").vppInstance |
| 69 | output := testVppProxy.vppctl( |
Filip Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 70 | "test proxy server server-uri %s://%s/555 client-uri tcp://%s/666", |
| 71 | proto, |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 72 | clientVeth.ip4AddressString(), |
| 73 | serverVeth.peer.ip4AddressString(), |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 74 | ) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 75 | s.log("proxy configured: " + output) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 78 | func VppProxyHttpTcpTest(s *NsSuite) { |
Filip Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 79 | proto := "tcp" |
| 80 | configureVppProxy(s, proto) |
| 81 | err := testProxyHttpTcp(s, proto) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 82 | s.assertNil(err, fmt.Sprint(err)) |
Filip Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 85 | func VppProxyHttpTlsTest(s *NsSuite) { |
Filip Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 86 | proto := "tls" |
| 87 | configureVppProxy(s, proto) |
| 88 | err := testProxyHttpTcp(s, proto) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 89 | s.assertNil(err, fmt.Sprint(err)) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 92 | func configureEnvoyProxy(s *NsSuite) { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 93 | envoyContainer := s.getContainerByName("envoy") |
adrianvillin | 7c67547 | 2024-02-12 02:44:53 -0500 | [diff] [blame] | 94 | err := envoyContainer.create() |
| 95 | s.assertNil(err, "Error creating envoy container: %s", err) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 96 | |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 97 | serverVeth := s.getInterfaceByName(serverInterface) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 98 | address := struct { |
| 99 | Server string |
| 100 | }{ |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 101 | Server: serverVeth.peer.ip4AddressString(), |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 102 | } |
| 103 | envoyContainer.createConfig( |
| 104 | "/etc/envoy/envoy.yaml", |
| 105 | "resources/envoy/proxy.yaml", |
| 106 | address, |
| 107 | ) |
Filip Tehlar | 9abba11 | 2023-03-07 10:13:19 +0100 | [diff] [blame] | 108 | s.assertNil(envoyContainer.start()) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 111 | func EnvoyProxyHttpTcpTest(s *NsSuite) { |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 112 | configureEnvoyProxy(s) |
Filip Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 113 | err := testProxyHttpTcp(s, "tcp") |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 114 | s.assertNil(err, fmt.Sprint(err)) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 115 | } |