Search in sources :

Example 1 with CustomResourceDefinitionCondition

use of io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionCondition in project strimzi by strimzi.

the class StructuralCrdIT method assertApiVersionsAreStructuralInApiextensionsV1.

private void assertApiVersionsAreStructuralInApiextensionsV1(String api, VersionRange<ApiVersion> shouldBeStructural) {
    Pattern pattern = Pattern.compile("[^.]spec\\.versions\\[([0-9]+)\\]\\.[^,]*?");
    CustomResourceDefinition crd = cluster.client().getClient().apiextensions().v1().customResourceDefinitions().withName(api).get();
    // We can't make the following assertion because the current version of fabric8 always requests
    // the CRD using v1beta1 api version, so the apiserver just replaces it and serves it.
    // assertEquals(crdApiVersion, ApiVersion.parse(crd.getApiVersion().replace("apiextensions.k8s.io/", "")));
    Set<ApiVersion> presentCrdApiVersions = crd.getSpec().getVersions().stream().map(v -> ApiVersion.parse(v.getName())).collect(Collectors.toSet());
    assertTrue(presentCrdApiVersions.contains(shouldBeStructural.lower()), "CRD has versions " + presentCrdApiVersions + " which doesn't include " + shouldBeStructural.lower() + " which should be structural");
    Map<Integer, ApiVersion> indexedVersions = new HashMap<>();
    int i = 0;
    for (CustomResourceDefinitionVersion version : crd.getSpec().getVersions()) {
        indexedVersions.put(i, ApiVersion.parse(version.getName()));
    }
    Optional<CustomResourceDefinitionCondition> first = crd.getStatus().getConditions().stream().filter(cond -> "NonStructuralSchema".equals(cond.getType()) && "True".equals(cond.getStatus())).findFirst();
    if (first.isPresent()) {
        Matcher matcher = pattern.matcher(first.get().getMessage());
        while (matcher.find()) {
            Integer index = Integer.valueOf(matcher.group(1));
            ApiVersion nonStructuralVersion = indexedVersions.get(index);
            if (shouldBeStructural.contains(nonStructuralVersion)) {
                fail(api + "/ " + nonStructuralVersion + " should be structural but there's a complaint about " + matcher.group());
            }
        }
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Set(java.util.Set) HashMap(java.util.HashMap) VersionRange(io.strimzi.api.annotations.VersionRange) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CustomResourceDefinitionCondition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionCondition) Matcher(java.util.regex.Matcher) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion) Map(java.util.Map) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TestUtils(io.strimzi.test.TestUtils) Optional(java.util.Optional) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) Pattern(java.util.regex.Pattern) ApiVersion(io.strimzi.api.annotations.ApiVersion) Pattern(java.util.regex.Pattern) ApiVersion(io.strimzi.api.annotations.ApiVersion) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) CustomResourceDefinitionCondition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionCondition) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion)

Example 2 with CustomResourceDefinitionCondition

use of io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionCondition in project strimzi-kafka-operator by strimzi.

the class StructuralCrdIT method assertApiVersionsAreStructuralInApiextensionsV1.

private void assertApiVersionsAreStructuralInApiextensionsV1(String api, VersionRange<ApiVersion> shouldBeStructural) {
    Pattern pattern = Pattern.compile("[^.]spec\\.versions\\[([0-9]+)\\]\\.[^,]*?");
    CustomResourceDefinition crd = cluster.client().getClient().apiextensions().v1().customResourceDefinitions().withName(api).get();
    // We can't make the following assertion because the current version of fabric8 always requests
    // the CRD using v1beta1 api version, so the apiserver just replaces it and serves it.
    // assertEquals(crdApiVersion, ApiVersion.parse(crd.getApiVersion().replace("apiextensions.k8s.io/", "")));
    Set<ApiVersion> presentCrdApiVersions = crd.getSpec().getVersions().stream().map(v -> ApiVersion.parse(v.getName())).collect(Collectors.toSet());
    assertTrue(presentCrdApiVersions.contains(shouldBeStructural.lower()), "CRD has versions " + presentCrdApiVersions + " which doesn't include " + shouldBeStructural.lower() + " which should be structural");
    Map<Integer, ApiVersion> indexedVersions = new HashMap<>();
    int i = 0;
    for (CustomResourceDefinitionVersion version : crd.getSpec().getVersions()) {
        indexedVersions.put(i, ApiVersion.parse(version.getName()));
    }
    Optional<CustomResourceDefinitionCondition> first = crd.getStatus().getConditions().stream().filter(cond -> "NonStructuralSchema".equals(cond.getType()) && "True".equals(cond.getStatus())).findFirst();
    if (first.isPresent()) {
        Matcher matcher = pattern.matcher(first.get().getMessage());
        while (matcher.find()) {
            Integer index = Integer.valueOf(matcher.group(1));
            ApiVersion nonStructuralVersion = indexedVersions.get(index);
            if (shouldBeStructural.contains(nonStructuralVersion)) {
                fail(api + "/ " + nonStructuralVersion + " should be structural but there's a complaint about " + matcher.group());
            }
        }
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Set(java.util.Set) HashMap(java.util.HashMap) VersionRange(io.strimzi.api.annotations.VersionRange) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) CustomResourceDefinitionCondition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionCondition) Matcher(java.util.regex.Matcher) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion) Map(java.util.Map) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) TestUtils(io.strimzi.test.TestUtils) Optional(java.util.Optional) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) Pattern(java.util.regex.Pattern) ApiVersion(io.strimzi.api.annotations.ApiVersion) Pattern(java.util.regex.Pattern) ApiVersion(io.strimzi.api.annotations.ApiVersion) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) CustomResourceDefinitionCondition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionCondition) CustomResourceDefinitionVersion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion)

Aggregations

CustomResourceDefinition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition)2 CustomResourceDefinitionCondition (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionCondition)2 CustomResourceDefinitionVersion (io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionVersion)2 ApiVersion (io.strimzi.api.annotations.ApiVersion)2 VersionRange (io.strimzi.api.annotations.VersionRange)2 TestUtils (io.strimzi.test.TestUtils)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Collectors (java.util.stream.Collectors)2 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)2 Assertions.fail (org.junit.jupiter.api.Assertions.fail)2 Test (org.junit.jupiter.api.Test)2