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