5
5
import cd .go .contrib .elasticagent .PluginRequest ;
6
6
import cd .go .contrib .elasticagent .PluginSettings ;
7
7
import cd .go .contrib .elasticagent .builders .PluginStatusReportViewBuilder ;
8
+ import cd .go .contrib .elasticagent .model .JobIdentifier ;
9
+ import cd .go .contrib .elasticagent .model .reports .agent .KubernetesElasticAgent ;
8
10
import cd .go .contrib .elasticagent .requests .AgentStatusReportRequest ;
9
11
import com .thoughtworks .go .plugin .api .response .GoPluginApiResponse ;
12
+ import freemarker .template .Template ;
10
13
import io .fabric8 .kubernetes .api .model .*;
11
14
import io .fabric8 .kubernetes .client .KubernetesClient ;
12
15
import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
23
26
import static org .hamcrest .Matchers .is ;
24
27
import static org .junit .Assert .assertThat ;
25
28
import static org .mockito .ArgumentMatchers .any ;
29
+ import static org .mockito .ArgumentMatchers .eq ;
26
30
import static org .mockito .Mockito .when ;
27
31
import static org .mockito .MockitoAnnotations .initMocks ;
28
32
@@ -68,6 +72,9 @@ public class AgentStatusReportExecutorTest {
68
72
@ Mock
69
73
private PodResource <Pod , DoneablePod > podresource ;
70
74
75
+ @ Mock
76
+ private Template template ;
77
+
71
78
@ Before
72
79
public void setUp () throws Exception {
73
80
initMocks (this );
@@ -91,7 +98,6 @@ public void setUp() throws Exception {
91
98
92
99
@ Test
93
100
public void shouldReturnAgentStatusReportBasedOnProvidedElasticAgentId () throws Exception {
94
-
95
101
when (statusReportRequest .getJobIdentifier ()).thenReturn (null );
96
102
when (statusReportRequest .getElasticAgentId ()).thenReturn (elasticAgentId );
97
103
@@ -100,14 +106,58 @@ public void shouldReturnAgentStatusReportBasedOnProvidedElasticAgentId() throws
100
106
when (pluginRequest .getPluginSettings ()).thenReturn (pluginSettings );
101
107
when (kubernetesClientFactory .kubernetes (pluginSettings )).thenReturn (client );
102
108
103
- when (builder .build (any (), any ())).thenReturn ("my-view" );
109
+ when (builder .getTemplate ("agent-status-report.template.ftlh" )).thenReturn (template );
110
+ when (builder .build (eq (template ), any (KubernetesElasticAgent .class ))).thenReturn ("my-view" );
104
111
105
112
GoPluginApiResponse response = executor .execute ();
106
113
107
114
assertThat (response .responseCode (), is (200 ));
108
115
assertThat (response .responseBody (), is ("{\" view\" :\" my-view\" }" ));
109
116
}
110
117
118
+ @ Test
119
+ public void shouldReturnErrorWhenPodForSpecifiedElasticAgentIdNotFound () throws Exception {
120
+ when (podList .getItems ()).thenReturn (new ArrayList <>()); // no matching pod for the specified elastic agent id
121
+
122
+ when (statusReportRequest .getJobIdentifier ()).thenReturn (null );
123
+ when (statusReportRequest .getElasticAgentId ()).thenReturn (elasticAgentId );
124
+
125
+ PluginSettings pluginSettings = new PluginSettings ();
126
+
127
+ when (pluginRequest .getPluginSettings ()).thenReturn (pluginSettings );
128
+ when (kubernetesClientFactory .kubernetes (pluginSettings )).thenReturn (client );
129
+
130
+ when (builder .getTemplate ("error.template.ftlh" )).thenReturn (template );
131
+ when (builder .build (eq (template ), any (RuntimeException .class ))).thenReturn ("my-error-view" );
132
+
133
+ GoPluginApiResponse response = executor .execute ();
134
+
135
+ assertThat (response .responseCode (), is (200 ));
136
+ assertThat (response .responseBody (), is ("{\" view\" :\" my-error-view\" }" ));
137
+ }
138
+
139
+ @ Test
140
+ public void shouldReturnErrorWhenPodForSpecifiedJobIdentifierNotFound () throws Exception {
141
+ when (client .pods ()).thenThrow (new RuntimeException ("Boom!" )); //can not find pod for specified job identitier
142
+
143
+ JobIdentifier jobIdentifier = new JobIdentifier ("up42" , 1L , "1" , "up42_stage" , "1" , "job_name" , 1L );
144
+ when (statusReportRequest .getJobIdentifier ()).thenReturn (jobIdentifier );
145
+ when (statusReportRequest .getElasticAgentId ()).thenReturn (null );
146
+
147
+ PluginSettings pluginSettings = new PluginSettings ();
148
+
149
+ when (pluginRequest .getPluginSettings ()).thenReturn (pluginSettings );
150
+ when (kubernetesClientFactory .kubernetes (pluginSettings )).thenReturn (client );
151
+
152
+ when (builder .getTemplate ("error.template.ftlh" )).thenReturn (template );
153
+ when (builder .build (eq (template ), any (RuntimeException .class ))).thenReturn ("my-error-view" );
154
+
155
+ GoPluginApiResponse response = executor .execute ();
156
+
157
+ assertThat (response .responseCode (), is (200 ));
158
+ assertThat (response .responseBody (), is ("{\" view\" :\" my-error-view\" }" ));
159
+ }
160
+
111
161
private Pod creatdDefaultPod () {
112
162
Pod pod = new Pod ();
113
163
pod .setMetadata (new ObjectMeta ());
0 commit comments