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