Skip to content

Commit

Permalink
add apoplication controller test
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Kieweg <mail@manuelkieweg.de>
  • Loading branch information
mkieweg committed Jun 25, 2024
1 parent 07df57a commit 0dc0a92
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
75 changes: 75 additions & 0 deletions controller/appcontroller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
9 changes: 7 additions & 2 deletions controller/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0dc0a92

Please sign in to comment.