|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY |
| 3 | + * All Rights Reserved. |
| 4 | + */ |
| 5 | + |
| 6 | +package org.testcontainers.containers.tarantool; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | + |
| 10 | +import org.testcontainers.containers.Container; |
| 11 | +import org.testcontainers.containers.utils.SslContext; |
| 12 | +import org.yaml.snakeyaml.Yaml; |
| 13 | + |
| 14 | +public final class TarantoolContainerLuaExecutor { |
| 15 | + |
| 16 | + private static final String EXECUTE_COMMAND_ERROR_TEMPLATE = |
| 17 | + "Executed command \"%s\" with exit code %d, stderr: \"%s\", stdout: \"%s\""; |
| 18 | + private static final String MTLS_COMMAND_TEMPLATE = |
| 19 | + "echo \" " + |
| 20 | + " print(require('yaml').encode( " + |
| 21 | + " {require('net.box').connect( " + |
| 22 | + " { uri='%s:%d', params = { transport='ssl', ssl_key_file = '%s', ssl_cert_file = '%s'" + |
| 23 | + " }}, " + |
| 24 | + " { user = '%s', password = '%s' } " + |
| 25 | + " ):eval('%s')}) " + |
| 26 | + " ); " + |
| 27 | + " os.exit(); " + |
| 28 | + "\" > container-tmp.lua &&" + |
| 29 | + " tarantool container-tmp.lua"; |
| 30 | + private static final String SSL_COMMAND_TEMPLATE = |
| 31 | + "echo \" " + |
| 32 | + " print(require('yaml').encode( " + |
| 33 | + " {require('net.box').connect( " + |
| 34 | + " { uri='%s:%d', params = { transport='ssl' }}, " + |
| 35 | + " { user = '%s', password = '%s' } " + |
| 36 | + " ):eval('%s')}) " + |
| 37 | + " ); " + |
| 38 | + " os.exit(); " + |
| 39 | + "\" > container-tmp.lua &&" + |
| 40 | + " tarantool container-tmp.lua"; |
| 41 | + private static final String COMMAND_TEMPLATE = "echo \" " + |
| 42 | + " print(require('yaml').encode( " + |
| 43 | + " {require('net.box').connect( " + |
| 44 | + " '%s:%d', " + |
| 45 | + " { user = '%s', password = '%s' } " + |
| 46 | + " ):eval('%s')}) " + |
| 47 | + " ); " + |
| 48 | + " os.exit(); " + |
| 49 | + "\" > container-tmp.lua &&" + |
| 50 | + " tarantool container-tmp.lua"; |
| 51 | + |
| 52 | + private static final String ENV_USERNAME_CMD = |
| 53 | + "echo ${TARANTOOL_USER_NAME:-${TT_CLI_USERNAME:-guest}}"; |
| 54 | + private static final String ENV_PASSWORD_CMD = |
| 55 | + "echo ${TARANTOOL_USER_PASSWORD:-${TT_CLI_PASSWORD:-}}"; |
| 56 | + |
| 57 | + private static final Yaml YAML = new Yaml(); |
| 58 | + |
| 59 | + private final Container<?> container; |
| 60 | + private final int port; |
| 61 | + |
| 62 | + public TarantoolContainerLuaExecutor(Container<?> container, int port) { |
| 63 | + this.container = container; |
| 64 | + this.port = port; |
| 65 | + } |
| 66 | + |
| 67 | + public String getExecResult(String command) throws Exception { |
| 68 | + Container.ExecResult result = this.container.execInContainer(command); |
| 69 | + if (result.getExitCode() != 0) { |
| 70 | + throw new RuntimeException("Cannot execute script: " + command); |
| 71 | + } |
| 72 | + return result.getStdout() |
| 73 | + .trim() |
| 74 | + .replace("\n", "") |
| 75 | + .replace("...", "") |
| 76 | + .replace("--", "") |
| 77 | + .trim(); |
| 78 | + } |
| 79 | + |
| 80 | + public Container.ExecResult executeCommand(String command) throws IOException, InterruptedException { |
| 81 | + return executeCommand(command, null); |
| 82 | + } |
| 83 | + |
| 84 | + public Container.ExecResult executeCommand(String command, SslContext sslContext) |
| 85 | + throws IOException, InterruptedException { |
| 86 | + if (!container.isRunning()) { |
| 87 | + throw new IllegalStateException("Cannot execute commands in stopped container"); |
| 88 | + } |
| 89 | + |
| 90 | + command = command.replace("\"", "\\\""); |
| 91 | + command = command.replace("\'", "\\\'"); |
| 92 | + |
| 93 | + String username = getUsernameFromEnv(); |
| 94 | + String password = getPasswordFromEnv(); |
| 95 | + String host = "localhost"; |
| 96 | + |
| 97 | + String bashCommand; |
| 98 | + if (sslContext == null) { |
| 99 | + bashCommand = String.format(COMMAND_TEMPLATE, |
| 100 | + host, port, |
| 101 | + username, password, |
| 102 | + command |
| 103 | + ); |
| 104 | + } else if (sslContext.getKeyFile() != null && sslContext.getCertFile() != null) { |
| 105 | + bashCommand = String.format(MTLS_COMMAND_TEMPLATE, |
| 106 | + host, port, |
| 107 | + sslContext.getKeyFile(), sslContext.getCertFile(), |
| 108 | + username, password, |
| 109 | + command |
| 110 | + ); |
| 111 | + } else { |
| 112 | + bashCommand = String.format(SSL_COMMAND_TEMPLATE, |
| 113 | + host, port, |
| 114 | + username, password, |
| 115 | + command |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + return container.execInContainer("sh", "-c", bashCommand); |
| 120 | + } |
| 121 | + |
| 122 | + public <T> T executeCommandDecoded(String command) throws IOException, InterruptedException { |
| 123 | + return executeCommandDecoded(command, null); |
| 124 | + } |
| 125 | + |
| 126 | + public <T> T executeCommandDecoded(String command, SslContext sslContext) |
| 127 | + throws IOException, InterruptedException { |
| 128 | + Container.ExecResult result = executeCommand(command, sslContext); |
| 129 | + |
| 130 | + if (result.getExitCode() != 0) { |
| 131 | + throw new IllegalStateException(String.format(EXECUTE_COMMAND_ERROR_TEMPLATE, |
| 132 | + command, result.getExitCode(), result.getStderr(), result.getStdout())); |
| 133 | + } |
| 134 | + |
| 135 | + return YAML.load(result.getStdout()); |
| 136 | + } |
| 137 | + |
| 138 | + private String getUsernameFromEnv() throws IOException, InterruptedException { |
| 139 | + Container.ExecResult result = container.execInContainer("sh", "-c", ENV_USERNAME_CMD); |
| 140 | + if (result.getExitCode() != 0) { |
| 141 | + return "guest"; |
| 142 | + } |
| 143 | + String username = result.getStdout().trim(); |
| 144 | + return username.isEmpty() ? "guest" : username; |
| 145 | + } |
| 146 | + |
| 147 | + private String getPasswordFromEnv() throws IOException, InterruptedException { |
| 148 | + Container.ExecResult result = container.execInContainer("sh", "-c", ENV_PASSWORD_CMD); |
| 149 | + if (result.getExitCode() != 0) { |
| 150 | + return ""; |
| 151 | + } |
| 152 | + return result.getStdout().trim(); |
| 153 | + } |
| 154 | +} |
0 commit comments