Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "os" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 7 | |
| 8 | "github.com/edwarnicke/exechelper" |
| 9 | ) |
| 10 | |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 11 | func testProxyHttpTcp(s *NsSuite, dockerInstance, action string, proxySetup func() error) error { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 12 | const outputFile = "test.data" |
| 13 | const srcFile = "10M" |
| 14 | stopServer := make(chan struct{}, 1) |
| 15 | serverRunning := make(chan struct{}, 1) |
| 16 | |
| 17 | volumeArgs := fmt.Sprintf("-v shared-vol:/tmp/%s", dockerInstance) |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 18 | s.assertNil(dockerRun(dockerInstance, volumeArgs), "failed to start container") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 19 | defer func() { exechelper.Run("docker stop " + dockerInstance) }() |
| 20 | |
| 21 | // start & configure vpp in the container |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 22 | _, err := hstExec(action, dockerInstance) |
| 23 | s.assertNil(err) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 24 | |
| 25 | fmt.Println("VPP running and configured...") |
| 26 | |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 27 | s.assertNil(proxySetup(), "failed to setup proxy") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 28 | fmt.Println("Proxy configured...") |
| 29 | |
| 30 | // create test file |
| 31 | 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] | 32 | s.assertNil(err, "failed to run truncate command") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 33 | defer func() { os.Remove(srcFile) }() |
| 34 | |
| 35 | fmt.Println("Test file created...") |
| 36 | |
| 37 | go startHttpServer(serverRunning, stopServer, ":666", "server") |
| 38 | // TODO better error handling and recovery |
| 39 | <-serverRunning |
| 40 | |
| 41 | defer func(chan struct{}) { |
| 42 | stopServer <- struct{}{} |
| 43 | }(stopServer) |
| 44 | |
| 45 | fmt.Println("http server started...") |
| 46 | |
| 47 | 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) |
| 48 | _, err = exechelper.CombinedOutput(c) |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 49 | s.assertNil(err, "failed to run wget") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 50 | stopServer <- struct{}{} |
| 51 | |
| 52 | defer func() { os.Remove(outputFile) }() |
| 53 | |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 54 | s.assertNil(assertFileSize(outputFile, srcFile)) |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | func setupEnvoy(ctx context.Context, dockerInstance string) error { |
| 59 | errCh := startEnvoy(ctx, dockerInstance) |
| 60 | select { |
| 61 | case err := <-errCh: |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 62 | return err |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 63 | default: |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 64 | } |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 65 | |
| 66 | go func(ctx context.Context, errCh <-chan error) { |
| 67 | for { |
| 68 | select { |
| 69 | // handle cancel() call from outside to gracefully stop the routine |
| 70 | case <-ctx.Done(): |
| 71 | return |
| 72 | default: |
| 73 | select { |
| 74 | case err := <-errCh: |
| 75 | fmt.Printf("error while running envoy: %v", err) |
| 76 | default: |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | }(ctx, errCh) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 81 | return nil |
| 82 | } |
| 83 | |
| 84 | func (s *NsSuite) TestVppProxyHttpTcp() { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 85 | dockerInstance := "vpp-proxy" |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 86 | err := testProxyHttpTcp(s, dockerInstance, "ConfigureVppProxy", configureVppProxy) |
| 87 | s.assertNil(err) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | func (s *NsSuite) TestEnvoyProxyHttpTcp() { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 91 | exechelper.Run("docker volume create --name=shared-vol") |
| 92 | defer func() { |
| 93 | exechelper.Run("docker stop envoy") |
| 94 | }() |
| 95 | |
| 96 | ctx, cancel := context.WithCancel(context.Background()) |
| 97 | |
| 98 | dockerInstance := "vpp-envoy" |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 99 | err := testProxyHttpTcp(s, dockerInstance, "ConfigureEnvoyProxy", func() error { |
| 100 | return setupEnvoy(ctx, dockerInstance) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 101 | }) |
Maros Ondrejicka | 98a91e8 | 2022-12-06 15:38:05 +0100 | [diff] [blame] | 102 | s.assertNil(err) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 103 | cancel() |
| 104 | } |