Skip to content

Commit

Permalink
Add unit test for examples for hostname
Browse files Browse the repository at this point in the history
Signed-off-by: Rizwana777 <rizwananaaz177@gmail.com>
  • Loading branch information
Rizwana777 committed Jan 30, 2024
1 parent c60705e commit 1d174c2
Showing 1 changed file with 68 additions and 46 deletions.
114 changes: 68 additions & 46 deletions controllers/argocd/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package argocd

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -220,63 +221,84 @@ func TestReconcileRouteForShorteningHostname(t *testing.T) {
routeAPIFound = true
ctx := context.Background()
logf.SetLogger(ZapLogger(true))
hostname := "myhostnameaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com"

argoCD := makeArgoCD(func(a *argoproj.ArgoCD) {
a.Spec.Server.Route.Enabled = true
a.Spec.ApplicationSet = &argoproj.ArgoCDApplicationSet{
WebhookServer: argoproj.WebhookServerSpec{
Route: argoproj.ArgoCDRouteSpec{
Enabled: true,
},
Host: hostname,
},
}
})

resObjs := []client.Object{argoCD}
subresObjs := []client.Object{argoCD}
runtimeObjs := []runtime.Object{}
sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install)
cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs)
r := makeTestReconciler(cl, sch)

assert.NoError(t, createNamespace(r, argoCD.Namespace, ""))

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: testArgoCDName,
Namespace: testNamespace,
tests := []struct {
testName string
expected string
hostname string
}{
{
testName: "longHostname",
hostname: "myhostnameaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com",
expected: "myhostnameaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com",
},
{
testName: "twentySixLetterHostname",
hostname: "myhostnametwentysixletteraaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com",
expected: "myhostnametwentysixletteraaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com",
},
}

// Check if it returns error when hostname is empty
_, err := r.Reconcile(context.TODO(), req)
assert.EqualError(t, err, "hostname is empty")
for _, v := range tests {
t.Run(v.testName, func(t *testing.T) {

argoCD := makeArgoCD(func(a *argoproj.ArgoCD) {
a.Spec.Server.Route.Enabled = true
a.Spec.ApplicationSet = &argoproj.ArgoCDApplicationSet{
WebhookServer: argoproj.WebhookServerSpec{
Route: argoproj.ArgoCDRouteSpec{
Enabled: true,
},
Host: v.hostname,
},
}
})

resObjs := []client.Object{argoCD}
subresObjs := []client.Object{argoCD}
runtimeObjs := []runtime.Object{}
sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install)
cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs)
r := makeTestReconciler(cl, sch)

assert.NoError(t, createNamespace(r, argoCD.Namespace, ""))

req := reconcile.Request{
NamespacedName: types.NamespacedName{
Name: testArgoCDName,
Namespace: testNamespace,
},
}

// second reconciliation after changing the hostname.
err = r.Client.Get(ctx, req.NamespacedName, argoCD)
fatalIfError(t, err, "failed to load ArgoCD %q: %s", testArgoCDName+"-server", err)
// Check if it returns error when hostname is empty
_, err := r.Reconcile(context.TODO(), req)
assert.EqualError(t, err, "hostname is empty")

argoCD.Spec.Server.Host = hostname
err = r.Client.Update(ctx, argoCD)
fatalIfError(t, err, "failed to update the ArgoCD: %s", err)
// second reconciliation after changing the hostname.
err = r.Client.Get(ctx, req.NamespacedName, argoCD)
fatalIfError(t, err, "failed to load ArgoCD %q: %s", testArgoCDName+"-server", err)

_, err = r.Reconcile(context.TODO(), req)
assert.NoError(t, err)
argoCD.Spec.Server.Host = v.hostname
err = r.Client.Update(ctx, argoCD)
fatalIfError(t, err, "failed to update the ArgoCD: %s", err)

loaded := &routev1.Route{}
err = r.Client.Get(ctx, types.NamespacedName{Name: testArgoCDName + "-server", Namespace: testNamespace}, loaded)
fatalIfError(t, err, "failed to load route %q: %s", testArgoCDName+"-server", err)
_, err = r.Reconcile(context.TODO(), req)
assert.NoError(t, err)

wantRoute := &routev1.RouteSpec{
Host: "myhostnameaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.redhat.com",
}
loaded := &routev1.Route{}
err = r.Client.Get(ctx, types.NamespacedName{Name: testArgoCDName + "-server", Namespace: testNamespace}, loaded)
fatalIfError(t, err, "failed to load route %q: %s", testArgoCDName+"-server", err)

if diff := cmp.Diff(wantRoute.Host, loaded.Spec.Host); diff != "" {
t.Fatalf("failed to reconcile route:\n%s", diff)
}
if diff := cmp.Diff(v.expected, loaded.Spec.Host); diff != "" {
t.Fatalf("failed to reconcile route:\n%s", diff)
}

// Check if first label is greater than 20
labels := strings.Split(loaded.Spec.Host, ".")
assert.True(t, len(labels[0]) > 20)

})
}
}

func makeReconciler(t *testing.T, acd *argoproj.ArgoCD, objs ...runtime.Object) *ReconcileArgoCD {
Expand Down

0 comments on commit 1d174c2

Please sign in to comment.