blob: 0ada6fad0f797b86a1358d7e4e6fa23728eea69b [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
4 "context"
5 "fmt"
6 "os"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00007
8 "github.com/edwarnicke/exechelper"
9)
10
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010011func testProxyHttpTcp(s *NsSuite, dockerInstance, action string, proxySetup func() error) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000012 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 Ondrejicka98a91e82022-12-06 15:38:05 +010018 s.assertNil(dockerRun(dockerInstance, volumeArgs), "failed to start container")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000019 defer func() { exechelper.Run("docker stop " + dockerInstance) }()
20
21 // start & configure vpp in the container
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010022 _, err := hstExec(action, dockerInstance)
23 s.assertNil(err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000024
25 fmt.Println("VPP running and configured...")
26
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010027 s.assertNil(proxySetup(), "failed to setup proxy")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000028 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 Ondrejicka98a91e82022-12-06 15:38:05 +010032 s.assertNil(err, "failed to run truncate command")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000033 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 Ondrejicka98a91e82022-12-06 15:38:05 +010049 s.assertNil(err, "failed to run wget")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000050 stopServer <- struct{}{}
51
52 defer func() { os.Remove(outputFile) }()
53
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010054 s.assertNil(assertFileSize(outputFile, srcFile))
55 return nil
56}
57
58func setupEnvoy(ctx context.Context, dockerInstance string) error {
59 errCh := startEnvoy(ctx, dockerInstance)
60 select {
61 case err := <-errCh:
Filip Tehlar229f5fc2022-08-09 14:44:47 +000062 return err
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010063 default:
Filip Tehlar229f5fc2022-08-09 14:44:47 +000064 }
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010065
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 Tehlar229f5fc2022-08-09 14:44:47 +000081 return nil
82}
83
84func (s *NsSuite) TestVppProxyHttpTcp() {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000085 dockerInstance := "vpp-proxy"
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010086 err := testProxyHttpTcp(s, dockerInstance, "ConfigureVppProxy", configureVppProxy)
87 s.assertNil(err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000088}
89
90func (s *NsSuite) TestEnvoyProxyHttpTcp() {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000091 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 Ondrejicka98a91e82022-12-06 15:38:05 +010099 err := testProxyHttpTcp(s, dockerInstance, "ConfigureEnvoyProxy", func() error {
100 return setupEnvoy(ctx, dockerInstance)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000101 })
Maros Ondrejicka98a91e82022-12-06 15:38:05 +0100102 s.assertNil(err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000103 cancel()
104}