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