diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..0373c0b --- /dev/null +++ b/docker-compose.yaml @@ -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" \ No newline at end of file diff --git a/src/main/java/org/testcontainers/containers/CephContainer.java b/src/main/java/org/testcontainers/containers/CephContainer.java index 15159ee..a0bc647 100644 --- a/src/main/java/org/testcontainers/containers/CephContainer.java +++ b/src/main/java/org/testcontainers/containers/CephContainer.java @@ -35,12 +35,16 @@ public class CephContainer extends GenericContainer { 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)); } @@ -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 @@ -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: addEnv("RGW_NAME", "localhost"); setWaitStrategy(Wait.forLogMessage(CEPH_END_START, 1) @@ -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); } @@ -106,4 +121,8 @@ public String getCephAccessKey() { public String getCephSecretKey() { return cephSecretKey; } + + public String getCephBucket() { + return cephBucket; + } } diff --git a/src/test/java/org/testcontainers/containers/CephClientTester.java b/src/test/java/org/testcontainers/containers/CephClientTester.java new file mode 100644 index 0000000..8e89c3f --- /dev/null +++ b/src/test/java/org/testcontainers/containers/CephClientTester.java @@ -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"); + } +} diff --git a/src/test/java/org/testcontainers/containers/CephContainerTest.java b/src/test/java/org/testcontainers/containers/CephContainerTest.java index 91d1e47..be9863f 100644 --- a/src/test/java/org/testcontainers/containers/CephContainerTest.java +++ b/src/test/java/org/testcontainers/containers/CephContainerTest.java @@ -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; @@ -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 objets = s3client.listObjectsV2("test-bucket").getObjectSummaries(); + List 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(); + } + // } }