Skip to content

Commit f549d66

Browse files
committed
Support MinIO test environments
1 parent ff16188 commit f549d66

11 files changed

+369
-10
lines changed

turms-server-test-common/src/main/java/im/turms/server/common/testing/environment/ContainerTestEnvironmentPropertyConst.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
/**
2121
* @author James Chen
2222
*/
23-
public class ContainerTestEnvironmentPropertyConst {
23+
public final class ContainerTestEnvironmentPropertyConst {
2424

2525
public static final String DOCKER_COMPOSE_TEST_FILE = "docker-compose.test.yml";
2626

27+
public static final String MINIO = "minio";
28+
public static final String MINIO_SERVICE_NAME = "minio_1";
29+
public static final int MINIO_SERVICE_PORT = 9000;
30+
public static final String MINIO_SERVICE_USERNAME = "minioadmin";
31+
public static final String MINIO_SERVICE_PASSWORD = "minioadmin";
32+
2733
public static final String MONGO = "mongodb-router";
2834
public static final String MONGO_SHARD = "mongodb-shard";
2935
public static final String MONGO_CONFIG = "mongodb-config";

turms-server-test-common/src/main/java/im/turms/server/common/testing/environment/TestEnvironmentContainer.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.yaml.snakeyaml.Yaml;
3232

3333
import static im.turms.server.common.testing.environment.ContainerTestEnvironmentPropertyConst.DOCKER_COMPOSE_TEST_FILE;
34+
import static im.turms.server.common.testing.environment.ContainerTestEnvironmentPropertyConst.MINIO;
3435
import static im.turms.server.common.testing.environment.ContainerTestEnvironmentPropertyConst.MONGO;
3536
import static im.turms.server.common.testing.environment.ContainerTestEnvironmentPropertyConst.MONGO_CONFIG;
3637
import static im.turms.server.common.testing.environment.ContainerTestEnvironmentPropertyConst.MONGO_SHARD;
@@ -52,6 +53,11 @@ public class TestEnvironmentContainer extends DockerComposeContainer<TestEnviron
5253
private final boolean hasContainer;
5354
private volatile boolean started;
5455

56+
/**
57+
* @param dockerCompose TODO: Use InputStream instead of File once DockerComposeContainer
58+
* supports
59+
* (https://github.com/testcontainers/testcontainers-java/issues/3431)
60+
*/
5561
public TestEnvironmentContainer(
5662
File dockerCompose,
5763
String dockerComposeConfig,
@@ -62,6 +68,7 @@ public TestEnvironmentContainer(
6268
}
6369

6470
public static TestEnvironmentContainer create(
71+
boolean setupMinio,
6572
boolean setupMongo,
6673
boolean setupRedis,
6774
boolean setupTurmsAdmin,
@@ -75,7 +82,8 @@ public static TestEnvironmentContainer create(
7582
} catch (IOException e) {
7683
throw new RuntimeException("Failed to create the temp docker compose file", e);
7784
}
78-
String config = buildDockerComposeConfig(setupMongo,
85+
String config = buildDockerComposeConfig(setupMinio,
86+
setupMongo,
7987
setupRedis,
8088
setupTurmsAdmin,
8189
setupTurmsGateway,
@@ -97,11 +105,8 @@ public static TestEnvironmentContainer create(
97105
|| setupTurmsService);
98106
}
99107

100-
/**
101-
* Fixme: Return InputStream instead of File once DockerComposeContainer supports
102-
* (https://github.com/testcontainers/testcontainers-java/issues/3431)
103-
*/
104108
private static String buildDockerComposeConfig(
109+
boolean setupMinio,
105110
boolean setupMongo,
106111
boolean setupRedis,
107112
boolean setupTurmsAdmin,
@@ -119,6 +124,9 @@ private static String buildDockerComposeConfig(
119124
Yaml yaml = new Yaml();
120125
Map<String, Object> config = yaml.load(resource);
121126
Map<String, Object> services = (Map<String, Object>) config.get("services");
127+
if (!setupMinio) {
128+
removeService(services, MINIO);
129+
}
122130
if (!setupMongo) {
123131
removeService(services, MONGO);
124132
removeService(services, MONGO_SHARD);

turms-server-test-common/src/main/java/im/turms/server/common/testing/environment/TestEnvironmentManager.java

+15-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import lombok.extern.slf4j.Slf4j;
3131
import org.springframework.util.StringUtils;
3232

33+
import im.turms.server.common.testing.environment.minio.MinioTestEnvironmentAware;
34+
import im.turms.server.common.testing.environment.minio.MinioTestEnvironmentManager;
3335
import im.turms.server.common.testing.environment.mongo.MongoTestEnvironmentAware;
3436
import im.turms.server.common.testing.environment.mongo.MongoTestEnvironmentManager;
3537
import im.turms.server.common.testing.environment.redis.RedisTestEnvironmentAware;
@@ -38,6 +40,7 @@
3840
import im.turms.server.common.testing.environment.turmsgateway.TurmsGatewayTestEnvironmentManager;
3941
import im.turms.server.common.testing.environment.turmsservice.TurmsServiceTestEnvironmentAware;
4042
import im.turms.server.common.testing.environment.turmsservice.TurmsServiceTestEnvironmentManager;
43+
import im.turms.server.common.testing.properties.MinioTestEnvironmentProperties;
4144
import im.turms.server.common.testing.properties.MongoTestEnvironmentProperties;
4245
import im.turms.server.common.testing.properties.RedisTestEnvironmentProperties;
4346
import im.turms.server.common.testing.properties.TestProperties;
@@ -51,14 +54,16 @@
5154
* implementation does NOT fully support just because we don't have these test cases.
5255
*/
5356
@Slf4j
54-
public class TestEnvironmentManager
55-
implements Closeable, MongoTestEnvironmentAware, RedisTestEnvironmentAware,
56-
TurmsGatewayTestEnvironmentAware, TurmsServiceTestEnvironmentAware {
57+
public class TestEnvironmentManager implements Closeable, MinioTestEnvironmentAware,
58+
MongoTestEnvironmentAware, RedisTestEnvironmentAware, TurmsGatewayTestEnvironmentAware,
59+
TurmsServiceTestEnvironmentAware {
5760

5861
public static final String DEFAULT_PROPERTIES_FILE = "application-test.yaml";
5962

6063
private final TestEnvironmentContainer testEnvironmentContainer;
6164

65+
@Delegate
66+
private final MinioTestEnvironmentManager minioTestEnvironmentManager;
6267
@Delegate
6368
private final MongoTestEnvironmentManager mongoTestEnvironmentManager;
6469
@Delegate
@@ -69,6 +74,7 @@ public class TestEnvironmentManager
6974
private final TurmsServiceTestEnvironmentManager turmsServiceTestEnvironmentManager;
7075

7176
private TestEnvironmentManager(TestProperties testProperties) {
77+
MinioTestEnvironmentProperties minioTestEnvironmentProperties = testProperties.getMinio();
7278
MongoTestEnvironmentProperties mongoTestEnvironmentProperties = testProperties.getMongo();
7379
RedisTestEnvironmentProperties redisTestEnvironmentProperties = testProperties.getRedis();
7480
TurmsAdminTestEnvironmentProperties turmsAdminTestEnvironmentProperties =
@@ -79,6 +85,8 @@ private TestEnvironmentManager(TestProperties testProperties) {
7985
testProperties.getTurmsService();
8086

8187
testEnvironmentContainer = TestEnvironmentContainer.create(
88+
minioTestEnvironmentProperties.getType()
89+
.equals(ServiceTestEnvironmentType.CONTAINER),
8290
mongoTestEnvironmentProperties.getType()
8391
.equals(ServiceTestEnvironmentType.CONTAINER),
8492
redisTestEnvironmentProperties.getType()
@@ -93,6 +101,9 @@ private TestEnvironmentManager(TestProperties testProperties) {
93101
turmsServiceTestEnvironmentProperties.getContainer()
94102
.getJvmOptions());
95103

104+
minioTestEnvironmentManager = new MinioTestEnvironmentManager(
105+
minioTestEnvironmentProperties,
106+
testEnvironmentContainer);
96107
mongoTestEnvironmentManager = new MongoTestEnvironmentManager(
97108
mongoTestEnvironmentProperties,
98109
testEnvironmentContainer);
@@ -140,7 +151,7 @@ public static TestEnvironmentManager fromPropertiesFile(String testPropertiesRes
140151
node = node.get(propertyName);
141152
if (null == node) {
142153
// Use default properties if the properties file is found,
143-
// but it doesn't specify properties.
154+
// but it doesn't specify test properties.
144155
return fromProperties(new TestProperties());
145156
}
146157
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (C) 2019 The Turms Project
3+
* https://github.com/turms-im/turms
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package im.turms.server.common.testing.environment.minio;
19+
20+
import org.testcontainers.containers.wait.strategy.Wait;
21+
22+
import im.turms.server.common.testing.ServiceLogConsumer;
23+
import im.turms.server.common.testing.environment.ServiceContainerTestEnvironmentManager;
24+
import im.turms.server.common.testing.environment.TestEnvironmentContainer;
25+
26+
import static im.turms.server.common.testing.environment.ContainerTestEnvironmentPropertyConst.MINIO_SERVICE_NAME;
27+
import static im.turms.server.common.testing.environment.ContainerTestEnvironmentPropertyConst.MINIO_SERVICE_PASSWORD;
28+
import static im.turms.server.common.testing.environment.ContainerTestEnvironmentPropertyConst.MINIO_SERVICE_PORT;
29+
import static im.turms.server.common.testing.environment.ContainerTestEnvironmentPropertyConst.MINIO_SERVICE_USERNAME;
30+
31+
/**
32+
* @author James Chen
33+
*/
34+
public class MinioContainerTestEnvironmentManager extends ServiceContainerTestEnvironmentManager
35+
implements MinioTestEnvironmentAware {
36+
37+
public MinioContainerTestEnvironmentManager(TestEnvironmentContainer container) {
38+
super(container, MINIO_SERVICE_NAME);
39+
container.withExposedService(serviceName, MINIO_SERVICE_PORT);
40+
container.withLogConsumer(serviceName, new ServiceLogConsumer(serviceName));
41+
container.waitingFor(serviceName, Wait.forHealthcheck());
42+
}
43+
44+
@Override
45+
public String getMinioHost() {
46+
return container.getServiceHost(serviceName, MINIO_SERVICE_PORT);
47+
}
48+
49+
@Override
50+
public int getMinioPort() {
51+
return container.getServicePort(serviceName, MINIO_SERVICE_PORT);
52+
}
53+
54+
@Override
55+
public String getMinioUsername() {
56+
return MINIO_SERVICE_USERNAME;
57+
}
58+
59+
@Override
60+
public String getMinioPassword() {
61+
return MINIO_SERVICE_PASSWORD;
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (C) 2019 The Turms Project
3+
* https://github.com/turms-im/turms
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package im.turms.server.common.testing.environment.minio;
19+
20+
import im.turms.server.common.testing.properties.MinioLocalTestEnvironmentProperties;
21+
22+
/**
23+
* @author James Chen
24+
*/
25+
public class MinioLocalTestEnvironmentManager implements MinioTestEnvironmentAware {
26+
27+
private final String host;
28+
private final int port;
29+
private final String username;
30+
private final String password;
31+
32+
public MinioLocalTestEnvironmentManager(MinioLocalTestEnvironmentProperties properties) {
33+
this.host = properties.getHost();
34+
this.port = properties.getPort();
35+
this.username = properties.getUsername();
36+
this.password = properties.getPassword();
37+
}
38+
39+
@Override
40+
public String getMinioHost() {
41+
return host;
42+
}
43+
44+
@Override
45+
public int getMinioPort() {
46+
return port;
47+
}
48+
49+
@Override
50+
public String getMinioUsername() {
51+
return username;
52+
}
53+
54+
@Override
55+
public String getMinioPassword() {
56+
return password;
57+
}
58+
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2019 The Turms Project
3+
* https://github.com/turms-im/turms
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package im.turms.server.common.testing.environment.minio;
19+
20+
import im.turms.server.common.testing.environment.ServiceTestEnvironmentAware;
21+
import im.turms.server.common.testing.environment.ServiceTestEnvironmentType;
22+
23+
/**
24+
* @author James Chen
25+
*/
26+
public interface MinioTestEnvironmentAware extends ServiceTestEnvironmentAware {
27+
28+
default ServiceTestEnvironmentType getMinioTestEnvironmentType() {
29+
throw new UnsupportedOperationException();
30+
}
31+
32+
default boolean isMinioRunning() {
33+
return isRunning();
34+
}
35+
36+
String getMinioHost();
37+
38+
int getMinioPort();
39+
40+
String getMinioUsername();
41+
42+
String getMinioPassword();
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (C) 2019 The Turms Project
3+
* https://github.com/turms-im/turms
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package im.turms.server.common.testing.environment.minio;
19+
20+
import im.turms.server.common.testing.environment.ServiceTestEnvironmentManager;
21+
import im.turms.server.common.testing.environment.ServiceTestEnvironmentType;
22+
import im.turms.server.common.testing.environment.TestEnvironmentContainer;
23+
import im.turms.server.common.testing.properties.MinioTestEnvironmentProperties;
24+
25+
/**
26+
* @author James Chen
27+
*/
28+
public class MinioTestEnvironmentManager extends ServiceTestEnvironmentManager
29+
implements MinioTestEnvironmentAware {
30+
31+
private final MinioTestEnvironmentAware environment;
32+
33+
public MinioTestEnvironmentManager(
34+
MinioTestEnvironmentProperties properties,
35+
TestEnvironmentContainer container) {
36+
super(properties.getType());
37+
environment = ServiceTestEnvironmentType.LOCAL == type
38+
? new MinioLocalTestEnvironmentManager(properties.getLocal())
39+
: new MinioContainerTestEnvironmentManager(container);
40+
}
41+
42+
@Override
43+
public ServiceTestEnvironmentType getMinioTestEnvironmentType() {
44+
return type;
45+
}
46+
47+
@Override
48+
public boolean isRunning() {
49+
return environment.isRunning();
50+
}
51+
52+
@Override
53+
public String getMinioHost() {
54+
return environment.getMinioHost();
55+
}
56+
57+
@Override
58+
public int getMinioPort() {
59+
return environment.getMinioPort();
60+
}
61+
62+
@Override
63+
public String getMinioUsername() {
64+
return environment.getMinioUsername();
65+
}
66+
67+
@Override
68+
public String getMinioPassword() {
69+
return environment.getMinioPassword();
70+
}
71+
}

0 commit comments

Comments
 (0)