blob: 797de5255fa67bafabce0d786098651374c97ccd [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "testing"
8
9 "github.com/edwarnicke/exechelper"
10)
11
12func testProxyHttpTcp(t *testing.T, dockerInstance string, proxySetup func() error) error {
13 const outputFile = "test.data"
14 const srcFile = "10M"
15 stopServer := make(chan struct{}, 1)
16 serverRunning := make(chan struct{}, 1)
17
18 volumeArgs := fmt.Sprintf("-v shared-vol:/tmp/%s", dockerInstance)
19 err := dockerRun(dockerInstance, volumeArgs)
20 if err != nil {
21 return fmt.Errorf("failed to start container: %v", err)
22 }
23 defer func() { exechelper.Run("docker stop " + dockerInstance) }()
24
25 // start & configure vpp in the container
26 _, err = hstExec(dockerInstance, dockerInstance)
27 if err != nil {
28 return fmt.Errorf("error starting vpp in container: %v", err)
29 }
30
31 fmt.Println("VPP running and configured...")
32
33 if err := proxySetup(); err != nil {
34 return fmt.Errorf("failed to setup proxy: %v", err)
35 }
36 fmt.Println("Proxy configured...")
37
38 // create test file
39 err = exechelper.Run(fmt.Sprintf("ip netns exec server truncate -s %s %s", srcFile, srcFile))
40 if err != nil {
41 return fmt.Errorf("failed to run truncate command")
42 }
43 defer func() { os.Remove(srcFile) }()
44
45 fmt.Println("Test file created...")
46
47 go startHttpServer(serverRunning, stopServer, ":666", "server")
48 // TODO better error handling and recovery
49 <-serverRunning
50
51 defer func(chan struct{}) {
52 stopServer <- struct{}{}
53 }(stopServer)
54
55 fmt.Println("http server started...")
56
57 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)
58 _, err = exechelper.CombinedOutput(c)
59 if err != nil {
60 return fmt.Errorf("failed to run wget: %v", err)
61 }
62 stopServer <- struct{}{}
63
64 defer func() { os.Remove(outputFile) }()
65
66 if err = assertFileSize(outputFile, srcFile); err != nil {
67 return err
68 }
69 return nil
70}
71
72func (s *NsSuite) TestVppProxyHttpTcp() {
73 t := s.T()
74 dockerInstance := "vpp-proxy"
75 err := testProxyHttpTcp(t, dockerInstance, configureVppProxy)
76 if err != nil {
77 t.Errorf("%v", err)
78 }
79}
80
81func (s *NsSuite) TestEnvoyProxyHttpTcp() {
82 t := s.T()
83 exechelper.Run("docker volume create --name=shared-vol")
84 defer func() {
85 exechelper.Run("docker stop envoy")
86 }()
87
88 ctx, cancel := context.WithCancel(context.Background())
89
90 dockerInstance := "vpp-envoy"
91 err := testProxyHttpTcp(t, dockerInstance, func() error {
92 return setupEnvoy(t, ctx, dockerInstance)
93 })
94 if err != nil {
95 t.Errorf("%v", err)
96 }
97 cancel()
98}