Permalink
Browse files

Add integration test for service to service communication

  • Loading branch information...
schrieveslaach committed Dec 4, 2018
1 parent 61ad8ee commit f339944aeddc613b5239eb31270d12ea5f5c1330
@@ -31,6 +31,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
@@ -29,17 +29,32 @@
import com.aixigo.preview.servant.rest.junit.extension.PREvantRestApiExtension;
import com.aixigo.preview.servant.rest.model.ServiceConfiguration;
import io.restassured.response.ValidatableResponse;
import org.junit.Rule;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.migrationsupport.rules.EnableRuleMigrationSupport;
import org.junit.rules.TemporaryFolder;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URI;

import static io.restassured.RestAssured.given;
import static java.util.Collections.singletonList;

@EnableRuleMigrationSupport
@ExtendWith(PREvantRestApiExtension.class)
class PREvantRestApiDockerTest {

/**
* TODO: Replace {@code TempDirectory} extension when junit 5.4.0 has been released
*/
@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
void shouldDeployDockerContainer_WhenRequestToDeployService(URI restApiURI) throws Exception {
String uri = postServiceConfiguration(restApiURI, "master", "nginx", "library", "nginx")
@@ -56,16 +71,68 @@ void shouldDeployDockerContainer_WhenRequestToDeployService(URI restApiURI) thro
.statusCode(200);
}

@Test
void shouldLinkDockerContainerToNetwork_WhenRequestToDeployService(URI restApiURI) throws Exception {
postServiceConfiguration(restApiURI, "master", "httpd", "library", "httpd");
postServiceConfiguration(restApiURI, "master",
new ServiceConfiguration("nginx", "library", "nginx")
.addVolume(createNginxConfigVolume()));

postServiceConfiguration(restApiURI, "master", "httpd", "library", "httpd");

given().baseUri(restApiURI.toString())
.get("/master/nginx")
.then()
.statusCode(200);


deleteApp(restApiURI, "master")
.statusCode(200);
}

private String createNginxConfigVolume() throws IOException {
File nginxConfig = temporaryFolder.newFile();
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(nginxConfig)))) {
bw.write("server {");
bw.newLine();
bw.write(" resolver 127.0.0.11 valid=0s;");
bw.newLine();
bw.write(" listen 80;");
bw.newLine();
bw.write(" server_name localhost;");
bw.newLine();
bw.write(" access_log /var/log/nginx/access.log main;");
bw.newLine();
bw.write(" location / {");
bw.newLine();
bw.write(" set $upstream httpd:80;");
bw.newLine();
bw.write(" proxy_pass http://$upstream;");
bw.newLine();
bw.write(" }");
bw.newLine();
bw.write("}");
bw.newLine();
}

return nginxConfig.getAbsolutePath() + ":/etc/nginx/conf.d/default.conf";
}

private ValidatableResponse postServiceConfiguration(URI restApiURI, String appName, String serviceName, String imageUser, String imageRepository) {
return postServiceConfiguration(restApiURI, appName, new ServiceConfiguration(serviceName, imageUser, imageRepository));
}

private ValidatableResponse postServiceConfiguration(URI restApiURI, String appName, ServiceConfiguration serviceConfiguration) {
return given()
.contentType("application/json")
.baseUri(restApiURI.toString())
.body(singletonList(new ServiceConfiguration(serviceName, imageUser, imageRepository)))
.body(singletonList(serviceConfiguration))
.when()
.post("api/apps/" + appName)
.then();
}


private ValidatableResponse deleteApp(URI restApiURI, String appName) {
return given()
.contentType("application/json")
@@ -26,11 +26,14 @@
* =========================LICENSE_END==================================
*/

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@AllArgsConstructor
import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@Data
public class ServiceConfiguration {

@@ -42,4 +45,11 @@

@NonNull
private final String imageRepository;

private List<String> volumes = new ArrayList<>();

public ServiceConfiguration addVolume(String volume) {
volumes.add(volume);
return this;
}
}
10 pom.xml
@@ -17,8 +17,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>

<license.licenseName>mit</license.licenseName>
<license.processStartTag>========================LICENSE_START=================================</license.processStartTag>
@@ -69,6 +69,12 @@
<version>${junit.jupiter.version}</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-migrationsupport</artifactId>
<version>${junit.jupiter.version}</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

0 comments on commit f339944

Please sign in to comment.