Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tests #835

Merged
merged 2 commits into from
Jan 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions controllers/applicationhealthcomment_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"context"
"time"

argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
Expand All @@ -11,11 +12,9 @@ import (
)

var _ = Describe("Application health comment controller", func() {
const timeout = time.Second * 3
const interval = time.Millisecond * 250
var app argocdv1alpha1.Application

BeforeEach(func() {
BeforeEach(func(ctx context.Context) {
app = argocdv1alpha1.Application{
TypeMeta: metav1.TypeMeta{
APIVersion: "argoproj.io/v1alpha1",
Expand All @@ -42,7 +41,7 @@ var _ = Describe("Application health comment controller", func() {
})

Context("When an application is healthy", func() {
It("Should notify a comment once", func() {
It("Should notify a comment once", func(ctx context.Context) {
By("Updating the application to progressing")
app.Status = argocdv1alpha1.ApplicationStatus{
Health: argocdv1alpha1.HealthStatus{
Expand All @@ -62,7 +61,7 @@ var _ = Describe("Application health comment controller", func() {
By("Updating the application to healthy")
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.Comments.CountBy(200) }, timeout, interval).Should(Equal(1))
Eventually(func() int { return githubMock.Comments.CountBy(200) }).Should(Equal(1))

By("Updating the application to progressing")
app.Status.Health.Status = health.HealthStatusProgressing
Expand All @@ -72,11 +71,11 @@ var _ = Describe("Application health comment controller", func() {
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Consistently(func() int { return githubMock.Comments.CountBy(200) }, 100*time.Millisecond).Should(Equal(1))
})
}, SpecTimeout(3*time.Second))
})

Context("When an application is degraded and then healthy", func() {
It("Should notify a comment for degraded and healthy", func() {
It("Should notify a comment for degraded and healthy", func(ctx context.Context) {
By("Updating the application to progressing")
app.Status = argocdv1alpha1.ApplicationStatus{
Health: argocdv1alpha1.HealthStatus{
Expand All @@ -96,12 +95,12 @@ var _ = Describe("Application health comment controller", func() {
By("Updating the application to degraded")
app.Status.Health.Status = health.HealthStatusDegraded
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.Comments.CountBy(201) }, timeout, interval).Should(Equal(1))
Eventually(func() int { return githubMock.Comments.CountBy(201) }).Should(Equal(1))

By("Updating the application to healthy")
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.Comments.CountBy(201) }, timeout, interval).Should(Equal(2))
})
Eventually(func() int { return githubMock.Comments.CountBy(201) }).Should(Equal(2))
}, SpecTimeout(3*time.Second))
})
})
33 changes: 16 additions & 17 deletions controllers/applicationhealthdeployment_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"context"
"time"

argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
Expand All @@ -14,11 +15,9 @@ import (
)

var _ = Describe("Application health deployment controller", func() {
const timeout = time.Second * 3
const interval = time.Millisecond * 250
var app argocdv1alpha1.Application

BeforeEach(func() {
BeforeEach(func(ctx context.Context) {
app = argocdv1alpha1.Application{
TypeMeta: metav1.TypeMeta{
APIVersion: "argoproj.io/v1alpha1",
Expand Down Expand Up @@ -60,7 +59,7 @@ var _ = Describe("Application health deployment controller", func() {
})

Context("When an application is healthy", func() {
It("Should notify a deployment status once", func() {
It("Should notify a deployment status once", func(ctx context.Context) {
githubMock.DeploymentStatuses.SetResponse(999300, []*github.DeploymentStatus{})

By("Updating the deployment annotation")
Expand All @@ -72,12 +71,12 @@ var _ = Describe("Application health deployment controller", func() {
By("Updating the application to degraded")
app.Status.Health.Status = health.HealthStatusDegraded
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999300) }, timeout, interval).Should(Equal(1))
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999300) }).Should(Equal(1))

By("Updating the application to healthy")
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999300) }, timeout, interval).Should(Equal(2))
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999300) }).Should(Equal(2))

By("Updating the application to progressing")
app.Status.Health.Status = health.HealthStatusProgressing
Expand All @@ -87,11 +86,11 @@ var _ = Describe("Application health deployment controller", func() {
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Consistently(func() int { return githubMock.DeploymentStatuses.CountBy(999300) }, "100ms").Should(Equal(2))
})
}, SpecTimeout(3*time.Second))
})

Context("When the deployment annotation is updated and then the application becomes healthy", func() {
It("Should notify a deployment status", func() {
It("Should notify a deployment status", func(ctx context.Context) {
githubMock.DeploymentStatuses.SetResponse(999301, []*github.DeploymentStatus{})

By("Updating the deployment annotation")
Expand All @@ -118,12 +117,12 @@ var _ = Describe("Application health deployment controller", func() {
By("Updating the application to healthy")
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999301) }, timeout, interval).Should(Equal(1))
})
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999301) }).Should(Equal(1))
}, SpecTimeout(3*time.Second))
})

Context("When an application became healthy before the deployment annotation is updated", func() {
It("Should notify a deployment status when the deployment annotation is valid", func() {
It("Should notify a deployment status when the deployment annotation is valid", func(ctx context.Context) {
githubMock.DeploymentStatuses.SetResponse(999302, []*github.DeploymentStatus{})

By("Updating the deployment annotation")
Expand All @@ -142,7 +141,7 @@ var _ = Describe("Application health deployment controller", func() {
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/int128/manifests/deployments/999302",
}
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999302) }, timeout, interval).Should(Equal(1))
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999302) }).Should(Equal(1))

By("Deleting the old deployment")
githubMock.DeploymentStatuses.SetResponse(999302, nil)
Expand All @@ -163,11 +162,11 @@ var _ = Describe("Application health deployment controller", func() {
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/int128/manifests/deployments/999303",
}
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999303) }, timeout, interval).Should(Equal(1))
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999303) }).Should(Equal(1))
Expect(githubMock.DeploymentStatuses.CountBy(999302)).Should(Equal(1))
})
}, SpecTimeout(3*time.Second))

