Matus Fabian | 8792e5c | 2024-08-14 12:38:20 +0200 | [diff] [blame^] | 1 | // Suite for VPP proxy testing |
| 2 | // |
| 3 | // The topology consists of 3 containers: curl (client), VPP (proxy), nginx (target HTTP server). |
| 4 | // VPP has 2 tap interfaces configured, one for client network and second for server/target network. |
| 5 | |
| 6 | package hst |
| 7 | |
| 8 | import ( |
| 9 | "fmt" |
| 10 | . "github.com/onsi/ginkgo/v2" |
| 11 | "reflect" |
| 12 | "runtime" |
| 13 | "strings" |
| 14 | ) |
| 15 | |
| 16 | // These correspond to names used in yaml config |
| 17 | const ( |
| 18 | ClientTapInterfaceName = "hstcln" |
| 19 | ServerTapInterfaceName = "hstsrv" |
| 20 | ) |
| 21 | |
| 22 | type VppProxySuite struct { |
| 23 | HstSuite |
| 24 | nginxPort uint16 |
| 25 | } |
| 26 | |
| 27 | var vppProxyTests = map[string][]func(s *VppProxySuite){} |
| 28 | var vppProxySoloTests = map[string][]func(s *VppProxySuite){} |
| 29 | |
| 30 | func RegisterVppProxyTests(tests ...func(s *VppProxySuite)) { |
| 31 | vppProxyTests[getTestFilename()] = tests |
| 32 | } |
| 33 | |
| 34 | func RegisterVppProxySoloTests(tests ...func(s *VppProxySuite)) { |
| 35 | vppProxySoloTests[getTestFilename()] = tests |
| 36 | } |
| 37 | |
| 38 | func (s *VppProxySuite) SetupSuite() { |
| 39 | s.HstSuite.SetupSuite() |
| 40 | s.LoadNetworkTopology("2taps") |
| 41 | s.LoadContainerTopology("vppProxy") |
| 42 | } |
| 43 | |
| 44 | func (s *VppProxySuite) SetupTest() { |
| 45 | s.HstSuite.SetupTest() |
| 46 | |
| 47 | // VPP HTTP connect-proxy |
| 48 | vppContainer := s.GetContainerByName(VppProxyContainerName) |
| 49 | vpp, err := vppContainer.newVppInstance(vppContainer.AllocatedCpus) |
| 50 | s.AssertNotNil(vpp, fmt.Sprint(err)) |
| 51 | s.AssertNil(vpp.Start()) |
| 52 | clientInterface := s.GetInterfaceByName(ClientTapInterfaceName) |
| 53 | s.AssertNil(vpp.createTap(clientInterface, 1)) |
| 54 | serverInterface := s.GetInterfaceByName(ServerTapInterfaceName) |
| 55 | s.AssertNil(vpp.createTap(serverInterface, 2)) |
| 56 | |
| 57 | // nginx HTTP server |
| 58 | nginxContainer := s.GetTransientContainerByName(NginxServerContainerName) |
| 59 | s.AssertNil(nginxContainer.Create()) |
| 60 | s.nginxPort = 80 |
| 61 | nginxSettings := struct { |
| 62 | LogPrefix string |
| 63 | Address string |
| 64 | Port uint16 |
| 65 | }{ |
| 66 | LogPrefix: nginxContainer.Name, |
| 67 | Address: serverInterface.Ip4AddressString(), |
| 68 | Port: s.nginxPort, |
| 69 | } |
| 70 | nginxContainer.CreateConfig( |
| 71 | "/nginx.conf", |
| 72 | "./resources/nginx/nginx_server.conf", |
| 73 | nginxSettings, |
| 74 | ) |
| 75 | s.AssertNil(nginxContainer.Start()) |
| 76 | } |
| 77 | |
| 78 | func (s *VppProxySuite) TearDownTest() { |
| 79 | if CurrentSpecReport().Failed() { |
| 80 | s.CollectNginxLogs(NginxServerContainerName) |
| 81 | } |
| 82 | s.HstSuite.TearDownTest() |
| 83 | } |
| 84 | |
| 85 | func (s *VppProxySuite) NginxPort() uint16 { |
| 86 | return s.nginxPort |
| 87 | } |
| 88 | |
| 89 | func (s *VppProxySuite) NginxAddr() string { |
| 90 | return s.GetInterfaceByName(ServerTapInterfaceName).Ip4AddressString() |
| 91 | } |
| 92 | |
| 93 | func (s *VppProxySuite) VppProxyAddr() string { |
| 94 | return s.GetInterfaceByName(ClientTapInterfaceName).Peer.Ip4AddressString() |
| 95 | } |
| 96 | |
| 97 | func (s *VppProxySuite) CurlRequest(targetUri string) (string, string) { |
| 98 | args := fmt.Sprintf("--insecure --noproxy '*' %s", targetUri) |
| 99 | body, log := s.RunCurlContainer(args) |
| 100 | return body, log |
| 101 | } |
| 102 | |
| 103 | func (s *VppProxySuite) CurlRequestViaTunnel(targetUri string, proxyUri string) (string, string) { |
| 104 | args := fmt.Sprintf("--insecure -p -x %s %s", proxyUri, targetUri) |
| 105 | body, log := s.RunCurlContainer(args) |
| 106 | return body, log |
| 107 | } |
| 108 | |
| 109 | func (s *VppProxySuite) CurlDownloadResource(uri string) { |
| 110 | args := fmt.Sprintf("--insecure --noproxy '*' --remote-name --output-dir /tmp %s", uri) |
| 111 | _, log := s.RunCurlContainer(args) |
| 112 | s.AssertNotContains(log, "Recv failure") |
| 113 | s.AssertContains(log, "HTTP/1.1 200") |
| 114 | } |
| 115 | |
| 116 | func (s *VppProxySuite) CurlDownloadResourceViaTunnel(uri string, proxyUri string) { |
| 117 | args := fmt.Sprintf("--insecure -p -x %s --remote-name --output-dir /tmp %s", proxyUri, uri) |
| 118 | _, log := s.RunCurlContainer(args) |
| 119 | s.AssertNotContains(log, "Recv failure") |
| 120 | s.AssertContains(log, "HTTP/1.1 200") |
| 121 | } |
| 122 | |
| 123 | var _ = Describe("VppProxySuite", Ordered, ContinueOnFailure, func() { |
| 124 | var s VppProxySuite |
| 125 | BeforeAll(func() { |
| 126 | s.SetupSuite() |
| 127 | }) |
| 128 | BeforeEach(func() { |
| 129 | s.SetupTest() |
| 130 | }) |
| 131 | AfterAll(func() { |
| 132 | s.TearDownSuite() |
| 133 | }) |
| 134 | AfterEach(func() { |
| 135 | s.TearDownTest() |
| 136 | }) |
| 137 | |
| 138 | for filename, tests := range vppProxyTests { |
| 139 | for _, test := range tests { |
| 140 | test := test |
| 141 | pc := reflect.ValueOf(test).Pointer() |
| 142 | funcValue := runtime.FuncForPC(pc) |
| 143 | testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2] |
| 144 | It(testName, func(ctx SpecContext) { |
| 145 | s.Log(testName + ": BEGIN") |
| 146 | test(&s) |
| 147 | }, SpecTimeout(SuiteTimeout)) |
| 148 | } |
| 149 | } |
| 150 | }) |
| 151 | |
| 152 | var _ = Describe("VppProxySuiteSolo", Ordered, ContinueOnFailure, func() { |
| 153 | var s VppProxySuite |
| 154 | BeforeAll(func() { |
| 155 | s.SetupSuite() |
| 156 | }) |
| 157 | BeforeEach(func() { |
| 158 | s.SetupTest() |
| 159 | }) |
| 160 | AfterAll(func() { |
| 161 | s.TearDownSuite() |
| 162 | }) |
| 163 | AfterEach(func() { |
| 164 | s.TearDownTest() |
| 165 | }) |
| 166 | |
| 167 | for filename, tests := range vppProxySoloTests { |
| 168 | for _, test := range tests { |
| 169 | test := test |
| 170 | pc := reflect.ValueOf(test).Pointer() |
| 171 | funcValue := runtime.FuncForPC(pc) |
| 172 | testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2] |
| 173 | It(testName, Label("SOLO"), func(ctx SpecContext) { |
| 174 | s.Log(testName + ": BEGIN") |
| 175 | test(&s) |
| 176 | }, SpecTimeout(SuiteTimeout)) |
| 177 | } |
| 178 | } |
| 179 | }) |