Skip to content

Commit

Permalink
Added Integration test for CustomResource with multiple versions
Browse files Browse the repository at this point in the history
+ Added IT for a CustomResource with two different versions v1 and
  v1beta1 using the same POJO by providing different
  CustomResourceDefinitionContexts

Related to fabric8io#2738 .
This test demonstrates that it's still supported. However we need to
discuss this whether this method should be marked as deprecatd or not
  • Loading branch information
rohanKanojia committed Mar 15, 2021
1 parent fced0b7 commit 27b9c8e
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kubernetes-itests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>crd-generator</artifactId>
<artifactId>crd-generator-apt</artifactId>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.bicycle;

import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;

@Version("v1")
@Group("demos.fabric8.io")
public class Bicycle extends CustomResource<BicycleSpec, BicycleStatus> {
private String apiVersion;

@Override
public void setApiVersion(String version) {
this.apiVersion = version;
}

@Override
public String getApiVersion() {
return this.apiVersion;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.bicycle;

import io.fabric8.kubernetes.client.CustomResourceList;

public class BicycleList extends CustomResourceList<Bicycle> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.bicycle;

public class BicycleSpec {
private int cadence;
private int gear;
private int speed;
private String modelName;

public int getCadence() {
return cadence;
}

public void setCadence(int cadence) {
this.cadence = cadence;
}

public int getGear() {
return gear;
}

public void setGear(int gear) {
this.gear = gear;
}

public int getSpeed() {
return speed;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public String getModelName() {
return modelName;
}

public void setModelName(String modelName) {
this.modelName = modelName;
}

@Override
public String toString() {
return "BicycleSpec{" +
"modelName=" + this.modelName +
"gear=" + this.gear +
"speed=" + this.speed +
"cadence=" + this.cadence + "}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.crd.bicycle;

public class BicycleStatus {
private boolean isInUse;
public boolean isInUse() {
return isInUse;
}

public void setInUse(boolean inUse) {
isInUse = inUse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes;

import io.fabric8.commons.AssumingK8sVersionAtLeast;
import io.fabric8.commons.ClusterEntity;
import io.fabric8.crd.bicycle.Bicycle;
import io.fabric8.crd.bicycle.BicycleList;
import io.fabric8.crd.bicycle.BicycleSpec;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.dsl.MixedOperation;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;
import org.arquillian.cube.kubernetes.impl.requirement.RequiresKubernetes;
import org.arquillian.cube.requirement.ArquillianConditionalRunner;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(ArquillianConditionalRunner.class)
@RequiresKubernetes
public class CustomResourceDefinitionMultipleVersionsSamePojoTest {
private MixedOperation<Bicycle, BicycleList, Resource<Bicycle>> v1BicycleClient;
private MixedOperation<Bicycle, BicycleList, Resource<Bicycle>> v1beta1BicycleClient;

@ArquillianResource
KubernetesClient client;

@ClassRule
public static final AssumingK8sVersionAtLeast assumingK8sVersion = new AssumingK8sVersionAtLeast("1", "16");

@BeforeClass
public static void init() {
ClusterEntity.apply(CustomResourceDefinitionMultipleVersionsSamePojoTest.class.getResourceAsStream("/bicycle-crd.yml"));
}

@Before
public void initPetClientAndCurrentNamespace() {
v1BicycleClient = client.customResources(new CustomResourceDefinitionContext.Builder()
.withGroup("demos.fabric8.io")
.withPlural("bicycles")
.withVersion("v1")
.withScope("Cluster")
.build(), Bicycle.class, BicycleList.class);
v1beta1BicycleClient = client.customResources(new CustomResourceDefinitionContext.Builder()
.withGroup("demos.fabric8.io")
.withPlural("bicycles")
.withVersion("v1beta1")
.withScope("Cluster")
.build(), Bicycle.class, BicycleList.class);
}

@Test
public void v1CreateListDelete() {
// Create
String name = "santa-cruz-heckler-mx-carbon";
Bicycle bicycle = createBicycle("v1", name, 60, 8, 20, "Heckler");
Bicycle createdBicycle = v1BicycleClient.create(bicycle);
assertThat(createdBicycle).isNotNull();

// List
BicycleList bicycleList = v1BicycleClient.list();
assertThat(bicycleList).isNotNull();
assertThat(bicycleList.getItems()).hasSizeGreaterThanOrEqualTo(1);

// Delete
Boolean isDeleted = v1BicycleClient.withName(name).delete();
assertThat(isDeleted).isTrue();
}

@Test
public void v1beta1CreateListDelete() {
// Create
String name = "pinarello-grevil-grx-gravel";
Bicycle bicycle = createBicycle("v1beta1", name, 68, 4, 18, "GRX 1x 650b Gravel");
Bicycle createdBicycle = v1beta1BicycleClient.create(bicycle);
assertThat(createdBicycle).isNotNull();

// List
BicycleList bicycleList = v1beta1BicycleClient.list();
assertThat(bicycleList).isNotNull();
assertThat(bicycleList.getItems()).hasSizeGreaterThanOrEqualTo(1);

// Delete
Boolean isDeleted = v1beta1BicycleClient.withName(name).delete();
assertThat(isDeleted).isTrue();
}

@AfterClass
public static void cleanup() {
ClusterEntity.remove(CustomResourceDefinitionMultipleVersionsSamePojoTest.class.getResourceAsStream("/bicycle-crd.yml"));
}

private Bicycle createBicycle(String version, String name, int cadence, int gear, int speed, String modelName) {
Bicycle bicycle = new Bicycle();
bicycle.setApiVersion(bicycle.getGroup() + "/" + version);
bicycle.setMetadata(new ObjectMetaBuilder().withName(name).build());
BicycleSpec bicycleSpec = new BicycleSpec();

bicycleSpec.setCadence(cadence);
bicycleSpec.setGear(gear);
bicycleSpec.setSpeed(speed);
bicycleSpec.setModelName(modelName);

return bicycle;
}
}
80 changes: 80 additions & 0 deletions kubernetes-itests/src/test/resources/bicycle-crd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#
# Copyright (C) 2015 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: bicycles.demos.fabric8.io
spec:
group: demos.fabric8.io
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cadence:
type: integer
gear:
type: integer
speed:
type: integer
modelName:
type: string
status:
type: object
properties:
isInUse:
type: boolean
subresources:
status: {}
- name: v1beta1
served: true
storage: false
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
cadence:
type: integer
gear:
type: integer
speed:
type: integer
modelName:
type: string
status:
type: object
properties:
isInUse:
type: boolean
subresources:
status: {}
scope: Cluster
names:
plural: bicycles
singular: bicycle
kind: Bicycle
shortNames:
- bicycle
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@
<artifactId>crd-generator</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>crd-generator-apt</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-model-core</artifactId>
Expand Down

0 comments on commit 27b9c8e

Please sign in to comment.