Search in sources :

Example 1 with Block

use of io.fabric8.utils.Block in project fabric8 by fabric8io.

the class Example method main.

public static void main(String[] args) {
    try {
        final KubernetesClient client = new DefaultKubernetesClient();
        assertThat(client).pods().runningStatus().hasSize(6);
        assertThat(client).pods().runningStatus().filterLabel("provider", "fabric8").assertSize().isGreaterThan(0);
        assertThat(client.services().inNamespace("default").withName("fabric8").get().getMetadata()).name().isEqualTo("fabric8");
        Map<String, String> consoleLabels = new HashMap<>();
        consoleLabels.put("component", "console");
        consoleLabels.put("provider", "fabric8");
        assertThat(client).podsForService("fabric8").runningStatus().extracting("metadata").extracting("labels").contains(consoleLabels);
        assertThat(client).podsForService("fabric8").runningStatus().hasSize(1).extracting("metadata").extracting("labels").contains(consoleLabels);
        assertThat(client).podsForService("fabric8").logs().doesNotContainText("Exception", "Error");
        assertThat(client).pods().logs().doesNotContainText("Exception", "Error");
        assertAssertionError(new Block() {

            @Override
            public void invoke() throws Exception {
                try {
                    assertThat(client.services().inNamespace("default").withName("doesNotExist").get().getMetadata()).name().isEqualTo("fabric8-console-controller");
                } catch (KubernetesClientException e) {
                    if (e.getCode() != 404) {
                        throw e;
                    } else {
                        throw new AssertionError(e);
                    }
                }
            }
        });
        assertAssertionError(new Block() {

            @Override
            public void invoke() throws Exception {
                try {
                    assertThat(client).pods().runningStatus().filterLabel("component", "doesNotExist").hasSize(1);
                } catch (KubernetesClientException e) {
                    if (e.getCode() != 404) {
                        throw e;
                    } else {
                        throw new AssertionError(e);
                    }
                }
            }
        });
        System.out.println("Done!");
    } catch (Throwable e) {
        System.out.println("Caught: " + e);
        e.printStackTrace();
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) HashMap(java.util.HashMap) Asserts.assertAssertionError(io.fabric8.utils.Asserts.assertAssertionError) Block(io.fabric8.utils.Block) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 2 with Block

use of io.fabric8.utils.Block in project fabric8 by fabric8io.

the class ExampleTest method testNullNavigationOnPod.

@Test
public void testNullNavigationOnPod() throws Exception {
    final Pod pod = new Pod();
    pod.setMetadata(null);
    assertAssertionError(new Block() {

        @Override
        public void invoke() throws Exception {
            assertThat(pod).metadata().name().isEqualTo("cheese");
        }
    });
}
Also used : Pod(io.fabric8.kubernetes.api.model.Pod) Block(io.fabric8.utils.Block) Test(org.junit.Test)

Example 3 with Block

use of io.fabric8.utils.Block in project fabric8 by fabric8io.

the class ExampleTest method testNavigationListAssertions.

@Test
public void testNavigationListAssertions() throws Exception {
    final String id1 = "abc";
    final String id2 = "def";
    Map<String, String> labels1 = new HashMap<>();
    labels1.put("foo", "bar");
    Map<String, String> labels2 = new HashMap<>();
    labels2.put("whatnot", "cheese");
    final Pod pod1 = new Pod();
    pod1.setMetadata(new ObjectMeta());
    pod1.getMetadata().setName(id1);
    pod1.getMetadata().setLabels(labels1);
    final Pod pod2 = new Pod();
    pod2.setMetadata(new ObjectMeta());
    pod2.getMetadata().setName(id2);
    pod2.getMetadata().setLabels(labels2);
    final PodList emptyPodList = new PodList();
    final PodList podList = new PodList();
    podList.setItems(new ArrayList<Pod>(Arrays.asList(pod1, pod2)));
    assertThat(emptyPodList).describedAs("emptyPodList").items().isEmpty();
    assertThat(podList).describedAs("podListWith2Items").items().first().metadata().name().isEqualTo(id1);
    assertThat(podList).describedAs("podListWith2Items").items().last().metadata().name().isEqualTo(id2);
    assertAssertionError(new Block() {

        @Override
        public void invoke() throws Exception {
            assertThat(podList).describedAs("podListWith2Items").items().item(-1).isNotNull();
        }
    });
    assertAssertionError(new Block() {

        @Override
        public void invoke() throws Exception {
            assertThat(podList).describedAs("podListWith2Items").items().item(2).isNotNull();
        }
    });
    assertAssertionError(new Block() {

        @Override
        public void invoke() throws Exception {
            assertThat(podList).describedAs("podListWith2Items").items().first().metadata().name().isEqualTo("shouldNotMatch");
        }
    });
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) HashMap(java.util.HashMap) Block(io.fabric8.utils.Block) Test(org.junit.Test)

Example 4 with Block

use of io.fabric8.utils.Block in project fabric8 by fabric8io.

the class ExampleTest method testNullNavigationOnRC.

@Test
public void testNullNavigationOnRC() throws Exception {
    final ReplicationController rc = new ReplicationController();
    assertAssertionError(new Block() {

        @Override
        public void invoke() throws Exception {
            assertThat(rc).spec().template().spec().containers().first().image().isEqualTo("someDockerImageName");
        }
    });
}
Also used : ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) Block(io.fabric8.utils.Block) Test(org.junit.Test)

Example 5 with Block

use of io.fabric8.utils.Block in project fabric8 by fabric8io.

the class ExampleTest method testJsonArrayOperation.

@Test
public void testJsonArrayOperation() throws Exception {
    final JSONArrayAssert dumpAllThreads = assertThat(client).jsonArrayOperation("java.lang:type=Threading", "dumpAllThreads", true, true);
    dumpAllThreads.assertSize().isGreaterThan(1);
    int size = dumpAllThreads.get().size();
    for (int i = 0; i < size; i++) {
        JSONObjectAssert object = dumpAllThreads.assertJSONObject(i);
        object.isNotNull();
        object.assertString("threadName").isNotEmpty();
    }
    // lets try access an invalid array
    final int badIndex = size + 100;
    assertAssertionError(new Block() {

        @Override
        public void invoke() throws Exception {
            dumpAllThreads.assertJSONObject(badIndex);
        }
    });
}
Also used : Block(io.fabric8.utils.Block) Test(org.junit.Test)

Aggregations

Block (io.fabric8.utils.Block)11 Test (org.junit.Test)7 Pod (io.fabric8.kubernetes.api.model.Pod)4 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 JobWithDetails (com.offbytwo.jenkins.model.JobWithDetails)1 ResolverPolicyEnum (io.fabric8.boot.commands.support.ResolverPolicyEnum)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)1 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)1 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)1 LogDispatcher (io.fabric8.maven.docker.log.LogDispatcher)1 Container (io.fabric8.maven.docker.model.Container)1 QueryService (io.fabric8.maven.docker.service.QueryService)1 Asserts.assertAssertionError (io.fabric8.utils.Asserts.assertAssertionError)1 BootstrapConfiguration (io.fabric8.zookeeper.bootstrap.BootstrapConfiguration)1