1818
1919import cd .go .contrib .elasticagent .model .JobIdentifier ;
2020import 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 ;
2124import io .fabric8 .kubernetes .client .KubernetesClient ;
25+ import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
26+ import io .fabric8 .kubernetes .client .dsl .PodResource ;
2227import org .joda .time .DateTime ;
2328import org .junit .Before ;
2429import org .junit .Test ;
30+ import org .mockito .InOrder ;
2531import org .mockito .Mock ;
2632
33+ import java .util .Collections ;
2734import java .util .HashMap ;
2835
2936import static org .junit .Assert .assertTrue ;
@@ -50,10 +57,13 @@ public class KubernetesAgentInstancesTest {
5057 @ Mock
5158 PluginRequest mockPluginRequest ;
5259
60+ @ Mock
61+ private MixedOperation <Pod , PodList , DoneablePod , PodResource <Pod , DoneablePod >> mockedOperation ;
62+
5363 private HashMap <String , String > testProperties ;
5464
5565 @ Before
56- public void setUp () throws Exception {
66+ public void setUp () {
5767 initMocks (this );
5868 testProperties = new HashMap <>();
5969 when (mockCreateAgentRequest .properties ()).thenReturn (testProperties );
@@ -64,7 +74,7 @@ public void setUp() throws Exception {
6474 }
6575
6676 @ Test
67- public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance () throws Exception {
77+ public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance () {
6878 KubernetesInstance kubernetesInstance = new KubernetesInstance (new DateTime (), "test" , "test-agent" , new HashMap <>(), 100L , PodState .Running );
6979 when (mockKubernetesInstanceFactory .create (mockCreateAgentRequest , mockPluginSettings , mockKubernetesClient , mockPluginRequest , true )).
7080 thenReturn (kubernetesInstance );
@@ -77,7 +87,7 @@ public void shouldCreateKubernetesPodUsingPodYamlAndCacheCreatedInstance() throw
7787 }
7888
7989 @ Test
80- public void shouldCreateKubernetesPodAndCacheCreatedInstance () throws Exception {
90+ public void shouldCreateKubernetesPodAndCacheCreatedInstance () {
8191 KubernetesInstance kubernetesInstance = new KubernetesInstance (new DateTime (), "test" , "test-agent" , new HashMap <>(), 100L , PodState .Running );
8292 when (mockKubernetesInstanceFactory .create (mockCreateAgentRequest , mockPluginSettings , mockKubernetesClient , mockPluginRequest , false )).
8393 thenReturn (kubernetesInstance );
@@ -88,7 +98,7 @@ public void shouldCreateKubernetesPodAndCacheCreatedInstance() throws Exception
8898 }
8999
90100 @ Test
91- public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs () throws Exception {
101+ public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs () {
92102 KubernetesInstance kubernetesInstance = new KubernetesInstance (new DateTime (), "test" , "test-agent" , new HashMap <>(), 100L , PodState .Running );
93103 when (mockKubernetesInstanceFactory .create (mockCreateAgentRequest , mockPluginSettings , mockKubernetesClient , mockPluginRequest , false )).
94104 thenReturn (kubernetesInstance );
@@ -106,7 +116,7 @@ public void shouldNotCreatePodWhenOutstandingRequestsExistForJobs() throws Excep
106116 }
107117
108118 @ Test
109- public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasReached () throws Exception {
119+ public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasReached () {
110120 //set maximum pending pod count to 1
111121 when (mockPluginSettings .getMaxPendingPods ()).thenReturn (1 );
112122
@@ -128,4 +138,23 @@ public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasRea
128138 agentInstances .create (mockCreateAgentRequest , mockPluginSettings , mockPluginRequest );
129139 verify (mockKubernetesInstanceFactory , times (0 )).create (any (), any (), any (), any (), any ());
130140 }
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+ }
131160}
0 commit comments