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