-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add test for ApplicationHealthCommentReconciler (#716)
* refactor: add test for ApplicationHealthCommentReconciler * refactor: split Its
- Loading branch information
Showing
4 changed files
with
194 additions
and
47 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
controllers/applicationhealthcomment_controller_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package controllers | ||
|
||
import ( | ||
"time" | ||
|
||
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" | ||
"github.com/argoproj/gitops-engine/pkg/health" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
var _ = Describe("Application health comment controller", func() { | ||
const timeout = time.Second * 3 | ||
const interval = time.Millisecond * 250 | ||
appKey := types.NamespacedName{Namespace: "default", Name: "app2"} | ||
|
||
Context("When an application is healthy", func() { | ||
It("Should notify a comment", func() { | ||
By("By creating an application") | ||
Expect(k8sClient.Create(ctx, &argocdv1alpha1.Application{ | ||
TypeMeta: metav1.TypeMeta{ | ||
APIVersion: "argoproj.io/v1alpha1", | ||
Kind: "Application", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: appKey.Name, | ||
Namespace: appKey.Namespace, | ||
}, | ||
Spec: argocdv1alpha1.ApplicationSpec{ | ||
Project: "default", | ||
Source: argocdv1alpha1.ApplicationSource{ | ||
RepoURL: "https://github.com/int128/argocd-commenter.git", | ||
Path: "test", | ||
TargetRevision: "main", | ||
}, | ||
Destination: argocdv1alpha1.ApplicationDestination{ | ||
Server: "https://kubernetes.default.svc", | ||
Namespace: "default", | ||
}, | ||
}, | ||
})).Should(Succeed()) | ||
|
||
By("By updating the health status to progressing") | ||
Eventually(func(g Gomega) { | ||
var app argocdv1alpha1.Application | ||
g.Expect(k8sClient.Get(ctx, appKey, &app)).Should(Succeed()) | ||
app.Status = argocdv1alpha1.ApplicationStatus{ | ||
Health: argocdv1alpha1.HealthStatus{ | ||
Status: health.HealthStatusProgressing, | ||
}, | ||
OperationState: &argocdv1alpha1.OperationState{ | ||
StartedAt: metav1.Now(), | ||
Operation: argocdv1alpha1.Operation{ | ||
Sync: &argocdv1alpha1.SyncOperation{ | ||
Revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | ||
}, | ||
}, | ||
}, | ||
} | ||
g.Expect(k8sClient.Update(ctx, &app)).Should(Succeed()) | ||
}, timeout, interval).Should(Succeed()) | ||
|
||
By("By updating the health status to healthy") | ||
Eventually(func(g Gomega) { | ||
var app argocdv1alpha1.Application | ||
g.Expect(k8sClient.Get(ctx, appKey, &app)).Should(Succeed()) | ||
app.Status.Health.Status = health.HealthStatusHealthy | ||
g.Expect(k8sClient.Update(ctx, &app)).Should(Succeed()) | ||
}, timeout, interval).Should(Succeed()) | ||
|
||
Eventually(func() int { | ||
return notificationMock.Comments.CountBy(appKey) | ||
}, timeout, interval).Should(Equal(1)) | ||
}) | ||
|
||
It("Should not notify a comment after healthy", func() { | ||
By("By updating the health status to progressing") | ||
Eventually(func(g Gomega) { | ||
var app argocdv1alpha1.Application | ||
g.Expect(k8sClient.Get(ctx, appKey, &app)).Should(Succeed()) | ||
app.Status.Health.Status = health.HealthStatusHealthy | ||
g.Expect(k8sClient.Update(ctx, &app)).Should(Succeed()) | ||
}, timeout, interval).Should(Succeed()) | ||
|
||
By("By updating the health status to healthy") | ||
Eventually(func(g Gomega) { | ||
var app argocdv1alpha1.Application | ||
g.Expect(k8sClient.Get(ctx, appKey, &app)).Should(Succeed()) | ||
app.Status.Health.Status = health.HealthStatusHealthy | ||
g.Expect(k8sClient.Update(ctx, &app)).Should(Succeed()) | ||
}, timeout, interval).Should(Succeed()) | ||
|
||
Consistently(func() int { | ||
return notificationMock.Comments.CountBy(appKey) | ||
}, 1*time.Second, interval).Should(Equal(1)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/int128/argocd-commenter/pkg/notification" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
type EventRecorder struct { | ||
m sync.Mutex | ||
counter map[string]int | ||
} | ||
|
||
func (r *EventRecorder) CountBy(key types.NamespacedName) int { | ||
r.m.Lock() | ||
defer r.m.Unlock() | ||
return r.counter[key.String()] | ||
} | ||
|
||
func (r *EventRecorder) call(event notification.Event) int { | ||
r.m.Lock() | ||
defer r.m.Unlock() | ||
|
||
if r.counter == nil { | ||
r.counter = make(map[string]int) | ||
} | ||
key := types.NamespacedName{Namespace: event.Application.Namespace, Name: event.Application.Name} | ||
r.counter[key.String()]++ | ||
return r.counter[key.String()] | ||
} | ||
|
||
type NotificationMock struct { | ||
Comments EventRecorder | ||
DeploymentStatuses EventRecorder | ||
} | ||
|
||
func (n *NotificationMock) Comment(ctx context.Context, event notification.Event) error { | ||
logger := log.FromContext(ctx) | ||
nth := n.Comments.call(event) | ||
logger.Info("called Comment", "nth", nth) | ||
return nil | ||
} | ||
|
||
func (n *NotificationMock) Deployment(ctx context.Context, event notification.Event) error { | ||
logger := log.FromContext(ctx) | ||
nth := n.DeploymentStatuses.call(event) | ||
logger.Info("called Deployment", "nth", nth) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters