Skip to content

Commit

Permalink
Host attribute unit test updates
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan West <[email protected]>
  • Loading branch information
jgwest committed May 22, 2024
1 parent d4be34e commit 73d27d7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
3 changes: 3 additions & 0 deletions controllers/argocd/keycloak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ func TestNewKeycloakTemplate_testRouteWhenHostIsEmpty(t *testing.T) {
a.Spec.SSO = &argoproj.ArgoCDSSOSpec{
Provider: "keycloak",
}

assert.True(t, a.Spec.SSO.Keycloak == nil || a.Spec.SSO.Keycloak.Host == "", "host must be empty, or keycloak must be nil (which implies host is empty)")

route := getKeycloakRouteTemplate(fakeNs, *a)
assert.Equal(t, route.Name, "${APPLICATION_NAME}")
assert.Equal(t, route.Namespace, fakeNs)
Expand Down
54 changes: 33 additions & 21 deletions controllers/argocd/sso_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package argocd
import (
"context"
"errors"
"strings"
"testing"

oappsv1 "github.com/openshift/api/apps/v1"
Expand All @@ -27,7 +28,6 @@ import (
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestReconcile_testKeycloakInstanceResources(t *testing.T) {
}
assert.Equal(t, deployment.Labels, testLabels)

testSelector := &v1.LabelSelector{
testSelector := &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": defaultKeycloakIdentifier,
},
Expand Down Expand Up @@ -472,6 +472,12 @@ func TestReconcile_testKeycloakRouteHost(t *testing.T) {
Host: "sso.test.example.com",
}

// Set templateAPIFound to true, to simulate running on an OpenShift machine
templateAPIFound = true
deploymentConfigAPIFound = true
ssoConfigLegalStatus = ""
defer removeTemplateAPI()

resObjs := []client.Object{a}
subresObjs := []client.Object{a}
runtimeObjs := []runtime.Object{}
Expand All @@ -483,26 +489,32 @@ func TestReconcile_testKeycloakRouteHost(t *testing.T) {

assert.NoError(t, r.reconcileSSO(a))

// Keycloak Route
route := getKeycloakRouteTemplate(a.Namespace, *a)

expectedRoute := &routev1.Route{
// Calls to reconcileSSO will create a TemplateInstance
templ := templatev1.TemplateInstance{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"application": "${APPLICATION_NAME}"},
Name: "${APPLICATION_NAME}",
Namespace: a.Namespace,
Annotations: map[string]string{"description": "Route for application's https service"},
},
TypeMeta: metav1.TypeMeta{APIVersion: "v1", Kind: "Route"},
Spec: routev1.RouteSpec{
Host: getKeycloakOpenshiftHost(a.Spec.SSO.Keycloak),
TLS: &routev1.TLSConfig{
Termination: "reencrypt",
},
To: routev1.RouteTargetReference{
Name: "${APPLICATION_NAME}",
},
Name: defaultTemplateIdentifier,
Namespace: a.Namespace,
},
}
assert.Equal(t, route, expectedRoute)
err := r.Client.Get(context.Background(), client.ObjectKeyFromObject(&templ), &templ)
assert.NoError(t, err)

// TemplateInstance contains a set of Objects, but we only care about the Route
matchFound := false
for _, obj := range templ.Spec.Template.Objects {

strVal := string(obj.Raw)

// Look for the Route object within the TemplateInstance
if strings.Contains(strVal, "\"kind\":\"Route\"") {

// Make sure the Route object contains the host
assert.Contains(t, strVal, "sso.test.example.com", "the Route portion of the template should contain the host value from above")
matchFound = true
}

}

assert.True(t, matchFound)

}
3 changes: 2 additions & 1 deletion controllers/argocd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ func TestReconcileArgoCD_reconcileStatusSSO(t *testing.T) {
wantSSOStatus: "Failed",
},
{
name: "both dex and keycloak configured and keycloak host is empty",
name: "both dex and keycloak configured, and keycloak host is empty",
argoCD: makeTestArgoCD(func(cr *argoproj.ArgoCD) {
cr.Spec.SSO = &argoproj.ArgoCDSSOSpec{
Provider: argoproj.SSOProviderTypeKeycloak,
Dex: &argoproj.ArgoCDDexSpec{
OpenShiftOAuth: true,
},
Keycloak: &argoproj.ArgoCDKeycloakSpec{},
}
}),
wantSSOStatus: "Failed",
Expand Down

0 comments on commit 73d27d7

Please sign in to comment.