refactor tests (#16)

This commit is contained in:
Anatolii Karlov 2019-07-23 13:17:31 +03:00 committed by GitHub
parent 2b0a6991db
commit 2d3815ecca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 191 deletions

2
Jenkinsfile vendored
View File

@ -14,5 +14,5 @@ build('file-storage', 'java-maven') {
def registry = 'dr2.rbkmoney.com'
def registryCredsId = 'jenkins_harbor'
javaServicePipeline(serviceName, useJava11, mvnArgs)
javaServicePipeline(serviceName, useJava11, mvnArgs, registry, registryCredsId)
}

View File

@ -1,9 +1,30 @@
version: '3'
version: '2.3'
services:
ceph-container:
file-storage:
build: target/
image: file-storage:1.0.0-SNAPSHOT
ports:
- "8022:8022"
depends_on:
- ceph
networks:
- fsnet
environment:
SERVICE_NAME: "file-storage"
storage.endpoint: "ceph:80"
storage.signingRegion: "RU"
storage.accessKey: "test"
storage.secretKey: "test"
storage.clientProtocol: "HTTP"
storage.clientMaxErrorRetry: "10"
storage.bucketName: "test"
ceph:
image: 'dr.rbkmoney.com/ceph-demo:latest'
ports:
- "32827:80"
networks:
- fsnet
environment:
- RGW_NAME=localhost
- NETWORK_AUTO_DETECT=4
@ -11,3 +32,6 @@ services:
- CEPH_DEMO_ACCESS_KEY=test
- CEPH_DEMO_SECRET_KEY=test
- CEPH_DEMO_BUCKET=TEST
networks:
fsnet:

27
pom.xml
View File

@ -7,10 +7,11 @@
<groupId>com.rbkmoney</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/>
</parent>
<artifactId>file-storage</artifactId>
<version>0.9.2-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>file-storage</name>
@ -24,11 +25,10 @@
<server.port>8022</server.port>
<exposed.ports>${server.port}</exposed.ports>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<metrics.version>1.1.0</metrics.version>
<dockerfile.base.service.tag>c0d0900ec19c907d866f8360031a836049985110</dockerfile.base.service.tag>
<!-- <dockerfile.registry>dr2.rbkmoney.com</dockerfile.registry>-->
<dockerfile.registry>${env.REGISTRY}</dockerfile.registry>
<shared.resources.version>0.3.6</shared.resources.version>
<woody.thrift.version>1.1.15</woody.thrift.version>
@ -48,11 +48,6 @@
<artifactId>woody-thrift</artifactId>
<version>${woody.thrift.version}</version>
</dependency>
<dependency>
<groupId>com.rbkmoney.logback</groupId>
<artifactId>nop-rolling</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.rbkmoney.geck</groupId>
<artifactId>common</artifactId>
@ -78,6 +73,12 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
@ -130,7 +131,13 @@
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.8.3</version>
<scope>compile</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.rbkmoney</groupId>
<artifactId>easyway</artifactId>
<version>0.1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
@ -168,7 +175,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.5</version>
<version>1.6.0</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>

View File

@ -1,35 +0,0 @@
package com.rbkmoney;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.testcontainers.containers.GenericContainer;
import java.util.Optional;
@NoArgsConstructor
@Setter
public class TestContainers {
private Boolean dockerContainersEnable;
private GenericContainer cephTestContainer;
public Optional<GenericContainer> getCephTestContainer() {
return Optional.ofNullable(cephTestContainer);
}
public Boolean isDockerContainersEnable() {
return dockerContainersEnable;
}
public void startTestContainers() {
if (!isDockerContainersEnable()) {
getCephTestContainer().ifPresent(GenericContainer::start);
}
}
public void stopTestContainers() {
if (!isDockerContainersEnable()) {
getCephTestContainer().ifPresent(GenericContainer::stop);
}
}
}

View File

@ -1,68 +0,0 @@
package com.rbkmoney;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import java.time.Duration;
public class TestContainersBuilder {
public static final String SIGNING_REGION = "RU";
public static final String AWS_ACCESS_KEY = "test";
public static final String AWS_SECRET_KEY = "test";
public static final String PROTOCOL = "HTTP";
public static final String MAX_ERROR_RETRY = "10";
public static final String BUCKET_NAME = "TEST";
private boolean dockerContainersEnable;
private boolean cephTestContainerEnable;
private TestContainersBuilder(boolean dockerContainersEnable) {
this.dockerContainersEnable = dockerContainersEnable;
}
public static TestContainersBuilder builder(boolean dockerContainersEnable) {
return new TestContainersBuilder(dockerContainersEnable);
}
public TestContainersBuilder addCephTestContainer() {
cephTestContainerEnable = true;
return this;
}
public TestContainers build() {
TestContainers testContainers = new TestContainers();
if (!dockerContainersEnable) {
addTestContainers(testContainers);
} else {
testContainers.setDockerContainersEnable(true);
}
return testContainers;
}
private void addTestContainers(TestContainers testContainers) {
if (cephTestContainerEnable) {
testContainers.setCephTestContainer(
new GenericContainer<>("dr.rbkmoney.com/ceph-demo:latest")
.withEnv("RGW_NAME", "localhost")
.withEnv("NETWORK_AUTO_DETECT", "4")
.withEnv("CEPH_DEMO_UID", "ceph-test")
.withEnv("CEPH_DEMO_ACCESS_KEY", AWS_ACCESS_KEY)
.withEnv("CEPH_DEMO_SECRET_KEY", AWS_SECRET_KEY)
.withEnv("CEPH_DEMO_BUCKET", BUCKET_NAME)
.withExposedPorts(5000, 80)
.waitingFor(getWaitStrategy("/api/v0.1/health"))
);
}
testContainers.setDockerContainersEnable(false);
}
private WaitStrategy getWaitStrategy(String path) {
return new HttpWaitStrategy()
.forPath(path)
.forStatusCode(200)
.withStartupTimeout(Duration.ofMinutes(10));
}
}

View File

@ -1,27 +1,31 @@
package com.rbkmoney.file.storage;
import com.rbkmoney.TestContainers;
import com.rbkmoney.TestContainersBuilder;
import com.rbkmoney.easyway.AbstractTestUtils;
import com.rbkmoney.easyway.TestContainers;
import com.rbkmoney.easyway.TestContainersBuilder;
import com.rbkmoney.easyway.TestContainersParameters;
import com.rbkmoney.woody.thrift.impl.http.THSpawnClientBuilder;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import lombok.extern.slf4j.Slf4j;
import org.junit.ClassRule;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.containers.FailureDetectingExternalResource;
import java.io.IOException;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@ -29,21 +33,31 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
@SpringBootTest(webEnvironment = RANDOM_PORT)
@ContextConfiguration(classes = FileStorageApplication.class, initializers = AbstractIntegrationTest.Initializer.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public abstract class AbstractIntegrationTest {
@Slf4j
public abstract class AbstractIntegrationTest extends AbstractTestUtils {
private static TestContainers testContainers = TestContainersBuilder.builder(false)
private static TestContainers testContainers = TestContainersBuilder.builderWithTestContainers(TestContainersParameters::new)
.addCephTestContainer()
.build();
@BeforeClass
public static void beforeClass() {
@ClassRule
public static final FailureDetectingExternalResource resource = new FailureDetectingExternalResource() {
@Override
protected void starting(Description description) {
testContainers.startTestContainers();
}
@AfterClass
public static void afterClass() {
@Override
protected void failed(Throwable e, Description description) {
log.warn("Test Container running was failed ", e);
}
@Override
protected void finished(Description description) {
testContainers.stopTestContainers();
}
};
@TestConfiguration
public static class TestContextConfiguration {
@ -66,22 +80,20 @@ public abstract class AbstractIntegrationTest {
@Override
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
FileStorageTestPropertyValuesBuilder.build(testContainers).applyTo(configurableApplicationContext);
TestPropertyValues.of(
testContainers.getEnvironmentProperties(
environmentProperties -> {
}
)
)
.applyTo(configurableApplicationContext);
}
protected Instant generateCurrentTimePlusDay() {
return LocalDateTime.now().plusDays(1).toInstant(getZoneOffset());
}
protected Instant generateCurrentTimePlusSecond() {
return LocalDateTime.now().plusSeconds(1).toInstant(getZoneOffset());
}
private ZoneOffset getZoneOffset() {
return ZoneOffset.systemDefault().getRules().getOffset(LocalDateTime.now());
}
protected HttpURLConnection getHttpURLConnection(URL url, String method, boolean doOutput) throws IOException {
return getHttpURLConnection(url, method, null, doOutput);
}

View File

@ -12,7 +12,6 @@ import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.testcontainers.shaded.org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
@ -241,8 +240,4 @@ public class FileStorageTest extends AbstractIntegrationTest {
URL url = Objects.requireNonNull(classLoader.getResource("respect"));
return Paths.get(url.toURI());
}
private String getContent(InputStream content) throws IOException {
return IOUtils.toString(content, StandardCharsets.UTF_8);
}
}

View File

@ -1,39 +0,0 @@
package com.rbkmoney.file.storage;
import com.rbkmoney.TestContainers;
import org.springframework.boot.test.util.TestPropertyValues;
import java.util.ArrayList;
import java.util.List;
import static com.rbkmoney.TestContainersBuilder.*;
public class FileStorageTestPropertyValuesBuilder {
public static TestPropertyValues build(TestContainers testContainers) {
List<String> strings = new ArrayList<>();
if (!testContainers.isDockerContainersEnable()) {
withUsingTestContainers(testContainers, strings);
} else {
withoutUsingTestContainers(strings);
}
strings.add("storage.signingRegion=" + SIGNING_REGION);
strings.add("storage.accessKey=" + AWS_ACCESS_KEY);
strings.add("storage.secretKey=" + AWS_SECRET_KEY);
strings.add("storage.clientProtocol=" + PROTOCOL);
strings.add("storage.clientMaxErrorRetry=" + MAX_ERROR_RETRY);
strings.add("storage.bucketName=" + BUCKET_NAME);
return TestPropertyValues.of(strings);
}
private static void withUsingTestContainers(TestContainers testContainers, List<String> strings) {
testContainers.getCephTestContainer().ifPresent(
c -> strings.add("storage.endpoint=" + c.getContainerIpAddress() + ":" + c.getMappedPort(80))
);
}
private static void withoutUsingTestContainers(List<String> strings) {
strings.add("storage.endpoint=localhost:32827");
}
}