KubernetesEnvironmentPodsValidator { public void validate(KubernetesEnvironment env) throws ValidationException { checkArgument( !env.getPodsData().isEmpty(), "Environment should contain at least 1 pod or deployment"); ensureHasMetaAndSpec(env.getPodsData()); ensureConfiguredMachinesHaveContainers(env); validatePodVolumes(env); }  void validate(KubernetesEnvironment env);  }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Environment should contain at least 1 pod or deployment") public void shouldThrowExceptionWhenEnvDoesNotHaveAnyPods() throws Exception { when(kubernetesEnvironment.getPodsData()).thenReturn(emptyMap()); podsValidator.validate(kubernetesEnvironment); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Environment contains machines that are missing in recipe: pod1/db") public void shouldThrowExceptionWhenMachineIsDeclaredButThereIsNotContainerInKubernetesRecipe() throws Exception { String podName = "pod1"; Pod pod = createPod("pod1", "main"); PodData podData = new PodData(pod.getSpec(), pod.getMetadata()); when(kubernetesEnvironment.getPodsData()).thenReturn(ImmutableMap.of(podName, podData)); when(kubernetesEnvironment.getMachines()) .thenReturn(ImmutableMap.of(podName + "/db", mock(InternalMachineConfig.class))); podsValidator.validate(kubernetesEnvironment); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Environment contains pod with missing metadata") public void shouldThrowExceptionWhenPodHasNoMetadata() throws Exception { PodData podData = new PodData(new PodSpec(), null); when(kubernetesEnvironment.getPodsData()).thenReturn(ImmutableMap.of("", podData)); podsValidator.validate(kubernetesEnvironment); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Pod 'pod1' with missing metadata") public void shouldThrowExceptionWhenPodHasNoSpec() throws Exception { PodData podData = new PodData(null, new ObjectMetaBuilder().withName("pod1").build()); when(kubernetesEnvironment.getPodsData()).thenReturn(ImmutableMap.of("pod1", podData)); podsValidator.validate(kubernetesEnvironment); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Pod 'pod1' contains volume 'user-data' with PVC sources that references missing PVC 'non-existing'") public void shouldThrowExceptionWhenPodHasVolumeThatReferencesMissingPVC() throws Exception { String podName = "pod1"; Pod pod = createPod("pod1", "main"); pod.getSpec() .getVolumes() .add( new VolumeBuilder() .withName("user-data") .withNewPersistentVolumeClaim() .withClaimName("non-existing") .endPersistentVolumeClaim() .build()); PodData podData = new PodData(pod.getSpec(), pod.getMetadata()); when(kubernetesEnvironment.getPodsData()).thenReturn(ImmutableMap.of(podName, podData)); when(kubernetesEnvironment.getMachines()) .thenReturn(ImmutableMap.of(podName + "/main", mock(InternalMachineConfig.class))); podsValidator.validate(kubernetesEnvironment); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "Container 'main' in pod 'pod1' contains volume mount that references missing volume 'non-existing'") public void shouldThrowExceptionWhenContainerHasVolumeMountThatReferencesMissingPodVolume() throws Exception { String podName = "pod1"; Pod pod = createPod("pod1", "main"); pod.getSpec() .getContainers() .get(0) .getVolumeMounts() .add(new VolumeMountBuilder().withName("non-existing").withMountPath("/tmp/data").build()); PodData podData = new PodData(pod.getSpec(), pod.getMetadata()); when(kubernetesEnvironment.getPodsData()).thenReturn(ImmutableMap.of(podName, podData)); when(kubernetesEnvironment.getMachines()) .thenReturn(ImmutableMap.of(podName + "/main", mock(InternalMachineConfig.class))); podsValidator.validate(kubernetesEnvironment); }