Skip to content

Commit

Permalink
added more tests to actually test healthz endpoint configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Vala <mvala@redhat.com>
  • Loading branch information
sparkoo committed Oct 6, 2021
1 parent cd3f01a commit 8b6a98b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 62 deletions.
101 changes: 40 additions & 61 deletions controllers/devworkspace/solver/che_routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/eclipse-che/che-operator/pkg/deploy/gateway"

dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
Expand Down Expand Up @@ -161,6 +163,7 @@ func relocatableDevWorkspaceRouting() *dwo.DevWorkspaceRouting {
Path: "/1/",
Attributes: attributes.Attributes{
urlRewriteSupportedEndpointAttributeName: apiext.JSON{Raw: []byte("\"true\"")},
string(dwo.TypeEndpointAttribute): apiext.JSON{Raw: []byte("\"main\"")},
},
},
{
Expand Down Expand Up @@ -324,34 +327,22 @@ func TestCreateRelocatedObjectsOpenshift(t *testing.T) {
infrastructure.InitializeForTesting(infrastructure.OpenShiftv4)
cl, _, objs := getSpecObjects(t, relocatableDevWorkspaceRouting())

t.Run("noIngresses", func(t *testing.T) {
if len(objs.Ingresses) != 0 {
t.Error()
}
})

t.Run("noRoutes", func(t *testing.T) {
if len(objs.Routes) != 0 {
t.Error()
}
})
assert.Empty(t, objs.Ingresses)
assert.Empty(t, objs.Routes)

t.Run("testPodAdditions", func(t *testing.T) {
if len(objs.PodAdditions.Containers) != 1 || objs.PodAdditions.Containers[0].Name != wsGatewayName {
t.Error("expected Container pod addition with Workspace Gateway. Got ", objs.PodAdditions)
}
if len(objs.PodAdditions.Volumes) != 1 || objs.PodAdditions.Volumes[0].Name != wsGatewayName {
t.Error("expected Volume pod addition for workspace gateway. Got ", objs.PodAdditions)
}
assert.Len(t, objs.PodAdditions.Containers, 1)
assert.Equal(t, objs.PodAdditions.Containers[0].Name, wsGatewayName)

assert.Len(t, objs.PodAdditions.Volumes, 1)
assert.Equal(t, objs.PodAdditions.Volumes[0].Name, wsGatewayName)
})

t.Run("traefikConfig", func(t *testing.T) {
cms := &corev1.ConfigMapList{}
cl.List(context.TODO(), cms)

if len(cms.Items) != 2 {
t.Errorf("there should be 2 configmaps created for the gateway config of the workspace but there were: %d", len(cms.Items))
}
assert.Len(t, cms.Items, 2)

var workspaceMainCfg *corev1.ConfigMap
var workspaceCfg *corev1.ConfigMap
Expand All @@ -364,72 +355,60 @@ func TestCreateRelocatedObjectsOpenshift(t *testing.T) {
}
}

if workspaceMainCfg == nil {
t.Fatalf("traefik configuration for the workspace not found")
}
assert.NotNil(t, workspaceMainCfg, "traefik configuration for the workspace not found")

traefikMainWorkspaceConfig := workspaceMainCfg.Data["wsid.yml"]

if len(traefikMainWorkspaceConfig) == 0 {
t.Fatal("No traefik config file found in the main workspace config configmap")
}
assert.NotEmpty(t, traefikMainWorkspaceConfig, "No traefik config file found in the main workspace config configmap")

traefikWorkspaceConfig := workspaceCfg.Data["workspace.yml"]
if len(traefikWorkspaceConfig) == 0 {
t.Fatal("No traefik config file found in the workspace config configmap")
}
assert.NotEmpty(t, traefikWorkspaceConfig, "No traefik config file found in the workspace config configmap")

workspaceConfig := gateway.TraefikConfig{}
if err := yaml.Unmarshal([]byte(traefikWorkspaceConfig), &workspaceConfig); err != nil {
t.Fatal(err)
}

if len(workspaceConfig.HTTP.Routers) != 1 {
t.Fatalf("Expected 1 traefik routers but got %d", len(workspaceConfig.HTTP.Routers))
}
assert.NoError(t, yaml.Unmarshal([]byte(traefikWorkspaceConfig), &workspaceConfig))

wsid := "wsid-m1-9999"
if _, ok := workspaceConfig.HTTP.Routers[wsid]; !ok {
t.Fatal("traefik config doesn't contain expected workspace configuration")
}

if len(workspaceConfig.HTTP.Routers[wsid].Middlewares) != 2 {
t.Fatalf("Expected 2 middlewares in router but got '%d'", len(workspaceConfig.HTTP.Routers[wsid].Middlewares))
}
assert.Contains(t, workspaceConfig.HTTP.Routers, wsid)
assert.Len(t, workspaceConfig.HTTP.Routers[wsid].Middlewares, 2)

workspaceMainConfig := gateway.TraefikConfig{}
if err := yaml.Unmarshal([]byte(traefikMainWorkspaceConfig), &workspaceMainConfig); err != nil {
t.Fatal(err)
}

if len(workspaceMainConfig.HTTP.Routers) != 1 {
t.Fatalf("Expected one route in main route config but got '%d'", len(workspaceMainConfig.HTTP.Routers))
}

if len(workspaceMainConfig.HTTP.Middlewares) != 3 {
t.Fatalf("Expected 3 middlewares set but got '%d'", len(workspaceConfig.HTTP.Middlewares))
}
assert.NoError(t, yaml.Unmarshal([]byte(traefikMainWorkspaceConfig), &workspaceMainConfig))
assert.Len(t, workspaceMainConfig.HTTP.Middlewares, 3)

wsid = "wsid"
mwares := []string{
wsid + gateway.AuthMiddlewareSuffix,
wsid + gateway.StripPrefixMiddlewareSuffix,
wsid + gateway.HeaderRewriteMiddlewareSuffix}
for _, mware := range mwares {
if _, ok := workspaceMainConfig.HTTP.Middlewares[mware]; !ok {
t.Fatalf("traefik config doesn't set middleware '%s'", mware)
}
assert.Contains(t, workspaceMainConfig.HTTP.Middlewares, mware)

found := false
for _, r := range workspaceMainConfig.HTTP.Routers[wsid].Middlewares {
if r == mware {
found = true
}
}
if !found {
t.Fatalf("traefik config route doesn't set middleware '%s'", mware)
}
assert.Truef(t, found, "traefik config route doesn't set middleware '%s'", mware)
}

t.Run("testHealthzEndpointInMainWorkspaceRoute", func(t *testing.T) {
healthzName := "wsid-healthz"
assert.Contains(t, workspaceMainConfig.HTTP.Routers, healthzName)
assert.Equal(t, workspaceMainConfig.HTTP.Routers[healthzName].Service, wsid)
assert.Equal(t, workspaceMainConfig.HTTP.Routers[healthzName].Rule, "Path(`/wsid/m1/9999/healthz`)")
assert.NotContains(t, workspaceMainConfig.HTTP.Routers[healthzName].Middlewares, "wsid"+gateway.AuthMiddlewareSuffix)
assert.Contains(t, workspaceMainConfig.HTTP.Routers[healthzName].Middlewares, "wsid"+gateway.StripPrefixMiddlewareSuffix)
assert.Contains(t, workspaceMainConfig.HTTP.Routers[healthzName].Middlewares, "wsid"+gateway.HeaderRewriteMiddlewareSuffix)
})

t.Run("testHealthzEndpointInWorkspaceRoute", func(t *testing.T) {
healthzName := "wsid-m1-9999-healthz"
assert.Contains(t, workspaceConfig.HTTP.Routers, healthzName)
assert.Equal(t, workspaceConfig.HTTP.Routers[healthzName].Service, healthzName)
assert.Equal(t, workspaceConfig.HTTP.Routers[healthzName].Rule, "Path(`/m1/9999/healthz`)")
assert.NotContains(t, workspaceConfig.HTTP.Routers[healthzName].Middlewares, healthzName+gateway.AuthMiddlewareSuffix)
assert.Contains(t, workspaceConfig.HTTP.Routers[healthzName].Middlewares, healthzName+gateway.StripPrefixMiddlewareSuffix)
})
})
}

