blob: e43787df89c0bf96185a48e52bde0c80cb091518 [file] [log] [blame]
Matus Fabian7301abe2024-08-21 17:25:41 +02001package hst
2
3import (
4 "fmt"
5 "reflect"
6 "runtime"
7 "strings"
8
9 . "github.com/onsi/ginkgo/v2"
10)
11
12// These correspond to names used in yaml config
13const (
14 NginxProxyContainerName = "nginx-proxy"
15 NginxServerContainerName = "nginx-server"
16 MirroringClientInterfaceName = "hstcln"
17 MirroringServerInterfaceName = "hstsrv"
18)
19
20var nginxProxyTests = map[string][]func(s *NginxProxySuite){}
21var nginxProxySoloTests = map[string][]func(s *NginxProxySuite){}
22
23type NginxProxySuite struct {
24 HstSuite
25 proxyPort uint16
26}
27
28func RegisterNginxProxyTests(tests ...func(s *NginxProxySuite)) {
29 nginxProxyTests[getTestFilename()] = tests
30}
31func RegisterNginxProxySoloTests(tests ...func(s *NginxProxySuite)) {
32 nginxProxySoloTests[getTestFilename()] = tests
33}
34
35func (s *NginxProxySuite) SetupSuite() {
36 s.HstSuite.SetupSuite()
37 s.LoadNetworkTopology("2taps")
38 s.LoadContainerTopology("nginxProxy")
39}
40
41func (s *NginxProxySuite) SetupTest() {
42 s.HstSuite.SetupTest()
43
44 // VPP
45 var sessionConfig Stanza
46 sessionConfig.
47 NewStanza("session").
48 Append("enable").
49 Append("use-app-socket-api")
50
51 vppContainer := s.GetContainerByName(VppContainerName)
52 vpp, err := vppContainer.newVppInstance(vppContainer.AllocatedCpus, sessionConfig)
53 s.AssertNotNil(vpp, fmt.Sprint(err))
54 s.AssertNil(vpp.Start())
55 clientInterface := s.GetInterfaceByName(MirroringClientInterfaceName)
56 s.AssertNil(vpp.createTap(clientInterface, 1))
57 serverInterface := s.GetInterfaceByName(MirroringServerInterfaceName)
58 s.AssertNil(vpp.createTap(serverInterface, 2))
59
60 // nginx proxy
61 nginxProxyContainer := s.GetTransientContainerByName(NginxProxyContainerName)
62 s.AssertNil(nginxProxyContainer.Create())
63 s.proxyPort = 80
64 values := struct {
65 LogPrefix string
66 Proxy string
67 Server string
68 Port uint16
69 }{
70 LogPrefix: nginxProxyContainer.Name,
71 Proxy: clientInterface.Peer.Ip4AddressString(),
72 Server: serverInterface.Ip4AddressString(),
73 Port: s.proxyPort,
74 }
75 nginxProxyContainer.CreateConfig(
76 "/nginx.conf",
77 "./resources/nginx/nginx_proxy_mirroring.conf",
78 values,
79 )
80 s.AssertNil(nginxProxyContainer.Start())
81
82 // nginx HTTP server
83 nginxServerContainer := s.GetTransientContainerByName(NginxServerContainerName)
84 s.AssertNil(nginxServerContainer.Create())
85 nginxSettings := struct {
86 LogPrefix string
87 Address string
88 }{
89 LogPrefix: nginxServerContainer.Name,
90 Address: serverInterface.Ip4AddressString(),
91 }
92 nginxServerContainer.CreateConfig(
93 "/nginx.conf",
94 "./resources/nginx/nginx_server_mirroring.conf",
95 nginxSettings,
96 )
97 s.AssertNil(nginxServerContainer.Start())
98
99 vpp.WaitForApp("nginx-", 5)
100}
101
102func (s *NginxProxySuite) TearDownTest() {
103 if CurrentSpecReport().Failed() {
104 s.CollectNginxLogs(NginxServerContainerName)
105 s.CollectNginxLogs(NginxProxyContainerName)
106 }
107 s.HstSuite.TearDownTest()
108}
109
110func (s *NginxProxySuite) ProxyPort() uint16 {
111 return s.proxyPort
112}
113
114func (s *NginxProxySuite) ProxyAddr() string {
115 return s.GetInterfaceByName(MirroringClientInterfaceName).Peer.Ip4AddressString()
116}
117
118func (s *NginxProxySuite) CurlDownloadResource(uri string) {
119 args := fmt.Sprintf("--insecure --noproxy '*' --remote-name --output-dir /tmp %s", uri)
120 _, log := s.RunCurlContainer(args)
121 s.AssertNotContains(log, "Recv failure")
122 s.AssertContains(log, "HTTP/1.1 200")
123}
124
125var _ = Describe("NginxProxySuite", Ordered, ContinueOnFailure, func() {
126 var s NginxProxySuite
127 BeforeAll(func() {
128 s.SetupSuite()
129 })
130 BeforeEach(func() {
131 s.SetupTest()
132 })
133 AfterAll(func() {
134 s.TearDownSuite()
135 })
136 AfterEach(func() {
137 s.TearDownTest()
138 })
139
140 for filename, tests := range nginxProxyTests {
141 for _, test := range tests {
142 test := test
143 pc := reflect.ValueOf(test).Pointer()
144 funcValue := runtime.FuncForPC(pc)
145 testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
146 It(testName, func(ctx SpecContext) {
147 s.Log(testName + ": BEGIN")
148 test(&s)
149 }, SpecTimeout(SuiteTimeout))
150 }
151 }
152})
153
154var _ = Describe("NginxProxySuiteSolo", Ordered, ContinueOnFailure, Serial, func() {
155 var s NginxProxySuite
156 BeforeAll(func() {
157 s.SetupSuite()
158 })
159 BeforeEach(func() {
160 s.SetupTest()
161 })
162 AfterAll(func() {
163 s.TearDownSuite()
164 })
165 AfterEach(func() {
166 s.TearDownTest()
167 })
168
169 for filename, tests := range nginxProxySoloTests {
170 for _, test := range tests {
171 test := test
172 pc := reflect.ValueOf(test).Pointer()
173 funcValue := runtime.FuncForPC(pc)
174 testName := filename + "/" + strings.Split(funcValue.Name(), ".")[2]
175 It(testName, Label("SOLO"), func(ctx SpecContext) {
176 s.Log(testName + ": BEGIN")
177 test(&s)
178 }, SpecTimeout(SuiteTimeout))
179 }
180 }
181})