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 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame^] | 10 | func testProxyHttpTcp(s *NsSuite, proxySetup func() error) 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 | |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 16 | s.assertNil(proxySetup(), "failed to setup proxy") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 17 | |
| 18 | // create test file |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame^] | 19 | 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] | 20 | s.assertNil(err, "failed to run truncate command") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 21 | defer func() { os.Remove(srcFile) }() |
| 22 | |
| 23 | fmt.Println("Test file created...") |
| 24 | |
| 25 | go startHttpServer(serverRunning, stopServer, ":666", "server") |
| 26 | // TODO better error handling and recovery |
| 27 | <-serverRunning |
| 28 | |
| 29 | defer func(chan struct{}) { |
| 30 | stopServer <- struct{}{} |
| 31 | }(stopServer) |
| 32 | |
| 33 | fmt.Println("http server started...") |
| 34 | |
| 35 | c := fmt.Sprintf("ip netns exec client wget --retry-connrefused --retry-on-http-error=503 --tries=10 -O %s 10.0.0.2:555/%s", outputFile, srcFile) |
| 36 | _, err = exechelper.CombinedOutput(c) |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 37 | s.assertNil(err, "failed to run wget") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 38 | stopServer <- struct{}{} |
| 39 | |
| 40 | defer func() { os.Remove(outputFile) }() |
| 41 | |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 42 | s.assertNil(assertFileSize(outputFile, srcFile)) |
| 43 | return nil |
| 44 | } |
| 45 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame^] | 46 | func configureVppProxy(s *NsSuite) error { |
| 47 | container := s.getContainerByName("vpp") |
| 48 | testVppProxy := NewVppInstance(container) |
| 49 | testVppProxy.setVppProxy() |
| 50 | err := testVppProxy.start() |
| 51 | s.assertNil(err, "failed to start and configure VPP") |
| 52 | fmt.Println("VPP running and configured...") |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 53 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame^] | 54 | output, err := testVppProxy.vppctl("test proxy server server-uri tcp://10.0.0.2/555 client-uri tcp://10.0.1.1/666") |
| 55 | fmt.Println("Proxy configured...", string(output)) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 56 | return nil |
| 57 | } |
| 58 | |
| 59 | func (s *NsSuite) TestVppProxyHttpTcp() { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame^] | 60 | err := testProxyHttpTcp(s, func() error { |
| 61 | return configureVppProxy(s) |
| 62 | }) |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 63 | s.assertNil(err) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame^] | 66 | func configureEnvoyProxy(s *NsSuite) error { |
| 67 | vppContainer := s.getContainerByName("vpp") |
| 68 | testVppForEnvoyProxy := NewVppInstance(vppContainer) |
| 69 | testVppForEnvoyProxy.setEnvoyProxy() |
| 70 | err := testVppForEnvoyProxy.start() |
| 71 | s.assertNil(err, "failed to start and configure VPP") |
| 72 | |
| 73 | envoyContainer := s.getContainerByName("envoy") |
| 74 | envoyContainer.run() |
| 75 | |
| 76 | fmt.Println("VPP running and configured...") |
| 77 | return nil |
| 78 | } |
| 79 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 80 | func (s *NsSuite) TestEnvoyProxyHttpTcp() { |
Maros Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame^] | 81 | err := testProxyHttpTcp(s, func() error { |
| 82 | return configureEnvoyProxy(s) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 83 | }) |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 84 | s.assertNil(err) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 85 | } |