Skip to content

Commit

Permalink
Add validation error when generateName field is specified in Elastic …
Browse files Browse the repository at this point in the history
…Agent Profile Pod Yaml
  • Loading branch information
GaneshSPatil committed Sep 5, 2019
1 parent e3894e8 commit 0bf5f55
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ public GoPluginApiResponse execute() {
validationError.put("message", "Should be one of `properties`, `remote`, `yaml`.");
result.add(validationError);
}
}
else {
} else {

boolean isSpecifiedUsingPodYaml = Boolean.valueOf(new HashMap<>(request.getProperties()).get(SPECIFIED_USING_POD_CONFIGURATION.getKey()));

Expand Down Expand Up @@ -127,12 +126,17 @@ private void validatePodYaml(HashMap<String, String> properties, ArrayList<Map<S
addNotBlankError(result, key, "Pod Configuration");
return;
}

Pod pod = new Pod();
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
mapper.readValue(KubernetesInstanceFactory.getTemplatizedPodSpec(podYaml), Pod.class);
pod = mapper.readValue(KubernetesInstanceFactory.getTemplatizedPodSpec(podYaml), Pod.class);
} catch (IOException e) {
addError(result, key, "Invalid Pod Yaml.");
return;
}

if (StringUtils.isNotBlank(pod.getMetadata().getGenerateName())) {
addError(result, key, "Invalid Pod Yaml. generateName field is not supported by GoCD. Please use {{ POD_POSTFIX }} instead.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ public void shouldAllowPodYamlConfiguration() throws Exception {
JSONAssert.assertEquals("[]", json, JSONCompareMode.NON_EXTENSIBLE);
}

@Test
public void shouldNotAllowPodYamlConfigurationWhenGenerateNameIsSpecified() throws Exception {
Map<String, String> properties = new HashMap<>();
properties.put("PodSpecType", "yaml");
String podYaml = "apiVersion: v1\n" +
"kind: Pod\n" +
"metadata:\n" +
" generateName: pod-name\n" +
" labels:\n" +
" app: web\n" +
"spec:\n" +
" containers:\n" +
" - name: gocd-agent-container\n" +
" image: gocd/fancy-agent-image:latest";

properties.put("PodConfiguration", podYaml);
ProfileValidateRequestExecutor executor = new ProfileValidateRequestExecutor(new ProfileValidateRequest(properties));
String json = executor.execute().responseBody();
JSONAssert.assertEquals("[{\"message\":\"Invalid Pod Yaml. generateName field is not supported by GoCD. Please use {{ POD_POSTFIX }} instead.\",\"key\":\"PodConfiguration\"}]", json, JSONCompareMode.NON_EXTENSIBLE);
}

@Test
public void shouldAllowJinjaTemplatedPodYaml() throws Exception {
Expand Down

0 comments on commit 0bf5f55

Please sign in to comment.