Skip to content

Commit faa7579

Browse files
authored
Merge pull request #36 from bdpiparva/pending-pod-count
Removed unwanted refresh agent call.
2 parents 246bdb8 + 7bae81c commit faa7579

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

src/main/java/cd/go/contrib/elasticagent/KubernetesAgentInstances.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public Agents instancesCreatedAfterTimeout(PluginSettings settings, Agents agent
150150

151151
@Override
152152
public void refreshAll(PluginRequest pluginRequest) {
153-
LOG.debug("[Refresh Instances]. Syncing k8s elastic agent pod information.");
153+
LOG.debug("[Refresh Instances] Syncing k8s elastic agent pod information.");
154154
KubernetesClient client = factory.client(pluginRequest.getPluginSettings());
155155
PodList list = client.pods().list();
156156

src/main/java/cd/go/contrib/elasticagent/KubernetesPlugin.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public GoPluginApiResponse handle(GoPluginApiRequest request) {
7272
case REQUEST_VALIDATE_PROFILE:
7373
return ProfileValidateRequest.fromJSON(request.requestBody()).executor().execute();
7474
case REQUEST_CREATE_AGENT:
75-
refreshInstances();
7675
return CreateAgentRequest.fromJSON(request.requestBody()).executor(agentInstances, pluginRequest).execute();
7776
case REQUEST_SHOULD_ASSIGN_WORK:
7877
refreshInstances();

src/test/java/cd/go/contrib/elasticagent/KubernetesAgentInstancesTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public void setUp() {
7171
when(factory.client(mockPluginSettings)).thenReturn(mockKubernetesClient);
7272
JobIdentifier jobId = new JobIdentifier("test", 1L, "Test pipeline", "test name", "1", "test job", 100L);
7373
when(mockCreateAgentRequest.jobIdentifier()).thenReturn(jobId);
74+
75+
final PodList podList = mock(PodList.class);
76+
when(mockKubernetesClient.pods()).thenReturn(mockedOperation);
77+
when(mockPluginRequest.getPluginSettings()).thenReturn(mockPluginSettings);
78+
when(mockedOperation.list()).thenReturn(podList);
79+
when(podList.getItems()).thenReturn(Collections.emptyList());
7480
}
7581

7682
@Test
@@ -141,11 +147,6 @@ public void shouldNotCreatePodsWhenOutstandingLimitOfPendingKubernetesPodsHasRea
141147

142148
@Test
143149
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());
149150
when(mockKubernetesInstanceFactory.create(mockCreateAgentRequest, mockPluginSettings, mockKubernetesClient, mockPluginRequest, false)).
150151
thenReturn(new KubernetesInstance(new DateTime(), "test", "test-agent", new HashMap<>(), 100L, PodState.Running));
151152

src/test/java/cd/go/contrib/elasticagent/executors/ServerPingRequestExecutorTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ public class ServerPingRequestExecutorTest extends BaseTest {
5151
@Mock
5252
private Pod mockedPod;
5353
@Mock
54+
private PluginRequest pluginRequest;
55+
@Mock
5456
private PodResource<Pod, DoneablePod> podResource;
5557
private ObjectMeta objectMetadata;
5658

5759
@Before
5860
public void setUp() {
5961
initMocks(this);
60-
when(factory.client(any(PluginSettings.class))).thenReturn(mockedClient);
62+
when(factory.client(any())).thenReturn(mockedClient);
6163
when(mockedClient.pods()).thenReturn(mockedOperation);
6264
when(mockedOperation.create(any(Pod.class))).thenAnswer(new Answer<Pod>() {
6365
@Override
@@ -74,6 +76,10 @@ public Pod answer(InvocationOnMock invocation) throws Throwable {
7476
objectMetadata.setCreationTimestamp(getSimpleDateFormat().format(new Date()));
7577

7678
when(mockedPod.getMetadata()).thenReturn(objectMetadata);
79+
80+
final PodList podList = mock(PodList.class);
81+
when(mockedOperation.list()).thenReturn(podList);
82+
when(podList.getItems()).thenReturn(Collections.emptyList());
7783
}
7884

7985
@Test
@@ -122,7 +128,7 @@ public void testShouldTerminateInstancesThatNeverAutoRegistered() throws Excepti
122128
KubernetesAgentInstances agentInstances = new KubernetesAgentInstances(factory);
123129
HashMap<String, String> properties = new HashMap<>();
124130
properties.put("Image", "foo");
125-
KubernetesInstance container = agentInstances.create(new CreateAgentRequest(null, properties, null, new JobIdentifier(1L)), createSettings(), null);
131+
KubernetesInstance container = agentInstances.create(new CreateAgentRequest(null, properties, null, new JobIdentifier(1L)), createSettings(), pluginRequest);
126132

127133
agentInstances.clock = new Clock.TestClock().forward(Period.minutes(11));
128134
PluginRequest pluginRequest = mock(PluginRequest.class);

src/test/java/cd/go/contrib/elasticagent/executors/ShouldAssignWorkRequestExecutorTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,15 @@
3333
import org.mockito.invocation.InvocationOnMock;
3434
import org.mockito.stubbing.Answer;
3535

36+
import java.util.Collections;
3637
import java.util.HashMap;
3738
import java.util.Map;
3839
import java.util.UUID;
3940

4041
import static org.hamcrest.Matchers.is;
4142
import static org.junit.Assert.assertThat;
4243
import static org.mockito.ArgumentMatchers.any;
44+
import static org.mockito.Mockito.mock;
4345
import static org.mockito.Mockito.when;
4446
import static org.mockito.MockitoAnnotations.initMocks;
4547

@@ -52,14 +54,21 @@ public class ShouldAssignWorkRequestExecutorTest extends BaseTest {
5254
@Mock
5355
private KubernetesClient mockedClient;
5456
@Mock
57+
private PluginRequest pluginRequest;
58+
@Mock
5559
private MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockedOperation;
5660
private String environment = "QA";
5761

5862
@Before
5963
public void setUp() throws Exception {
6064
initMocks(this);
61-
when(factory.client(any(PluginSettings.class))).thenReturn(mockedClient);
65+
when(factory.client(any())).thenReturn(mockedClient);
6266
when(mockedClient.pods()).thenReturn(mockedOperation);
67+
68+
final PodList podList = mock(PodList.class);
69+
when(mockedOperation.list()).thenReturn(podList);
70+
when(podList.getItems()).thenReturn(Collections.emptyList());
71+
6372
when(mockedOperation.create(any(Pod.class))).thenAnswer(new Answer<Pod>() {
6473
@Override
6574
public Pod answer(InvocationOnMock invocation) throws Throwable {
@@ -71,7 +80,7 @@ public Pod answer(InvocationOnMock invocation) throws Throwable {
7180
agentInstances = new KubernetesAgentInstances(factory);
7281
properties.put("foo", "bar");
7382
properties.put("Image", "gocdcontrib/ubuntu-docker-elastic-agent");
74-
instance = agentInstances.create(new CreateAgentRequest(UUID.randomUUID().toString(), properties, environment, new JobIdentifier(100L)), createSettings(), null);
83+
instance = agentInstances.create(new CreateAgentRequest(UUID.randomUUID().toString(), properties, environment, new JobIdentifier(100L)), createSettings(), pluginRequest);
7584
}
7685

7786
@Test

0 commit comments

Comments
 (0)