blob: 952bcc5aa76f688ee85a190d7b1e9d52a08fc2a4 [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
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010010func testProxyHttpTcp(s *NsSuite, proxySetup func() error) 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
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010016 s.assertNil(proxySetup(), "failed to setup proxy")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000017
18 // create test file
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010019 err := exechelper.Run(fmt.Sprintf("ip netns exec server truncate -s %s %s", srcFile, srcFile))
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010020 s.assertNil(err, "failed to run truncate command")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000021 defer func() { os.Remove(srcFile) }()
22
23 fmt.Println("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 fmt.Println("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)
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010037 s.assertNil(err, "failed to run wget")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000038 stopServer <- struct{}{}
39
40 defer func() { os.Remove(outputFile) }()
41
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010042 s.assertNil(assertFileSize(outputFile, srcFile))
43 return nil
44}
45
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010046func 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 fmt.Println("VPP running and configured...")
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010053
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010054 output, err := testVppProxy.vppctl("test proxy server server-uri tcp://10.0.0.2/555 client-uri tcp://10.0.1.1/666")
55 fmt.Println("Proxy configured...", string(output))
Filip Tehlar229f5fc2022-08-09 14:44:47 +000056 return nil
57}
58
59func (s *NsSuite) TestVppProxyHttpTcp() {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010060 err := testProxyHttpTcp(s, func() error {
61 return configureVppProxy(s)
62 })
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010063 s.assertNil(err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000064}
65
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010066func 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 fmt.Println("VPP running and configured...")
77 return nil
78}
79
Filip Tehlar229f5fc2022-08-09 14:44:47 +000080func (s *NsSuite) TestEnvoyProxyHttpTcp() {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010081 err := testProxyHttpTcp(s, func() error {
82 return configureEnvoyProxy(s)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000083 })
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010084 s.assertNil(err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000085}