Search in sources :

Example 36 with KubernetesListBuilder

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

the class VolumePermissionEnricherTest method testAdapt.

@Test
public void testAdapt() throws Exception {
    final TestConfig[] data = new TestConfig[] { new TestConfig(null, null), new TestConfig(null, VolumePermissionEnricher.ENRICHER_NAME, "volumeA"), new TestConfig(null, VolumePermissionEnricher.ENRICHER_NAME, "volumeA", "volumeB") };
    for (final TestConfig tc : data) {
        final ProcessorConfig config = new ProcessorConfig(null, null, Collections.singletonMap(VolumePermissionEnricher.ENRICHER_NAME, new TreeMap(Collections.singletonMap(VolumePermissionEnricher.Config.permission.name(), tc.permission))));
        // Setup mock behaviour
        new Expectations() {

            {
                context.getConfig();
                result = config;
            }
        };
        VolumePermissionEnricher enricher = new VolumePermissionEnricher(context);
        PodTemplateBuilder ptb = createEmptyPodTemplate();
        for (String vn : tc.volumeNames) {
            ptb = addVolume(ptb, vn);
        }
        KubernetesListBuilder klb = new KubernetesListBuilder().addToPodTemplateItems(ptb.build());
        enricher.adapt(klb);
        PodTemplate pt = (PodTemplate) klb.buildItem(0);
        String initContainers = pt.getTemplate().getMetadata().getAnnotations().get(InitContainerHandler.INIT_CONTAINER_ANNOTATION);
        boolean shouldHaveInitContainer = tc.volumeNames.length > 0;
        assertEquals(shouldHaveInitContainer, initContainers != null);
        if (!shouldHaveInitContainer) {
            continue;
        }
        JSONArray ja = new JSONArray(initContainers);
        assertEquals(1, ja.length());
        JSONObject jo = ja.getJSONObject(0);
        assertEquals(tc.initContainerName, jo.get("name"));
        String permission = Strings.isNullOrBlank(tc.permission) ? "777" : tc.permission;
        JSONArray chmodCmd = new JSONArray();
        chmodCmd.put("chmod");
        chmodCmd.put(permission);
        for (String vn : tc.volumeNames) {
            chmodCmd.put("/tmp/" + vn);
        }
        assertEquals(chmodCmd.toString(), jo.getJSONArray("command").toString());
    }
}
Also used : Expectations(mockit.Expectations) JSONArray(org.json.JSONArray) TreeMap(java.util.TreeMap) ProcessorConfig(io.fabric8.maven.core.config.ProcessorConfig) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 37 with KubernetesListBuilder

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

the class DockerHealthCheckEnricherTest method testEnrichFromDoubleImage.

@Test
public void testEnrichFromDoubleImage() throws Exception {
    // Setup mock behaviour
    new Expectations() {

        {
            context.getImages();
            result = Arrays.asList(new ImageConfiguration.Builder().alias("myImage").buildConfig(new BuildImageConfiguration.Builder().healthCheck(new HealthCheckConfiguration.Builder().mode(HealthCheckMode.cmd).cmd("/bin/check").timeout("1s").interval("1h1s").retries(3).build()).build()).build(), new ImageConfiguration.Builder().alias("myImage2").buildConfig(new BuildImageConfiguration.Builder().healthCheck(new HealthCheckConfiguration.Builder().mode(HealthCheckMode.cmd).cmd("/xxx/check").timeout("3s").interval("3h1s").retries(9).build()).build()).build());
        }
    };
    KubernetesListBuilder builder = addDeployment(createDeployment("myImage"), "myImage2");
    DockerHealthCheckEnricher enricher = new DockerHealthCheckEnricher(context);
    enricher.addMissingResources(builder);
    KubernetesList list = builder.build();
    assertEquals(2, list.getItems().size());
    assertHealthCheckMatching(builder.build().getItems().get(0), "livenessProbe", "/bin/check", 1, 3601, 3);
    assertHealthCheckMatching(builder.build().getItems().get(0), "readinessProbe", "/bin/check", 1, 3601, 3);
    assertHealthCheckMatching(builder.build().getItems().get(1), "livenessProbe", "/xxx/check", 3, 10801, 9);
    assertHealthCheckMatching(builder.build().getItems().get(1), "readinessProbe", "/xxx/check", 3, 10801, 9);
}
Also used : Expectations(mockit.Expectations) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Test(org.junit.Test)

Example 38 with KubernetesListBuilder

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

the class DockerHealthCheckEnricherTest method testInvalidHealthCheck.

@Test
public void testInvalidHealthCheck() throws Exception {
    // Setup mock behaviour
    new Expectations() {

        {
            context.getImages();
            result = Arrays.asList(new ImageConfiguration.Builder().alias("myImage").buildConfig(new BuildImageConfiguration.Builder().healthCheck(new HealthCheckConfiguration.Builder().mode(HealthCheckMode.none).build()).build()).build());
        }
    };
    KubernetesListBuilder builder = createDeployment("myImage");
    DockerHealthCheckEnricher enricher = new DockerHealthCheckEnricher(context);
    enricher.addMissingResources(builder);
    KubernetesList list = builder.build();
    assertEquals(1, list.getItems().size());
    assertNoProbes(list.getItems().get(0));
}
Also used : Expectations(mockit.Expectations) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Test(org.junit.Test)

Example 39 with KubernetesListBuilder

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

the class DependencyEnricher method filterAndAddItemsToBuilder.

public void filterAndAddItemsToBuilder(KubernetesListBuilder builder, List<HasMetadata> items) {
    Map<KindAndName, Integer> aIndexMap = new HashMap<>();
    int nItems = 0;
    // Populate map with existing items in the builder
    for (int index = 0; index < builder.getItems().size(); index++, nItems++) {
        HasMetadata aItem = builder.getItems().get(index);
        KindAndName aKey = new KindAndName(aItem);
        aIndexMap.put(aKey, index);
    }
    for (HasMetadata item : items) {
        KindAndName aKey = new KindAndName(item);
        if (aIndexMap.containsKey(aKey)) {
            // Merge the override fragments, and remove duplicate
            HasMetadata duplicateItem = builder.getItems().get(aIndexMap.get(aKey));
            item = KubernetesResourceUtil.mergeResources(item, duplicateItem, log, false);
            builder.setToItems(aIndexMap.get(aKey), item);
        } else {
            aIndexMap.put(aKey, nItems++);
            builder.addToItems(item);
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) KindAndName(io.fabric8.maven.core.util.KindAndName)

Example 40 with KubernetesListBuilder

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

the class DependencyEnricherTest method checkDuplicatesInResource.

@Test
public void checkDuplicatesInResource() throws Exception {
    // Generate given Resources
    KubernetesListBuilder aBuilder = createResourcesForTest();
    // Enrich
    KubernetesList aResourceList = enrichResources(aBuilder);
    // Assert
    assertTrue(aResourceList.getItems() != null);
    assertEquals(checkUniqueResources(aResourceList.getItems()), true);
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Test(org.junit.Test)

Aggregations

KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)30 Test (org.junit.Test)25 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)17 Expectations (mockit.Expectations)13 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)5 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 File (java.io.File)5 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)3 Secret (io.fabric8.kubernetes.api.model.Secret)3 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)3 IOException (java.io.IOException)3 Map (java.util.Map)3 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 Visitable (io.fabric8.kubernetes.api.builder.Visitable)2 Visitor (io.fabric8.kubernetes.api.builder.Visitor)2 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)2