Search in sources :

Example 1 with WatchEvent

use of io.fabric8.kubernetes.api.model.WatchEvent 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 WatchEvent

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

the class OpenshiftBuildServiceTest method createMockServer.

protected WebServerEventCollector<OpenShiftMockServer> createMockServer(BuildService.BuildServiceConfig config, boolean success, long buildDelay, boolean buildConfigExists, boolean imageStreamExists) {
    OpenShiftMockServer mockServer = new OpenShiftMockServer(false);
    WebServerEventCollector<OpenShiftMockServer> collector = new WebServerEventCollector<>(mockServer);
    BuildConfig bc = new BuildConfigBuilder().withNewMetadata().withName(projectName + config.getS2iBuildNameSuffix()).endMetadata().withNewSpec().endSpec().build();
    ImageStream imageStream = new ImageStreamBuilder().withNewMetadata().withName(projectName).endMetadata().withStatus(new ImageStreamStatusBuilder().addNewTagLike(new NamedTagEventListBuilder().addNewItem().withImage("abcdef0123456789").endItem().build()).endTag().build()).build();
    KubernetesList builds = new KubernetesListBuilder().withItems(new BuildBuilder().withNewMetadata().withName(projectName).endMetadata().build()).withNewMetadata().withResourceVersion("1").endMetadata().build();
    String buildStatus = success ? "Complete" : "Fail";
    Build build = new BuildBuilder().withNewMetadata().withResourceVersion("2").endMetadata().withNewStatus().withPhase(buildStatus).endStatus().build();
    if (!buildConfigExists) {
        mockServer.expect().get().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix()).andReply(collector.record("build-config-check").andReturn(404, "")).once();
        mockServer.expect().post().withPath("/oapi/v1/namespaces/test/buildconfigs").andReply(collector.record("new-build-config").andReturn(201, bc)).once();
    } else {
        mockServer.expect().patch().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix()).andReply(collector.record("patch-build-config").andReturn(200, bc)).once();
    }
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix()).andReply(collector.record("build-config-check").andReturn(200, bc)).always();
    if (!imageStreamExists) {
        mockServer.expect().get().withPath("/oapi/v1/namespaces/test/imagestreams/" + projectName).andReturn(404, "").once();
    }
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/imagestreams/" + projectName).andReturn(200, imageStream).always();
    mockServer.expect().post().withPath("/oapi/v1/namespaces/test/imagestreams").andReturn(201, imageStream).once();
    mockServer.expect().post().withPath("/oapi/v1/namespaces/test/buildconfigs/" + projectName + config.getS2iBuildNameSuffix() + "/instantiatebinary?commit=").andReply(collector.record("pushed").andReturn(201, imageStream)).once();
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/builds").andReply(collector.record("check-build").andReturn(200, builds)).always();
    mockServer.expect().get().withPath("/oapi/v1/namespaces/test/builds?labelSelector=openshift.io/build-config.name%3D" + projectName + config.getS2iBuildNameSuffix()).andReturn(200, builds).always();
    mockServer.expect().withPath("/oapi/v1/namespaces/test/builds/" + projectName).andReturn(200, build).always();
    mockServer.expect().withPath("/oapi/v1/namespaces/test/builds?fieldSelector=metadata.name%3D" + projectName + "&watch=true").andUpgradeToWebSocket().open().waitFor(buildDelay).andEmit(new WatchEvent(build, "MODIFIED")).done().always();
    return collector;
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ImageStreamStatusBuilder(io.fabric8.openshift.api.model.ImageStreamStatusBuilder) ImageStream(io.fabric8.openshift.api.model.ImageStream) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) WebServerEventCollector(io.fabric8.maven.core.util.WebServerEventCollector) BuildConfigBuilder(io.fabric8.openshift.api.model.BuildConfigBuilder) BuildBuilder(io.fabric8.openshift.api.model.BuildBuilder) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) OpenShiftMockServer(io.fabric8.openshift.client.server.mock.OpenShiftMockServer) Build(io.fabric8.openshift.api.model.Build) NamedTagEventListBuilder(io.fabric8.openshift.api.model.NamedTagEventListBuilder) BuildConfig(io.fabric8.openshift.api.model.BuildConfig) WatchEvent(io.fabric8.kubernetes.api.model.WatchEvent)

Example 3 with WatchEvent

use of io.fabric8.kubernetes.api.model.WatchEvent 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 4 with WatchEvent

use of io.fabric8.kubernetes.api.model.WatchEvent 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 5 with WatchEvent

use of io.fabric8.kubernetes.api.model.WatchEvent 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)

Aggregations

WatchEvent (io.fabric8.kubernetes.api.model.WatchEvent)6 Pod (io.fabric8.kubernetes.api.model.Pod)5 PodListBuilder (io.fabric8.kubernetes.api.model.PodListBuilder)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.jupiter.api.Test)4 OpenShiftMockServer (io.fabric8.openshift.client.server.mock.OpenShiftMockServer)2 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)1 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)1 LabelSelectorBuilder (io.fabric8.kubernetes.api.model.LabelSelectorBuilder)1 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 ProcessUtil (io.fabric8.maven.core.util.ProcessUtil)1 WebServerEventCollector (io.fabric8.maven.core.util.WebServerEventCollector)1 Logger (io.fabric8.maven.docker.util.Logger)1 Build (io.fabric8.openshift.api.model.Build)1 BuildBuilder (io.fabric8.openshift.api.model.BuildBuilder)1 BuildConfig (io.fabric8.openshift.api.model.BuildConfig)1 BuildConfigBuilder (io.fabric8.openshift.api.model.BuildConfigBuilder)1 ImageStream (io.fabric8.openshift.api.model.ImageStream)1 ImageStreamBuilder (io.fabric8.openshift.api.model.ImageStreamBuilder)1