Skip to content

Commit

Permalink
refactor: use generateName and patch update (#718)
Browse files Browse the repository at this point in the history
* refactor: use generateName and patch update

* refactor: extract BeforeEach
  • Loading branch information
int128 authored Sep 19, 2022
1 parent 2b70e4b commit 87f53bf
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 168 deletions.
113 changes: 52 additions & 61 deletions controllers/applicationhealthcomment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,92 +9,83 @@ import (
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("Application health comment controller", func() {
const timeout = time.Second * 3
const interval = time.Millisecond * 250
appKey := types.NamespacedName{Namespace: "default", Name: "app2"}
var app argocdv1alpha1.Application
var appKey types.NamespacedName

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",
BeforeEach(func() {
app = argocdv1alpha1.Application{
TypeMeta: metav1.TypeMeta{
APIVersion: "argoproj.io/v1alpha1",
Kind: "Application",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "fixture-",
Namespace: "default",
},
Spec: argocdv1alpha1.ApplicationSpec{
Project: "default",
Source: argocdv1alpha1.ApplicationSource{
RepoURL: "https://github.com/int128/argocd-commenter.git",
Path: "test",
TargetRevision: "main",
},
ObjectMeta: metav1.ObjectMeta{
Name: appKey.Name,
Namespace: appKey.Namespace,
Destination: argocdv1alpha1.ApplicationDestination{
Server: "https://kubernetes.default.svc",
Namespace: "default",
},
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())
},
}
Expect(k8sClient.Create(ctx, &app)).Should(Succeed())
appKey = types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
})

Context("When an application is healthy", func() {
It("Should notify a comment once", 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 = argocdv1alpha1.ApplicationStatus{
Health: argocdv1alpha1.HealthStatus{
Status: health.HealthStatusProgressing,
},
OperationState: &argocdv1alpha1.OperationState{
StartedAt: metav1.Now(),
Operation: argocdv1alpha1.Operation{
Sync: &argocdv1alpha1.SyncOperation{
Revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
patch := client.MergeFrom(app.DeepCopy())
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())
},
}
Expect(k8sClient.Patch(ctx, &app, patch)).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())
patch = client.MergeFrom(app.DeepCopy())
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Patch(ctx, &app, patch)).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())
patch = client.MergeFrom(app.DeepCopy())
app.Status.Health.Status = health.HealthStatusProgressing
Expect(k8sClient.Patch(ctx, &app, patch)).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())
patch = client.MergeFrom(app.DeepCopy())
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Patch(ctx, &app, patch)).Should(Succeed())

Consistently(func() int {
return notificationMock.Comments.CountBy(appKey)
}, 1*time.Second, interval).Should(Equal(1))
}, 100*time.Millisecond).Should(Equal(1))
})
})
})
117 changes: 54 additions & 63 deletions controllers/applicationhealthdeployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,95 +9,86 @@ import (
. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("Application health deployment controller", func() {
const timeout = time.Second * 3
const interval = time.Millisecond * 250
appKey := types.NamespacedName{Namespace: "default", Name: "app3"}
var app argocdv1alpha1.Application
var appKey types.NamespacedName

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",
BeforeEach(func() {
app = argocdv1alpha1.Application{
TypeMeta: metav1.TypeMeta{
APIVersion: "argoproj.io/v1alpha1",
Kind: "Application",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "fixture-",
Namespace: "default",
Annotations: map[string]string{
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/int128/argocd-commenter/deployments/1234567890",
},
ObjectMeta: metav1.ObjectMeta{
Name: appKey.Name,
Namespace: appKey.Namespace,
Annotations: map[string]string{
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/int128/argocd-commenter/deployments/1234567890",
},
},
Spec: argocdv1alpha1.ApplicationSpec{
Project: "default",
Source: argocdv1alpha1.ApplicationSource{
RepoURL: "https://github.com/int128/argocd-commenter.git",
Path: "test",
TargetRevision: "main",
},
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",
},
Destination: argocdv1alpha1.ApplicationDestination{
Server: "https://kubernetes.default.svc",
Namespace: "default",
},
})).Should(Succeed())
},
}
Expect(k8sClient.Create(ctx, &app)).Should(Succeed())
appKey = types.NamespacedName{Namespace: app.Namespace, Name: app.Name}
})

Context("When an application is healthy", func() {
It("Should notify a deployment status once", 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 = argocdv1alpha1.ApplicationStatus{
Health: argocdv1alpha1.HealthStatus{
Status: health.HealthStatusProgressing,
},
OperationState: &argocdv1alpha1.OperationState{
StartedAt: metav1.Now(),
Operation: argocdv1alpha1.Operation{
Sync: &argocdv1alpha1.SyncOperation{
Revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
patch := client.MergeFrom(app.DeepCopy())
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())
},
}
Expect(k8sClient.Patch(ctx, &app, patch)).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())
patch = client.MergeFrom(app.DeepCopy())
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Patch(ctx, &app, patch)).Should(Succeed())

Eventually(func() int {
return notificationMock.DeploymentStatuses.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())
patch = client.MergeFrom(app.DeepCopy())
app.Status.Health.Status = health.HealthStatusProgressing
Expect(k8sClient.Patch(ctx, &app, patch)).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())
patch = client.MergeFrom(app.DeepCopy())
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Patch(ctx, &app, patch)).Should(Succeed())

Consistently(func() int {
return notificationMock.DeploymentStatuses.CountBy(appKey)
}, 1*time.Second, interval).Should(Equal(1))
}, 100*time.Millisecond).Should(Equal(1))
})
})
})
Loading

0 comments on commit 87f53bf

Please sign in to comment.