Skip to content

Commit 7ff2d4a

Browse files
authored
Refactor tests (#1024)
* Refactor tests * Fix
1 parent 3e0c4ec commit 7ff2d4a

File tree

5 files changed

+94
-108
lines changed

5 files changed

+94
-108
lines changed

internal/controller/applicationhealthcomment_controller_test.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,27 @@ import (
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414
)
1515

16-
var _ = Describe("Application health comment controller", func() {
16+
var _ = Describe("Comment on health status changed", func() {
1717
var app argocdv1alpha1.Application
18+
var comment githubmock.Comment
1819

1920
BeforeEach(func(ctx context.Context) {
21+
By("Setting up a comment endpoint")
22+
comment = githubmock.Comment{}
23+
githubServer.AddHandlers(map[string]http.Handler{
24+
"GET /api/v3/repos/test/health-comment/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa101/pulls": githubmock.ListPullRequestsWithCommit(101),
25+
"GET /api/v3/repos/test/health-comment/pulls/101/files": githubmock.ListFiles(),
26+
"POST /api/v3/repos/test/health-comment/issues/101/comments": comment.CreateEndpoint(),
27+
})
28+
29+
By("Creating an application")
2030
app = argocdv1alpha1.Application{
2131
TypeMeta: metav1.TypeMeta{
2232
APIVersion: "argoproj.io/v1alpha1",
2333
Kind: "Application",
2434
},
2535
ObjectMeta: metav1.ObjectMeta{
26-
GenerateName: "fixture-",
36+
GenerateName: "fixture-health-comment-",
2737
Namespace: "default",
2838
},
2939
Spec: argocdv1alpha1.ApplicationSpec{
@@ -44,13 +54,6 @@ var _ = Describe("Application health comment controller", func() {
4454

4555
Context("When an application is healthy", func() {
4656
It("Should notify a comment once", func(ctx context.Context) {
47-
var comment githubmock.Comment
48-
githubServer.AddHandlers(map[string]http.Handler{
49-
"GET /api/v3/repos/test/health-comment/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa101/pulls": githubmock.ListPullRequestsWithCommit(101),
50-
"GET /api/v3/repos/test/health-comment/pulls/101/files": githubmock.ListFiles(),
51-
"POST /api/v3/repos/test/health-comment/issues/101/comments": comment.CreateEndpoint(),
52-
})
53-
5457
By("Updating the application to progressing")
5558
app.Status = argocdv1alpha1.ApplicationStatus{
5659
Health: argocdv1alpha1.HealthStatus{
@@ -85,13 +88,6 @@ var _ = Describe("Application health comment controller", func() {
8588

8689
Context("When an application is degraded and then healthy", func() {
8790
It("Should notify a comment for degraded and healthy", func(ctx context.Context) {
88-
var comment githubmock.Comment
89-
githubServer.AddHandlers(map[string]http.Handler{
90-
"GET /api/v3/repos/test/health-comment/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa102/pulls": githubmock.ListPullRequestsWithCommit(102),
91-
"GET /api/v3/repos/test/health-comment/pulls/102/files": githubmock.ListFiles(),
92-
"POST /api/v3/repos/test/health-comment/issues/102/comments": comment.CreateEndpoint(),
93-
})
94-
9591
By("Updating the application to progressing")
9692
app.Status = argocdv1alpha1.ApplicationStatus{
9793
Health: argocdv1alpha1.HealthStatus{
@@ -101,7 +97,7 @@ var _ = Describe("Application health comment controller", func() {
10197
StartedAt: metav1.Now(),
10298
Operation: argocdv1alpha1.Operation{
10399
Sync: &argocdv1alpha1.SyncOperation{
104-
Revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa102",
100+
Revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa101",
105101
},
106102
},
107103
},

internal/controller/applicationhealthdeployment_controller_test.go

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@ import (
1515
"sigs.k8s.io/controller-runtime/pkg/client"
1616
)
1717

18-
var _ = Describe("Application health deployment controller", func() {
18+
var _ = Describe("Deployment status on health status changed", func() {
1919
var app argocdv1alpha1.Application
20+
var deploymentStatus githubmock.DeploymentStatus
2021

2122
BeforeEach(func(ctx context.Context) {
23+
By("Setting up a deployment status endpoint")
24+
deploymentStatus = githubmock.DeploymentStatus{}
25+
githubServer.AddHandlers(map[string]http.Handler{
26+
"GET /api/v3/repos/test/health-deployment/deployments/101/statuses": deploymentStatus.ListEndpoint(),
27+
"POST /api/v3/repos/test/health-deployment/deployments/101/statuses": deploymentStatus.CreateEndpoint(),
28+
})
29+
30+
By("Creating an application")
2231
app = argocdv1alpha1.Application{
2332
TypeMeta: metav1.TypeMeta{
2433
APIVersion: "argoproj.io/v1alpha1",
2534
Kind: "Application",
2635
},
2736
ObjectMeta: metav1.ObjectMeta{
28-
GenerateName: "fixture-",
37+
GenerateName: "fixture-deployment-status-health-",
2938
Namespace: "default",
3039
},
3140
Spec: argocdv1alpha1.ApplicationSpec{
@@ -43,6 +52,7 @@ var _ = Describe("Application health deployment controller", func() {
4352
}
4453
Expect(k8sClient.Create(ctx, &app)).Should(Succeed())
4554

55+
By("Updating the application to progressing")
4656
app.Status = argocdv1alpha1.ApplicationStatus{
4757
Health: argocdv1alpha1.HealthStatus{
4858
Status: health.HealthStatusProgressing,
@@ -61,12 +71,6 @@ var _ = Describe("Application health deployment controller", func() {
6171

6272
Context("When an application is healthy", func() {
6373
It("Should notify a deployment status once", func(ctx context.Context) {
64-
var deploymentStatus githubmock.DeploymentStatus
65-
githubServer.AddHandlers(map[string]http.Handler{
66-
"GET /api/v3/repos/test/health-deployment/deployments/101/statuses": deploymentStatus.ListEndpoint(),
67-
"POST /api/v3/repos/test/health-deployment/deployments/101/statuses": deploymentStatus.CreateEndpoint(),
68-
})
69-
7074
By("Updating the deployment annotation")
7175
app.Annotations = map[string]string{
7276
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/health-deployment/deployments/101",
@@ -96,12 +100,6 @@ var _ = Describe("Application health deployment controller", func() {
96100

97101
Context("When the deployment annotation is updated and then the application becomes healthy", func() {
98102
It("Should notify a deployment status", func(ctx context.Context) {
99-
var deploymentStatus githubmock.DeploymentStatus
100-
githubServer.AddHandlers(map[string]http.Handler{
101-
"GET /api/v3/repos/test/health-deployment/deployments/102/statuses": deploymentStatus.ListEndpoint(),
102-
"POST /api/v3/repos/test/health-deployment/deployments/102/statuses": deploymentStatus.CreateEndpoint(),
103-
})
104-
105103
By("Updating the deployment annotation")
106104
app.Annotations = map[string]string{
107105
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/health-deployment/deployments/999",
@@ -114,7 +112,7 @@ var _ = Describe("Application health deployment controller", func() {
114112

115113
By("Updating the deployment annotation")
116114
app.Annotations = map[string]string{
117-
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/health-deployment/deployments/102",
115+
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/health-deployment/deployments/101",
118116
}
119117
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
120118
Consistently(func() int { return deploymentStatus.CreateCount() }, "100ms").Should(BeZero())
@@ -132,14 +130,6 @@ var _ = Describe("Application health deployment controller", func() {
132130

133131
Context("When an application became healthy before the deployment annotation is updated", func() {
134132
It("Should notify a deployment status when the deployment annotation is valid", func(ctx context.Context) {
135-
var deploymentStatusOld, deploymentStatusNew githubmock.DeploymentStatus
136-
githubServer.AddHandlers(map[string]http.Handler{
137-
"GET /api/v3/repos/test/health-deployment/deployments/103/statuses": deploymentStatusOld.ListEndpoint(),
138-
"POST /api/v3/repos/test/health-deployment/deployments/103/statuses": deploymentStatusOld.CreateEndpoint(),
139-
"GET /api/v3/repos/test/health-deployment/deployments/104/statuses": deploymentStatusNew.ListEndpoint(),
140-
"POST /api/v3/repos/test/health-deployment/deployments/104/statuses": deploymentStatusNew.CreateEndpoint(),
141-
})
142-
143133
By("Updating the deployment annotation")
144134
app.Annotations = map[string]string{
145135
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/health-deployment/deployments/999",
@@ -149,17 +139,24 @@ var _ = Describe("Application health deployment controller", func() {
149139
By("Updating the application to healthy")
150140
app.Status.Health.Status = health.HealthStatusHealthy
151141
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
152-
Consistently(func() int { return deploymentStatusOld.CreateCount() }, "100ms").Should(BeZero())
142+
Consistently(func() int { return deploymentStatus.CreateCount() }, "100ms").Should(BeZero())
153143

154144
By("Updating the deployment annotation")
155145
app.Annotations = map[string]string{
156-
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/health-deployment/deployments/103",
146+
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/health-deployment/deployments/101",
157147
}
158148
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
159-
Eventually(func() int { return deploymentStatusOld.CreateCount() }).Should(Equal(1))
149+
Eventually(func() int { return deploymentStatus.CreateCount() }).Should(Equal(1))
160150

161151
By("Deleting the old deployment")
162-
deploymentStatusOld.NotFound = true
152+
deploymentStatus.NotFound = true
153+
154+
By("Setting up the new deployment status endpoint")
155+
var newDeploymentStatus githubmock.DeploymentStatus
156+
githubServer.AddHandlers(map[string]http.Handler{
157+
"GET /api/v3/repos/test/health-deployment/deployments/102/statuses": newDeploymentStatus.ListEndpoint(),
158+
"POST /api/v3/repos/test/health-deployment/deployments/102/statuses": newDeploymentStatus.CreateEndpoint(),
159+
})
163160

164161
By("Updating the application to progressing")
165162
app.Status.Health.Status = health.HealthStatusProgressing
@@ -168,15 +165,15 @@ var _ = Describe("Application health deployment controller", func() {
168165
By("Updating the application to healthy")
169166
app.Status.Health.Status = health.HealthStatusHealthy
170167
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
171-
Consistently(func() int { return deploymentStatusNew.CreateCount() }, "100ms").Should(BeZero())
168+
Consistently(func() int { return newDeploymentStatus.CreateCount() }, "100ms").Should(BeZero())
172169

173170
By("Updating the deployment annotation")
174171
app.Annotations = map[string]string{
175-
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/health-deployment/deployments/104",
172+
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/health-deployment/deployments/102",
176173
}
177174
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
178-
Eventually(func() int { return deploymentStatusNew.CreateCount() }).Should(Equal(1))
179-
Expect(deploymentStatusOld.CreateCount()).Should(Equal(1))
175+
Eventually(func() int { return newDeploymentStatus.CreateCount() }).Should(Equal(1))
176+
Expect(deploymentStatus.CreateCount()).Should(Equal(1))
180177
}, SpecTimeout(3*time.Second))
181178

182179
It("Should retry a deployment status until timeout", func(ctx context.Context) {

internal/controller/applicationphasecomment_controller_test.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,27 @@ import (
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1414
)
1515

16-
var _ = Describe("Application phase controller", func() {
16+
var _ = Describe("Comment on sync operation phase changed", func() {
1717
var app argocdv1alpha1.Application
18+
var comment githubmock.Comment
1819

1920
BeforeEach(func(ctx context.Context) {
21+
By("Setting up a comment endpoint")
22+
comment = githubmock.Comment{}
23+
githubServer.AddHandlers(map[string]http.Handler{
24+
"GET /api/v3/repos/test/phase-comment/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa101/pulls": githubmock.ListPullRequestsWithCommit(101),
25+
"GET /api/v3/repos/test/phase-comment/pulls/101/files": githubmock.ListFiles(),
26+
"POST /api/v3/repos/test/phase-comment/issues/101/comments": comment.CreateEndpoint(),
27+
})
28+
29+
By("Creating an application")
2030
app = argocdv1alpha1.Application{
2131
TypeMeta: metav1.TypeMeta{
2232
APIVersion: "argoproj.io/v1alpha1",
2333
Kind: "Application",
2434
},
2535
ObjectMeta: metav1.ObjectMeta{
26-
GenerateName: "fixture-",
36+
GenerateName: "fixture-phase-comment-",
2737
Namespace: "default",
2838
},
2939
Spec: argocdv1alpha1.ApplicationSpec{
@@ -44,13 +54,6 @@ var _ = Describe("Application phase controller", func() {
4454

4555
Context("When an application is synced", func() {
4656
It("Should notify a comment", func(ctx context.Context) {
47-
var comment githubmock.Comment
48-
githubServer.AddHandlers(map[string]http.Handler{
49-
"GET /api/v3/repos/test/phase-comment/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa101/pulls": githubmock.ListPullRequestsWithCommit(101),
50-
"GET /api/v3/repos/test/phase-comment/pulls/101/files": githubmock.ListFiles(),
51-
"POST /api/v3/repos/test/phase-comment/issues/101/comments": comment.CreateEndpoint(),
52-
})
53-
5457
By("Updating the application to running")
5558
app.Status = argocdv1alpha1.ApplicationStatus{
5659
OperationState: &argocdv1alpha1.OperationState{
@@ -75,21 +78,14 @@ var _ = Describe("Application phase controller", func() {
7578

7679
Context("When an application sync operation is failed", func() {
7780
It("Should notify a comment", func(ctx context.Context) {
78-
var comment githubmock.Comment
79-
githubServer.AddHandlers(map[string]http.Handler{
80-
"GET /api/v3/repos/test/phase-comment/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa102/pulls": githubmock.ListPullRequestsWithCommit(102),
81-
"GET /api/v3/repos/test/phase-comment/pulls/102/files": githubmock.ListFiles(),
82-
"POST /api/v3/repos/test/phase-comment/issues/102/comments": comment.CreateEndpoint(),
83-
})
84-
8581
By("Updating the application to running")
8682
app.Status = argocdv1alpha1.ApplicationStatus{
8783
OperationState: &argocdv1alpha1.OperationState{
8884
Phase: synccommon.OperationRunning,
8985
StartedAt: metav1.Now(),
9086
Operation: argocdv1alpha1.Operation{
9187
Sync: &argocdv1alpha1.SyncOperation{
92-
Revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa102",
88+
Revision: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa101",
9389
},
9490
},
9591
},

internal/controller/applicationphasedeployment_controller_test.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,26 @@ import (
1414
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1515
)
1616

17-
var _ = Describe("Application phase controller", func() {
17+
var _ = Describe("Deployment status on sync operation phase changed", func() {
1818
var app argocdv1alpha1.Application
19+
var deploymentStatus githubmock.DeploymentStatus
1920

2021
BeforeEach(func(ctx context.Context) {
22+
By("Setting up a deployment status endpoint")
23+
deploymentStatus = githubmock.DeploymentStatus{}
24+
githubServer.AddHandlers(map[string]http.Handler{
25+
"GET /api/v3/repos/test/phase-deployment/deployments/101/statuses": deploymentStatus.ListEndpoint(),
26+
"POST /api/v3/repos/test/phase-deployment/deployments/101/statuses": deploymentStatus.CreateEndpoint(),
27+
})
28+
29+
By("Creating an application")
2130
app = argocdv1alpha1.Application{
2231
TypeMeta: metav1.TypeMeta{
2332
APIVersion: "argoproj.io/v1alpha1",
2433
Kind: "Application",
2534
},
2635
ObjectMeta: metav1.ObjectMeta{
27-
GenerateName: "fixture-",
36+
GenerateName: "fixture-deployment-status-phase-",
2837
Namespace: "default",
2938
},
3039
Spec: argocdv1alpha1.ApplicationSpec{
@@ -45,12 +54,6 @@ var _ = Describe("Application phase controller", func() {
4554

4655
Context("When an application is synced", func() {
4756
It("Should notify a deployment status", func(ctx context.Context) {
48-
var deploymentStatus githubmock.DeploymentStatus
49-
githubServer.AddHandlers(map[string]http.Handler{
50-
"GET /api/v3/repos/test/phase-deployment/deployments/101/statuses": deploymentStatus.ListEndpoint(),
51-
"POST /api/v3/repos/test/phase-deployment/deployments/101/statuses": deploymentStatus.CreateEndpoint(),
52-
})
53-
5457
By("Updating the deployment annotation")
5558
app.Annotations = map[string]string{
5659
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/phase-deployment/deployments/101",
@@ -95,15 +98,9 @@ var _ = Describe("Application phase controller", func() {
9598

9699
Context("When an application sync operation is failed", func() {
97100
It("Should notify a deployment status", func(ctx context.Context) {
98-
var deploymentStatus githubmock.DeploymentStatus
99-
githubServer.AddHandlers(map[string]http.Handler{
100-
"GET /api/v3/repos/test/phase-deployment/deployments/102/statuses": deploymentStatus.ListEndpoint(),
101-
"POST /api/v3/repos/test/phase-deployment/deployments/102/statuses": deploymentStatus.CreateEndpoint(),
102-
})
103-
104101
By("Updating the deployment annotation")
105102
app.Annotations = map[string]string{
106-
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/phase-deployment/deployments/102",
103+
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/phase-deployment/deployments/101",
107104
}
108105
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
109106

@@ -131,12 +128,6 @@ var _ = Describe("Application phase controller", func() {
131128

132129
Context("When an application was synced before the deployment annotation is updated", func() {
133130
It("Should skip the notification", func(ctx context.Context) {
134-
var deploymentStatus githubmock.DeploymentStatus
135-
githubServer.AddHandlers(map[string]http.Handler{
136-
"GET /api/v3/repos/test/phase-deployment/deployments/103/statuses": deploymentStatus.ListEndpoint(),
137-
"POST /api/v3/repos/test/phase-deployment/deployments/103/statuses": deploymentStatus.CreateEndpoint(),
138-
})
139-
140131
By("Updating the deployment annotation")
141132
app.Annotations = map[string]string{
142133
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/phase-deployment/deployments/999",
@@ -159,7 +150,7 @@ var _ = Describe("Application phase controller", func() {
159150

160151
By("Updating the deployment annotation")
161152
app.Annotations = map[string]string{
162-
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/phase-deployment/deployments/103",
153+
"argocd-commenter.int128.github.io/deployment-url": "https://api.github.com/repos/test/phase-deployment/deployments/101",
163154
}
164155
Expect(k8sClient.Update(ctx, &app)).Should(Succeed())
165156
// this test depends on requeueIntervalWhenDeploymentNotFound and takes longer time

0 commit comments

Comments
 (0)