Search in sources :

Example 1 with PodListBuilder

use of io.fabric8.kubernetes.api.model.PodListBuilder in project fabric8-maven-plugin by fabric8io.

the class PortForwardServiceTest method testSimpleScenario.

@Test
public void testSimpleScenario() throws Exception {
    // Cannot test more complex scenarios due to errors in mockwebserver
    OpenShiftMockServer mockServer = new OpenShiftMockServer(false);
    Pod pod1 = new PodBuilder().withNewMetadata().withName("mypod").addToLabels("mykey", "myvalue").withResourceVersion("1").endMetadata().withNewStatus().withPhase("run").endStatus().build();
    PodList pods1 = new PodListBuilder().withItems(pod1).withNewMetadata().withResourceVersion("1").endMetadata().build();
    mockServer.expect().get().withPath("/api/v1/namespaces/test/pods?labelSelector=mykey%3Dmyvalue").andReturn(200, pods1).always();
    mockServer.expect().get().withPath("/api/v1/namespaces/test/pods").andReturn(200, pods1).always();
    mockServer.expect().get().withPath("/api/v1/namespaces/test/pods?labelSelector=mykey%3Dmyvalue&watch=true").andUpgradeToWebSocket().open().waitFor(1000).andEmit(new WatchEvent(pod1, "MODIFIED")).done().always();
    mockServer.expect().get().withPath("/api/v1/namespaces/test/pods?resourceVersion=1&watch=true").andUpgradeToWebSocket().open().waitFor(1000).andEmit(new WatchEvent(pod1, "MODIFIED")).done().always();
    OpenShiftClient client = mockServer.createOpenShiftClient();
    PortForwardService service = new PortForwardService(clientToolsService, logger, client) {

        @Override
        public ProcessUtil.ProcessExecutionContext forwardPortAsync(Logger externalProcessLogger, String pod, int remotePort, int localPort) throws Fabric8ServiceException {
            return new ProcessUtil.ProcessExecutionContext(process, Collections.<Thread>emptyList(), logger);
        }
    };
    try (Closeable c = service.forwardPortAsync(logger, new LabelSelectorBuilder().withMatchLabels(Collections.singletonMap("mykey", "myvalue")).build(), 8080, 9000)) {
        Thread.sleep(3000);
    }
}
Also used : LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Closeable(java.io.Closeable) Logger(io.fabric8.maven.docker.util.Logger) ProcessUtil(io.fabric8.maven.core.util.ProcessUtil) PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) Test(org.junit.Test)

Example 2 with PodListBuilder

use of io.fabric8.kubernetes.api.model.PodListBuilder in project hono by eclipse.

the class KubernetesBasedAdapterInstanceStatusServiceTest method testServiceDetectsTerminatedContainer.

/**
 * Verifies that a terminated container in an adapter pod is detected by the status service.
 *
 * @throws InterruptedException if test execution gets interrupted.
 */
@Test
public void testServiceDetectsTerminatedContainer() throws InterruptedException {
    final CountDownLatch eventLatch = new CountDownLatch(4);
    final Pod pod0 = createAdapterPodWithRunningContainer("testPod0");
    final String pod0ContainerId = KubernetesBasedAdapterInstanceStatusService.getShortContainerId(pod0.getStatus().getContainerStatuses().get(0).getContainerID());
    assertThat(pod0ContainerId).isNotNull();
    final Pod pod1WithFirstContainer = createAdapterPodWithRunningContainer("testPod1");
    final String pod1FirstContainerId = KubernetesBasedAdapterInstanceStatusService.getShortContainerId(pod1WithFirstContainer.getStatus().getContainerStatuses().get(0).getContainerID());
    assertThat(pod1FirstContainerId).isNotNull();
    final Pod pod1WithFirstContainerTerminated = createCopyWithTerminatedContainer(pod1WithFirstContainer);
    final Pod pod1WithSecondContainer = createAdapterPodWithRunningContainer("testPod1");
    final String pod1SecondContainerId = KubernetesBasedAdapterInstanceStatusService.getShortContainerId(pod1WithSecondContainer.getStatus().getContainerStatuses().get(0).getContainerID());
    assertThat(pod1SecondContainerId).isNotNull();
    server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder().addToItems(pod0, pod1WithFirstContainer).build()).once();
    server.expect().withPath("/api/v1/namespaces/test/pods?allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(10).andEmit(new WatchEvent(pod1WithFirstContainerTerminated, "MODIFIED")).waitFor(20).andEmit(new WatchEvent(pod1WithSecondContainer, "MODIFIED")).done().once();
    statusService = new KubernetesBasedAdapterInstanceStatusService(client) {

        @Override
        protected void onAdapterContainerAdded(final String containerId) {
            LOG.debug("onAdapterContainerAdded; containerId: '{}'", containerId);
            eventLatch.countDown();
        }

        @Override
        protected void onAdapterContainerRemoved(final String containerId) {
            LOG.debug("onAdapterContainerRemoved; containerId: '{}'", containerId);
            eventLatch.countDown();
        }
    };
    assertThat(statusService).isNotNull();
    if (eventLatch.await(10, TimeUnit.SECONDS)) {
        assertThat(statusService.getActiveAdapterInstanceContainerIds().isPresent()).isTrue();
        assertThat(statusService.getActiveAdapterInstanceContainerIds().get()).containsExactly(pod0ContainerId, pod1SecondContainerId);
        final String adapterInstanceId0 = pod0.getMetadata().getName() + "_" + pod0ContainerId + "_1";
        assertThat(statusService.getStatus(adapterInstanceId0)).isEqualTo(AdapterInstanceStatus.ALIVE);
        final String adapterInstanceId1 = pod1WithFirstContainer.getMetadata().getName() + "_" + pod1FirstContainerId + "_1";
        assertThat(statusService.getStatus(adapterInstanceId1)).isEqualTo(AdapterInstanceStatus.DEAD);
    } else {
        fail("added/removed pod not detected");
    }
}
Also used : PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 3 with PodListBuilder

use of io.fabric8.kubernetes.api.model.PodListBuilder in project hono by eclipse.

the class KubernetesBasedAdapterInstanceStatusServiceTest method testServiceDetectsAddedPod.

/**
 * Verifies that an added adapter pod is detected by the status service.
 *
 * @throws InterruptedException if test execution gets interrupted.
 */
@Test
public void testServiceDetectsAddedPod() throws InterruptedException {
    final CountDownLatch eventLatch = new CountDownLatch(2);
    final Pod pod0 = createAdapterPodWithRunningContainer("testPod0");
    final String pod0ContainerId = KubernetesBasedAdapterInstanceStatusService.getShortContainerId(pod0.getStatus().getContainerStatuses().get(0).getContainerID());
    assertThat(pod0ContainerId).isNotNull();
    server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder().addToItems(pod0).build()).once();
    final Pod pod1 = createAdapterPodWithRunningContainer("testPod1");
    final String pod1ContainerId = KubernetesBasedAdapterInstanceStatusService.getShortContainerId(pod1.getStatus().getContainerStatuses().get(0).getContainerID());
    assertThat(pod1ContainerId).isNotNull();
    server.expect().withPath("/api/v1/namespaces/test/pods?allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(10).andEmit(new WatchEvent(pod1, "ADDED")).done().once();
    statusService = new KubernetesBasedAdapterInstanceStatusService(client) {

        @Override
        protected void onAdapterContainerAdded(final String containerId) {
            LOG.debug("onAdapterContainerAdded; containerId: '{}'", containerId);
            eventLatch.countDown();
        }
    };
    assertThat(statusService).isNotNull();
    if (eventLatch.await(10, TimeUnit.SECONDS)) {
        assertThat(statusService.getActiveAdapterInstanceContainerIds().isPresent()).isTrue();
        assertThat(statusService.getActiveAdapterInstanceContainerIds().get()).containsExactly(pod0ContainerId, pod1ContainerId);
        final String adapterInstanceId0 = pod0.getMetadata().getName() + "_" + pod0ContainerId + "_1";
        assertThat(statusService.getStatus(adapterInstanceId0)).isEqualTo(AdapterInstanceStatus.ALIVE);
        final String adapterInstanceId1 = pod1.getMetadata().getName() + "_" + pod1ContainerId + "_1";
        assertThat(statusService.getStatus(adapterInstanceId1)).isEqualTo(AdapterInstanceStatus.ALIVE);
    } else {
        fail("added pod not detected");
    }
}
Also used : PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with PodListBuilder

use of io.fabric8.kubernetes.api.model.PodListBuilder in project hono by eclipse.

the class KubernetesBasedAdapterInstanceStatusServiceTest method testServiceDetectsDeletedPod.

/**
 * Verifies that a deleted adapter pod is detected by the status service.
 *
 * @throws InterruptedException if test execution gets interrupted.
 */
@Test
public void testServiceDetectsDeletedPod() throws InterruptedException {
    final CountDownLatch eventLatch = new CountDownLatch(3);
    final Pod pod0 = createAdapterPodWithRunningContainer("testPod0");
    final String pod0ContainerId = KubernetesBasedAdapterInstanceStatusService.getShortContainerId(pod0.getStatus().getContainerStatuses().get(0).getContainerID());
    assertThat(pod0ContainerId).isNotNull();
    final Pod pod1 = createAdapterPodWithRunningContainer("testPod1");
    final String pod1ContainerId = KubernetesBasedAdapterInstanceStatusService.getShortContainerId(pod1.getStatus().getContainerStatuses().get(0).getContainerID());
    assertThat(pod1ContainerId).isNotNull();
    server.expect().withPath("/api/v1/namespaces/test/pods").andReturn(200, new PodListBuilder().addToItems(pod0, pod1).build()).once();
    server.expect().withPath("/api/v1/namespaces/test/pods?allowWatchBookmarks=true&watch=true").andUpgradeToWebSocket().open().waitFor(10).andEmit(new WatchEvent(pod1, "DELETED")).done().once();
    statusService = new KubernetesBasedAdapterInstanceStatusService(client) {

        @Override
        protected void onAdapterContainerAdded(final String containerId) {
            LOG.debug("onAdapterContainerAdded; containerId: '{}'", containerId);
            eventLatch.countDown();
        }

        @Override
        protected void onAdapterContainerRemoved(final String containerId) {
            LOG.debug("onAdapterContainerRemoved; containerId: '{}'", containerId);
            eventLatch.countDown();
        }
    };
    assertThat(statusService).isNotNull();
    if (eventLatch.await(10, TimeUnit.SECONDS)) {
        assertThat(statusService.getActiveAdapterInstanceContainerIds().isPresent()).isTrue();
        assertThat(statusService.getActiveAdapterInstanceContainerIds().get()).containsExactly(pod0ContainerId);
        final String adapterInstanceId0 = pod0.getMetadata().getName() + "_" + pod0ContainerId + "_1";
        assertThat(statusService.getStatus(adapterInstanceId0)).isEqualTo(AdapterInstanceStatus.ALIVE);
        final String adapterInstanceId1 = pod1.getMetadata().getName() + "_" + pod1ContainerId + "_1";
        assertThat(statusService.getStatus(adapterInstanceId1)).isEqualTo(AdapterInstanceStatus.DEAD);
    } else {
        fail("added/removed pod not detected");
    }
}
Also used : PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 5 with PodListBuilder

use of io.fabric8.kubernetes.api.model.PodListBuilder in project syndesis by syndesisio.

the class ActivityTrackingControllerTest method testLogsController.

@Test
public void testLogsController() throws IOException {
    final String expectedDBState = resource("logs-controller-db.json").trim();
    final String podLogs = resource("test-pod-x23x.txt");
    try (ActivityTrackingController controller = new ActivityTrackingController(jsondb, dbi, null) {

        @Override
        protected PodList listPods() {
            return new PodListBuilder().addNewItem().withNewMetadata().withName("test-pod-x23x").addToLabels(OpenShiftService.COMPONENT_LABEL, "integration").addToLabels(OpenShiftService.DEPLOYMENT_VERSION_LABEL, "3").addToLabels(OpenShiftService.INTEGRATION_ID_LABEL, "my-integration").endMetadata().withNewStatus().withPhase("Running").endStatus().endItem().build();
        }

        @Override
        protected boolean isPodRunning(String name) {
            return true;
        }

        @Override
        protected void watchLog(String podName, Consumer<InputStream> handler, String sinceTime) throws IOException {
            executor.execute(() -> {
                handler.accept(new ByteArrayInputStream(podLogs.getBytes(StandardCharsets.UTF_8)));
            });
        }
    }) {
        controller.setStartupDelay("0 seconds");
        controller.setRetention("1000000000 days");
        controller.open();
        // Eventually all the log data should make it into the jsondb
        given().await().atMost(20, SECONDS).pollInterval(1, SECONDS).untilAsserted(() -> {
            String db = jsondb.getAsString("/", new GetOptions().prettyPrint(true));
            assertThat(db).isEqualTo(expectedDBState);
        });
    }
}
Also used : PodListBuilder(io.fabric8.kubernetes.api.model.PodListBuilder) Consumer(java.util.function.Consumer) ByteArrayInputStream(java.io.ByteArrayInputStream) GetOptions(io.syndesis.server.jsondb.GetOptions) Test(org.junit.Test)

Aggregations

PodListBuilder (io.fabric8.kubernetes.api.model.PodListBuilder)7 Pod (io.fabric8.kubernetes.api.model.Pod)6 WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.jupiter.api.Test)4 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)2 Test (org.junit.Test)2 KubernetesContainerClient (de.zalando.ep.zalenium.container.kubernetes.KubernetesContainerClient)1 Container (io.fabric8.kubernetes.api.model.Container)1 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)1 EnvVarBuilder (io.fabric8.kubernetes.api.model.EnvVarBuilder)1 HostAlias (io.fabric8.kubernetes.api.model.HostAlias)1 HostAliasBuilder (io.fabric8.kubernetes.api.model.HostAliasBuilder)1 LabelSelectorBuilder (io.fabric8.kubernetes.api.model.LabelSelectorBuilder)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)1 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)1 PodStatus (io.fabric8.kubernetes.api.model.PodStatus)1 PodStatusBuilder (io.fabric8.kubernetes.api.model.PodStatusBuilder)1 Service (io.fabric8.kubernetes.api.model.Service)1