@@ -13,6 +13,7 @@ import (
13
13
"github.com/gruntwork-io/terratest/modules/k8s"
14
14
"github.com/gruntwork-io/terratest/modules/random"
15
15
"github.com/imroc/req/v3"
16
+ "github.com/stretchr/testify/assert"
16
17
"github.com/tidwall/gjson"
17
18
)
18
19
@@ -145,6 +146,110 @@ func TestPathBasedRouting(t *testing.T) {
145
146
}
146
147
}
147
148
149
+ func TestPathBasedRoutAppServers (t * testing.T ) {
150
+ // Path to the helm chart we will test
151
+ helmChartPath , e := filepath .Abs ("../../charts" )
152
+ if e != nil {
153
+ t .Fatalf (e .Error ())
154
+ }
155
+ username := "admin"
156
+ password := "admin"
157
+
158
+ namespaceName := "ml-" + strings .ToLower (random .UniqueId ())
159
+ kubectlOptions := k8s .NewKubectlOptions ("" , "" , namespaceName )
160
+
161
+ // Setup the args for helm install using custom values.yaml file
162
+ options := & helm.Options {
163
+ ValuesFiles : []string {"../test_data/values/tls_pbr_appser_values.yaml" },
164
+ KubectlOptions : k8s .NewKubectlOptions ("" , "" , namespaceName ),
165
+ }
166
+
167
+ t .Logf ("====Creating namespace: " + namespaceName )
168
+ k8s .CreateNamespace (t , kubectlOptions , namespaceName )
169
+
170
+ defer t .Logf ("====Deleting namespace: " + namespaceName )
171
+ defer k8s .DeleteNamespace (t , kubectlOptions , namespaceName )
172
+
173
+ t .Logf ("====Installing Helm Chart" )
174
+ releaseName := "test-path"
175
+ helm .Install (t , options , helmChartPath , releaseName )
176
+
177
+ podName := releaseName + "-1"
178
+ svcName := releaseName + "-haproxy"
179
+
180
+ // wait until the pod is in Ready status
181
+ k8s .WaitUntilPodAvailable (t , kubectlOptions , podName , 15 , 20 * time .Second )
182
+
183
+ tunnel := k8s .NewTunnel (
184
+ kubectlOptions , k8s .ResourceTypeService , svcName , 8080 , 80 )
185
+ defer tunnel .Close ()
186
+ tunnel .ForwardPort (t )
187
+
188
+ client := req .C ().
189
+ SetCommonBasicAuth (username , password ).
190
+ SetCommonRetryCount (10 ).
191
+ SetCommonRetryFixedInterval (10 * time .Second )
192
+
193
+ endpoint := "http://localhost:8080/manage/manage/v2/servers?group-id=Default&server-type=http&format=json"
194
+ fmt .Println (endpoint )
195
+ testServerReq , err := os .ReadFile ("../test_data/path_based_test_data/test-server.json" )
196
+ if err != nil {
197
+ fmt .Print (err )
198
+ }
199
+
200
+ //create new app server: test-server
201
+ resp , err := client .R ().
202
+ SetHeader ("Content-type" , "application/json" ).
203
+ SetBodyJsonString (string (testServerReq )).
204
+ AddRetryCondition (func (resp * req.Response , err error ) bool {
205
+ if err != nil {
206
+ t .Logf ("error: %s" , err .Error ())
207
+ }
208
+ t .Logf ("StatusCode: %d" , resp .GetStatusCode ())
209
+ if resp .GetStatusCode () != 201 {
210
+ t .Log ("Waiting for MarkLogic cluster to be ready" )
211
+ }
212
+ return resp .GetStatusCode () != 201
213
+ }).
214
+ Post (endpoint )
215
+ if err != nil {
216
+ t .Fatalf (err .Error ())
217
+ }
218
+ defer resp .Body .Close ()
219
+
220
+ //test the additional app servers path
221
+ path := "test"
222
+ endpoint = fmt .Sprintf ("http://localhost:8080/%s" , path )
223
+ t .Logf ("Verifying path based routing using %s" , endpoint )
224
+ resp , err = client .R ().
225
+ AddRetryCondition (func (resp * req.Response , err error ) bool {
226
+ if err != nil {
227
+ t .Logf ("error: %s" , err .Error ())
228
+ }
229
+ t .Logf ("StatusCode: %d" , resp .GetStatusCode ())
230
+ if resp .GetStatusCode () != 500 {
231
+ t .Log ("Waiting for MarkLogic cluster to be ready" )
232
+ }
233
+ return resp .GetStatusCode () != 500
234
+ }).
235
+ Get (endpoint )
236
+
237
+ if err != nil {
238
+ t .Errorf ("Error routing to %s" , path )
239
+ t .Fatalf (err .Error ())
240
+ }
241
+ defer resp .Body .Close ()
242
+
243
+ //the response for test-server should be 500 and error message XDMP-MODNOTFOUND
244
+ //because test-server exist and there is no app running on it
245
+ assert .Equal (t , 500 , resp .GetStatusCode ())
246
+ body , err := io .ReadAll (resp .Body )
247
+ if err != nil {
248
+ t .Fatalf (err .Error ())
249
+ }
250
+ assert .Contains (t , string (body ), "XDMP-MODNOTFOUND" )
251
+ }
252
+
148
253
func TestPathBasedRoutingWithTLS (t * testing.T ) {
149
254
// Path to the helm chart we will test
150
255
helmChartPath , e := filepath .Abs ("../../charts" )
0 commit comments