Skip to content

Add tests migrations #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,4 @@
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package org.testcontainers.containers;

import org.junit.Assert;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Testcontainers
public class TarantoolCartridgeMigrationsTest {

@Container
private static final TarantoolCartridgeContainer container =
new TarantoolCartridgeContainer(
"Dockerfile",
"cartridge/instances.yml",
"cartridge/topology.lua")
.withDirectoryBinding("cartridge")
.withStartupTimeout(Duration.ofSeconds(300))
.withLogConsumer(new Slf4jLogConsumer(
LoggerFactory.getLogger(TarantoolCartridgeStaticContainerTest.class)));

@BeforeAll
public static void test_start_container() {
if (!container.isRunning()) {
try {
container.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

@AfterAll
public static void clear_stop_container() {
if (container.isRunning()) {
container.stop();
}
}

@Test
public void test_migrations_migrator() throws Exception {
container.executeCommand("require('migrator').up()").get();
}

@Test
public void test_migrations_curl() throws IOException, InterruptedException {

String urlStr = "http://" + container.getRouterHost() + ":" + "8081" + "/migrations/up";
int code = -1;
org.testcontainers.containers.Container.ExecResult answer = container.execInContainer("curl", "-X", "POST", urlStr);
code = answer.getExitCode();
Assert.assertEquals(0, code);
}

@Test
public void test_migrations_http() throws Exception {

HttpURLConnection connection;
OutputStream os = null;
InputStreamReader inputStreamReader = null;
BufferedReader bfR = null;
StringBuilder strBuilder = new StringBuilder();

Map<String, String> bodyHttpPostRequest = new HashMap<>();
byte[] outSteamByte = bodyHttpPostRequest.toString().getBytes(StandardCharsets.UTF_8);

try {
String urlStr = "http://" + container.getRouterHost() + ":" + container.getAPIPort() + "/migrations/up";
connection = createConnection(urlStr);
os = connection.getOutputStream();
os.write(outSteamByte);

if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStreamReader = new InputStreamReader(connection.getInputStream());
bfR = new BufferedReader(inputStreamReader);
String line;
while ((line = bfR.readLine()) != null) {
strBuilder.append(line);
}
}

} catch (MalformedURLException ex) {
} catch (IOException e) {
} finally {
inputStreamReader.close();
os.close();
bfR.close();
}
Assert.assertTrue(strBuilder.toString().contains("applied"));
}

public HttpURLConnection createConnection(String urlStr) throws IOException {
HttpURLConnection connection = null;
URL url = new URL(urlStr);

connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setConnectTimeout(200);
connection.setReadTimeout(200);
connection.connect();

return connection;
}

}
2 changes: 2 additions & 0 deletions src/test/resources/cartridge/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ end

-- For faster set up, discovery all buckets at once
require('vshard.consts').BUCKET_CHUNK_SIZE = 30000
require('migrator')

local cartridge = require('cartridge')
local ok, err = cartridge.cfg({
Expand All @@ -38,6 +39,7 @@ local ok, err = cartridge.cfg({
'app.roles.api_router',
'app.roles.api_storage',
'app.roles.custom',
'migrator',
},
cluster_cookie = 'testapp-cluster-cookie',
})
Expand Down
31 changes: 31 additions & 0 deletions src/test/resources/cartridge/migrations/001_spaсe_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
return{

up = function()
local utils = require('migrator.utils')

local spaceTest = box.schema.space.create('spaceTest', { if_not_exists = true })
spaceTest:format({
{ name = 'sessionId', type = 'string' },
{ name = 'data', type = 'map', is_nullable = true },
{ name = 'ts', type = 'number' },

-- vshard bucket id
{ name = 'bucket_id', type = 'unsigned', is_nullable = false },
})

spaceTest:create_index('primary', { parts = { { field = 'sessionId' } },
unique = true,
if_not_exists = true })

spaceTest:create_index('bucket_id', {
parts = { 'bucket_id' },
unique = false,
if_not_exists = true
})

utils.register_sharding_key('spaceTest', { 'sessionId' })

return true

end
}
1 change: 1 addition & 0 deletions src/test/resources/cartridge/testapp-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies = {
'lua >= 5.1',
'cartridge == 2.7.3-1',
'crud == 0.10.0-1',
'migrations == 0.4.1-1',
}
build = {
type = 'none';
Expand Down