1
1
package org .testcontainers .oceanbase ;
2
2
3
3
import org .testcontainers .containers .JdbcDatabaseContainer ;
4
+ import org .testcontainers .containers .wait .strategy .Wait ;
4
5
import org .testcontainers .utility .DockerImageName ;
5
6
6
7
/**
@@ -26,14 +27,54 @@ public class OceanBaseCEContainer extends JdbcDatabaseContainer<OceanBaseCEConta
26
27
27
28
private static final Integer RPC_PORT = 2882 ;
28
29
29
- private static final String DEFAULT_TEST_TENANT_NAME = "test" ;
30
+ private static final String DEFAULT_USER_TENANT_NAME = "test" ;
30
31
31
- private static final String DEFAULT_USERNAME = "root" ;
32
+ private static final String DEFAULT_USER = "root" ;
32
33
33
34
private static final String DEFAULT_PASSWORD = "" ;
34
35
35
36
private static final String DEFAULT_DATABASE_NAME = "test" ;
36
37
38
+ public enum Mode {
39
+ /**
40
+ * Use as much hardware resources as possible for deployment by default,
41
+ * and all environment variables are available.
42
+ */
43
+ NORMAL ,
44
+ /**
45
+ * Use the minimum hardware resources for deployment by default,
46
+ * and all environment variables are available.
47
+ */
48
+ MINI ,
49
+ /**
50
+ * Use minimal hardware resources and pre-built deployment files for quick startup,
51
+ * and password of user tenant is the only available environment variable.
52
+ */
53
+ SLIM ;
54
+
55
+ public static Mode fromString (String mode ) {
56
+ if (mode == null ) {
57
+ throw new IllegalArgumentException ("Mode cannot be null" );
58
+ }
59
+ switch (mode .toUpperCase ()) {
60
+ case "NORMAL" :
61
+ return NORMAL ;
62
+ case "MINI" :
63
+ return MINI ;
64
+ case "SLIM" :
65
+ return SLIM ;
66
+ default :
67
+ throw new IllegalArgumentException ("Unknown mode: " + mode );
68
+ }
69
+ }
70
+ }
71
+
72
+ private Mode mode = Mode .SLIM ;
73
+
74
+ private String tenantName = DEFAULT_USER_TENANT_NAME ;
75
+
76
+ private String password = DEFAULT_PASSWORD ;
77
+
37
78
public OceanBaseCEContainer (String dockerImageName ) {
38
79
this (DockerImageName .parse (dockerImageName ));
39
80
}
@@ -43,6 +84,28 @@ public OceanBaseCEContainer(DockerImageName dockerImageName) {
43
84
dockerImageName .assertCompatibleWith (DEFAULT_IMAGE_NAME );
44
85
45
86
addExposedPorts (SQL_PORT , RPC_PORT );
87
+ setWaitStrategy (Wait .forLogMessage (".*boot success!.*" , 1 ));
88
+ }
89
+
90
+ @ Override
91
+ protected void configure () {
92
+ addEnv ("MODE" , mode .name ().toLowerCase ());
93
+
94
+ if (!DEFAULT_USER_TENANT_NAME .equals (tenantName )) {
95
+ if (mode == Mode .SLIM ) {
96
+ throw new IllegalArgumentException ("Tenant name is not configurable on slim mode" );
97
+ }
98
+ addEnv ("OB_TENANT_NAME" , tenantName );
99
+ }
100
+
101
+ if (!DEFAULT_PASSWORD .equals (password )) {
102
+ addEnv ("OB_TENANT_PASSWORD" , password );
103
+ }
104
+ }
105
+
106
+ @ Override
107
+ protected void waitUntilContainerStarted () {
108
+ getWaitStrategy ().waitUntilReady (this );
46
109
}
47
110
48
111
@ Override
@@ -64,19 +127,31 @@ public String getDatabaseName() {
64
127
65
128
@ Override
66
129
public String getUsername () {
67
- // In OceanBase, the jdbc username is related to the name of user, tenant and cluster,
68
- // if a tenant name other than the default value 'test' is used, you should manually
69
- // construct the jdbc username by yourself.
70
- return DEFAULT_USERNAME + "@" + DEFAULT_TEST_TENANT_NAME ;
130
+ return DEFAULT_USER + "@" + tenantName ;
71
131
}
72
132
73
133
@ Override
74
134
public String getPassword () {
75
- return DEFAULT_PASSWORD ;
135
+ return password ;
76
136
}
77
137
78
138
@ Override
79
139
protected String getTestQueryString () {
80
140
return "SELECT 1" ;
81
141
}
142
+
143
+ public OceanBaseCEContainer withMode (String mode ) {
144
+ this .mode = Mode .fromString (mode );
145
+ return this ;
146
+ }
147
+
148
+ public OceanBaseCEContainer withTenantName (String tenantName ) {
149
+ this .tenantName = tenantName ;
150
+ return this ;
151
+ }
152
+
153
+ public OceanBaseCEContainer withPassword (String password ) {
154
+ this .password = password ;
155
+ return this ;
156
+ }
82
157
}
0 commit comments