aboutsummaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java
blob: 56a2b2aeca261673c9e9f506792582be99cafae2 (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
// Copyright 2019 Oath Inc. 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.HashSet;
import java.util.Set;

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

    private final Set<String> runningUnits = new HashSet<>();

    private TestTerminal terminal;

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

    /** Create expectation for given unit */
    public Expectation expect(String unit) {
        return new Expectation(unit, this);
    }

    private void startUnit(String unit) {
        runningUnits.add(unit);
    }

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

    public static class Expectation {

        private final String unit;
        private final SystemCtlTester systemCtl;

        public Expectation(String unit, SystemCtlTester systemCtl) {
            this.unit = unit;
            this.systemCtl = systemCtl;
        }

        /** Create expectation for given unit */
        public Expectation expect(String name) {
            return systemCtl.expect(name);
        }

        /** Expect that this will be started */
        public Expectation toStart() {
            return toStart(true);
        }

        /** Expect that this is already started */
        public Expectation isStarted() {
            return toStart(false);
        }

        /** Expect that given unit will be restarted */
        public Expectation toRestart() {
            systemCtl.expectCommand("systemctl restart " + unit + " 2>&1", 0, "");
            systemCtl.startUnit(unit);
            return this;
        }

        /** Expect that this will be stopped */
        public Expectation toStop() {
            systemCtl.expectCommand("systemctl stop " + unit + " 2>&1", 0, "");
            systemCtl.runningUnits.remove(unit);
            return this;
        }

        /** Expect query for state of this */
        public Expectation toQueryState() {
            systemCtl.expectCommand("systemctl --quiet is-active " + unit + ".service 2>&1",
                                    systemCtl.runningUnits.contains(unit) ? 0 : 1, "");
            return this;
        }

        /** Expect that this will be enabled */
        public Expectation toEnable() {
            return toEnable(true);
        }

        /** Expect that given unit is already enabled */
        public Expectation isEnabled() {
            return toEnable(false);
        }

        private Expectation toStart(boolean start) {
            systemCtl.expectCommand("systemctl show " + unit + " 2>&1", 0,
                                    "ActiveState=" + (start ? "inactive" : "active"));
            if (start) {
                systemCtl.expectCommand("systemctl start " + unit + " 2>&1", 0, "");
                systemCtl.startUnit(unit);
            }
            return this;
        }

        private Expectation toEnable(boolean enable) {
            systemCtl.expectCommand("systemctl --quiet is-enabled " + unit + " 2>&1", enable ? 1 : 0, "");
            if (enable) {
                systemCtl.expectCommand("systemctl enable " + unit + " 2>&1", 0, "");
            }
            return this;
        }

    }

}