It("Should retry a deployment status until timeout", func() {
It("Should retry a deployment status until timeout", func(ctx context.Context) {
By("Updating the deployment annotation")
app.Annotations = map[string]string{
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/int128/manifests/deployments/999999",
Expand All @@ -194,7 +193,7 @@ var _ = Describe("Application health deployment controller", func() {
"reason": "DeploymentNotFoundRetryTimeout",
})).Should(Succeed())
g.Expect(eventList.Items).Should(HaveLen(1))
}, timeout, interval)
})
})
}, SpecTimeout(3*time.Second))
})
})
21 changes: 10 additions & 11 deletions controllers/applicationphasecomment_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"context"
"time"

argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
Expand All @@ -11,11 +12,9 @@ import (
)

var _ = Describe("Application phase controller", func() {
const timeout = time.Second * 3
const interval = time.Millisecond * 250
var app argocdv1alpha1.Application

BeforeEach(func() {
BeforeEach(func(ctx context.Context) {
app = argocdv1alpha1.Application{
TypeMeta: metav1.TypeMeta{
APIVersion: "argoproj.io/v1alpha1",
Expand All @@ -42,7 +41,7 @@ var _ = Describe("Application phase controller", func() {
})

Context("When an application is synced", func() {
It("Should notify a comment", func() {
It("Should notify a comment", func(ctx context.Context) {
By("Updating the application to running")
app.Status = argocdv1alpha1.ApplicationStatus{
OperationState: &argocdv1alpha1.OperationState{
Expand All @@ -56,17 +55,17 @@ var _ = Describe("Application phase controller", func() {
},
}
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.Comments.CountBy(100) }, timeout, interval).Should(Equal(1))
Eventually(func() int { return githubMock.Comments.CountBy(100) }).Should(Equal(1))

By("Updating the application to succeeded")
app.Status.OperationState.Phase = synccommon.OperationSucceeded
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.Comments.CountBy(100) }, timeout, interval).Should(Equal(2))
})
Eventually(func() int { return githubMock.Comments.CountBy(100) }).Should(Equal(2))
}, SpecTimeout(3*time.Second))
})

Context("When an application sync operation is failed", func() {
It("Should notify a comment", func() {
It("Should notify a comment", func(ctx context.Context) {
By("Updating the application to running")
app.Status = argocdv1alpha1.ApplicationStatus{
OperationState: &argocdv1alpha1.OperationState{
Expand All @@ -80,12 +79,12 @@ var _ = Describe("Application phase controller", func() {
},
}
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.Comments.CountBy(101) }, timeout, interval).Should(Equal(1))
Eventually(func() int { return githubMock.Comments.CountBy(101) }).Should(Equal(1))

By("Updating the application to failed")
app.Status.OperationState.Phase = synccommon.OperationFailed
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.Comments.CountBy(101) }, timeout, interval).Should(Equal(2))
})
Eventually(func() int { return githubMock.Comments.CountBy(101) }).Should(Equal(2))
}, SpecTimeout(3*time.Second))
})
})
30 changes: 15 additions & 15 deletions controllers/applicationphasedeployment_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"context"
"time"

argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
Expand All @@ -13,11 +14,9 @@ import (
)

var _ = Describe("Application phase controller", func() {
const timeout = time.Second * 3
const interval = time.Millisecond * 250
var app argocdv1alpha1.Application

BeforeEach(func() {
BeforeEach(func(ctx context.Context) {
app = argocdv1alpha1.Application{
TypeMeta: metav1.TypeMeta{
APIVersion: "argoproj.io/v1alpha1",
Expand All @@ -44,7 +43,7 @@ var _ = Describe("Application phase controller", func() {
})

Context("When an application is synced", func() {
It("Should notify a deployment status", func() {
It("Should notify a deployment status", func(ctx context.Context) {
githubMock.DeploymentStatuses.SetResponse(999100, []*github.DeploymentStatus{})

By("Updating the deployment annotation")
Expand All @@ -66,17 +65,17 @@ var _ = Describe("Application phase controller", func() {
},
}
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999100) }, timeout, interval).Should(Equal(1))
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999100) }).Should(Equal(1))

By("Updating the application to succeeded")
app.Status.OperationState.Phase = synccommon.OperationSucceeded
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999100) }, timeout, interval).Should(Equal(2))
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999100) }).Should(Equal(2))

By("Updating the application to healthy")
app.Status.Health.Status = health.HealthStatusHealthy
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999100) }, timeout, interval).Should(Equal(3))
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999100) }).Should(Equal(3))

