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);
}
}
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;
}
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");
}
}
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");
}
}
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");
}
}
Aggregations