Skip to content

Commit 7fd5bb3

Browse files
committed
Add test to verify the get status report using elastic agent id behavior
1 parent f5f6622 commit 7fd5bb3

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package cd.go.contrib.elasticagent.executors;
2+
3+
import cd.go.contrib.elasticagent.Constants;
4+
import cd.go.contrib.elasticagent.KubernetesClientFactory;
5+
import cd.go.contrib.elasticagent.PluginRequest;
6+
import cd.go.contrib.elasticagent.PluginSettings;
7+
import cd.go.contrib.elasticagent.builders.PluginStatusReportViewBuilder;
8+
import cd.go.contrib.elasticagent.requests.AgentStatusReportRequest;
9+
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
10+
import io.fabric8.kubernetes.api.model.*;
11+
import io.fabric8.kubernetes.client.KubernetesClient;
12+
import io.fabric8.kubernetes.client.dsl.MixedOperation;
13+
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
14+
import io.fabric8.kubernetes.client.dsl.PodResource;
15+
import io.fabric8.kubernetes.client.dsl.Resource;
16+
import org.junit.Before;
17+
import org.junit.Test;
18+
import org.mockito.Mock;
19+
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
23+
import static org.hamcrest.Matchers.is;
24+
import static org.junit.Assert.assertThat;
25+
import static org.mockito.ArgumentMatchers.any;
26+
import static org.mockito.Mockito.when;
27+
import static org.mockito.MockitoAnnotations.initMocks;
28+
29+
public class AgentStatusReportExecutorTest {
30+
31+
private Pod pod;
32+
private AgentStatusReportExecutor executor;
33+
private String elasticAgentId = "elastic-agent-id";
34+
35+
@Mock
36+
private AgentStatusReportRequest statusReportRequest;
37+
38+
@Mock
39+
private PluginRequest pluginRequest;
40+
41+
@Mock
42+
private KubernetesClientFactory kubernetesClientFactory;
43+
44+
@Mock
45+
private KubernetesClient client;
46+
47+
@Mock
48+
private PluginStatusReportViewBuilder builder;
49+
50+
@Mock
51+
private MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockedOperation;
52+
53+
@Mock
54+
private NonNamespaceOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockedNamespaceOperation;
55+
56+
@Mock
57+
private PodList podList;
58+
59+
@Mock
60+
private MixedOperation<Event, EventList, DoneableEvent, Resource<Event, DoneableEvent>> events;
61+
62+
@Mock
63+
private NonNamespaceOperation<Event, EventList, DoneableEvent, Resource<Event, DoneableEvent>> eventspace;
64+
65+
@Mock
66+
private EventList eventsList;
67+
68+
@Mock
69+
private PodResource<Pod, DoneablePod> podresource;
70+
71+
@Before
72+
public void setUp() throws Exception {
73+
initMocks(this);
74+
pod = creatdDefaultPod();
75+
pod.getMetadata().setName(elasticAgentId);
76+
executor = new AgentStatusReportExecutor(statusReportRequest, pluginRequest, kubernetesClientFactory, builder);
77+
78+
when(client.pods()).thenReturn(mockedOperation);
79+
when(mockedOperation.inNamespace(Constants.KUBERNETES_NAMESPACE)).thenReturn(mockedNamespaceOperation);
80+
when(mockedNamespaceOperation.list()).thenReturn(podList);
81+
when(podList.getItems()).thenReturn(Arrays.asList(pod));
82+
83+
when(mockedNamespaceOperation.withName(elasticAgentId)).thenReturn(podresource);
84+
when(podresource.getLog()).thenReturn("agent-logs");
85+
86+
when(client.events()).thenReturn(events);
87+
when(events.inAnyNamespace()).thenReturn(eventspace);
88+
when(eventspace.list()).thenReturn(eventsList);
89+
when(eventsList.getItems()).thenReturn(new ArrayList<>());
90+
}
91+
92+
@Test
93+
public void shouldReturnAgentStatusReportBasedOnProvidedElasticAgentId() throws Exception {
94+
95+
when(statusReportRequest.getJobIdentifier()).thenReturn(null);
96+
when(statusReportRequest.getElasticAgentId()).thenReturn(elasticAgentId);
97+
98+
PluginSettings pluginSettings = new PluginSettings();
99+
100+
when(pluginRequest.getPluginSettings()).thenReturn(pluginSettings);
101+
when(kubernetesClientFactory.kubernetes(pluginSettings)).thenReturn(client);
102+
103+
when(builder.build(any(), any())).thenReturn("my-view");
104+
105+
GoPluginApiResponse response = executor.execute();
106+
107+
assertThat(response.responseCode(), is(200));
108+
assertThat(response.responseBody(), is("{\"view\":\"my-view\"}"));
109+
}
110+
111+
private Pod creatdDefaultPod() {
112+
Pod pod = new Pod();
113+
pod.setMetadata(new ObjectMeta());
114+
PodSpec spec = new PodSpec();
115+
spec.setContainers(Arrays.asList(new Container()));
116+
pod.setSpec(spec);
117+
PodStatus status = new PodStatus();
118+
status.setContainerStatuses(Arrays.asList(new ContainerStatus()));
119+
pod.setStatus(status);
120+
return pod;
121+
}
122+
123+
}

0 commit comments

Comments
 (0)