aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java
blob: 32da4f455c1b91072c7f1a51e65bf744d19f2051 (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
// 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.systemd;

import com.yahoo.vespa.hosted.node.admin.task.util.process.TestTerminal;

import java.util.function.Consumer;

/**
 * A {@link SystemCtl} tester that simplifies testing interaction with systemd units.
 *
 * @author mpolden
 */
public class SystemCtlTester extends SystemCtl {

    private final TestTerminal terminal;

    public SystemCtlTester(TestTerminal terminal) {
        super(terminal);
        this.terminal = terminal;
    }

    public Expectation expectServiceExists(String unit) {
        return new Expectation(wantedReturn ->
                expectCommand("systemctl list-unit-files " + unit + ".service 2>&1", 0, (wantedReturn ? 1 : 0) + " unit files listed."));
    }

    public Expectation expectIsActive(String unit) {
        return new Expectation(wantedReturn -> {
            expectCommand("systemctl --quiet is-active " + unit + ".service 2>&1", wantedReturn ? 0 : 1, "");
        });
    }

    public Expectation expectEnable(String unit) { return forChangeEnabledState(unit, true); }
    public Expectation expectDisable(String unit) { return forChangeEnabledState(unit, false); }
    public Expectation expectStart(String unit) { return forChangeRunningState(unit, true); }
    public Expectation expectStop(String unit) { return forChangeRunningState(unit, false); }

    public SystemCtlTester expectRestart(String unit) {
        expectCommand("systemctl restart " + unit + " 2>&1", 0, "");
        return this;
    }

    public SystemCtlTester expectReload(String unit) {
        expectCommand("systemctl reload " + unit + " 2>&1", 0, "");
        return this;
    }

    public SystemCtlTester expectDaemonReload() {
        expectCommand("systemctl daemon-reload 2>&1", 0, "");
        return this;
    }

    public SystemCtlTester expectGetServiceProperty(String unit, String property, String output) {
        expectCommand("systemctl show --property " + property + " --value " + unit + ".service 2>&1", 0, output);
        return this;
    }

    private void expectCommand(String command, int exitCode, String output) {
        terminal.expectCommand((useSudo() ? "sudo " : "") + command, exitCode, output);
    }

    private Expectation forChangeEnabledState(String unit, boolean enable) {
        return new Expectation(wantedReturn -> {
            expectCommand("systemctl --quiet is-enabled " + unit + " 2>&1", enable != wantedReturn ? 0 : 1, "");
            if (wantedReturn)
                expectCommand("systemctl " + (enable ? "enable" : "disable") + " " + unit + " 2>&1", 0, "");
        });
    }

    private Expectation forChangeRunningState(String unit, boolean start) {
        return new Expectation(wantedReturn -> {
            expectCommand("systemctl show " + unit + " 2>&1", 0, "ActiveState=" + (start != wantedReturn ? "active" : "inactive"));
            if (wantedReturn)
                expectCommand("systemctl " + (start ? "start" : "stop") + " " + unit + " 2>&1", 0, "");
        });
    }

    public class Expectation {
        private final Consumer<Boolean> converger;
        public Expectation(Consumer<Boolean> converger) {
            this.converger = converger;
        }

        /** Mock the return value of the converge(TaskContext) method for this operation (true iff system was modified) */
        public SystemCtlTester andReturn(boolean value) {
            converger.accept(value);
            return SystemCtlTester.this;
        }
    }

}