|
16 | 16 | * Wrapper around tarantoolctl utility.
|
17 | 17 | */
|
18 | 18 | public class TarantoolControl {
|
| 19 | + public class TarantoolControlException extends RuntimeException { |
| 20 | + int code; |
| 21 | + String stdout; |
| 22 | + String stderr; |
| 23 | + |
| 24 | + TarantoolControlException(int code, String stdout, String stderr) { |
| 25 | + super("returned exitcode " + code + "\n" + |
| 26 | + "[stdout]\n" + stdout + "\n[stderr]\n" + stderr); |
| 27 | + this.code = code; |
| 28 | + this.stdout = stdout; |
| 29 | + this.stderr = stderr; |
| 30 | + } |
| 31 | + } |
| 32 | + |
19 | 33 | protected static final String tntCtlWorkDir = System.getProperty("tntCtlWorkDir",
|
20 | 34 | new File("testroot").getAbsolutePath());
|
21 | 35 | protected static final String instanceDir = new File("src/test").getAbsolutePath();
|
22 | 36 | protected static final String tarantoolCtlConfig = new File("src/test/.tarantoolctl").getAbsolutePath();
|
23 | 37 | protected static final int RESTART_TIMEOUT = 2000;
|
24 | 38 |
|
| 39 | + static { |
| 40 | + try { |
| 41 | + setupWorkDirectory(); |
| 42 | + } catch (IOException e) { |
| 43 | + throw new RuntimeException("Can't setup test root directory!", e); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + protected static void setupWorkDirectory() throws IOException { |
| 48 | + try { |
| 49 | + rmdir(tntCtlWorkDir); |
| 50 | + } catch (IOException ignored) { |
| 51 | + /* No-op. */ |
| 52 | + } |
| 53 | + |
| 54 | + mkdir(tntCtlWorkDir); |
| 55 | + for (File c : new File(instanceDir).listFiles()) |
| 56 | + if (c.getName().endsWith(".lua")) |
| 57 | + copyFile(c, tntCtlWorkDir); |
| 58 | + copyFile(tarantoolCtlConfig, tntCtlWorkDir); |
| 59 | + } |
| 60 | + |
25 | 61 | // Based on https://stackoverflow.com/a/779529
|
26 |
| - private void rmdir(File f) throws IOException { |
| 62 | + private static void rmdir(File f) throws IOException { |
27 | 63 | if (f.isDirectory()) {
|
28 | 64 | for (File c : f.listFiles())
|
29 | 65 | rmdir(c);
|
30 | 66 | }
|
31 | 67 | f.delete();
|
32 | 68 | }
|
33 | 69 |
|
34 |
| - private void rmdir(String f) throws IOException { |
| 70 | + private static void rmdir(String f) throws IOException { |
35 | 71 | rmdir(new File(f));
|
36 | 72 | }
|
37 | 73 |
|
38 |
| - private void mkdir(File f) throws IOException { |
| 74 | + private static void mkdir(File f) throws IOException { |
39 | 75 | f.mkdirs();
|
40 | 76 | }
|
41 | 77 |
|
42 |
| - private void mkdir(String f) throws IOException { |
| 78 | + private static void mkdir(String f) throws IOException { |
43 | 79 | mkdir(new File(f));
|
44 | 80 | }
|
45 | 81 |
|
@@ -79,23 +115,6 @@ private static String loadStream(InputStream s) throws IOException {
|
79 | 115 | return sb.toString();
|
80 | 116 | }
|
81 | 117 |
|
82 |
| - protected void setupWorkDirectory() throws IOException { |
83 |
| - rmdir(tntCtlWorkDir); |
84 |
| - mkdir(tntCtlWorkDir); |
85 |
| - for (File c : new File(instanceDir).listFiles()) |
86 |
| - if (c.getName().endsWith(".lua")) |
87 |
| - copyFile(c, tntCtlWorkDir); |
88 |
| - copyFile(tarantoolCtlConfig, tntCtlWorkDir); |
89 |
| - } |
90 |
| - |
91 |
| - TarantoolControl() { |
92 |
| - try { |
93 |
| - setupWorkDirectory(); |
94 |
| - } catch (IOException e) { |
95 |
| - throw new RuntimeException(e); |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 | 118 | /**
|
100 | 119 | * Control the given tarantool instance via tarantoolctl utility.
|
101 | 120 | *
|
@@ -158,16 +177,85 @@ public void run() {
|
158 | 177 | } catch (IOException e) {
|
159 | 178 | /* No-op. */
|
160 | 179 | }
|
161 |
| - throw new RuntimeException("returned exitcode " + code + "\n" + |
162 |
| - "[stdout]\n" + stdout + "\n[stderr]\n" + stderr); |
| 180 | + throw new TarantoolControlException(code, stdout, stderr); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Wait until the instance will be started. |
| 186 | + * |
| 187 | + * Use tarantoolctl status instanceName. |
| 188 | + * |
| 189 | + * Then test connection using text console (TarantoolTcpConsole) to ensure |
| 190 | + * the instance was entirely started or fallback to binary port |
| 191 | + * (TarantoolLocalConsole) when the test console was not opened. |
| 192 | + */ |
| 193 | + public void waitStarted(String instanceName) { |
| 194 | + while (status(instanceName) != 0) |
| 195 | + sleep(); |
| 196 | + |
| 197 | + while (true) { |
| 198 | + try { |
| 199 | + openConsole(instanceName).close(); |
| 200 | + break; |
| 201 | + } catch (Exception ignored) { |
| 202 | + /* No-op. */ |
| 203 | + } |
| 204 | + sleep(); |
163 | 205 | }
|
164 | 206 | }
|
165 | 207 |
|
| 208 | + /** |
| 209 | + * Wait until the instance will be stopped. |
| 210 | + * |
| 211 | + * Use tarantoolctl status instanceName. |
| 212 | + */ |
| 213 | + public void waitStopped(String instanceName) { |
| 214 | + while (status(instanceName) != 1) |
| 215 | + sleep(); |
| 216 | + } |
| 217 | + |
166 | 218 | public void start(String instanceName) {
|
167 | 219 | executeCommand("start", instanceName);
|
168 | 220 | }
|
169 | 221 |
|
170 | 222 | public void stop(String instanceName) {
|
171 | 223 | executeCommand("stop", instanceName);
|
172 | 224 | }
|
| 225 | + |
| 226 | + /** |
| 227 | + * Wrapper for `tarantoolctl status instanceName`. |
| 228 | + * |
| 229 | + * Return exit code of the command: |
| 230 | + * |
| 231 | + * * 0 -- started; |
| 232 | + * * 1 -- stopped; |
| 233 | + * * 2 -- pid file exists, control socket inaccessible. |
| 234 | + */ |
| 235 | + public int status(String instanceName) { |
| 236 | + try { |
| 237 | + executeCommand("status", instanceName); |
| 238 | + } catch (TarantoolControlException e) { |
| 239 | + return e.code; |
| 240 | + } |
| 241 | + |
| 242 | + return 0; |
| 243 | + } |
| 244 | + |
| 245 | + /* |
| 246 | + * XXX: This function is planned to use text console (from ADMIN |
| 247 | + * environment variable) when it is availiable for the instance and |
| 248 | + * fallback to TarantoolLocalConsole. |
| 249 | + */ |
| 250 | + public TarantoolConsole openConsole(String instanceName) { |
| 251 | + return TarantoolConsole.open(tntCtlWorkDir, instanceName); |
| 252 | + } |
| 253 | + |
| 254 | + public static void sleep() { |
| 255 | + try { |
| 256 | + Thread.sleep(100); |
| 257 | + } catch (InterruptedException e) { |
| 258 | + throw new RuntimeException(e); |
| 259 | + } |
| 260 | + } |
173 | 261 | }
|
0 commit comments