745f042a4b6c1503a594ebf6cfcf8b71fcc8ebc7
[vpp.git] / extras / hs-test / proxy_test.go
1 package main
2
3 import (
4         "fmt"
5         "os"
6
7         "github.com/edwarnicke/exechelper"
8 )
9
10 func testProxyHttpTcp(s *NsSuite, proxySetup func() error) error {
11         const outputFile = "test.data"
12         const srcFile = "10M"
13         stopServer := make(chan struct{}, 1)
14         serverRunning := make(chan struct{}, 1)
15
16         s.assertNil(proxySetup(), "failed to setup proxy")
17
18         // create test file
19         err := exechelper.Run(fmt.Sprintf("ip netns exec server truncate -s %s %s", srcFile, srcFile))
20         s.assertNil(err, "failed to run truncate command")
21         defer func() { os.Remove(srcFile) }()
22
23         s.log("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         s.log("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)
37         s.assertNil(err, "failed to run wget")
38         stopServer <- struct{}{}
39
40         defer func() { os.Remove(outputFile) }()
41
42         s.assertNil(assertFileSize(outputFile, srcFile))
43         return nil
44 }
45
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         s.log("VPP running and configured...")
53
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         s.log("Proxy configured...", string(output))
56         return nil
57 }
58
59 func (s *NsSuite) TestVppProxyHttpTcp() {
60         err := testProxyHttpTcp(s, func() error {
61                 return configureVppProxy(s)
62         })
63         s.assertNil(err)
64 }
65
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         s.log("VPP running and configured...")
77         return nil
78 }
79
80 func (s *NsSuite) TestEnvoyProxyHttpTcp() {
81         err := testProxyHttpTcp(s, func() error {
82                 return configureEnvoyProxy(s)
83         })
84         s.assertNil(err)
85 }