summaryrefslogtreecommitdiffstats
path: root/vespa-testrunner-components/src/test/java/com/yahoo/vespa/hosted/testrunner/TestRunnerTest.java
blob: 4612f5b217afaaf20624fc7ba4a0455804cb4db7 (plain) (blame)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.testrunner;

import com.yahoo.vespa.testrunner.legacy.TestProfile;
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.logging.LogRecord;

import static org.junit.Assert.assertEquals;
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 logFile;
    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();
        logFile = tmp.newFile("maven.log").toPath();
        configFile = tmp.newFile("testConfig.json").toPath();
        settingsFile = tmp.newFile("settings.xml").toPath();
    }

    @Test
    public void ansiCodesAreConvertedToHtml() throws InterruptedException {
        TestRunner runner = new TestRunner(artifactsPath, testPath, logFile, configFile, settingsFile,
                                           __ -> new ProcessBuilder("echo", Ansi.ansi().fg(Ansi.Color.RED).a("Hello!").reset().toString()));
        runner.test(TestProfile.SYSTEM_TEST, new byte[0]);
        while (runner.getStatus() == TestRunner.Status.RUNNING) {
            Thread.sleep(10);
        }
        Iterator<LogRecord> log = runner.getLog(-1).iterator();
        log.next();
        LogRecord record = log.next();
        assertEquals("<span style=\"color: red;\">Hello!</span>", record.getMessage());
        assertEquals(0, runner.getLog(record.getSequenceNumber()).size());
        assertEquals(TestRunner.Status.SUCCESS, runner.getStatus());
    }

    @Test
    public void noTestJarIsAFailure() throws InterruptedException, IOException {
        Files.delete(artifactsPath.resolve("my-tests.jar"));
        TestRunner runner = new TestRunner(artifactsPath, testPath, logFile, configFile, settingsFile,
                                           __ -> new ProcessBuilder("This is a command that doesn't exist, for sure!"));
        runner.test(TestProfile.SYSTEM_TEST, new byte[0]);
        while (runner.getStatus() == TestRunner.Status.RUNNING) {
            Thread.sleep(10);
        }
        Iterator<LogRecord> log = runner.getLog(-1).iterator();
        log.next();
        LogRecord record = log.next();
        assertEquals("Failed to execute maven command: This is a command that doesn't exist, for sure!", record.getMessage());
        assertTrue(record.getThrown() instanceof TestRunner.NoTestsException);
        assertEquals(TestRunner.Status.FAILURE, runner.getStatus());
    }

    @Test
    public void errorLeadsToError() throws InterruptedException {
        TestRunner runner = new TestRunner(artifactsPath, testPath, logFile, configFile, settingsFile,
                                           __ -> new ProcessBuilder("false"));
        runner.test(TestProfile.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, logFile, configFile, settingsFile,
                                           __ -> new ProcessBuilder("false"));
        runner.test(TestProfile.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, logFile, configFile, settingsFile,
                                           __ -> new ProcessBuilder("echo", "Hello!"));
        runner.test(TestProfile.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));
        assertEquals("Hello!\n", new String(Files.readAllBytes(logFile)));
    }

    @Test
    public void runnerCanBeReused() throws InterruptedException, IOException {
        TestRunner runner = new TestRunner(artifactsPath, testPath, logFile, configFile, settingsFile,
                                           __ -> new ProcessBuilder("sleep", "0.1"));
        runner.test(TestProfile.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(TestProfile.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());
    }

}