Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix linter issues to be more in line with java rules #180

Merged
merged 4 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/main/java/org/testcontainers/containers/CephContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public CephContainer(final String dockerImageName) {
this(DockerImageName.parse(dockerImageName));
}


/**
* Constructor
* @param dockerImageName
* Sets default Ulimit so the container isn't limited by docker default security (it would take 5 minutes to load)
*/
public CephContainer(final DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
Expand All @@ -67,6 +73,11 @@ public CephContainer(final DockerImageName dockerImageName) {
.withUlimits(DEFAULT_ULIMITS));
}

/**
* @Override default configure of generic container
* set necessary env variables for Ceph
* Set wait strategy to wait for log if not set
*/
@Override
public void configure() {
addExposedPorts(CEPH_MON_DEFAULT_PORT, CEPH_RGW_DEFAULT_PORT);
Expand Down Expand Up @@ -102,15 +113,15 @@ public void configure() {
}

public CephContainer withSslDisabled() {
return super.withCreateContainerCmdModifier((cmd) -> {
cmd.withEntrypoint(
"bash",
"-c",
"sed -i '/^rgw frontends = .*/a rgw verify ssl = false\\\n" +
"rgw crypt require ssl = false' /opt/ceph-container/bin/demo;\n" +
"/opt/ceph-container/bin/demo;"
);
});
return super.withCreateContainerCmdModifier(cmd ->
cmd.withEntrypoint(
"bash",
"-c",
"sed -i '/^rgw frontends = .*/a rgw verify ssl = false\\\n" +
"rgw crypt require ssl = false' /opt/ceph-container/bin/demo;\n" +
"/opt/ceph-container/bin/demo;"
)
);
}

public CephContainer withCephAccessKey(String cephAccessKey) {
Expand Down
77 changes: 41 additions & 36 deletions src/test/java/org/testcontainers/containers/CephContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,19 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class CephContainerTest {
class CephContainerTest {

@Test
public void testBasicUsage() throws Exception {
void testBasicUsage() throws Exception {
try (
// minioContainer {
CephContainer container = new CephContainer()
// }
CephContainer container = new CephContainer()
) {
container.start();
// We should not modify demo script by default
assertThat(getDemoScriptFromContainer(container)).doesNotContain(
"rgw frontends = ${RGW_FRONTEND}\n" +
"rgw verify ssl = false\n" +
"rgw crypt require ssl = false"
"rgw verify ssl = false\n" +
"rgw crypt require ssl = false"
);
assertThat(container.getCephAccessKey()).isEqualTo("demo");
assertThat(container.getCephSecretKey()).isSameAs("b36361c4-1589-42f7-a369-d9dafb926d55");
Expand All @@ -63,9 +61,9 @@ public void testBasicUsage() throws Exception {

HeadBucketResponse headBucketResponse = s3client.headBucket(HeadBucketRequest.builder().bucket("demo").build());
assertThat(headBucketResponse.sdkHttpResponse().isSuccessful())
.isTrue();
.isTrue();
assertThat(headBucketResponse.sdkHttpResponse().headers().get("X-RGW-Quota-Max-Buckets").get(0))
.isEqualTo("1000");
.isEqualTo("1000");

final String key = "my-objectname";

Expand All @@ -76,41 +74,41 @@ public void testBasicUsage() throws Exception {
assertThat(putObjectResponse.expiration()).isNull();

ResponseInputStream<GetObjectResponse>
responseInputStream = s3client.getObject(request -> request.bucket("demo").key(key));
responseInputStream = s3client.getObject(request -> request.bucket("demo").key(key));
assertThat(responseInputStream).hasContent("This is a file");

ListObjectsV2Response objets = s3client.listObjectsV2((builder) -> builder.bucket("demo"));
assertThat(objets.contents().size()).isEqualTo(1);
ListObjectsV2Response objets = s3client.listObjectsV2(builder -> builder.bucket("demo"));
assertThat(objets.contents()).hasSize(1);
jarlah marked this conversation as resolved.
Show resolved Hide resolved
assertThat(objets.contents().get(0).key()).isEqualTo(key);

assertThatThrownBy(() -> {
s3client.putObject(
getSSECPutObjectRequest("demo", key),
RequestBody.fromFile(getTestFile())
);
})
assertThatThrownBy(() ->
s3client.putObject(
getSSECPutObjectRequest("demo", key),
RequestBody.fromFile(getTestFile())
)
)
.isInstanceOf(software.amazon.awssdk.services.s3.model.S3Exception.class)
.hasMessageContaining("Service: S3, Status Code: 400, Request ID: ");
}
}

@Test
public void testOverrides() throws Exception {
void testOverrides() throws Exception {
try (
// cephOverrides {
CephContainer container = new CephContainer("quay.io/ceph/demo:latest")
.withCephAccessKey("testuser123")
.withCephSecretKey("testpassword123")
.withCephBucket("testbucket123")
.withSslDisabled()
// }
// cephOverrides
jarlah marked this conversation as resolved.
Show resolved Hide resolved
CephContainer container = new CephContainer("quay.io/ceph/demo:latest")
.withCephAccessKey("testuser123")
.withCephSecretKey("testpassword123")
.withCephBucket("testbucket123")
.withSslDisabled()

) {
container.start();
// we should have modified the demo script
assertThat(getDemoScriptFromContainer(container)).contains(
"rgw frontends = ${RGW_FRONTEND}\n" +
"rgw verify ssl = false\n" +
"rgw crypt require ssl = false"
"rgw verify ssl = false\n" +
"rgw crypt require ssl = false"
);
assertThat(container.getWaitStrategy()).isInstanceOf(LogMessageWaitStrategy.class);
assertThat(container.getCephAccessKey()).isEqualTo("testuser123");
Expand All @@ -132,18 +130,17 @@ public void testOverrides() throws Exception {
}

@Test
public void testSpecificDaemonImage() throws URISyntaxException {
void testSpecificDaemonImage() throws URISyntaxException {
DockerImageName daemonImage =
DockerImageName.parse("quay.io/ceph/daemon:v7.0.3-stable-7.0-quincy-centos-stream8-x86_64")
.asCompatibleSubstituteFor("quay.io/ceph/demo");
try (
// cephOverrides {
// cephOverrides
CephContainer container = new CephContainer(daemonImage)
.withCephAccessKey("testuser123")
.withCephSecretKey("testpassword123")
.withCephBucket("testbucket123")
.withCommand("demo")
// }
) {
container.start();
assertThat(container.getWaitStrategy()).isInstanceOf(LogMessageWaitStrategy.class);
Expand All @@ -155,6 +152,10 @@ public void testSpecificDaemonImage() throws URISyntaxException {
}
}

/**
* Test that startupStrategy override works
* Keep validating issue<a href="https://github.com/jarlah/testcontainers-ceph/issues/176"> #176</a>
*/
@Test
void testOverrideStartupStrategy() {
DockerImageName daemonImage =
Expand All @@ -165,7 +166,7 @@ void testOverrideStartupStrategy() {
.withCephAccessKey("testuser123")
.withCephSecretKey("testpassword123")
.withCephBucket("testbucket123")
.withCommand("demo");
.withCommand("demo")
) {
container.setWaitStrategy(Wait.forListeningPort());
container.start();
Expand All @@ -174,6 +175,10 @@ void testOverrideStartupStrategy() {
}
}

/**
* Test that WaitingFor override works
* Keep validating issue<a href="https://github.com/jarlah/testcontainers-ceph/issues/176"> #176</a>
*/
@Test
void testOverrideWaitingFor() {
DockerImageName daemonImage =
Expand All @@ -184,15 +189,14 @@ void testOverrideWaitingFor() {
.withCephAccessKey("testuser123")
.withCephSecretKey("testpassword123")
.withCephBucket("testbucket123")
.withCommand("demo");
.withCommand("demo")
) {
container.waitingFor(Wait.forListeningPort());
container.start();
assertThat(container.isRunning()).isTrue();
assertThat(container.getWaitStrategy()).isInstanceOf(HostPortWaitStrategy.class);
}
}
// configuringClient {

private static S3Client getS3client(CephContainer container) throws URISyntaxException {
final AwsBasicCredentials credentials = AwsBasicCredentials.create(
Expand All @@ -209,12 +213,13 @@ private static S3Client getS3client(CephContainer container) throws URISyntaxExc
.build())
.build();
}
// }


private static String getDemoScriptFromContainer(CephContainer container) throws IOException {
container.copyFileFromContainer("/opt/ceph-container/bin/demo", "demo-script");
return new String(Files.readAllBytes(Paths.get("demo-script")));
}

@NotNull
private File getTestFile() throws URISyntaxException {
return Paths.get(Objects.requireNonNull(this.getClass().getResource("/object_to_upload.txt")).toURI()).toFile();
Expand All @@ -228,7 +233,7 @@ private static SecretKey getPasswordBasedKey(char[] password) throws NoSuchAlgor
return SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(pbeKeySpec);
}

private static PutObjectRequest getSSECPutObjectRequest(String bucket, String key) throws NoSuchAlgorithmException, InvalidKeySpecException, URISyntaxException {
private static PutObjectRequest getSSECPutObjectRequest(String bucket, String key) throws NoSuchAlgorithmException, InvalidKeySpecException {
SecretKey secretKey = getPasswordBasedKey("testtesttesttesttesttest123".toCharArray());
byte[] customerKeyBytes = secretKey.getEncoded();
String customerKeyMD5;
Expand Down