hs-test: use assert-like approach in test cases
[vpp.git] / extras / hs-test / proxy_test.go
1 package main
2
3 import (
4         "context"
5         "fmt"
6         "os"
7
8         "github.com/edwarnicke/exechelper"
9 )
10
11 func testProxyHttpTcp(s *NsSuite, dockerInstance, action string, proxySetup func() error) error {
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)
18         s.assertNil(dockerRun(dockerInstance, volumeArgs), "failed to start container")
19         defer func() { exechelper.Run("docker stop " + dockerInstance) }()
20
21         // start & configure vpp in the container
22         _, err := hstExec(action, dockerInstance)
23         s.assertNil(err)
24
25         fmt.Println("VPP running and configured...")
26
27         s.assertNil(proxySetup(), "failed to setup proxy")
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))
32         s.assertNil(err, "failed to run truncate command")
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)
49         s.assertNil(err, "failed to run wget")
50         stopServer <- struct{}{}
51
52         defer func() { os.Remove(outputFile) }()
53
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:
62                 return err
63         default:
64         }
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)
81         return nil
82 }
83
84 func (s *NsSuite) TestVppProxyHttpTcp() {
85         dockerInstance := "vpp-proxy"
86         err := testProxyHttpTcp(s, dockerInstance, "ConfigureVppProxy", configureVppProxy)
87         s.assertNil(err)
88 }
89
90 func (s *NsSuite) TestEnvoyProxyHttpTcp() {
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"
99         err := testProxyHttpTcp(s, dockerInstance, "ConfigureEnvoyProxy", func() error {
100                 return setupEnvoy(ctx, dockerInstance)
101         })
102         s.assertNil(err)
103         cancel()
104 }