Expand Down
12 changes: 11 additions & 1 deletion pkg/deploy/gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestRandomCookieSecret(t *testing.T) {
}
}

func TestOauthProxyConfigUnauthorizedRegistries(t *testing.T) {
func TestOauthProxyConfigUnauthorizedPaths(t *testing.T) {
t.Run("no skip auth", func(t *testing.T) {
configmap := getGatewayOauthProxyConfigSpec(&orgv1.CheCluster{
Spec: orgv1.CheClusterSpec{
Expand Down Expand Up @@ -242,6 +242,16 @@ func TestOauthProxyConfigUnauthorizedRegistries(t *testing.T) {
t.Error("oauth config should skip auth for plugin and devfile registry.", config)
}
})

t.Run("skip '/healthz' path", func(t *testing.T) {
configmap := getGatewayOauthProxyConfigSpec(&orgv1.CheCluster{
Spec: orgv1.CheClusterSpec{
Auth: orgv1.CheClusterSpecAuth{
NativeUserMode: util.NewBoolPointer(true),
}}}, "blabol")
config := configmap.Data["oauth-proxy.cfg"]
assert.Contains(t, config, "/healthz$")
})
}

func TestTokenValidityCheckOnOpenShiftNativeUser(t *testing.T) {
Expand Down

0 comments on commit 8b6a98b

Please sign in to comment.