From 0dc0a92591f61e6056f1bcb92f08bc71c3c62891 Mon Sep 17 00:00:00 2001 From: Manuel Kieweg Date: Tue, 25 Jun 2024 09:35:08 +0000 Subject: [PATCH] add apoplication controller test Signed-off-by: Manuel Kieweg --- controller/appcontroller_test.go | 75 ++++++++++++++++++++++++++++++++ controller/health_test.go | 9 +++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/controller/appcontroller_test.go b/controller/appcontroller_test.go index 8d74a4ebb8f56..0230142697bc8 100644 --- a/controller/appcontroller_test.go +++ b/controller/appcontroller_test.go @@ -12,6 +12,7 @@ import ( "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/apimachinery/pkg/labels" "k8s.io/client-go/rest" clustercache "github.com/argoproj/gitops-engine/pkg/cache" @@ -25,6 +26,7 @@ import ( "github.com/argoproj/gitops-engine/pkg/utils/kube" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + v1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" apierr "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -1515,6 +1517,79 @@ func TestUpdateReconciledAt(t *testing.T) { }) } +func TestUpdateHealthStatusTransitionTime(t *testing.T) { + deployment := kube.MustToUnstructured(&v1.Deployment{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "apps/v1", + Kind: "Deployment", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "demo", + Namespace: "default", + }, + }) + testCases := []struct { + name string + app *v1alpha1.Application + configMapData map[string]string + expectedStatus health.HealthStatusCode + }{ + { + name: "Degraded to Healthy", + app: newFakeAppWithHealthAndTime(health.HealthStatusDegraded, testTimestamp), + configMapData: map[string]string{ + "resource.customizations": ` +apps/Deployment: + health.lua: | + hs = {} + hs.status = "Healthy" + hs.message = "" + return hs`, + }, + expectedStatus: health.HealthStatusHealthy, + }, + { + name: "Healthy to Degraded", + app: newFakeAppWithHealthAndTime(health.HealthStatusHealthy, testTimestamp), + configMapData: map[string]string{ + "resource.customizations": ` +apps/Deployment: + health.lua: | + hs = {} + hs.status = "Degraded" + hs.message = "" + return hs`, + }, + expectedStatus: health.HealthStatusDegraded, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + ctrl := newFakeController(&fakeData{ + apps: []runtime.Object{tc.app, &defaultProj}, + manifestResponse: &apiclient.ManifestResponse{ + Manifests: []string{}, + Namespace: test.FakeDestNamespace, + Server: test.FakeClusterURL, + Revision: "abc123", + }, + managedLiveObjs: map[kube.ResourceKey]*unstructured.Unstructured{ + kube.GetResourceKey(deployment): deployment, + }, + configMapData: tc.configMapData, + }, nil) + + ctrl.processAppRefreshQueueItem() + apps, err := ctrl.appLister.List(labels.Everything()) + assert.NoError(t, err) + assert.NotEmpty(t, apps) + assert.Equal(t, tc.expectedStatus, apps[0].Status.Health.Status) + assert.NotEqual(t, testTimestamp, apps[0].Status.Health.LastTransitionTime) + }) + } +} + func TestProjectErrorToCondition(t *testing.T) { app := newFakeApp() app.Spec.Project = "wrong project" diff --git a/controller/health_test.go b/controller/health_test.go index d2282ca7cc65d..3b3d6c8f3efcb 100644 --- a/controller/health_test.go +++ b/controller/health_test.go @@ -22,7 +22,13 @@ import ( ) var ( - app = &appv1.Application{} + app = &appv1.Application{ + Status: appv1.ApplicationStatus{ + Health: appv1.HealthStatus{ + LastTransitionTime: metav1.NewTime(time.Date(2020, time.January, 1, 12, 0, 0, 0, time.UTC)), + }, + }, + } testTimestamp = metav1.NewTime(time.Date(2020, time.January, 1, 12, 0, 0, 0, time.UTC)) ) @@ -64,7 +70,6 @@ func TestSetApplicationHealth(t *testing.T) { resourceStatuses := initStatuses(resources) // Populate health status resourceStatuses[0].Health.Status = health.HealthStatusHealthy - app.Status.Health.LastTransitionTime = testTimestamp healthStatus, err := setApplicationHealth(resources, resourceStatuses, lua.ResourceHealthOverrides{}, app, true) firstHealthStatusTransitionTime := healthStatus.LastTransitionTime