Skip to content

Commit

Permalink
Add an integration test for BuildConfig instantiation
Browse files Browse the repository at this point in the history
We instantiate BuildConfig in Eclipse JKube while doing S2I
builds. Adding an integration test for that so that we don't break this
again in future.

Related to fabric8io#2668
  • Loading branch information
rohanKanojia committed Dec 22, 2020
1 parent 22e2e6f commit 839ef1f
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 0 deletions.
Expand Up @@ -18,9 +18,15 @@

import io.fabric8.commons.ClusterEntity;
import io.fabric8.commons.ReadyEntity;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import io.fabric8.openshift.api.model.Build;
import io.fabric8.openshift.api.model.BuildConfig;
import io.fabric8.openshift.api.model.BuildConfigBuilder;
import io.fabric8.openshift.api.model.BuildConfigList;
import io.fabric8.openshift.api.model.BuildRequest;
import io.fabric8.openshift.api.model.BuildRequestBuilder;
import io.fabric8.openshift.api.model.BuildSourceBuilder;
import io.fabric8.openshift.client.OpenShiftClient;
import org.arquillian.cube.kubernetes.api.Session;
Expand All @@ -31,8 +37,11 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static io.fabric8.kubernetes.client.utils.KubernetesResourceUtil.getName;
import static io.fabric8.kubernetes.client.utils.KubernetesResourceUtil.getOrCreateAnnotations;
import static junit.framework.TestCase.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
Expand Down Expand Up @@ -108,4 +117,53 @@ public void createOrReplace() {
assertEquals("bc-createorreplace", buildConfig.getMetadata().getName());
assertEquals("https://github.com/openshift/test2", buildConfig.getSpec().getSource().getGit().getUri());
}

@Test
public void instantiateAndWatchBuild() throws InterruptedException {
// Setup
client.load(getClass().getResourceAsStream("/ruby-new-app.yml")).inNamespace(session.getNamespace()).createOrReplace();
await().atMost(10, TimeUnit.SECONDS).until(() -> client.imageStreamTags().inNamespace(session.getNamespace()).withName("ruby-25-centos7:latest").get() != null);

// Given
BuildRequest buildRequest = new BuildRequestBuilder().withNewMetadata().withName("ruby-hello-world").endMetadata().build();
CountDownLatch buildEventReceivedLatch = new CountDownLatch(1);

// When
Build startedBuild = client.buildConfigs().inNamespace(session.getNamespace()).withName("ruby-hello-world").instantiate(buildRequest);
TestBuildWatcher testBuildWatcher = new TestBuildWatcher(buildEventReceivedLatch, getName(startedBuild));
Watch watcher = client.builds().inNamespace(session.getNamespace()).withName(getName(startedBuild)).watch(testBuildWatcher);

// Then
assertNotNull(startedBuild);
assertEquals("ruby-hello-world", getOrCreateAnnotations(startedBuild).get("openshift.io/build-config.name"));
assertTrue(buildEventReceivedLatch.await(5, TimeUnit.SECONDS));

// Cleanup
watcher.close();
client.load(getClass().getResourceAsStream("/ruby-new-app.yml")).inNamespace(session.getNamespace()).delete();
}

private static class TestBuildWatcher implements Watcher<Build> {
private final String resourceName;
private final CountDownLatch eventRecievedForResourceLatch;

private TestBuildWatcher(CountDownLatch countDownLatch, String resourceName) {
this.resourceName = resourceName;
this.eventRecievedForResourceLatch = countDownLatch;
}

@Override
public void eventReceived(Action action, Build resource) {
if (getName(resource).equals(resourceName)) {
eventRecievedForResourceLatch.countDown();
}
}

@Override
public void onClose(WatcherException cause) { }

public CountDownLatch getEventRecievedForResourceLatch() {
return this.eventRecievedForResourceLatch;
}
}
}
177 changes: 177 additions & 0 deletions kubernetes-itests/src/test/resources/ruby-new-app.yml
@@ -0,0 +1,177 @@
#
# 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.
#

---
kind: List
apiVersion: v1
metadata: {}
items:
- kind: ImageStreamTag
apiVersion: image.openshift.io/v1
metadata:
name: ruby-25-centos7:latest
creationTimestamp:
labels:
app: ruby-hello-world
annotations:
openshift.io/generated-by: OpenShiftNewApp
openshift.io/imported-from: centos/ruby-25-centos7
tag:
name: latest
annotations:
from:
kind: DockerImage
name: centos/ruby-25-centos7
generation:
importPolicy: {}
referencePolicy:
type: ''
generation: 0
lookupPolicy:
local: false
image:
metadata:
creationTimestamp:
dockerImageMetadata:
kind: DockerImage
apiVersion: '1.0'
Id: ''
Created:
ContainerConfig: {}
dockerImageMetadataVersion: '1.0'
- kind: ImageStream
apiVersion: image.openshift.io/v1
metadata:
name: ruby-hello-world
creationTimestamp:
labels:
app: ruby-hello-world
annotations:
openshift.io/generated-by: OpenShiftNewApp
spec:
lookupPolicy:
local: false
status:
dockerImageRepository: ''
- kind: BuildConfig
apiVersion: build.openshift.io/v1
metadata:
name: ruby-hello-world
creationTimestamp:
labels:
app: ruby-hello-world
annotations:
openshift.io/generated-by: OpenShiftNewApp
spec:
triggers:
- type: GitHub
github:
secret: fNT8t2Euy4fGKmqWKwo7
- type: Generic
generic:
secret: XWUZamVhVLlNOAPDdSbS
- type: ConfigChange
- type: ImageChange
imageChange: {}
source:
type: Git
git:
uri: https://github.com/openshift/ruby-hello-world
strategy:
type: Docker
dockerStrategy:
from:
kind: ImageStreamTag
name: ruby-25-centos7:latest
output:
to:
kind: ImageStreamTag
name: ruby-hello-world:latest
resources: {}
postCommit: {}
nodeSelector:
status:
lastVersion: 0
- kind: DeploymentConfig
apiVersion: apps.openshift.io/v1
metadata:
name: ruby-hello-world
creationTimestamp:
labels:
app: ruby-hello-world
annotations:
openshift.io/generated-by: OpenShiftNewApp
spec:
strategy:
resources: {}
triggers:
- type: ConfigChange
- type: ImageChange
imageChangeParams:
automatic: true
containerNames:
- ruby-hello-world
from:
kind: ImageStreamTag
name: ruby-hello-world:latest
replicas: 1
test: false
selector:
app: ruby-hello-world
deploymentconfig: ruby-hello-world
template:
metadata:
creationTimestamp:
labels:
app: ruby-hello-world
deploymentconfig: ruby-hello-world
annotations:
openshift.io/generated-by: OpenShiftNewApp
spec:
containers:
- name: ruby-hello-world
image: ruby-hello-world:latest
ports:
- containerPort: 8080
protocol: TCP
resources: {}
status:
latestVersion: 0
observedGeneration: 0
replicas: 0
updatedReplicas: 0
availableReplicas: 0
unavailableReplicas: 0
- kind: Service
apiVersion: v1
metadata:
name: ruby-hello-world
creationTimestamp:
labels:
app: ruby-hello-world
annotations:
openshift.io/generated-by: OpenShiftNewApp
spec:
ports:
- name: 8080-tcp
protocol: TCP
port: 8080
targetPort: 8080
selector:
app: ruby-hello-world
deploymentconfig: ruby-hello-world
status:
loadBalancer: {}

0 comments on commit 839ef1f

Please sign in to comment.