Skip to content

Commit de9fa03

Browse files
committed
Add ceph support
1 parent aefdc91 commit de9fa03

File tree

4 files changed

+262
-0
lines changed

4 files changed

+262
-0
lines changed

modules/ceph/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>org.testcontainers</groupId>
9+
<artifactId>testcontainers-parent</artifactId>
10+
<version>0-SNAPSHOT</version>
11+
<relativePath>../../pom.xml</relativePath>
12+
</parent>
13+
14+
<artifactId>ceph</artifactId>
15+
<name>TestContainers :: ceph</name>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>${project.groupId}</groupId>
20+
<artifactId>testcontainers</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.amazonaws</groupId>
25+
<artifactId>aws-java-sdk-s3</artifactId>
26+
<version>1.11.231</version>
27+
</dependency>
28+
</dependencies>
29+
30+
31+
</project>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package org.testcontainers.containers;
2+
3+
import com.amazonaws.auth.AWSCredentialsProvider;
4+
import com.amazonaws.auth.AWSStaticCredentialsProvider;
5+
import com.amazonaws.auth.BasicAWSCredentials;
6+
import com.amazonaws.client.builder.AwsClientBuilder;
7+
import org.testcontainers.containers.wait.LogMessageWaitStrategy;
8+
9+
import java.time.Duration;
10+
11+
public class CephContainer<SELF extends CephContainer<SELF>> extends GenericContainer<SELF> {
12+
13+
private static final String IMAGE = "ceph/demo";
14+
15+
private String awsAccessKey = "ceph";
16+
17+
private String awsSecretKey = "ceph";
18+
19+
private String bucketName = "CEPH";
20+
21+
private String rgwName = "localhost";
22+
23+
private String demoUid = "ceph";
24+
25+
private NetworkAutoDetectMode networkAutoDetectMode = NetworkAutoDetectMode.IPV4_ONLY;
26+
27+
private Duration startupTimeout = Duration.ofSeconds(20);
28+
29+
public CephContainer() {
30+
super(IMAGE + ":latest");
31+
}
32+
33+
public CephContainer(String dockerImageName) {
34+
super(dockerImageName);
35+
}
36+
37+
@Override
38+
protected void configure() {
39+
withEnv("RGW_NAME", rgwName);
40+
withEnv("NETWORK_AUTO_DETECT", networkAutoDetectMode.value);
41+
withEnv("CEPH_DEMO_UID", demoUid);
42+
withEnv("CEPH_DEMO_ACCESS_KEY", awsAccessKey);
43+
withEnv("CEPH_DEMO_SECRET_KEY", awsSecretKey);
44+
withEnv("CEPH_DEMO_BUCKET", bucketName);
45+
withExposedPorts(6789, 6800, 6801, 6802, 6803, 6804, 6805, 80, 5000);
46+
waitingFor(
47+
new LogMessageWaitStrategy()
48+
.withRegEx(".*\\/entrypoint.sh: SUCCESS\n")
49+
.withStartupTimeout(startupTimeout)
50+
);
51+
}
52+
53+
public AWSCredentialsProvider getAWSCredentialsProvider() {
54+
return new AWSStaticCredentialsProvider(
55+
new BasicAWSCredentials(awsAccessKey, awsSecretKey)
56+
);
57+
}
58+
59+
public AwsClientBuilder.EndpointConfiguration getAWSEndpointConfiguration() {
60+
return new AwsClientBuilder.EndpointConfiguration(
61+
getContainerIpAddress() + ":" + getMappedPort(80),
62+
"us-east-1"
63+
);
64+
}
65+
66+
public SELF withAwsAccessKey(String awsAccessKey) {
67+
this.awsAccessKey = awsAccessKey;
68+
return self();
69+
}
70+
71+
public SELF withAwsSecretKey(String awsSecretKey) {
72+
this.awsSecretKey = awsSecretKey;
73+
return self();
74+
}
75+
76+
public String getBucketName() {
77+
return bucketName;
78+
}
79+
80+
public SELF withBucketName(String bucketName) {
81+
//because s3cmd transforming bucket name to uppercase
82+
this.bucketName = bucketName.toUpperCase();
83+
return self();
84+
}
85+
86+
public String getRgwName() {
87+
return rgwName;
88+
}
89+
90+
public SELF withRgwName(String rgwName) {
91+
this.rgwName = rgwName;
92+
return self();
93+
}
94+
95+
public String getDemoUid() {
96+
return demoUid;
97+
}
98+
99+
public SELF withDemoUid(String demoUid) {
100+
this.demoUid = demoUid;
101+
return self();
102+
}
103+
104+
public NetworkAutoDetectMode getNetworkAutoDetectMode() {
105+
return networkAutoDetectMode;
106+
}
107+
108+
public SELF withNetworkAutoDetectMode(NetworkAutoDetectMode networkAutoDetectMode) {
109+
this.networkAutoDetectMode = networkAutoDetectMode;
110+
return self();
111+
}
112+
113+
public Duration getStartupTimeout() {
114+
return startupTimeout;
115+
}
116+
117+
public SELF withStartupTimeout(Duration startupTimeout) {
118+
this.startupTimeout = startupTimeout;
119+
return self();
120+
}
121+
122+
public enum NetworkAutoDetectMode {
123+
IPV6_OR_IPV4("1"),
124+
IPV4_ONLY("4"),
125+
IPV6_ONLY("6");
126+
127+
private String value;
128+
129+
NetworkAutoDetectMode(String value) {
130+
this.value = value;
131+
}
132+
}
133+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.testcontainers.containers;
2+
3+
import com.amazonaws.ClientConfiguration;
4+
import com.amazonaws.Protocol;
5+
import com.amazonaws.services.s3.AmazonS3;
6+
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
7+
import com.amazonaws.services.s3.model.Bucket;
8+
import com.amazonaws.services.s3.model.S3Object;
9+
import org.apache.commons.io.IOUtils;
10+
import org.junit.Before;
11+
import org.junit.Rule;
12+
import org.junit.Test;
13+
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.net.URL;
17+
import java.nio.charset.StandardCharsets;
18+
import java.time.Duration;
19+
import java.time.Instant;
20+
import java.util.Date;
21+
import java.util.List;
22+
23+
import static org.junit.Assert.*;
24+
25+
public class CephContainerTest {
26+
27+
@Rule
28+
public CephContainer cephContainer = new CephContainer()
29+
.withAwsAccessKey("test")
30+
.withAwsSecretKey("test")
31+
.withBucketName("test");
32+
33+
private AmazonS3 amazonS3;
34+
35+
@Before
36+
public void setup() {
37+
amazonS3 = AmazonS3ClientBuilder.standard()
38+
.withCredentials(cephContainer.getAWSCredentialsProvider())
39+
.withEndpointConfiguration(cephContainer.getAWSEndpointConfiguration())
40+
.withPathStyleAccessEnabled(true)
41+
.withClientConfiguration(
42+
new ClientConfiguration()
43+
.withProtocol(Protocol.HTTP)
44+
.withSignerOverride("S3SignerType")
45+
)
46+
.build();
47+
}
48+
49+
@Test
50+
public void testS3Bucket() {
51+
String bucketName = "test2";
52+
53+
//check current bucket
54+
assertTrue(amazonS3.doesBucketExistV2(cephContainer.getBucketName()));
55+
56+
//create another bucket
57+
amazonS3.createBucket(bucketName);
58+
assertTrue(amazonS3.doesBucketExistV2(bucketName));
59+
List<Bucket> buckets = amazonS3.listBuckets();
60+
assertEquals(2, buckets.size());
61+
assertTrue(buckets.stream().anyMatch(
62+
bucket -> bucket.getName().equals(bucketName)
63+
));
64+
65+
//remove bucket
66+
amazonS3.deleteBucket(bucketName);
67+
assertFalse(amazonS3.doesBucketExistV2(bucketName));
68+
buckets = amazonS3.listBuckets();
69+
assertEquals(1, buckets.size());
70+
assertFalse(buckets.stream().anyMatch(
71+
bucket -> bucket.getName().equals(bucketName)
72+
));
73+
}
74+
75+
@Test
76+
public void testS3Object() throws IOException {
77+
String objectId = "test";
78+
String testData = "This is test data";
79+
80+
//put object
81+
amazonS3.putObject(cephContainer.getBucketName(), objectId, testData);
82+
assertEquals(1, amazonS3.listObjects(cephContainer.getBucketName()).getObjectSummaries().size());
83+
S3Object object = amazonS3.getObject(cephContainer.getBucketName(), objectId);
84+
assertEquals(testData, IOUtils.toString(object.getObjectContent(), StandardCharsets.UTF_8));
85+
86+
//generate presigned url and download file
87+
URL url = amazonS3.generatePresignedUrl(
88+
cephContainer.getBucketName(),
89+
objectId,
90+
Date.from(Instant.now().plusSeconds(Duration.ofMinutes(5).toMillis())));
91+
92+
try (InputStream inputStream = url.openStream()) {
93+
assertEquals(testData, IOUtils.toString(inputStream, StandardCharsets.UTF_8));
94+
}
95+
}
96+
97+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
<module>modules/selenium</module>
167167
<module>modules/nginx</module>
168168
<module>modules/jdbc-test</module>
169+
<module>modules/ceph</module>
169170
</modules>
170171

171172
<profiles>

0 commit comments

Comments
 (0)