blob: e646a96921446d4b3b29e90d0bb03fd720a5ee4c [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
Adrian Villin4677d922024-06-14 09:32:39 +02004 . "fd.io/hs-test/infra"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00005 "fmt"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00006 "github.com/edwarnicke/exechelper"
Adrian Villincee15aa2024-03-14 11:42:55 -04007 . "github.com/onsi/ginkgo/v2"
Adrian Villin4677d922024-06-14 09:32:39 +02008 "os"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00009)
10
Adrian Villincee15aa2024-03-14 11:42:55 -040011func init() {
Adrian Villin4677d922024-06-14 09:32:39 +020012 RegisterNsTests(VppProxyHttpTcpTest, VppProxyHttpTlsTest, EnvoyProxyHttpTcpTest)
Adrian Villincee15aa2024-03-14 11:42:55 -040013}
14
Filip Tehlar3336eef2023-11-29 07:40:18 +010015func testProxyHttpTcp(s *NsSuite, proto string) error {
Adrian Villin4677d922024-06-14 09:32:39 +020016 var outputFile string = s.ProcessIndex + "test" + s.Ppid + ".data"
17 var srcFilePpid string = s.ProcessIndex + "httpTestFile" + s.Ppid
Adrian Villineaa7d912024-06-06 04:26:30 -040018 const srcFileNoPpid = "httpTestFile"
adrianvillin28bd8f02024-02-13 06:00:02 -050019 const fileSize string = "10M"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000020 stopServer := make(chan struct{}, 1)
21 serverRunning := make(chan struct{}, 1)
Adrian Villin4677d922024-06-14 09:32:39 +020022 serverNetns := s.GetNetNamespaceByName("srv")
23 clientNetns := s.GetNetNamespaceByName("cln")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000024
Filip Tehlar229f5fc2022-08-09 14:44:47 +000025 // create test file
Adrian Villineaa7d912024-06-06 04:26:30 -040026 err := exechelper.Run(fmt.Sprintf("ip netns exec %s truncate -s %s %s", serverNetns, fileSize, srcFilePpid))
Adrian Villin4677d922024-06-14 09:32:39 +020027 s.AssertNil(err, "failed to run truncate command: "+fmt.Sprint(err))
Adrian Villineaa7d912024-06-06 04:26:30 -040028 defer func() { os.Remove(srcFilePpid) }()
Filip Tehlar229f5fc2022-08-09 14:44:47 +000029
Adrian Villin4677d922024-06-14 09:32:39 +020030 s.Log("test file created...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000031
Adrian Villincee15aa2024-03-14 11:42:55 -040032 go func() {
33 defer GinkgoRecover()
Adrian Villin4677d922024-06-14 09:32:39 +020034 s.StartHttpServer(serverRunning, stopServer, ":666", serverNetns)
Adrian Villincee15aa2024-03-14 11:42:55 -040035 }()
Filip Tehlar229f5fc2022-08-09 14:44:47 +000036 // TODO better error handling and recovery
37 <-serverRunning
38
39 defer func(chan struct{}) {
40 stopServer <- struct{}{}
41 }(stopServer)
42
Adrian Villin4677d922024-06-14 09:32:39 +020043 s.Log("http server started...")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000044
Adrian Villin4677d922024-06-14 09:32:39 +020045 clientVeth := s.GetInterfaceByName(ClientInterface)
adrianvillinfbf5f2b2024-02-13 03:26:25 -050046 c := fmt.Sprintf("ip netns exec %s wget --no-proxy --retry-connrefused"+
47 " --retry-on-http-error=503 --tries=10 -O %s ", clientNetns, outputFile)
Filip Tehlar3336eef2023-11-29 07:40:18 +010048 if proto == "tls" {
49 c += " --secure-protocol=TLSv1_3 --no-check-certificate https://"
50 }
Adrian Villin4677d922024-06-14 09:32:39 +020051 c += fmt.Sprintf("%s:555/%s", clientVeth.Ip4AddressString(), srcFileNoPpid)
52 s.Log(c)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000053 _, err = exechelper.CombinedOutput(c)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000054
55 defer func() { os.Remove(outputFile) }()
56
Adrian Villin4677d922024-06-14 09:32:39 +020057 s.AssertNil(err, "failed to run wget: '%s', cmd: %s", err, c)
adrianvillinfbf5f2b2024-02-13 03:26:25 -050058 stopServer <- struct{}{}
59
Adrian Villin4677d922024-06-14 09:32:39 +020060 s.AssertNil(AssertFileSize(outputFile, srcFilePpid))
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010061 return nil
62}
63
Filip Tehlar3336eef2023-11-29 07:40:18 +010064func configureVppProxy(s *NsSuite, proto string) {
Adrian Villin4677d922024-06-14 09:32:39 +020065 serverVeth := s.GetInterfaceByName(ServerInterface)
66 clientVeth := s.GetInterfaceByName(ClientInterface)
Maros Ondrejicka98a91e82022-12-06 15:38:05 +010067
Adrian Villin4677d922024-06-14 09:32:39 +020068 testVppProxy := s.GetContainerByName("vpp").VppInstance
69 output := testVppProxy.Vppctl(
Filip Tehlar3336eef2023-11-29 07:40:18 +010070 "test proxy server server-uri %s://%s/555 client-uri tcp://%s/666",
71 proto,
Adrian Villin4677d922024-06-14 09:32:39 +020072 clientVeth.Ip4AddressString(),
73 serverVeth.Peer.Ip4AddressString(),
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010074 )
Adrian Villin4677d922024-06-14 09:32:39 +020075 s.Log("proxy configured: " + output)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000076}
77
Adrian Villincee15aa2024-03-14 11:42:55 -040078func VppProxyHttpTcpTest(s *NsSuite) {
Filip Tehlar3336eef2023-11-29 07:40:18 +010079 proto := "tcp"
80 configureVppProxy(s, proto)
81 err := testProxyHttpTcp(s, proto)
Adrian Villin4677d922024-06-14 09:32:39 +020082 s.AssertNil(err, fmt.Sprint(err))
Filip Tehlar3336eef2023-11-29 07:40:18 +010083}
84
Adrian Villincee15aa2024-03-14 11:42:55 -040085func VppProxyHttpTlsTest(s *NsSuite) {
Filip Tehlar3336eef2023-11-29 07:40:18 +010086 proto := "tls"
87 configureVppProxy(s, proto)
88 err := testProxyHttpTcp(s, proto)
Adrian Villin4677d922024-06-14 09:32:39 +020089 s.AssertNil(err, fmt.Sprint(err))
Filip Tehlar229f5fc2022-08-09 14:44:47 +000090}
91
Maros Ondrejicka85396a52023-02-28 12:49:43 +010092func configureEnvoyProxy(s *NsSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +020093 envoyContainer := s.GetContainerByName("envoy")
Adrian Villin25140012024-07-09 15:31:36 +020094 s.AssertNil(envoyContainer.Create())
Maros Ondrejicka85396a52023-02-28 12:49:43 +010095
Adrian Villin4677d922024-06-14 09:32:39 +020096 serverVeth := s.GetInterfaceByName(ServerInterface)
Maros Ondrejicka85396a52023-02-28 12:49:43 +010097 address := struct {
98 Server string
99 }{
Adrian Villin4677d922024-06-14 09:32:39 +0200100 Server: serverVeth.Peer.Ip4AddressString(),
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100101 }
Adrian Villin4677d922024-06-14 09:32:39 +0200102 envoyContainer.CreateConfig(
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100103 "/etc/envoy/envoy.yaml",
104 "resources/envoy/proxy.yaml",
105 address,
106 )
Adrian Villin4677d922024-06-14 09:32:39 +0200107 s.AssertNil(envoyContainer.Start())
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100108}
109
Adrian Villincee15aa2024-03-14 11:42:55 -0400110func EnvoyProxyHttpTcpTest(s *NsSuite) {
Maros Ondrejicka85396a52023-02-28 12:49:43 +0100111 configureEnvoyProxy(s)
Filip Tehlar3336eef2023-11-29 07:40:18 +0100112 err := testProxyHttpTcp(s, "tcp")
Adrian Villin4677d922024-06-14 09:32:39 +0200113 s.AssertNil(err, fmt.Sprint(err))
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000114}