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

feat: allow to override bucket name #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3"

services:

ceph-demo:
image: quay.io/ceph/demo:latest-quincy
environment:
CEPH_DEMO_UID: "demo"
CEPH_DEMO_ACCESS_KEY: "demo"
CEPH_DEMO_SECRET_KEY: "demo"
CEPH_DEMO_BUCKET: "demo"
CEPH_PUBLIC_NETWORK: "0.0.0.0/0"
MON_IP: "127.0.0.1"
RGW_NAME: "localhost"
ports:
- "8080:8080"
23 changes: 21 additions & 2 deletions src/main/java/org/testcontainers/containers/CephContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ public class CephContainer extends GenericContainer<CephContainer> {

private static final String CEPH_DEMO_UID = "admin";

private static final String CEPH_DEMO_BUCKET = "test-bucket";

private static final String CEPH_END_START = ".*/opt/ceph-container/bin/demo: SUCCESS.*";

private String cephAccessKey;

private String cephSecretKey;

private String cephBucket;

public CephContainer() {
this(DEFAULT_IMAGE_NAME.withTag(DEFAULT_IMAGE_TAG));
}
Expand All @@ -59,6 +63,12 @@ public void configure() {
addExposedPorts(CEPH_MON_DEFAULT_PORT, CEPH_RGW_DEFAULT_PORT);

addEnv("CEPH_DEMO_UID", CEPH_DEMO_UID);
addEnv(
"CEPH_DEMO_BUCKET",
this.cephBucket != null
? this.cephBucket
: (this.cephBucket = CEPH_DEMO_BUCKET)
);
addEnv(
"CEPH_DEMO_ACCESS_KEY",
this.cephAccessKey != null
Expand All @@ -71,10 +81,10 @@ public void configure() {
? this.cephSecretKey
: (this.cephSecretKey = CEPH_RGW_DEFAULT_SECRET_KEY)
);
addEnv("NETWORK_AUTO_DETECT", "1");
addEnv("CEPH_DAEMON", "DEMO");
addEnv("CEPH_PUBLIC_NETWORK", "0.0.0.0/0");
// This needs to be 127.0.0.1, if not the demo image will not start properly
addEnv("MON_IP", "127.0.0.1");
// This is important because without it, we cant access ceph from http://localhost:<PORT>
addEnv("RGW_NAME", "localhost");

setWaitStrategy(Wait.forLogMessage(CEPH_END_START, 1)
Expand All @@ -91,6 +101,11 @@ public CephContainer withCephSecretKey(String cephSecretKey) {
return this;
}

public CephContainer withCephBucket(String cephBucket) {
this.cephBucket = cephBucket;
return this;
}

public int getCephPort() {
return getMappedPort(CEPH_RGW_DEFAULT_PORT);
}
Expand All @@ -106,4 +121,8 @@ public String getCephAccessKey() {
public String getCephSecretKey() {
return cephSecretKey;
}

public String getCephBucket() {
return cephBucket;
}
}
34 changes: 34 additions & 0 deletions src/test/java/org/testcontainers/containers/CephClientTester.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.testcontainers.containers;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;

/**
* Used to test intricate details about Ceph.
* Start docker compose with docker compose up -d
* Then run this main function to test if demo bucket exists
*/
public class CephClientTester {
public static void main(String[] args) {
AWSCredentials credentials = new BasicAWSCredentials(
"demo",
"demo"
);
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
"http://localhost:8080",
""
);
AmazonS3 client = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(endpointConfiguration)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withPathStyleAccessEnabled(true)
.build();
boolean bucketExist = client.doesBucketExistV2("demo");
System.out.println(bucketExist ? "SUCCESS": "Failed to find bucket");
}
}
57 changes: 33 additions & 24 deletions src/test/java/org/testcontainers/containers/CephContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.amazonaws.services.s3.model.S3ObjectSummary;
import org.junit.Test;

import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;

Expand All @@ -25,51 +26,59 @@ public void testBasicUsage() throws Exception {
) {
container.start();

assertThat(container.getCephAccessKey()).isNotBlank();
assertThat(container.getCephSecretKey()).isNotBlank();
assertThat(container.getCephAccessKey()).isEqualTo("accessKey");
assertThat(container.getCephSecretKey()).isEqualTo("secretKey");
assertThat(container.getCephBucket()).isEqualTo("test-bucket");

// configuringClient {
AWSCredentials credentials = new BasicAWSCredentials(
container.getCephAccessKey(),
container.getCephSecretKey()
);
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
container.getCephUrl().toString(),
""
);
AmazonS3 s3client = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(endpointConfiguration)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withPathStyleAccessEnabled(true)
.build();
// }
AmazonS3 s3client = getS3client(container);

s3client.createBucket("test-bucket");
assertThat(s3client.doesBucketExistV2("test-bucket")).isTrue();
assertThat(s3client.doesBucketExistV2(container.getCephBucket())).isTrue();

URL file = this.getClass().getResource("/object_to_upload.txt");
assertThat(file).isNotNull();
s3client.putObject("test-bucket", "my-objectname", file.getFile());
s3client.putObject(container.getCephBucket(), "my-objectname", file.getFile());

List<S3ObjectSummary> objets = s3client.listObjectsV2("test-bucket").getObjectSummaries();
List<S3ObjectSummary> objets = s3client.listObjectsV2(container.getCephBucket()).getObjectSummaries();
assertThat(objets.size()).isEqualTo(1);
assertThat(objets.get(0).getKey()).isEqualTo("my-objectname");
}
}

@Test
public void testOverrides() {
public void testOverrides() throws URISyntaxException {
try (
// cephOverrides {
CephContainer container = new CephContainer("quay.io/ceph/demo:latest")
.withCephAccessKey("testuser123")
.withCephSecretKey("testpassword123");
.withCephSecretKey("testpassword123")
.withCephBucket("testbucket123")
// }
) {
container.start();
assertThat(container.getCephAccessKey()).isEqualTo("testuser123");
assertThat(container.getCephSecretKey()).isEqualTo("testpassword123");
assertThat(container.getCephBucket()).isEqualTo("testbucket123");
AmazonS3 s3client = getS3client(container);
assertThat(s3client.doesBucketExistV2("testbucket123")).isTrue();
}
}

// configuringClient {
private static AmazonS3 getS3client(CephContainer container) throws URISyntaxException {
AWSCredentials credentials = new BasicAWSCredentials(
container.getCephAccessKey(),
container.getCephSecretKey()
);
AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(
container.getCephUrl().toString(),
""
);
return AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(endpointConfiguration)
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withPathStyleAccessEnabled(true)
.build();
}
// }
}