public static Container fromContainerDescriptionToPodContainer( ContainerDescription description) { if (description == null) { return null; } Container podContainer = new Container(); podContainer.name = description.name; podContainer.image = description.image; podContainer.workingDir = description.workingDir; podContainer.env = fromContainerDescriptionEnvsToPodContainersEnvs(description.env); podContainer.ports = fromContainerDescriptionPortsToPodContainerPorts( description.portBindings); podContainer.livenessProbe = fromContainerDescriptionHealthConfigToPodContainerProbe (description.healthConfig); setContainerDescriptionResourcesToPodContainerResources(description, podContainer); if (!isNullOrEmpty(description.command)) { // Container hold it's command as 2 separate string arrays - one for command // and one for it's args. In this approach we assume that the command is the first // element and rest elements are it's args, for ContainerDescription's command. String[] splittedCommand = description.command[0].split(" "); podContainer.command = Collections.singletonList(splittedCommand[0]); podContainer.args = Arrays.stream(splittedCommand).skip(1) .collect(Collectors.toList()); } if (description.privileged != null) { podContainer.securityContext = new SecurityContext(); podContainer.securityContext.privileged = description.privileged; } return podContainer; }
private PodState createPodState() throws Throwable { PodState podState = new PodState(); podState.pod = new Pod(); podState.pod.spec = new PodSpec(); podState.pod.spec.containers = new ArrayList<>(); Container container1 = new Container(); container1.name = "container1"; Container container2 = new Container(); container2.name = "container2"; Container container3 = new Container(); container3.name = "container3"; Container container4 = new Container(); container4.name = "container4"; Container container5 = new Container(); container5.name = "container5"; Container container6 = new Container(); container6.name = "container6"; podState.pod.spec.containers.add(container1); podState.pod.spec.containers.add(container2); podState.pod.spec.containers.add(container3); podState.pod.spec.containers.add(container4); podState.pod.spec.containers.add(container5); podState.pod.spec.containers.add(container6); podState.pod.metadata = new ObjectMeta(); podState.pod.metadata.selfLink = "/api/v1/namespaces/default/pods/test-pod"; podState.pod.metadata.uid = UUID.randomUUID().toString(); podState.documentSelfLink = podState.pod.metadata.uid; podState = doPost(podState, PodFactoryService.SELF_LINK); return podState; }
@Test public void testSetContainerDescriptionResourcesToPodContainerResources() { ContainerDescription description = new ContainerDescription(); description.memoryLimit = 100000L; description.cpuShares = 3; Container podContainer = new Container(); setContainerDescriptionResourcesToPodContainerResources(description, podContainer); String expectedPodContainerMemoryLimit = "100000"; String expectedPodContainerCpuShares = "3"; String actualPodContainerMemoryLimit = (String) podContainer.resources.limits.get("memory"); String actualPodContainerCpuShares = (String) podContainer.resources.limits.get("cpu"); assertEquals(expectedPodContainerMemoryLimit, actualPodContainerMemoryLimit); assertEquals(expectedPodContainerCpuShares, actualPodContainerCpuShares); }
private Container createPodContainer() { Container podContainer = new Container(); podContainer.name = CONTAINER_NAME; podContainer.image = CONTAINER_NAME; podContainer.command = Arrays.asList("app.sh"); podContainer.args = Arrays.asList("start"); podContainer.workingDir = CONTAINER_WORKING_DIR; ContainerPort port = new ContainerPort(); port.containerPort = 80; port.protocol = "TCP"; port.hostPort = 8080; podContainer.ports = Arrays.asList(port); EnvVar env = new EnvVar(); env.name = "name"; env.value = "value"; podContainer.env = Arrays.asList(env); SecurityContext ctx = new SecurityContext(); ctx.privileged = Boolean.TRUE; podContainer.securityContext = ctx; Probe probe = new Probe(); probe.tcpSocket = new TCPSocketAction(); probe.timeoutSeconds = 60; probe.failureThreshold = 3; podContainer.livenessProbe = probe; return podContainer; }
@Test public void testSetPodContainerResourcesToContainerDescriptionResources() { Container podContainer = new Container(); podContainer.resources = new ResourceRequirements(); podContainer.resources.limits = new HashMap<>(); podContainer.resources.limits.put("memory", "100M"); podContainer.resources.limits.put("cpu", "500m"); ContainerDescription containerDescription = new ContainerDescription(); setPodContainerResourcesToContainerDescriptionResources(podContainer, containerDescription); Long expectedMemoryLimit = 100000000L; Long actualMemoryLimit = containerDescription.memoryLimit; Integer expectedCpuShares = 1; Integer actualCpuShares = containerDescription.cpuShares; assertEquals(expectedMemoryLimit, actualMemoryLimit); assertEquals(expectedCpuShares, actualCpuShares); }
podState.pod.spec = new PodSpec(); podState.pod.spec.containers = new ArrayList<>(); Container container1 = new Container(); container1.name = "container1"; Container container2 = new Container(); container2.name = "container2"; Container container3 = new Container(); container3.name = "container3"; podState.pod.spec.containers.add(container1);
@Test public void testAdapterRequestOnPeriodicMaintenance() throws Throwable { PodState podState = new PodState(); podState.pod = new Pod(); podState.pod.spec = new PodSpec(); podState.pod.spec.containers = new ArrayList<>(); Container container1 = new Container(); container1.name = "container1"; container1.image = "test-image"; podState.pod.spec.containers.add(container1); podState.pod.metadata = new ObjectMeta(); podState.pod.metadata.selfLink = "/api/v1/namespaces/default/pods/test-pod"; podState.pod.metadata.name = "test-pod"; podState = doPost(podState, PodFactoryService.SELF_LINK); waitFor(() -> MockKubernetesAdapterService.requestOnInspect != null); assertNotNull(MockKubernetesAdapterService.requestOnInspect); assertEquals(KubernetesOperationType.INSPECT.id, MockKubernetesAdapterService .requestOnInspect.operationTypeId); assertEquals(podState.documentSelfLink, MockKubernetesAdapterService.requestOnInspect .resourceReference.getPath()); }
podState.pod.spec = new PodSpec(); podState.pod.spec.containers = new ArrayList<>(); Container container1 = new Container(); container1.name = "container1"; container1.image = "test-image";
podState.pod.spec = new PodSpec(); podState.pod.spec.containers = new ArrayList<>(); Container container1 = new Container(); container1.name = "container1"; container1.image = "test-image"; updatedPod.spec = new PodSpec(); updatedPod.spec.containers = new ArrayList<>(); Container updatedContainer = new Container(); updatedContainer.name = "new-container1"; updatedContainer.image = "new-test-image";
@Test public void TestCorrectMapContainer() { ContainerState outState = new ContainerState(); Container inContainer = new Container(); ContainerStatus inStatus = new ContainerStatus();
@Test public void testCreate() throws Throwable { PodState podState = new PodState(); podState.pod = new Pod(); podState.pod.spec = new PodSpec(); podState.pod.spec.containers = new ArrayList<>(); Container container1 = new Container(); podState.pod.spec.containers.add(container1); podState.pod.metadata = new ObjectMeta(); podState.pod.metadata.selfLink = "/api/v1/namespaces/default/pods/test-pod"; podState.parentLink = kubernetesHostState.documentSelfLink; podState = doPost(podState, PodFactoryService.SELF_LINK); provisioningTaskLink = createProvisioningTask(); AdapterRequest request = new AdapterRequest(); request.resourceReference = UriUtils.buildUri(host, podState.documentSelfLink); request.serviceTaskCallback = ServiceTaskCallback.create(provisioningTaskLink); request.operationTypeId = KubernetesOperationType.CREATE.id; doOperation(KubernetesAdapterService.SELF_LINK, request); waitForPropertyValue(provisioningTaskLink, MockTaskState.class, "taskInfo.stage", TaskState.TaskStage.FAILED); }
@Test public void testConvertPodContainerProbeToHealthConfigTCP() { Container podContainer = new Container(); podContainer.livenessProbe = new Probe(); podContainer.livenessProbe.tcpSocket = new TCPSocketAction(); podContainer.livenessProbe.tcpSocket.port = "8080"; podContainer.livenessProbe.timeoutSeconds = 3; HealthConfig expectedHealthConfig = new HealthConfig(); expectedHealthConfig.protocol = RequestProtocol.TCP; expectedHealthConfig.port = 8080; expectedHealthConfig.timeoutMillis = 3000; HealthConfig actualHealthConfig = fromPodContainerProbeToContainerDescriptionHealthConfig( podContainer); assertNotNull(actualHealthConfig); assertEquals(actualHealthConfig.protocol, expectedHealthConfig.protocol); assertEquals(actualHealthConfig.port, expectedHealthConfig.port); assertEquals(actualHealthConfig.timeoutMillis, expectedHealthConfig.timeoutMillis); }
@Test public void TestMapContainerWithNullInputStatus() { ContainerState outState = new ContainerState(); Container inContainer = new Container(); inContainer.name = "test"; inContainer.image = "image"; KubernetesContainerStateMapper.mapContainer(outState, inContainer, null); Assert.assertNull(outState.name); Assert.assertNull(outState.names); Assert.assertNull(outState.id); Assert.assertNull(outState.image); Assert.assertNull(outState.command); Assert.assertNull(outState.env); Assert.assertNull(outState.ports); Assert.assertNull(outState.powerState); }
state.pod.spec = new PodSpec(); state.pod.spec.containers = new ArrayList<>(); Container c = new Container(); c.name = "fake"; state.pod.spec.containers.add(c);
@Test public void testConvertPodContainerProbeToHealthConfigHTTP() { Container podContainer = new Container(); podContainer.livenessProbe = new Probe(); podContainer.livenessProbe.httpGet = new HTTPGetAction(); podContainer.livenessProbe.httpGet.port = "8080"; podContainer.livenessProbe.httpGet.path = "/health"; podContainer.livenessProbe.timeoutSeconds = 3; HealthConfig expectedHealthConfig = new HealthConfig(); expectedHealthConfig.protocol = RequestProtocol.HTTP; expectedHealthConfig.httpVersion = HttpVersion.HTTP_v1_1; expectedHealthConfig.httpMethod = Action.GET; expectedHealthConfig.urlPath = "/health"; expectedHealthConfig.port = 8080; expectedHealthConfig.timeoutMillis = 3000; HealthConfig actualHealthConfig = fromPodContainerProbeToContainerDescriptionHealthConfig( podContainer); assertNotNull(actualHealthConfig); assertEquals(actualHealthConfig.protocol, expectedHealthConfig.protocol); assertEquals(actualHealthConfig.port, expectedHealthConfig.port); assertEquals(actualHealthConfig.httpMethod, expectedHealthConfig.httpMethod); assertEquals(actualHealthConfig.httpVersion, expectedHealthConfig.httpVersion); assertEquals(actualHealthConfig.urlPath, expectedHealthConfig.urlPath); assertEquals(actualHealthConfig.timeoutMillis, expectedHealthConfig.timeoutMillis); }