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