-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathAptTest.java
111 lines (94 loc) · 4.19 KB
/
AptTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package com.vaticle.typedb.core.test.deployment;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.ProcessResult;
import org.zeroturnaround.exec.StartedProcess;
import java.io.IOException;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;
import static org.junit.Assert.assertTrue;
public class AptTest {
private static final Logger LOG = LoggerFactory.getLogger(AptTest.class);
private static final String aptSnapshot = "https://repo.typedb.com/public/public-snapshot/deb/ubuntu";
private static final String aptRelease = "https://repo.typedb.com/public/public-release/deb/ubuntu";
private static final String pubkey1 = "17507562824cfdcc";
private static final String pubkey2 = "https://cli-assets.heroku.com/apt/release.key";
private static final String pubkey3 = "https://dl.google.com/linux/linux_signing_key.pub";
private static final Path versionFile = Paths.get("VERSION");
private static final int typeDBPort = 1729;
private final String commit;
private final ProcessExecutor executor;
private StartedProcess typeDBProcess;
public AptTest() {
commit = System.getenv("TEST_DEPLOYMENT_APT_COMMIT");
executor = new ProcessExecutor().directory(Paths.get(".").toFile()).readOutput(true);
}
@Test
public void test() throws InterruptedException, IOException, TimeoutException {
setup();
install();
start();
stop();
}
private void setup() throws InterruptedException, TimeoutException, IOException {
execute("sudo", "apt-key", "adv", "--keyserver", "keyserver.ubuntu.com", "--recv", pubkey1);
execute("sudo", "add-apt-repository", "deb " + aptSnapshot + " trusty main");
execute("sudo", "add-apt-repository", "deb " + aptRelease + " trusty main");
execute("bash", "-c", "curl -L " + pubkey2 + " | sudo apt-key add -");
execute("bash", "-c", "wget -q -O - " + pubkey3 + " | sudo apt-key add -");
execute("sudo", "apt", "update");
}
private void install() throws InterruptedException, TimeoutException, IOException {
System.out.println("core = " + commit);
Files.writeString(versionFile, commit, StandardCharsets.US_ASCII);
execute("sudo", "apt", "install", "-y", "typedb=0.0.0-" + commit);
}
private void start() throws InterruptedException, IOException {
typeDBProcess = executor.command("typedb", "server", "--development-mode.enable=true").start();
waitUntilReady();
assertTrue("TypeDB failed to start", typeDBProcess.getProcess().isAlive());
System.out.println("TypeDB server started");
}
private void stop() {
if (typeDBProcess != null) {
System.out.println("Stopping TypeDB server");
typeDBProcess.getProcess().destroy();
System.out.println("TypeDB server stopped");
}
}
private void waitUntilReady() throws InterruptedException {
for (int attempt = 0; !isTypeDBServerReady() && attempt < 25; attempt++) {
Thread.sleep(1000);
}
}
private static boolean isTypeDBServerReady() {
try {
Socket s = new Socket("localhost", typeDBPort);
s.close();
return true;
} catch (IOException e) {
return false;
}
}
private ProcessResult execute(String... cmd) throws InterruptedException, TimeoutException, IOException {
ProcessResult result = executor.command(cmd).execute();
if (result.getExitValue() != 0) {
LOG.error("An error has occurred.");
LOG.error(" - cmd: " + Arrays.toString(cmd));
LOG.error(" - output: " + result.outputString());
throw new RuntimeException();
} else return result;
}
}