Skip to content

Commit

Permalink
chore(POC): integration tests (#208)
Browse files Browse the repository at this point in the history
* chore(integrationTest): Add first integration tests with localstack and testcontainers
  • Loading branch information
guicamest authored Oct 17, 2023
1 parent 0ad4f31 commit 6726734
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ jobs:

- name: Execute Gradle build
run: ./gradlew build

- name: Run Integration tests
run: ./gradlew integrationTest
env:
TESTCONTAINERS_CHECKS_DISABLE: true
37 changes: 37 additions & 0 deletions THIRD-PARTY
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,40 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

** Testcontainers Java -- https://testcontainers.com/ -- The MIT License (MIT)

Copyright (c) 2015-2019 Richard North

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

** Localstack -- https://localstack.cloud/ -- Copyright (c) 2017+ LocalStack contributors
Copyright (c) 2016 Atlassian Pty Ltd

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.
31 changes: 23 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
id 'signing'
id 'maven-publish'
id 'jacoco'
id 'jvm-test-suite'
id 'com.github.johnrengelman.shadow' version '7.1.2'
}

Expand Down Expand Up @@ -48,11 +49,6 @@ sourceSets {

dependencies {
implementation 'io.reactivex.rxjava3:rxjava:3.1.8'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
testImplementation 'org.mockito:mockito-core:5.5.0'
testImplementation 'org.assertj:assertj-core:3.24.2'
testImplementation 'org.mockito:mockito-junit-jupiter:5.5.0'
testImplementation 'com.github.stefanbirkner:system-lambda:1.2.1'

implementation platform('software.amazon.awssdk:bom:2.20.158')

Expand Down Expand Up @@ -121,9 +117,28 @@ javadoc {
}
}

test {
useJUnitPlatform()
finalizedBy jacocoTestReport
// JVM test suite plugin https://docs.gradle.org/7.3.3/userguide/jvm_test_suite_plugin.html
testing {
suites {
integrationTest(JvmTestSuite) {
dependencies {
implementation project
implementation 'org.testcontainers:junit-jupiter:1.19.1'
implementation 'org.testcontainers:localstack:1.19.1'
implementation 'org.testcontainers:testcontainers:1.19.1'
}
}
withType(JvmTestSuite).configureEach {
useJUnitJupiter('5.10.0')
dependencies {
implementation 'org.assertj:assertj-core:3.24.2'
implementation 'org.mockito:mockito-core:5.5.0'
implementation 'org.assertj:assertj-core:3.24.2'
implementation 'org.mockito:mockito-junit-jupiter:5.5.0'
implementation 'com.github.stefanbirkner:system-lambda:1.2.1'
}
}
}
}

jacocoTestReport {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package software.amazon.nio.spi.s3;

import org.testcontainers.containers.Container;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.utility.DockerImageName;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.S3;
import static software.amazon.nio.spi.s3.config.S3NioSpiConfiguration.S3_SPI_ENDPOINT_PROTOCOL_PROPERTY;

abstract class Containers {

static final LocalStackContainer LOCAL_STACK_CONTAINER;

static {
LOCAL_STACK_CONTAINER = new LocalStackContainer(
DockerImageName.parse("localstack/localstack:2.3.2")
).withServices(S3).withEnv("PROVIDER_OVERRIDE_S3", "v3");
LOCAL_STACK_CONTAINER.start();
System.setProperty(S3_SPI_ENDPOINT_PROTOCOL_PROPERTY, "http");
}

public static void createBucket(String name) {
assertThatCode(() -> {
Container.ExecResult execResult = LOCAL_STACK_CONTAINER.execInContainer(("awslocal s3api create-bucket --bucket " + name).split(" "));
assertThat(execResult.getExitCode()).isZero();
}).as("Failed to create bucket '%s'", name)
.doesNotThrowAnyException();
}

public static void putObject(String bucket, String key) {
assertThatCode(() -> {
Container.ExecResult execResult = LOCAL_STACK_CONTAINER.execInContainer(("awslocal s3api put-object --bucket " + bucket + " --key " + key).split(" "));
assertThat(execResult.getExitCode()).isZero();
}).as("Failed to put object '%s' in bucket '%s'", key, bucket)
.doesNotThrowAnyException();
}

public static String localStackConnectionEndpoint() {
return localStackConnectionEndpoint(null, null);
}

public static String localStackConnectionEndpoint(String key, String secret) {
String accessKey = key != null ? key : LOCAL_STACK_CONTAINER.getAccessKey();
String secretKey = secret != null ? secret : LOCAL_STACK_CONTAINER.getSecretKey();
return String.format("s3x://%s:%s@%s", accessKey, secretKey, localStackHost());
}

private static String localStackHost() {
return LOCAL_STACK_CONTAINER.getEndpoint().getHost() + ":" + LOCAL_STACK_CONTAINER.getEndpoint().getPort();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package software.amazon.nio.spi.s3;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.assertj.core.api.BDDAssertions.then;
import static software.amazon.nio.spi.s3.Containers.localStackConnectionEndpoint;
import static software.amazon.nio.spi.s3.Containers.putObject;

@DisplayName("Files$exists()")
public class FilesExistsTest {

@Nested
@DisplayName("should be false")
class FileDoesNotExist {

@Test
@DisplayName("when bucket does not exist")
public void fileExistsShouldReturnFalseWhenBucketNotFound() {
final Path path = Paths.get(URI.create(localStackConnectionEndpoint() + "/does-not-exist"));
then(Files.exists(path)).isFalse();
}

@Test
@DisplayName("when bucket exists but file doesn't")
public void fileExistsShouldReturnFalseWhenBucketExistsAndFileNotFound() {
Containers.createBucket("sink");
final Path path = Paths.get(URI.create(localStackConnectionEndpoint() + "/sink/missing-file"));
then(Files.exists(path)).isFalse();
}
}

@Nested
@DisplayName("should be true")
class FileExists {

@BeforeEach
public void createBucket() {
Containers.createBucket("sink");
}

@Test
@DisplayName("when bucket and file exist")
public void fileExistsShouldReturnTrueWhenBucketExistsAndFileFound() {
putObject("sink", "sample-file.txt");
final Path path = Paths.get(URI.create(localStackConnectionEndpoint() + "/sink/sample-file.txt"));
then(Files.exists(path)).isTrue();
}

@Test
@DisplayName("for bucket path when it exists")
public void fileExistsShouldReturnTrueWhenBucketExists() {
final Path path = Paths.get(URI.create(localStackConnectionEndpoint() + "/sink/"));
then(Files.exists(path)).isTrue();
}
}
}
24 changes: 24 additions & 0 deletions src/integrationTest/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
~ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
~
-->

<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="info">
<appender-ref ref="STDOUT" />
</root>
<logger name="software.amazon.awssdk.profiles.internal.ProfileFileReader" level="error"/>

<!-- https://java.testcontainers.org/supported_docker_environment/logging_config/ -->
<logger name="org.testcontainers" level="INFO"/>
<logger name="tc" level="INFO"/>
<logger name="com.github.dockerjava" level="WARN"/>
<logger name="com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire" level="OFF"/>

</configuration>

0 comments on commit 6726734

Please sign in to comment.