-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(POC): integration tests (#208)
* chore(integrationTest): Add first integration tests with localstack and testcontainers
- Loading branch information
Showing
7 changed files
with
207 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/integrationTest/java/software/amazon/nio/spi/s3/Containers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/integrationTest/java/software/amazon/nio/spi/s3/FilesExistsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |