19
19
import cd .go .contrib .elasticagent .model .JobIdentifier ;
20
20
import cd .go .contrib .elasticagent .requests .CreateAgentRequest ;
21
21
import io .fabric8 .kubernetes .api .model .DoneablePod ;
22
+ import io .fabric8 .kubernetes .api .model .ObjectMeta ;
22
23
import io .fabric8 .kubernetes .api .model .Pod ;
23
24
import io .fabric8 .kubernetes .api .model .PodList ;
24
25
import io .fabric8 .kubernetes .client .KubernetesClient ;
30
31
import org .mockito .InOrder ;
31
32
import org .mockito .Mock ;
32
33
34
+ import java .util .Arrays ;
33
35
import java .util .Collections ;
34
36
import java .util .HashMap ;
37
+ import java .util .Map ;
35
38
39
+ import static cd .go .contrib .elasticagent .Constants .JOB_ID_LABEL_KEY ;
36
40
import static org .junit .Assert .assertTrue ;
37
41
import static org .mockito .ArgumentMatchers .any ;
38
42
import static org .mockito .Mockito .*;
@@ -60,6 +64,8 @@ public class KubernetesAgentInstancesTest {
60
64
@ Mock
61
65
private MixedOperation <Pod , PodList , DoneablePod , PodResource <Pod , DoneablePod >> mockedOperation ;
62
66
67
+ @ Mock
68
+ private PodList podList ;
63
69
private HashMap <String , String > testProperties ;
64
70
65
71
@ Before
@@ -72,7 +78,6 @@ public void setUp() {
72
78
JobIdentifier jobId = new JobIdentifier ("test" , 1L , "Test pipeline" , "test name" , "1" , "test job" , 100L );
73
79
when (mockCreateAgentRequest .jobIdentifier ()).thenReturn (jobId );
74
80
75
- final PodList podList = mock (PodList .class );
76
81
when (mockKubernetesClient .pods ()).thenReturn (mockedOperation );
77
82
when (mockPluginRequest .getPluginSettings ()).thenReturn (mockPluginSettings );
78
83
when (mockedOperation .list ()).thenReturn (podList );
@@ -117,6 +122,18 @@ public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs() {
117
122
verify (mockKubernetesInstanceFactory , times (1 )).create (any (), any (), any (), any (), any ());
118
123
reset (mockKubernetesInstanceFactory );
119
124
125
+ final Map <String , String > labels = new HashMap <>();
126
+ labels .put (JOB_ID_LABEL_KEY , jobId .getJobId ().toString ());
127
+ labels .put (Constants .KUBERNETES_POD_KIND_LABEL_KEY , Constants .KUBERNETES_POD_KIND_LABEL_VALUE );
128
+
129
+ final Pod pod = mock (Pod .class );
130
+ final ObjectMeta objectMeta = mock (ObjectMeta .class );
131
+ when (pod .getMetadata ()).thenReturn (objectMeta );
132
+ when (objectMeta .getLabels ()).thenReturn (labels );
133
+ when (objectMeta .getName ()).thenReturn ("test-agent" );
134
+ when (podList .getItems ()).thenReturn (Arrays .asList (pod ));
135
+ when (mockKubernetesInstanceFactory .fromKubernetesPod (pod )).thenReturn (kubernetesInstance );
136
+
120
137
agentInstances .create (mockCreateAgentRequest , mockPluginSettings , mockPluginRequest );
121
138
verify (mockKubernetesInstanceFactory , times (0 )).create (any (), any (), any (), any (), any ());
122
139
}
@@ -140,6 +157,18 @@ public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasRea
140
157
verify (mockKubernetesInstanceFactory , times (1 )).create (any (), any (), any (), any (), any ());
141
158
reset (mockKubernetesInstanceFactory );
142
159
160
+ final Map <String , String > labels = new HashMap <>();
161
+ labels .put (JOB_ID_LABEL_KEY , jobId .getJobId ().toString ());
162
+ labels .put (Constants .KUBERNETES_POD_KIND_LABEL_KEY , Constants .KUBERNETES_POD_KIND_LABEL_VALUE );
163
+
164
+ final Pod pod = mock (Pod .class );
165
+ final ObjectMeta objectMeta = mock (ObjectMeta .class );
166
+ when (pod .getMetadata ()).thenReturn (objectMeta );
167
+ when (objectMeta .getLabels ()).thenReturn (labels );
168
+ when (objectMeta .getName ()).thenReturn ("test-agent" );
169
+ when (podList .getItems ()).thenReturn (Arrays .asList (pod ));
170
+ when (mockKubernetesInstanceFactory .fromKubernetesPod (pod )).thenReturn (kubernetesInstance );
171
+
143
172
//second create agent request
144
173
agentInstances .create (mockCreateAgentRequest , mockPluginSettings , mockPluginRequest );
145
174
verify (mockKubernetesInstanceFactory , times (0 )).create (any (), any (), any (), any (), any ());
0 commit comments