aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/process/TestTerminal.java
blob: bf231b7c35bfad0346310955b64e0af3582c1f56 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.node.admin.task.util.process;

import com.yahoo.vespa.hosted.node.admin.component.TaskContext;

import java.util.function.Function;

/**
 * @author hakonhall
 */
public class TestTerminal implements Terminal {
    private final TerminalImpl realTerminal;
    private final TestProcessFactory testProcessFactory = new TestProcessFactory();

    public TestTerminal() {
        this.realTerminal = new TerminalImpl(testProcessFactory);
    }

    /** Get the TestProcessFactory the terminal was started with. */
    public TestProcessFactory getTestProcessFactory() { return testProcessFactory; }

    /** Forward call to spawn() to callback. */
    public TestTerminal interceptCommand(String commandDescription,
                                         Function<CommandLine, ChildProcess2> callback) {
        testProcessFactory.interceptSpawn(commandDescription, callback);
        return this;
    }

    /** Wraps expectSpawn in TestProcessFactory, provided here as convenience. */
    public TestTerminal expectCommand(String commandLine, TestChildProcess2 toReturn) {
        testProcessFactory.expectSpawn(commandLine, toReturn);
        return this;
    }

    /** Wraps expectSpawn in TestProcessFactory, provided here as convenience. */
    public TestTerminal expectCommand(String commandLine, int exitCode, String output) {
        testProcessFactory.expectSpawn(commandLine, new TestChildProcess2(exitCode, output));
        return this;
    }

    /** Verifies command line matches commandLine, and returns successfully with output "". */
    public TestTerminal expectCommand(String commandLine) {
        expectCommand(commandLine, 0, "");
        return this;
    }

    /** Wraps expectSpawn in TestProcessFactory, provided here as convenience. */
    public TestTerminal ignoreCommand(String output) {
        testProcessFactory.ignoreSpawn(output);
        return this;
    }

    /** Wraps expectSpawn in TestProcessFactory, provided here as convenience. */
    public TestTerminal ignoreCommand() {
        testProcessFactory.ignoreSpawn();
        return this;
    }

    public void verifyAllCommandsExecuted() {
        testProcessFactory.verifyAllCommandsExecuted();
    }

    @Override
    public CommandLine newCommandLine(TaskContext taskContext) {
        return realTerminal.newCommandLine(taskContext);
    }
}