Skip to content

Commit

Permalink
Merge pull request #2689 from rohanKanojia/pr/buildconfig-instantiate-it
Browse files Browse the repository at this point in the history
  • Loading branch information
fusesource-ci authored Dec 23, 2020
2 parents b6e0f7f + 39756a1 commit 6a717e4
Show file tree
Hide file tree
Showing 2 changed files with 278 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* 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.openshift;

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.BuildRequest;
import io.fabric8.openshift.api.model.BuildRequestBuilder;
import io.fabric8.openshift.client.OpenShiftClient;
import org.arquillian.cube.kubernetes.api.Session;
import org.arquillian.cube.openshift.impl.requirement.RequiresOpenshift;
import org.arquillian.cube.requirement.ArquillianConditionalRunner;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.After;
import org.junit.Before;
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.awaitility.Awaitility.await;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@RunWith(ArquillianConditionalRunner.class)
@RequiresOpenshift
public class BuildConfigWatchIT {
@ArquillianResource
OpenShiftClient client;

@ArquillianResource
Session session;

@Before
public void initOcNewApp() {
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);
}

@Test
public void instantiateAndWatchBuild() throws InterruptedException {
// 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));
watcher.close();
}

@After
public void cleanOcNewApp() {
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) { }
}
}
177 changes: 177 additions & 0 deletions kubernetes-itests/src/test/resources/ruby-new-app.yml
Original file line number Diff line number Diff line change
@@ -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 6a717e4

Please sign in to comment.