1
1
package org .testcontainers .containers ;
2
2
3
+ import org .apache .commons .lang3 .StringUtils ;
3
4
import org .testcontainers .containers .wait .strategy .Wait ;
4
5
import org .testcontainers .utility .DockerImageName ;
5
6
@@ -25,12 +26,53 @@ public class OceanBaseContainer extends JdbcDatabaseContainer<OceanBaseContainer
25
26
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName .parse (DOCKER_IMAGE_NAME );
26
27
27
28
private static final Integer SQL_PORT = 2881 ;
28
-
29
29
private static final Integer RPC_PORT = 2882 ;
30
30
31
- private final String databaseName = "test" ;
32
- private final String username = "root@test" ;
33
- private final String password = "" ;
31
+ private static final String SYSTEM_TENANT = "sys" ;
32
+ private static final String DEFAULT_USERNAME = "root" ;
33
+ private static final String DEFAULT_PASSWORD = "" ;
34
+ private static final String DEFAULT_TENANT_NAME = "test" ;
35
+ private static final String DEFAULT_DATABASE_NAME = "test" ;
36
+
37
+ /**
38
+ * The deployment mode of OceanBase. See <a href="https://hub.docker.com/r/oceanbase/oceanbase-ce">Docker Hub</a> for more details.
39
+ */
40
+ enum Mode {
41
+ /**
42
+ * Standard standalone deployment with a monitor service.
43
+ */
44
+ NORMAL ,
45
+
46
+ /**
47
+ * Similar to 'normal' mode, but uses less hardware resources.
48
+ */
49
+ MINI ,
50
+
51
+ /**
52
+ * Standalone deployment without the monitor service, which uses the least hardware resources.
53
+ */
54
+ SLIM ;
55
+
56
+ static Mode fromString (String mode ) {
57
+ if (StringUtils .isEmpty (mode )) {
58
+ throw new IllegalArgumentException ("Mode cannot be null or empty" );
59
+ }
60
+ switch (mode .trim ().toLowerCase ()) {
61
+ case "normal" :
62
+ return NORMAL ;
63
+ case "mini" :
64
+ return MINI ;
65
+ case "slim" :
66
+ return SLIM ;
67
+ default :
68
+ throw new IllegalArgumentException ("Unsupported mode: " + mode );
69
+ }
70
+ }
71
+ }
72
+
73
+ private Mode mode = Mode .SLIM ;
74
+ private String sysRootPassword = DEFAULT_PASSWORD ;
75
+ private String tenantName = DEFAULT_TENANT_NAME ;
34
76
35
77
public OceanBaseContainer (String dockerImageName ) {
36
78
this (DockerImageName .parse (dockerImageName ));
@@ -45,49 +87,100 @@ public OceanBaseContainer(DockerImageName dockerImageName) {
45
87
addExposedPorts (SQL_PORT , RPC_PORT );
46
88
}
47
89
90
+ @ Override
91
+ protected void waitUntilContainerStarted () {
92
+ getWaitStrategy ().waitUntilReady (this );
93
+ }
94
+
48
95
public Integer getSqlPort () {
49
- return getMappedPort (SQL_PORT );
96
+ return getActualPort (SQL_PORT );
50
97
}
51
98
52
- public Integer getRpcPort ( ) {
53
- return getMappedPort (RPC_PORT );
99
+ public Integer getActualPort ( int port ) {
100
+ return "host" . equals ( getNetworkMode ()) ? port : getMappedPort (port );
54
101
}
55
102
56
103
@ Override
57
104
public String getDriverClassName () {
58
- return "com.oceanbase .jdbc.Driver" ;
105
+ return "com.mysql.cj .jdbc.Driver" ;
59
106
}
60
107
61
108
@ Override
62
109
public String getJdbcUrl () {
110
+ return getJdbcUrl (DEFAULT_DATABASE_NAME );
111
+ }
112
+
113
+ public String getJdbcUrl (String databaseName ) {
63
114
String additionalUrlParams = constructUrlParameters ("?" , "&" );
64
- return (
65
- "jdbc:oceanbase://" + getHost () + ":" + getMappedPort (SQL_PORT ) + "/" + databaseName + additionalUrlParams
66
- );
115
+ return "jdbc:mysql://" + getHost () + ":" + getSqlPort () + "/" + databaseName + additionalUrlParams ;
67
116
}
68
117
69
118
@ Override
70
119
public String getDatabaseName () {
71
- return databaseName ;
120
+ return DEFAULT_DATABASE_NAME ;
72
121
}
73
122
74
123
@ Override
75
124
public String getUsername () {
76
- return username ;
125
+ return DEFAULT_USERNAME + "@" + tenantName ;
77
126
}
78
127
79
128
@ Override
80
129
public String getPassword () {
81
- return password ;
130
+ return DEFAULT_PASSWORD ;
82
131
}
83
132
84
133
@ Override
85
134
protected String getTestQueryString () {
86
135
return "SELECT 1" ;
87
136
}
88
137
138
+ /**
139
+ * Set the deployment mode.
140
+ *
141
+ * @param mode the deployment mode, can be 'slim', 'mini' or 'normal'
142
+ * @return this
143
+ */
144
+ public OceanBaseContainer withMode (String mode ) {
145
+ this .mode = Mode .fromString (mode );
146
+ return self ();
147
+ }
148
+
149
+ /**
150
+ * Set the root password of sys tenant.
151
+ *
152
+ * @param sysRootPassword the root password of sys tenant
153
+ * @return this
154
+ */
155
+ public OceanBaseContainer withSysRootPassword (String sysRootPassword ) {
156
+ if (sysRootPassword == null ) {
157
+ throw new IllegalArgumentException ("The root password of sys tenant cannot be null" );
158
+ }
159
+ this .sysRootPassword = sysRootPassword ;
160
+ return self ();
161
+ }
162
+
163
+ /**
164
+ * Set the non-system tenant name to be created for testing.
165
+ *
166
+ * @param tenantName the tenant name to be created
167
+ * @return this
168
+ */
169
+ public OceanBaseContainer withTenantName (String tenantName ) {
170
+ if (StringUtils .isEmpty (tenantName )) {
171
+ throw new IllegalArgumentException ("Tenant name cannot be null or empty" );
172
+ }
173
+ if (SYSTEM_TENANT .equals (tenantName )) {
174
+ throw new IllegalArgumentException ("Tenant name cannot be " + SYSTEM_TENANT );
175
+ }
176
+ this .tenantName = tenantName ;
177
+ return self ();
178
+ }
179
+
89
180
@ Override
90
- protected void waitUntilContainerStarted () {
91
- getWaitStrategy ().waitUntilReady (this );
181
+ protected void configure () {
182
+ withEnv ("MODE" , mode .toString ().toLowerCase ());
183
+ withEnv ("OB_ROOT_PASSWORD" , sysRootPassword );
184
+ withEnv ("OB_TENANT_NAME" , tenantName );
92
185
}
93
186
}
0 commit comments