blob: 784e52768aadf662cb897704cd92c9437bced65c [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
Filip Tehlar229f5fc2022-08-09 14:44:47 +00004 "fmt"
5 "os"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00006
7 "github.com/edwarnicke/exechelper"
8)
9
Filip Tehlarbb98aac2022-12-21 14:40:35 +010010func testProxyHttpTcp(s *NsSuite) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000011 const outputFile = "test.data"
12 const srcFile = "10M"
13 stopServer := make(chan struct{}, 1)
14 serverRunning := make(chan struct{}, 1)
15
Filip Tehlar229f5fc2022-08-09 14:44:47 +000016 // create test file
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010017 err := exechelper.Run(fmt.Sprintf("ip netns exec server truncate -s %s %s", srcFile, srcFile))
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010018 s.assertNil(err, "failed to run truncate command")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000019 defer func() { os.Remove(srcFile) }()
20
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010021 s.log("test file created...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000022
23 go startHttpServer(serverRunning, stopServer, ":666", "server")
24 // TODO better error handling and recovery
25 <-serverRunning
26
27 defer func(chan struct{}) {
28 stopServer <- struct{}{}
29 }(stopServer)
30
Maros Ondrejicka87531802022-12-19 20:35:27 +010031 s.log("http server started...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000032
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010033 clientVeth := s.netInterfaces[clientInterface]
34 c := fmt.Sprintf("ip netns exec client wget --no-proxy --retry-connrefused"+
35 " --retry-on-http-error=503 --tries=10"+
36 " -O %s %s:555/%s",
37 outputFile,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010038 clientVeth.IP4AddressString(),
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010039 srcFile,
40 )
41 s.log(c)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000042 _, err = exechelper.CombinedOutput(c)
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010043 s.assertNil(err, "failed to run wget")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000044 stopServer <- struct{}{}
45
46 defer func() { os.Remove(outputFile) }()
47
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010048 s.assertNil(assertFileSize(outputFile, srcFile))
49 return nil
50}
51
Maros Ondrejicka85396a52023-02-28 12:49:43 +010052func configureVppProxy(s *NsSuite) {
Maros Ondrejicka40cba402023-02-23 13:19:15 +010053 serverVeth := s.netInterfaces[serverInterface]
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010054 clientVeth := s.netInterfaces[clientInterface]
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010055
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010056 testVppProxy := s.getContainerByName("vpp").vppInstance
57 output := testVppProxy.vppctl(
58 "test proxy server server-uri tcp://%s/555 client-uri tcp://%s/666",
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010059 clientVeth.IP4AddressString(),
Maros Ondrejicka40cba402023-02-23 13:19:15 +010060 serverVeth.Peer().IP4AddressString(),
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010061 )
62 s.log("proxy configured...", output)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000063}
64
65func (s *NsSuite) TestVppProxyHttpTcp() {
Maros Ondrejicka85396a52023-02-28 12:49:43 +010066 configureVppProxy(s)
67 err := testProxyHttpTcp(s)
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010068 s.assertNil(err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000069}
70
Maros Ondrejicka85396a52023-02-28 12:49:43 +010071func configureEnvoyProxy(s *NsSuite) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010072 envoyContainer := s.getContainerByName("envoy")
Maros Ondrejicka85396a52023-02-28 12:49:43 +010073 envoyContainer.create()
74
75 serverVeth := s.netInterfaces[serverInterface]
76 address := struct {
77 Server string
78 }{
79 Server: serverVeth.Peer().IP4AddressString(),
80 }
81 envoyContainer.createConfig(
82 "/etc/envoy/envoy.yaml",
83 "resources/envoy/proxy.yaml",
84 address,
85 )
Filip Tehlar9abba112023-03-07 10:13:19 +010086 s.assertNil(envoyContainer.start())
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010087}
88
Filip Tehlar229f5fc2022-08-09 14:44:47 +000089func (s *NsSuite) TestEnvoyProxyHttpTcp() {
Maros Ondrejicka85396a52023-02-28 12:49:43 +010090 configureEnvoyProxy(s)
91 err := testProxyHttpTcp(s)
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010092 s.assertNil(err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000093}