By("Updating the application to running")
app.Status.OperationState.Phase = synccommon.OperationRunning
Expand All @@ -86,11 +85,11 @@ var _ = Describe("Application phase controller", func() {
app.Status.OperationState.Phase = synccommon.OperationSucceeded
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Consistently(func() int { return githubMock.DeploymentStatuses.CountBy(999100) }, "100ms").Should(Equal(3))
})
}, SpecTimeout(3*time.Second))
})

Context("When an application sync operation is failed", func() {
It("Should notify a deployment status", func() {
It("Should notify a deployment status", func(ctx context.Context) {
githubMock.DeploymentStatuses.SetResponse(999101, []*github.DeploymentStatus{})

By("Updating the deployment annotation")
Expand All @@ -112,17 +111,17 @@ var _ = Describe("Application phase controller", func() {
},
}
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999101) }, timeout, interval).Should(Equal(1))
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999101) }).Should(Equal(1))

By("Updating the application to failed")
app.Status.OperationState.Phase = synccommon.OperationFailed
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999101) }, timeout, interval).Should(Equal(2))
})
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999101) }).Should(Equal(2))
}, SpecTimeout(3*time.Second))
})

Context("When an application was synced before the deployment annotation is updated", func() {
It("Should skip the notification", func() {
It("Should skip the notification", func(ctx context.Context) {
githubMock.DeploymentStatuses.SetResponse(999102, []*github.DeploymentStatus{})

By("Updating the deployment annotation")
Expand Down Expand Up @@ -150,7 +149,8 @@ var _ = Describe("Application phase controller", func() {
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/int128/manifests/deployments/999102",
}
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999102) }, timeout, interval).Should(Equal(1))
})
// this test depends on requeueIntervalWhenDeploymentNotFound and takes longer time
Eventually(func() int { return githubMock.DeploymentStatuses.CountBy(999102) }, 3*time.Second).Should(Equal(1))
}, SpecTimeout(5*time.Second))
})
})
20 changes: 8 additions & 12 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ import (

var (
k8sClient client.Client
testEnv *envtest.Environment
ctx context.Context
cancel context.CancelFunc
githubMock GithubMock
)

Expand All @@ -64,7 +61,6 @@ var _ = BeforeSuite(func() {
func(o *zap.Options) {
o.TimeEncoder = zapcore.RFC3339NanoTimeEncoder
}))
ctx, cancel = context.WithCancel(context.TODO())

By("find the CRD of Argo CD Application resource in Go module")
crdPaths, err := filepath.Glob(filepath.Join(
Expand All @@ -76,11 +72,18 @@ var _ = BeforeSuite(func() {

By("bootstrapping test environment")
crdPaths = append(crdPaths, filepath.Join("..", "config", "crd", "bases"))
testEnv = &envtest.Environment{
testEnv := &envtest.Environment{
CRDDirectoryPaths: crdPaths,
ErrorIfCRDPathMissing: true,
}

ctx, cancel := context.WithCancel(context.TODO())
DeferCleanup(func() {
cancel()
By("tearing down the test environment")
Expect(testEnv.Stop()).Should(Succeed())
})

cfg, err := testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
Expand Down Expand Up @@ -158,10 +161,3 @@ var _ = BeforeSuite(func() {
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
}()
})

var _ = AfterSuite(func() {
cancel()
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})