18
18
19
19
import cd .go .contrib .elasticagent .model .JobIdentifier ;
20
20
import cd .go .contrib .elasticagent .requests .CreateAgentRequest ;
21
+ import io .fabric8 .kubernetes .api .model .DoneablePod ;
22
+ import io .fabric8 .kubernetes .api .model .Pod ;
23
+ import io .fabric8 .kubernetes .api .model .PodList ;
21
24
import io .fabric8 .kubernetes .client .KubernetesClient ;
25
+ import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
26
+ import io .fabric8 .kubernetes .client .dsl .PodResource ;
22
27
import org .joda .time .DateTime ;
23
28
import org .junit .Before ;
24
29
import org .junit .Test ;
30
+ import org .mockito .InOrder ;
25
31
import org .mockito .Mock ;
26
32
33
+ import java .util .Collections ;
27
34
import java .util .HashMap ;
28
35
29
36
import static org .junit .Assert .assertTrue ;
@@ -50,10 +57,13 @@ public class KubernetesAgentInstancesTest {
50
57
@ Mock
51
58
PluginRequest mockPluginRequest ;
52
59
60
+ @ Mock
61
+ private MixedOperation <Pod , PodList , DoneablePod , PodResource <Pod , DoneablePod >> mockedOperation ;
62
+
53
63
private HashMap <String , String > testProperties ;
54
64
55
65
@ Before
56
- public void setUp () throws Exception {
66
+ public void setUp () {
57
67
initMocks (this );
58
68
testProperties = new HashMap <>();
59
69
when (mockCreateAgentRequest .properties ()).thenReturn (testProperties );
@@ -64,7 +74,7 @@ public void setUp() throws Exception {
64
74
}
65
75
66
76
@ Test
67
- public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance () throws Exception {
77
+ public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance () {
68
78
KubernetesInstance kubernetesInstance = new KubernetesInstance (new DateTime (), "test" , "test-agent" , new HashMap <>(), 100L , PodState .Running );
69
79
when (mockKubernetesInstanceFactory .create (mockCreateAgentRequest , mockPluginSettings , mockKubernetesClient , mockPluginRequest , true )).
70
80
thenReturn (kubernetesInstance );
@@ -77,7 +87,7 @@ public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance() throw
77
87
}
78
88
79
89
@ Test
80
- public void shouldCreateKubernetesPodAndCacheCreatedInstance () throws Exception {
90
+ public void shouldCreateKubernetesPodAndCacheCreatedInstance () {
81
91
KubernetesInstance kubernetesInstance = new KubernetesInstance (new DateTime (), "test" , "test-agent" , new HashMap <>(), 100L , PodState .Running );
82
92
when (mockKubernetesInstanceFactory .create (mockCreateAgentRequest , mockPluginSettings , mockKubernetesClient , mockPluginRequest , false )).
83
93
thenReturn (kubernetesInstance );
@@ -88,7 +98,7 @@ public void shouldCreateKubernetesPodAndCacheCreatedInstance() throws Exception
88
98
}
89
99
90
100
@ Test
91
- public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs () throws Exception {
101
+ public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs () {
92
102
KubernetesInstance kubernetesInstance = new KubernetesInstance (new DateTime (), "test" , "test-agent" , new HashMap <>(), 100L , PodState .Running );
93
103
when (mockKubernetesInstanceFactory .create (mockCreateAgentRequest , mockPluginSettings , mockKubernetesClient , mockPluginRequest , false )).
94
104
thenReturn (kubernetesInstance );
@@ -106,7 +116,7 @@ public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs() throws Excep
106
116
}
107
117
108
118
@ Test
109
- public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasReached () throws Exception {
119
+ public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasReached () {
110
120
//set maximum pending pod count to 1
111
121
when (mockPluginSettings .getMaxPendingPods ()).thenReturn (1 );
112
122
@@ -128,4 +138,23 @@ public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasRea
128
138
agentInstances .create (mockCreateAgentRequest , mockPluginSettings , mockPluginRequest );
129
139
verify (mockKubernetesInstanceFactory , times (0 )).create (any (), any (), any (), any (), any ());
130
140
}
141
+
142
+ @ Test
143
+ public void shouldSyncPodsStateFromClusterBeforeCreatingPod () {
144
+ final PodList podList = mock (PodList .class );
145
+ when (mockKubernetesClient .pods ()).thenReturn (mockedOperation );
146
+ when (mockPluginRequest .getPluginSettings ()).thenReturn (mockPluginSettings );
147
+ when (mockedOperation .list ()).thenReturn (podList );
148
+ when (podList .getItems ()).thenReturn (Collections .emptyList ());
149
+ when (mockKubernetesInstanceFactory .create (mockCreateAgentRequest , mockPluginSettings , mockKubernetesClient , mockPluginRequest , false )).
150
+ thenReturn (new KubernetesInstance (new DateTime (), "test" , "test-agent" , new HashMap <>(), 100L , PodState .Running ));
151
+
152
+ final KubernetesAgentInstances agentInstances = new KubernetesAgentInstances (factory , mockKubernetesInstanceFactory );
153
+
154
+ agentInstances .create (mockCreateAgentRequest , mockPluginSettings , mockPluginRequest );
155
+
156
+ InOrder inOrder = inOrder (mockKubernetesInstanceFactory , mockedOperation );
157
+ inOrder .verify (mockedOperation ).list ();
158
+ inOrder .verify (mockKubernetesInstanceFactory ).create (mockCreateAgentRequest , mockPluginSettings , mockKubernetesClient , mockPluginRequest , false );
159
+ }
131
160
}
0 commit comments