@@ -17,7 +17,7 @@ import (
1717)
1818
1919var one = int32 (1 )
20- var dep = & appsv1.Deployment {
20+ var mockDep = & appsv1.Deployment {
2121 ObjectMeta : v1.ObjectMeta {
2222 Name : "test-deployment" ,
2323 Namespace : "testnamespace" ,
@@ -26,7 +26,7 @@ var dep = &appsv1.Deployment{
2626 Spec : appsv1.DeploymentSpec {Replicas : & one },
2727}
2828
29- var cloneDep = & appsv1.Deployment {
29+ var mockCloneDep = & appsv1.Deployment {
3030 ObjectMeta : v1.ObjectMeta {
3131 Name : "test-deployment-primary" ,
3232 Namespace : "testnamespace" ,
@@ -35,7 +35,7 @@ var cloneDep = &appsv1.Deployment{
3535 Spec : appsv1.DeploymentSpec {Replicas : & one },
3636}
3737
38- func setupPlugin (t * testing.T ) (* Plugin , * clients.KubernetesMock ) {
38+ func setupPlugin (t * testing.T ) (* Plugin , * clients.KubernetesMock , * appsv1. Deployment , * appsv1. Deployment ) {
3939
4040 retryTimeout = 10 * time .Millisecond
4141 retryInterval = 1 * time .Millisecond
@@ -52,11 +52,11 @@ func setupPlugin(t *testing.T) (*Plugin, *clients.KubernetesMock) {
5252 p .config .Deployment = "test-deployment"
5353 p .config .Namespace = "testnamespace"
5454
55- return p , km
55+ return p , km , mockDep . DeepCopy (), mockCloneDep . DeepCopy ()
5656}
5757
5858func TestInitPrimaryDoesNothingWhenPrimaryExists (t * testing.T ) {
59- p , km := setupPlugin (t )
59+ p , km , dep , cloneDep := setupPlugin (t )
6060
6161 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
6262 km .On ("GetDeployment" , mock .Anything , "test-deployment-primary" , "testnamespace" ).Return (cloneDep , nil )
@@ -72,7 +72,7 @@ func TestInitPrimaryDoesNothingWhenPrimaryExists(t *testing.T) {
7272}
7373
7474func TestInitPrimaryDoesNothingWhenCandidateDoesNotExist (t * testing.T ) {
75- p , km := setupPlugin (t )
75+ p , km , _ , _ := setupPlugin (t )
7676
7777 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
7878 km .On ("GetDeployment" , mock .Anything , "test-deployment-primary" , "testnamespace" ).Return (nil , fmt .Errorf ("Primary not found" ))
@@ -84,7 +84,7 @@ func TestInitPrimaryDoesNothingWhenCandidateDoesNotExist(t *testing.T) {
8484}
8585
8686func TestInitPrimaryCreatesPrimaryWhenCandidateExists (t * testing.T ) {
87- p , km := setupPlugin (t )
87+ p , km , dep , cloneDep := setupPlugin (t )
8888
8989 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
9090 km .On ("GetDeployment" , mock .Anything , "test-deployment-primary" , "testnamespace" ).Once ().Return (nil , fmt .Errorf ("Primary not found" ))
@@ -97,10 +97,14 @@ func TestInitPrimaryCreatesPrimaryWhenCandidateExists(t *testing.T) {
9797 require .Equal (t , interfaces .RuntimeDeploymentUpdate , status )
9898
9999 km .AssertCalled (t , "UpsertDeployment" , mock .Anything , mock .Anything )
100+
101+ // check that the runtimedeploymentversion label is added to ensure the validating webhook ignores this deployment
102+ depArg := getUpsertDeployment (km .Mock )
103+ require .Equal (t , depArg .Labels [interfaces .RuntimeDeploymentVersionLabel ], "1" )
100104}
101105
102106func TestPromoteCandidateDoesNothingWhenCandidateNotExists (t * testing.T ) {
103- p , km := setupPlugin (t )
107+ p , km , _ , _ := setupPlugin (t )
104108
105109 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
106110 km .On ("GetHealthyDeployment" , mock .Anything , "test-deployment" , "testnamespace" ).Return (nil , clients .ErrDeploymentNotFound )
@@ -113,7 +117,7 @@ func TestPromoteCandidateDoesNothingWhenCandidateNotExists(t *testing.T) {
113117}
114118
115119func TestPromoteCandidateDeletesExistingPrimaryAndUpserts (t * testing.T ) {
116- p , km := setupPlugin (t )
120+ p , km , dep , _ := setupPlugin (t )
117121
118122 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
119123 km .On ("GetHealthyDeployment" , mock .Anything , "test-deployment" , "testnamespace" ).Once ().Return (dep , nil )
@@ -127,10 +131,14 @@ func TestPromoteCandidateDeletesExistingPrimaryAndUpserts(t *testing.T) {
127131
128132 km .AssertCalled (t , "DeleteDeployment" , mock .Anything , "test-deployment-primary" , "testnamespace" )
129133 km .AssertCalled (t , "UpsertDeployment" , mock .Anything , mock .Anything )
134+
135+ // check that the runtimedeploymentversion label is added to ensure the validating webhook ignores this deployment
136+ depArg := getUpsertDeployment (km .Mock )
137+ require .Equal (t , depArg .Labels [interfaces .RuntimeDeploymentVersionLabel ], "1" )
130138}
131139
132140func TestRemoveCandidateDoesNothingWhenCandidateNotFound (t * testing.T ) {
133- p , km := setupPlugin (t )
141+ p , km , _ , _ := setupPlugin (t )
134142
135143 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
136144 km .On ("GetDeployment" , mock .Anything , "test-deployment" , "testnamespace" ).Once ().Return (nil , clients .ErrDeploymentNotFound )
@@ -142,7 +150,7 @@ func TestRemoveCandidateDoesNothingWhenCandidateNotFound(t *testing.T) {
142150}
143151
144152func TestRemoveCandidateScalesWhenCandidateFound (t * testing.T ) {
145- p , km := setupPlugin (t )
153+ p , km , dep , _ := setupPlugin (t )
146154
147155 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
148156 km .On ("GetDeployment" , mock .Anything , "test-deployment" , "testnamespace" ).Once ().Return (dep , nil )
@@ -156,7 +164,7 @@ func TestRemoveCandidateScalesWhenCandidateFound(t *testing.T) {
156164}
157165
158166func TestRestoreDoesNothingWhenNoPrimaryFound (t * testing.T ) {
159- p , km := setupPlugin (t )
167+ p , km , _ , _ := setupPlugin (t )
160168
161169 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
162170 km .On ("GetDeployment" , mock .Anything , "test-deployment-primary" , "testnamespace" ).Once ().Return (nil , clients .ErrDeploymentNotFound )
@@ -166,7 +174,7 @@ func TestRestoreDoesNothingWhenNoPrimaryFound(t *testing.T) {
166174}
167175
168176func TestRestoreCallsDeleteWhenPrimaryFound (t * testing.T ) {
169- p , km := setupPlugin (t )
177+ p , km , _ , cloneDep := setupPlugin (t )
170178
171179 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
172180 km .On ("GetDeployment" , mock .Anything , "test-deployment-primary" , "testnamespace" ).Once ().Return (cloneDep , nil )
@@ -176,10 +184,14 @@ func TestRestoreCallsDeleteWhenPrimaryFound(t *testing.T) {
176184
177185 err := p .RestoreOriginal (context .Background ())
178186 require .NoError (t , err )
187+
188+ // check that the labels are removed
189+ depArg := getUpsertDeployment (km .Mock )
190+ require .Equal (t , depArg .Labels [interfaces .RuntimeDeploymentVersionLabel ], "" )
179191}
180192
181193func TestRestoreProceedesWhenExistingCandidateNotFound (t * testing.T ) {
182- p , km := setupPlugin (t )
194+ p , km , _ , cloneDep := setupPlugin (t )
183195
184196 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
185197 km .On ("GetDeployment" , mock .Anything , "test-deployment-primary" , "testnamespace" ).Once ().Return (cloneDep , nil )
@@ -192,7 +204,7 @@ func TestRestoreProceedesWhenExistingCandidateNotFound(t *testing.T) {
192204}
193205
194206func TestRemovePrimaryCallsDelete (t * testing.T ) {
195- p , km := setupPlugin (t )
207+ p , km , _ , _ := setupPlugin (t )
196208
197209 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
198210 km .On ("DeleteDeployment" , mock .Anything , "test-deployment-primary" , "testnamespace" ).Once ().Return (nil )
@@ -202,7 +214,7 @@ func TestRemovePrimaryCallsDelete(t *testing.T) {
202214}
203215
204216func TestRemovePrimaryReturnsErrorWhenDeleteIsGenericError (t * testing.T ) {
205- p , km := setupPlugin (t )
217+ p , km , _ , _ := setupPlugin (t )
206218
207219 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
208220 km .On ("DeleteDeployment" , mock .Anything , "test-deployment-primary" , "testnamespace" ).Once ().Return (fmt .Errorf ("test" ))
@@ -212,11 +224,23 @@ func TestRemovePrimaryReturnsErrorWhenDeleteIsGenericError(t *testing.T) {
212224}
213225
214226func TestRemovePrimaryReturnsNoErrorWhenDeleteIsNotFoundError (t * testing.T ) {
215- p , km := setupPlugin (t )
227+ p , km , _ , _ := setupPlugin (t )
216228
217229 testutils .ClearMockCall (& km .Mock , "GetDeployment" )
218230 km .On ("DeleteDeployment" , mock .Anything , "test-deployment-primary" , "testnamespace" ).Once ().Return (clients .ErrDeploymentNotFound )
219231
220232 err := p .RemovePrimary (context .Background ())
221233 require .NoError (t , err )
222234}
235+
236+ func getUpsertDeployment (mock mock.Mock ) * appsv1.Deployment {
237+ for _ , c := range mock .Calls {
238+ if c .Method == "UpsertDeployment" {
239+ if dep , ok := c .Arguments .Get (1 ).(* appsv1.Deployment ); ok {
240+ return dep
241+ }
242+ }
243+ }
244+
245+ return nil
246+ }
0 commit comments