Skip to content
This repository has been archived by the owner on Aug 12, 2018. It is now read-only.

Commit

Permalink
OpenShift test assistant
Browse files Browse the repository at this point in the history
  • Loading branch information
Gytis Trikleris committed Mar 30, 2017
0 parents commit 78b41a2
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
*.iml
target
39 changes: 39 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.obsidian.booster.common</groupId>
<artifactId>booster-common</artifactId>
<version>1-SNAPSHOT</version>
<properties>
<assertj.version>3.6.1</assertj.version>
<awaitifily.version>1.7.0</awaitifily.version>
<rest-assured.version>2.9.0</rest-assured.version>
<openshift-client.version>1.4.34</openshift-client.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
</dependency>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>openshift-client</artifactId>
<version>${openshift-client.version}</version>
</dependency>
<dependency>
<groupId>com.jayway.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>${awaitifily.version}</version>
</dependency>
</dependencies>
</project>
159 changes: 159 additions & 0 deletions src/main/java/io/obsidian/booster/common/OpenShiftTestAssistant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright 2016-2017 Red Hat, Inc, and individual contributors.
*
* 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.obsidian.booster.common;

import com.jayway.restassured.RestAssured;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.dsl.NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.fabric8.openshift.api.model.DeploymentConfig;
import io.fabric8.openshift.api.model.Route;
import io.fabric8.openshift.client.OpenShiftClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.jayway.awaitility.Awaitility.await;
import static org.assertj.core.api.Assertions.assertThat;

/**
* @author <a href="http://escoffier.me">Clement Escoffier</a>
*/
public class OpenShiftTestAssistant {

private final OpenShiftClient client;
private final String project;
private String applicationName;
private Map<String, NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata, Boolean>> created
= new LinkedHashMap<>();

public OpenShiftTestAssistant() {
client = new DefaultKubernetesClient().adapt(OpenShiftClient.class);
project = client.getNamespace();
}

public List<? extends HasMetadata> deploy(String name, File template) throws IOException {
try (FileInputStream fis = new FileInputStream(template)) {
NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata, Boolean> declarations = client.load(fis);
List<HasMetadata> entities = declarations.createOrReplace();
created.put(name, declarations);
System.out.println(name + " deployed, " + entities.size() + " object(s) created.");

return entities;
}
}

public String deployApplication() throws IOException {
applicationName = System.getProperty("app.name");

List<? extends HasMetadata> entities
= deploy("application", new File("target/classes/META-INF/fabric8/openshift.yml"));

Optional<String> first = entities.stream()
.filter(hm -> hm instanceof DeploymentConfig)
.map(hm -> (DeploymentConfig) hm)
.map(dc -> dc.getMetadata()
.getName())
.findFirst();
if (applicationName == null && first.isPresent()) {
applicationName = first.get();
}

Route route = client.adapt(OpenShiftClient.class)
.routes()
.inNamespace(project)
.withName(applicationName)
.get();
assertThat(route).isNotNull();
RestAssured.baseURI = "http://" + Objects.requireNonNull(route)
.getSpec()
.getHost();
System.out.println("Route url: " + RestAssured.baseURI);

return applicationName;
}

public void cleanup() {
List<String> keys = new ArrayList<>(created.keySet());
Collections.reverse(keys);
for (String key : keys) {
System.out.println("Deleting " + key);
created.remove(key)
.delete();
}
}

public void awaitApplicationReadinessOrFail() {
await().atMost(5, TimeUnit.MINUTES)
.until(() -> {
List<Pod> list = client.pods()
.inNamespace(project)
.list()
.getItems();
return list.stream()
.filter(pod ->
pod.getMetadata()
.getName()
.startsWith(applicationName))
.filter(this::isRunning)
.collect(Collectors.toList())
.size() >= 1;
}
);

}

private boolean isRunning(Pod pod) {
return "running".equalsIgnoreCase(pod.getStatus()
.getPhase());
}


public OpenShiftClient client() {
return client;
}

public String project() {
return project;
}

public String applicationName() {
return applicationName;
}

public void awaitPodReadinessOrFail(Predicate<Pod> filter) {
await().atMost(5, TimeUnit.MINUTES)
.until(() -> {
List<Pod> list = client.pods()
.inNamespace(project)
.list()
.getItems();
return list.stream()
.filter(filter)
.filter(this::isRunning)
.collect(Collectors.toList())
.size() >= 1;
}
);
}
}

0 comments on commit 78b41a2

Please sign in to comment.