Skip to content

Commit

Permalink
Fix unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Rizwana777 <rizwananaaz177@gmail.com>
  • Loading branch information
Rizwana777 committed Jan 12, 2024
1 parent 79826b5 commit b1f9deb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
12 changes: 6 additions & 6 deletions controllers/argocd/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
26 changes: 9 additions & 17 deletions controllers/argocd/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
})
Expand All @@ -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{
Expand All @@ -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 != "" {
Expand Down

0 comments on commit b1f9deb

Please sign in to comment.