16
16
17
17
package cd .go .contrib .elasticagent .executors ;
18
18
19
+ import cd .go .contrib .elasticagent .KubernetesClientFactory ;
19
20
import cd .go .contrib .elasticagent .PluginRequest ;
20
21
import cd .go .contrib .elasticagent .model .ServerInfo ;
21
- import cd .go .contrib .elasticagent .requests .ValidatePluginSettings ;
22
+ import cd .go .contrib .elasticagent .requests .ValidatePluginSettingsRequest ;
22
23
import com .thoughtworks .go .plugin .api .response .GoPluginApiResponse ;
24
+ import io .fabric8 .kubernetes .api .model .DoneableNamespace ;
25
+ import io .fabric8 .kubernetes .api .model .Namespace ;
26
+ import io .fabric8 .kubernetes .api .model .NamespaceBuilder ;
27
+ import io .fabric8 .kubernetes .api .model .NamespaceList ;
28
+ import io .fabric8 .kubernetes .client .KubernetesClient ;
29
+ import io .fabric8 .kubernetes .client .dsl .NonNamespaceOperation ;
30
+ import io .fabric8 .kubernetes .client .dsl .Resource ;
23
31
import org .json .JSONException ;
24
32
import org .junit .Before ;
25
33
import org .junit .Test ;
26
34
import org .mockito .Mock ;
27
35
import org .skyscreamer .jsonassert .JSONAssert ;
28
36
37
+ import java .util .Arrays ;
38
+ import java .util .Collections ;
39
+ import java .util .List ;
40
+ import java .util .stream .Collectors ;
41
+
29
42
import static org .hamcrest .Matchers .is ;
30
43
import static org .junit .Assert .assertThat ;
44
+ import static org .mockito .ArgumentMatchers .any ;
31
45
import static org .mockito .Mockito .when ;
32
46
import static org .mockito .MockitoAnnotations .initMocks ;
33
47
34
48
public class ValidateConfigurationExecutorTest {
35
-
36
49
@ Mock
37
50
private PluginRequest pluginRequest ;
51
+
52
+ @ Mock
53
+ KubernetesClientFactory factory ;
54
+ @ Mock
55
+ private KubernetesClient client ;
56
+ @ Mock
57
+ private NonNamespaceOperation <Namespace , NamespaceList , DoneableNamespace , Resource <Namespace , DoneableNamespace >> mockedOperation ;
58
+ @ Mock
59
+ NamespaceList namespaceList ;
38
60
private ServerInfo serverInfo ;
39
61
40
62
@ Before
@@ -46,12 +68,17 @@ public void setUp() {
46
68
"\" secure_site_url\" : \" https://example.com:8154/go\" \n " +
47
69
"}" );
48
70
when (pluginRequest .getSeverInfo ()).thenReturn (serverInfo );
71
+ when (factory .client (any ())).thenReturn (client );
72
+ when (client .namespaces ()).thenReturn (mockedOperation );
73
+ when (mockedOperation .list ()).thenReturn (namespaceList );
49
74
}
50
75
51
76
@ Test
52
77
public void shouldValidateABadConfiguration () throws Exception {
53
- ValidatePluginSettings settings = new ValidatePluginSettings ();
54
- GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , pluginRequest ).execute ();
78
+ when (namespaceList .getItems ()).thenReturn (getNamespaceList ("default" ));
79
+
80
+ ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest ();
81
+ GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , pluginRequest , factory ).execute ();
55
82
56
83
assertThat (response .responseCode (), is (200 ));
57
84
JSONAssert .assertEquals ("[\n " +
@@ -68,23 +95,27 @@ public void shouldValidateABadConfiguration() throws Exception {
68
95
69
96
@ Test
70
97
public void shouldValidateAGoodConfiguration () throws Exception {
71
- ValidatePluginSettings settings = new ValidatePluginSettings ();
98
+ when (namespaceList .getItems ()).thenReturn (getNamespaceList ("default" ));
99
+
100
+ ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest ();
72
101
settings .put ("go_server_url" , "https://ci.example.com/go" );
73
102
settings .put ("kubernetes_cluster_url" , "https://cluster.example.com" );
74
103
settings .put ("oauth_token" , "some-token" );
75
- GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , null ).execute ();
104
+ GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , null , factory ).execute ();
76
105
77
106
assertThat (response .responseCode (), is (200 ));
78
107
JSONAssert .assertEquals ("[]" , response .responseBody (), true );
79
108
}
80
109
81
110
@ Test
82
111
public void shouldValidateGoServerUrl () throws Exception {
83
- ValidatePluginSettings settings = new ValidatePluginSettings ();
112
+ when (namespaceList .getItems ()).thenReturn (getNamespaceList ("default" ));
113
+
114
+ ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest ();
84
115
serverInfo .setSecureSiteUrl (null );
85
116
settings .put ("kubernetes_cluster_url" , "https://cluster.example.com" );
86
117
settings .put ("oauth_token" , "some-token" );
87
- GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , pluginRequest ).execute ();
118
+ GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , pluginRequest , factory ).execute ();
88
119
89
120
assertThat (response .responseCode (), is (200 ));
90
121
JSONAssert .assertEquals ("[" +
@@ -97,11 +128,13 @@ public void shouldValidateGoServerUrl() throws Exception {
97
128
98
129
@ Test
99
130
public void shouldValidateGoServerHTTPSUrlFormat () throws Exception {
100
- ValidatePluginSettings settings = new ValidatePluginSettings ();
131
+ when (namespaceList .getItems ()).thenReturn (getNamespaceList ("default" ));
132
+
133
+ ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest ();
101
134
settings .put ("go_server_url" , "foo.com/go(" );
102
135
settings .put ("kubernetes_cluster_url" , "https://cluster.example.com" );
103
136
settings .put ("oauth_token" , "some-token" );
104
- GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , pluginRequest ).execute ();
137
+ GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , pluginRequest , factory ).execute ();
105
138
106
139
assertThat (response .responseCode (), is (200 ));
107
140
JSONAssert .assertEquals ("[" +
@@ -114,11 +147,13 @@ public void shouldValidateGoServerHTTPSUrlFormat() throws Exception {
114
147
115
148
@ Test
116
149
public void shouldValidateGoServerUrlFormat () throws Exception {
117
- ValidatePluginSettings settings = new ValidatePluginSettings ();
150
+ when (namespaceList .getItems ()).thenReturn (getNamespaceList ("default" ));
151
+
152
+ ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest ();
118
153
settings .put ("go_server_url" , "https://foo.com" );
119
154
settings .put ("kubernetes_cluster_url" , "https://cluster.example.com" );
120
155
settings .put ("oauth_token" , "some-token" );
121
- GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , pluginRequest ).execute ();
156
+ GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , pluginRequest , factory ).execute ();
122
157
123
158
assertThat (response .responseCode (), is (200 ));
124
159
JSONAssert .assertEquals ("[" +
@@ -131,11 +166,13 @@ public void shouldValidateGoServerUrlFormat() throws Exception {
131
166
132
167
@ Test
133
168
public void shouldValidateOAuthTokenWhenAuthenticationStrategyIsSetToOauthToken () throws JSONException {
134
- ValidatePluginSettings settings = new ValidatePluginSettings ();
169
+ when (namespaceList .getItems ()).thenReturn (getNamespaceList ("default" ));
170
+
171
+ ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest ();
135
172
settings .put ("go_server_url" , "https://foo.com/go" );
136
173
settings .put ("kubernetes_cluster_url" , "https://cluster.example.com" );
137
174
138
- GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , pluginRequest ).execute ();
175
+ GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , pluginRequest , factory ).execute ();
139
176
140
177
assertThat (response .responseCode (), is (200 ));
141
178
@@ -146,4 +183,34 @@ public void shouldValidateOAuthTokenWhenAuthenticationStrategyIsSetToOauthToken(
146
183
" }\n " +
147
184
"]" , response .responseBody (), true );
148
185
}
186
+
187
+ @ Test
188
+ public void shouldValidateNamespaceExistence () throws JSONException {
189
+ when (namespaceList .getItems ()).thenReturn (getNamespaceList ("default" ));
190
+
191
+ ValidatePluginSettingsRequest settings = new ValidatePluginSettingsRequest ();
192
+ settings .put ("go_server_url" , "https://ci.example.com/go" );
193
+ settings .put ("kubernetes_cluster_url" , "https://cluster.example.com" );
194
+ settings .put ("oauth_token" , "some-token" );
195
+ settings .put ("namespace" , "gocd" );
196
+ GoPluginApiResponse response = new ValidateConfigurationExecutor (settings , null , factory ).execute ();
197
+
198
+ assertThat (response .responseCode (), is (200 ));
199
+ JSONAssert .assertEquals ("[\n " +
200
+ " {\n " +
201
+ " \" message\" : \" Namespace `gocd` does not exist in you cluster. Run \\ \" kubectl create namespace gocd\\ \" to create a namespace.\" ,\n " +
202
+ " \" key\" : \" namespace\" \n " +
203
+ " }\n " +
204
+ "]" , response .responseBody (), true );
205
+ }
206
+
207
+ private List <Namespace > getNamespaceList (String ... namespaces ) {
208
+ if (namespaces == null || namespaces .length == 0 ) {
209
+ return Collections .emptyList ();
210
+ }
211
+
212
+ return Arrays .asList (namespaces ).stream ()
213
+ .map (namespaceName -> new NamespaceBuilder ().withNewMetadata ().withName ("default" ).endMetadata ().build ())
214
+ .collect (Collectors .toList ());
215
+ }
149
216
}
0 commit comments