Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 4 | . "fd.io/hs-test/infra" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 5 | "fmt" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 6 | "github.com/edwarnicke/exechelper" |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 7 | . "github.com/onsi/ginkgo/v2" |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 8 | "os" |
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() { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 12 | RegisterNsTests(VppProxyHttpTcpTest, VppProxyHttpTlsTest, EnvoyProxyHttpTcpTest) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 13 | } |
| 14 | |
Filip Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 15 | func testProxyHttpTcp(s *NsSuite, proto string) error { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 16 | var outputFile string = s.ProcessIndex + "test" + s.Ppid + ".data" |
| 17 | var srcFilePpid string = s.ProcessIndex + "httpTestFile" + s.Ppid |
Adrian Villin | eaa7d91 | 2024-06-06 04:26:30 -0400 | [diff] [blame] | 18 | const srcFileNoPpid = "httpTestFile" |
adrianvillin | 28bd8f0 | 2024-02-13 06:00:02 -0500 | [diff] [blame] | 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) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [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 |
Adrian Villin | eaa7d91 | 2024-06-06 04:26:30 -0400 | [diff] [blame] | 26 | err := exechelper.Run(fmt.Sprintf("ip netns exec %s truncate -s %s %s", serverNetns, fileSize, srcFilePpid)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 27 | s.AssertNil(err, "failed to run truncate command: "+fmt.Sprint(err)) |
Adrian Villin | eaa7d91 | 2024-06-06 04:26:30 -0400 | [diff] [blame] | 28 | defer func() { os.Remove(srcFilePpid) }() |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 29 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [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() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 34 | s.StartHttpServer(serverRunning, stopServer, ":666", serverNetns) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 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 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 43 | s.Log("http server started...") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 44 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [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 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 51 | c += fmt.Sprintf("%s:555/%s", clientVeth.Ip4AddressString(), srcFileNoPpid) |
| 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 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 57 | s.AssertNil(err, "failed to run wget: '%s', cmd: %s", err, c) |
adrianvillin | fbf5f2b | 2024-02-13 03:26:25 -0500 | [diff] [blame] | 58 | stopServer <- struct{}{} |
| 59 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 60 | s.AssertNil(AssertFileSize(outputFile, srcFilePpid)) |
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) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [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 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [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, |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 72 | clientVeth.Ip4AddressString(), |
| 73 | serverVeth.Peer.Ip4AddressString(), |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 74 | ) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [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 | 4677d92 | 2024-06-14 09:32:39 +0200 | [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 | 4677d92 | 2024-06-14 09:32:39 +0200 | [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) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 93 | envoyContainer := s.GetContainerByName("envoy") |
Adrian Villin | 2514001 | 2024-07-09 15:31:36 +0200 | [diff] [blame] | 94 | s.AssertNil(envoyContainer.Create()) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 95 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 96 | serverVeth := s.GetInterfaceByName(ServerInterface) |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 97 | address := struct { |
| 98 | Server string |
| 99 | }{ |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 100 | Server: serverVeth.Peer.Ip4AddressString(), |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 101 | } |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 102 | envoyContainer.CreateConfig( |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 103 | "/etc/envoy/envoy.yaml", |
| 104 | "resources/envoy/proxy.yaml", |
| 105 | address, |
| 106 | ) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 107 | s.AssertNil(envoyContainer.Start()) |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 110 | func EnvoyProxyHttpTcpTest(s *NsSuite) { |
Maros Ondrejicka | 85396a5 | 2023-02-28 12:49:43 +0100 | [diff] [blame] | 111 | configureEnvoyProxy(s) |
Filip Tehlar | 3336eef | 2023-11-29 07:40:18 +0100 | [diff] [blame] | 112 | err := testProxyHttpTcp(s, "tcp") |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 113 | s.AssertNil(err, fmt.Sprint(err)) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 114 | } |