Skip to content

Commit 027ab2d

Browse files
authored
Add tests migrations (#42)
* Add tests with migrations Co-authored-by: CMX\vlutsenko <Vladrock01>
1 parent 871af8f commit 027ab2d

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package org.testcontainers.containers;
2+
3+
import org.junit.Assert;
4+
import org.junit.jupiter.api.AfterAll;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
import org.slf4j.LoggerFactory;
8+
import org.testcontainers.containers.output.Slf4jLogConsumer;
9+
import org.testcontainers.junit.jupiter.Container;
10+
import org.testcontainers.junit.jupiter.Testcontainers;
11+
12+
import java.io.BufferedReader;
13+
import java.io.IOException;
14+
import java.io.InputStreamReader;
15+
import java.io.OutputStream;
16+
import java.net.HttpURLConnection;
17+
import java.net.MalformedURLException;
18+
import java.net.URL;
19+
import java.nio.charset.StandardCharsets;
20+
import java.time.Duration;
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
@Testcontainers
25+
public class TarantoolCartridgeMigrationsTest {
26+
27+
@Container
28+
private static final TarantoolCartridgeContainer container =
29+
new TarantoolCartridgeContainer(
30+
"Dockerfile",
31+
"cartridge/instances.yml",
32+
"cartridge/topology.lua")
33+
.withDirectoryBinding("cartridge")
34+
.withStartupTimeout(Duration.ofSeconds(300))
35+
.withLogConsumer(new Slf4jLogConsumer(
36+
LoggerFactory.getLogger(TarantoolCartridgeStaticContainerTest.class)));
37+
38+
@BeforeAll
39+
public static void test_start_container() {
40+
if (!container.isRunning()) {
41+
try {
42+
container.start();
43+
} catch (Exception e) {
44+
throw new RuntimeException(e);
45+
}
46+
}
47+
}
48+
49+
@AfterAll
50+
public static void clear_stop_container() {
51+
if (container.isRunning()) {
52+
container.stop();
53+
}
54+
}
55+
56+
@Test
57+
public void test_migrations_migrator() throws Exception {
58+
container.executeCommand("require('migrator').up()").get();
59+
}
60+
61+
@Test
62+
public void test_migrations_curl() throws IOException, InterruptedException {
63+
64+
String urlStr = "http://" + container.getRouterHost() + ":" + "8081" + "/migrations/up";
65+
int code = -1;
66+
org.testcontainers.containers.Container.ExecResult answer = container.execInContainer("curl", "-X", "POST", urlStr);
67+
code = answer.getExitCode();
68+
Assert.assertEquals(0, code);
69+
}
70+
71+
@Test
72+
public void test_migrations_http() throws Exception {
73+
74+
HttpURLConnection connection;
75+
OutputStream os = null;
76+
InputStreamReader inputStreamReader = null;
77+
BufferedReader bfR = null;
78+
StringBuilder strBuilder = new StringBuilder();
79+
80+
Map<String, String> bodyHttpPostRequest = new HashMap<>();
81+
byte[] outSteamByte = bodyHttpPostRequest.toString().getBytes(StandardCharsets.UTF_8);
82+
83+
try {
84+
String urlStr = "http://" + container.getRouterHost() + ":" + container.getAPIPort() + "/migrations/up";
85+
connection = createConnection(urlStr);
86+
os = connection.getOutputStream();
87+
os.write(outSteamByte);
88+
89+
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
90+
inputStreamReader = new InputStreamReader(connection.getInputStream());
91+
bfR = new BufferedReader(inputStreamReader);
92+
String line;
93+
while ((line = bfR.readLine()) != null) {
94+
strBuilder.append(line);
95+
}
96+
}
97+
98+
} catch (MalformedURLException ex) {
99+
} catch (IOException e) {
100+
} finally {
101+
inputStreamReader.close();
102+
os.close();
103+
bfR.close();
104+
}
105+
Assert.assertTrue(strBuilder.toString().contains("applied"));
106+
}
107+
108+
public HttpURLConnection createConnection(String urlStr) throws IOException {
109+
HttpURLConnection connection = null;
110+
URL url = new URL(urlStr);
111+
112+
connection = (HttpURLConnection) url.openConnection();
113+
connection.setRequestMethod("POST");
114+
connection.setDoOutput(true);
115+
connection.setDoInput(true);
116+
connection.setRequestProperty("Content-Type", "application/json");
117+
connection.setConnectTimeout(200);
118+
connection.setReadTimeout(200);
119+
connection.connect();
120+
121+
return connection;
122+
}
123+
124+
}

src/test/resources/cartridge/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ end
2828

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

3233
local cartridge = require('cartridge')
3334
local ok, err = cartridge.cfg({
@@ -38,6 +39,7 @@ local ok, err = cartridge.cfg({
3839
'app.roles.api_router',
3940
'app.roles.api_storage',
4041
'app.roles.custom',
42+
'migrator',
4143
},
4244
cluster_cookie = 'testapp-cluster-cookie',
4345
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
return{
2+
3+
up = function()
4+
local utils = require('migrator.utils')
5+
6+
local spaceTest = box.schema.space.create('spaceTest', { if_not_exists = true })
7+
spaceTest:format({
8+
{ name = 'sessionId', type = 'string' },
9+
{ name = 'data', type = 'map', is_nullable = true },
10+
{ name = 'ts', type = 'number' },
11+
12+
-- vshard bucket id
13+
{ name = 'bucket_id', type = 'unsigned', is_nullable = false },
14+
})
15+
16+
spaceTest:create_index('primary', { parts = { { field = 'sessionId' } },
17+
unique = true,
18+
if_not_exists = true })
19+
20+
spaceTest:create_index('bucket_id', {
21+
parts = { 'bucket_id' },
22+
unique = false,
23+
if_not_exists = true
24+
})
25+
26+
utils.register_sharding_key('spaceTest', { 'sessionId' })
27+
28+
return true
29+
30+
end
31+
}

src/test/resources/cartridge/testapp-scm-1.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies = {
99
'lua >= 5.1',
1010
'cartridge == 2.7.3-1',
1111
'crud == 0.10.0-1',
12+
'migrations == 0.4.1-1',
1213
}
1314
build = {
1415
type = 'none';

0 commit comments

Comments
 (0)