Skip to content

Commit 66df59b

Browse files
committed
Add test for building paths
Refactor the relevant part of the code away from the main function. That helps mocking the Applications for which we want to build paths. This allows us to cover building the paths for the gateway by separate tests.
1 parent cf7a968 commit 66df59b

File tree

2 files changed

+103
-19
lines changed

2 files changed

+103
-19
lines changed

controllers/cloud.redhat.com/providers/web/resources_caddygateway.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,30 @@ func makeWebGatewayConfigMap(p *providers.Provider) (string, error) {
250250
}
251251
bopHostname := fmt.Sprintf("%s-%s.%s.svc:8090", p.Env.GetClowdName(), "mbop", p.Env.GetClowdNamespace())
252252

253+
upstreamList, whitelistStrings := buildUpstreamAndWhiteLists(bopHostname, appList)
254+
255+
cmData, err := GenerateConfig(
256+
getCertHostname(p.Env.Status.Hostname),
257+
fmt.Sprintf("http://%s", bopHostname),
258+
whitelistStrings,
259+
upstreamList,
260+
)
261+
if err != nil {
262+
return "", err
263+
}
264+
265+
cm.Data = map[string]string{
266+
"Caddyfile.json": cmData,
267+
}
268+
269+
h := sha256.New()
270+
h.Write([]byte(cmData))
271+
hash := fmt.Sprintf("%x", h.Sum(nil))
272+
273+
return hash, p.Cache.Update(CoreCaddyConfigMap, cm)
274+
}
275+
276+
func buildUpstreamAndWhiteLists(bopHostname string, appList *crd.ClowdAppList) ([]ProxyRoute, []string) {
253277
whitelistStrings := []string{}
254278
upstreamList := []ProxyRoute{{
255279
Upstream: bopHostname,
@@ -307,25 +331,7 @@ func makeWebGatewayConfigMap(p *providers.Provider) (string, error) {
307331
}
308332
}
309333

310-
cmData, err := GenerateConfig(
311-
getCertHostname(p.Env.Status.Hostname),
312-
fmt.Sprintf("http://%s", bopHostname),
313-
whitelistStrings,
314-
upstreamList,
315-
)
316-
if err != nil {
317-
return "", err
318-
}
319-
320-
cm.Data = map[string]string{
321-
"Caddyfile.json": cmData,
322-
}
323-
324-
h := sha256.New()
325-
h.Write([]byte(cmData))
326-
hash := fmt.Sprintf("%x", h.Sum(nil))
327-
328-
return hash, p.Cache.Update(CoreCaddyConfigMap, cm)
334+
return upstreamList, whitelistStrings
329335
}
330336

331337
func makeWebGatewayDeployment(_ *crd.ClowdEnvironment, o obj.ClowdObject, objMap providers.ObjectMap, _ bool, _ bool) error {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package web
2+
3+
import (
4+
"testing"
5+
6+
crd "github.com/RedHatInsights/clowder/apis/cloud.redhat.com/v1alpha1"
7+
"github.com/stretchr/testify/assert"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
)
10+
11+
func TestBuildUpstreamAndWhiteLists(t *testing.T) {
12+
bopHostname := "test-bop-hostname"
13+
appList := &crd.ClowdAppList{
14+
Items: []crd.ClowdApp{
15+
{
16+
Spec: crd.ClowdAppSpec{
17+
Deployments: []crd.Deployment{
18+
{
19+
Name: "test-deployment-1",
20+
WebServices: crd.WebServices{
21+
Public: crd.PublicWebService{
22+
Enabled: true,
23+
APIPath: "test-api",
24+
WhitelistPaths: []string{"/whitelist-path-1", "/whitelist-path-2"},
25+
},
26+
},
27+
},
28+
},
29+
},
30+
ObjectMeta: metav1.ObjectMeta{
31+
Name: "test-app-1",
32+
Namespace: "test-namespace-1",
33+
},
34+
},
35+
{
36+
Spec: crd.ClowdAppSpec{
37+
Deployments: []crd.Deployment{
38+
{
39+
Name: "test-deployment-2",
40+
WebServices: crd.WebServices{
41+
Public: crd.PublicWebService{
42+
Enabled: true,
43+
APIPaths: []crd.APIPath{
44+
"/api-path-1",
45+
"/api-path-2",
46+
},
47+
},
48+
},
49+
},
50+
},
51+
},
52+
ObjectMeta: metav1.ObjectMeta{
53+
Name: "test-app-2",
54+
Namespace: "test-namespace-2",
55+
},
56+
},
57+
},
58+
}
59+
60+
// Call the function under test
61+
upstreamList, whitelistStrings := buildUpstreamAndWhiteLists(bopHostname, appList)
62+
63+
expectedUpstreamList := []ProxyRoute{
64+
{Upstream: bopHostname, Path: "/v1/registrations*"},
65+
{Upstream: bopHostname, Path: "/v1/check_registration*"},
66+
{Upstream: "test-app-1-test-deployment-1.test-namespace-1.svc:8000", Path: "/api/test-api/*"},
67+
{Upstream: "test-app-2-test-deployment-2.test-namespace-2.svc:8000", Path: "/api-path-1"},
68+
{Upstream: "test-app-2-test-deployment-2.test-namespace-2.svc:8000", Path: "/api-path-2"},
69+
}
70+
71+
expectedWhitelistStrings := []string{
72+
"/whitelist-path-1",
73+
"/whitelist-path-2",
74+
}
75+
76+
assert.ElementsMatch(t, expectedUpstreamList, upstreamList, "Upstream list does not match expected")
77+
assert.ElementsMatch(t, expectedWhitelistStrings, whitelistStrings, "Whitelist strings do not match expected")
78+
}

0 commit comments

Comments
 (0)