From 1d9bab83cb3f185e941a7cd11b4c917dad81fef8 Mon Sep 17 00:00:00 2001 From: Rizwana777 Date: Fri, 12 Jan 2024 14:46:53 +0530 Subject: [PATCH] Fix unit test Signed-off-by: Rizwana777 --- controllers/argocd/route.go | 12 ++++++------ controllers/argocd/route_test.go | 26 +++++++++----------------- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/controllers/argocd/route.go b/controllers/argocd/route.go index 00b88a774..26f970f93 100644 --- a/controllers/argocd/route.go +++ b/controllers/argocd/route.go @@ -389,6 +389,11 @@ func shortenHostname(hostname string) (string, error) { // Check and truncate the FIRST label if longer than 63 characters if len(labels[0]) > 63 { labels[0] = labels[0][:63] + + // Check if the FIRST label is < 20 after truncating and return an error if true + if len(labels[0]) < 20 { + return "", fmt.Errorf("first label length is less than 20 characters") + } } // Check other labels and return an error if any is longer than 63 characters @@ -402,14 +407,9 @@ func shortenHostname(hostname string) (string, error) { resultHostname := strings.Join(labels, ".") // Check and shorten the overall hostname if > 253 characters - if len(resultHostname) > 255 { + if len(resultHostname) > 253 { resultHostname = resultHostname[:253] } - // Check if the FIRST label is < 20 and return an error if true - if len(labels[0]) < 20 { - return "", fmt.Errorf("first label length is less than 20 characters") - } - return resultHostname, nil } diff --git a/controllers/argocd/route_test.go b/controllers/argocd/route_test.go index 8bad48708..e8c66de07 100644 --- a/controllers/argocd/route_test.go +++ b/controllers/argocd/route_test.go @@ -213,17 +213,20 @@ func TestReconcileRouteUnsetsInsecure(t *testing.T) { } } -func TestReconcileRoute(t *testing.T) { +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.Server.Host = hostname a.Spec.ApplicationSet = &argoproj.ArgoCDApplicationSet{ WebhookServer: argoproj.WebhookServerSpec{ Route: argoproj.ArgoCDRouteSpec{ Enabled: true, }, + Host: hostname, }, } }) @@ -234,18 +237,7 @@ func TestReconcileRoute(t *testing.T) { sch := makeTestReconcilerScheme(argoproj.AddToScheme, configv1.Install, routev1.Install) cl := makeTestReconcilerClient(sch, resObjs, subresObjs, runtimeObjs) r := makeTestReconciler(cl, sch) - name := "argocd-applicationset-controller-webhook" - route := &routev1.Route{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: testNamespace, - }, - Spec: routev1.RouteSpec{ - Host: name + "-openshift-gitops.apps-crc.testing", - }, - } - err := r.Client.Create(context.TODO(), route) - assert.NoError(t, err) + assert.NoError(t, createNamespace(r, argoCD.Namespace, "")) req := reconcile.Request{ @@ -255,15 +247,15 @@ func TestReconcileRoute(t *testing.T) { }, } - _, err = r.Reconcile(context.TODO(), req) + _, err := r.Reconcile(context.TODO(), req) assert.NoError(t, err) loaded := &routev1.Route{} - err = r.Client.Get(ctx, types.NamespacedName{Name: name, Namespace: testNamespace}, loaded) - fatalIfError(t, err, "failed to load route %q: %s", name, err) + 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) wantRoute := &routev1.RouteSpec{ - Host: "argocd-applicationset-controller-webhook-openshift-gitops.apps-", + Host: "myhostnameaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", } if diff := cmp.Diff(wantRoute.Host, loaded.Spec.Host); diff != "" {