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
112
113
114
115
116
117
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.testrunner;
import org.fusesource.jansi.Ansi;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.concurrent.ExecutionException;
import java.util.logging.LogRecord;
import static com.yahoo.vespa.testrunner.TestRunner.Suite.STAGING_TEST;
import static com.yahoo.vespa.testrunner.TestRunner.Suite.SYSTEM_TEST;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* Unit tests relying on a UNIX shell >_<
*
* @author jvenstad
*/
public class TestRunnerTest {
@Rule
public TemporaryFolder tmp = new TemporaryFolder();
private Path artifactsPath;
private Path testPath;
private Path configFile;
private Path settingsFile;
@Before
public void setup() throws IOException {
artifactsPath = tmp.newFolder("artifacts").toPath();
Files.createFile(artifactsPath.resolve("my-tests.jar"));
Files.createFile(artifactsPath.resolve("my-fat-test.jar"));
testPath = tmp.newFolder("testData").toPath();
configFile = tmp.newFile("testConfig.json").toPath();
settingsFile = tmp.newFile("settings.xml").toPath();
}
@Test
public void noTestJarIsANoTests() throws InterruptedException, IOException, ExecutionException {
Files.delete(artifactsPath.resolve("my-tests.jar"));
TestRunner runner = new TestRunner(artifactsPath, testPath, configFile, settingsFile,
__ -> new ProcessBuilder("This is a command that doesn't exist, for sure!"));
runner.test(SYSTEM_TEST, new byte[0]).get();
assertFalse(runner.getLog(-1).iterator().hasNext());
assertEquals(TestRunner.Status.NO_TESTS, runner.getStatus());
}
@Test
public void errorLeadsToError() throws InterruptedException {
TestRunner runner = new TestRunner(artifactsPath, testPath, configFile, settingsFile,
__ -> new ProcessBuilder("false"));
runner.test(SYSTEM_TEST, new byte[0]);
while (runner.getStatus() == TestRunner.Status.RUNNING) {
Thread.sleep(10);
}
assertEquals(1, runner.getLog(-1).size());
assertEquals(TestRunner.Status.FAILURE, runner.getStatus());
}
@Test
public void failureLeadsToFailure() throws InterruptedException {
TestRunner runner = new TestRunner(artifactsPath, testPath, configFile, settingsFile,
__ -> new ProcessBuilder("false"));
runner.test(SYSTEM_TEST, new byte[0]);
while (runner.getStatus() == TestRunner.Status.RUNNING) {
Thread.sleep(10);
}
assertEquals(1, runner.getLog(-1).size());
assertEquals(TestRunner.Status.FAILURE, runner.getStatus());
}
@Test
public void filesAreGenerated() throws InterruptedException, IOException {
TestRunner runner = new TestRunner(artifactsPath, testPath, configFile, settingsFile,
__ -> new ProcessBuilder("echo", "Hello!"));
runner.test(SYSTEM_TEST, "config".getBytes());
while (runner.getStatus() == TestRunner.Status.RUNNING) {
Thread.sleep(10);
}
assertEquals("config", new String(Files.readAllBytes(configFile)));
assertTrue(Files.exists(testPath.resolve("pom.xml")));
assertTrue(Files.exists(settingsFile));
}
@Test
public void runnerCanBeReused() throws InterruptedException, IOException {
TestRunner runner = new TestRunner(artifactsPath, testPath, configFile, settingsFile,
__ -> new ProcessBuilder("sleep", "0.1"));
runner.test(SYSTEM_TEST, "config".getBytes());
assertEquals(TestRunner.Status.RUNNING, runner.getStatus());
while (runner.getStatus() == TestRunner.Status.RUNNING) {
Thread.sleep(10);
}
assertEquals(1, runner.getLog(-1).size());
assertEquals(TestRunner.Status.SUCCESS, runner.getStatus());
runner.test(STAGING_TEST, "newConfig".getBytes());
while (runner.getStatus() == TestRunner.Status.RUNNING) {
Thread.sleep(10);
}
assertEquals("newConfig", new String(Files.readAllBytes(configFile)));
assertEquals(1, runner.getLog(-1).size());
}
}
|