blob: c2f9b6f2825b9ea240e6b0fc7d1e0259044a8d4d [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 Tehlar3336eef2023-11-29 07:40:18 +010010func testProxyHttpTcp(s *NsSuite, proto string) error {
adrianvillin28bd8f02024-02-13 06:00:02 -050011 var outputFile string = "test" + s.pid + ".data"
12 var srcFilePid string = "httpTestFile" + s.pid
13 const srcFileNoPid = "httpTestFile"
14 const fileSize string = "10M"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000015 stopServer := make(chan struct{}, 1)
16 serverRunning := make(chan struct{}, 1)
adrianvillin28bd8f02024-02-13 06:00:02 -050017 serverNetns := s.getNetNamespaceByName("srv")
18 clientNetns := s.getNetNamespaceByName("cln")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000019
Filip Tehlar229f5fc2022-08-09 14:44:47 +000020 // create test file
adrianvillin28bd8f02024-02-13 06:00:02 -050021 err := exechelper.Run(fmt.Sprintf("ip netns exec %s truncate -s %s %s", serverNetns, fileSize, srcFilePid))
adrianvillin7c675472024-02-12 02:44:53 -050022 s.assertNil(err, "failed to run truncate command: " + fmt.Sprint(err))
adrianvillin28bd8f02024-02-13 06:00:02 -050023 defer func() { os.Remove(srcFilePid) }()
Filip Tehlar229f5fc2022-08-09 14:44:47 +000024
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010025 s.log("test file created...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000026
adrianvillinfbf5f2b2024-02-13 03:26:25 -050027 go s.startHttpServer(serverRunning, stopServer, ":666", serverNetns)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000028 // TODO better error handling and recovery
29 <-serverRunning
30
31 defer func(chan struct{}) {
32 stopServer <- struct{}{}
33 }(stopServer)
34
Maros Ondrejicka87531802022-12-19 20:35:27 +010035 s.log("http server started...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000036
adrianvillin28bd8f02024-02-13 06:00:02 -050037 clientVeth := s.getInterfaceByName(clientInterface)
adrianvillinfbf5f2b2024-02-13 03:26:25 -050038 c := fmt.Sprintf("ip netns exec %s wget --no-proxy --retry-connrefused"+
39 " --retry-on-http-error=503 --tries=10 -O %s ", clientNetns, outputFile)
Filip Tehlar3336eef2023-11-29 07:40:18 +010040 if proto == "tls" {
41 c += " --secure-protocol=TLSv1_3 --no-check-certificate https://"
42 }
adrianvillin28bd8f02024-02-13 06:00:02 -050043 c += fmt.Sprintf("%s:555/%s", clientVeth.ip4AddressString(), srcFileNoPid)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010044 s.log(c)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000045 _, err = exechelper.CombinedOutput(c)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000046
47 defer func() { os.Remove(outputFile) }()
48
adrianvillinfbf5f2b2024-02-13 03:26:25 -050049 s.assertNil(err, "failed to run wget: '%s', cmd: %s", err, c)
50 stopServer <- struct{}{}
51
adrianvillin28bd8f02024-02-13 06:00:02 -050052 s.assertNil(assertFileSize(outputFile, srcFilePid))
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010053 return nil
54}
55
Filip Tehlar3336eef2023-11-29 07:40:18 +010056func configureVppProxy(s *NsSuite, proto string) {
adrianvillin28bd8f02024-02-13 06:00:02 -050057 serverVeth := s.getInterfaceByName(serverInterface)
58 clientVeth := s.getInterfaceByName(clientInterface)
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010059
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010060 testVppProxy := s.getContainerByName("vpp").vppInstance
61 output := testVppProxy.vppctl(
Filip Tehlar3336eef2023-11-29 07:40:18 +010062 "test proxy server server-uri %s://%s/555 client-uri tcp://%s/666",
63 proto,
Maros Ondrejickae7625d02023-02-28 16:55:01 +010064 clientVeth.ip4AddressString(),
65 serverVeth.peer.ip4AddressString(),
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010066 )
67 s.log("proxy configured...", output)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000068}
69
70func (s *NsSuite) TestVppProxyHttpTcp() {
Filip Tehlar3336eef2023-11-29 07:40:18 +010071 proto := "tcp"
72 configureVppProxy(s, proto)
73 err := testProxyHttpTcp(s, proto)
adrianvillin7c675472024-02-12 02:44:53 -050074 s.assertNil(err, err)
Filip Tehlar3336eef2023-11-29 07:40:18 +010075}
76
77func (s *NsSuite) TestVppProxyHttpTls() {
78 proto := "tls"
79 configureVppProxy(s, proto)
80 err := testProxyHttpTcp(s, proto)
adrianvillin7c675472024-02-12 02:44:53 -050081 s.assertNil(err, err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000082}
83
Maros Ondrejicka85396a52023-02-28 12:49:43 +010084func configureEnvoyProxy(s *NsSuite) {
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010085 envoyContainer := s.getContainerByName("envoy")
adrianvillin7c675472024-02-12 02:44:53 -050086 err := envoyContainer.create()
87 s.assertNil(err, "Error creating envoy container: %s", err)
Maros Ondrejicka85396a52023-02-28 12:49:43 +010088
adrianvillin28bd8f02024-02-13 06:00:02 -050089 serverVeth := s.getInterfaceByName(serverInterface)
Maros Ondrejicka85396a52023-02-28 12:49:43 +010090 address := struct {
91 Server string
92 }{
Maros Ondrejickae7625d02023-02-28 16:55:01 +010093 Server: serverVeth.peer.ip4AddressString(),
Maros Ondrejicka85396a52023-02-28 12:49:43 +010094 }
95 envoyContainer.createConfig(
96 "/etc/envoy/envoy.yaml",
97 "resources/envoy/proxy.yaml",
98 address,
99 )
Filip Tehlar9abba112023-03-07 10:13:19 +0100100 s.assertNil(envoyContainer.start())
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100101}
102
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000103func (s *NsSuite) TestEnvoyProxyHttpTcp() {
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100104 configureEnvoyProxy(s)
Filip Tehlar3336eef2023-11-29 07:40:18 +0100105 err := testProxyHttpTcp(s, "tcp")
adrianvillin7c675472024-02-12 02:44:53 -0500106 s.assertNil(err, err)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000107}