summaryrefslogtreecommitdiffstats
path: root/node-admin
diff options
context:
space:
mode:
authorValerij Fredriksen <valerij92@gmail.com>2020-04-04 17:20:14 +0200
committerValerij Fredriksen <valerijf@verizonmedia.com>2020-04-04 23:25:52 +0200
commit2eb79dd21aa2f4c43a306b3261d7783bc386a8d9 (patch)
tree63f1bf606053b023cba3c20d095dad15bd757164 /node-admin
parent84b1401cb1a92885205a39a13b76f98df7762f08 (diff)
Refactor SystemCtlTester more in-line with YumTester
Diffstat (limited to 'node-admin')
-rw-r--r--node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java122
-rw-r--r--node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTesterTest.java52
2 files changed, 97 insertions, 77 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java
index c7264c2fe4d..82906253f24 100644
--- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java
+++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTester.java
@@ -3,8 +3,7 @@ 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;
+import java.util.function.Consumer;
/**
* A {@link SystemCtl} tester that simplifies testing interaction with systemd units.
@@ -13,8 +12,6 @@ import java.util.Set;
*/
public class SystemCtlTester extends SystemCtl {
- private final Set<String> runningUnits = new HashSet<>();
-
private final TestTerminal terminal;
public SystemCtlTester(TestTerminal terminal) {
@@ -22,93 +19,64 @@ public class SystemCtlTester extends SystemCtl {
this.terminal = terminal;
}
- /** Create expectation for given unit */
- public Expectation expect(String unit) {
- return new Expectation(unit, this);
+ 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."));
}
- private void startUnit(String unit) {
- runningUnits.add(unit);
+ public Expectation expectIsActive(String unit) {
+ return new Expectation(wantedReturn -> {
+ expectCommand("systemctl --quiet is-active " + unit + ".service 2>&1", wantedReturn ? 0 : 1, "");
+ });
}
- private void expectCommand(String command, int exitCode, String output) {
- terminal.expectCommand((useSudo() ? "sudo " : "") + command, exitCode, output);
- }
+ 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 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);
- }
+ public SystemCtlTester expectRestart(String unit) {
+ expectCommand("systemctl restart " + unit + " 2>&1", 0, "");
+ return this;
+ }
- /** Expect that given unit will be restarted */
- public Expectation toRestart() {
- systemCtl.expectCommand("systemctl restart " + unit + " 2>&1", 0, "");
- systemCtl.startUnit(unit);
- return this;
- }
+ public SystemCtlTester expectDaemonReload() {
+ expectCommand("systemctl daemon-reload 2>&1", 0, "");
+ 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;
- }
+ private void expectCommand(String command, int exitCode, String output) {
+ terminal.expectCommand((useSudo() ? "sudo " : "") + command, exitCode, output);
+ }
- /** Expect that this will be enabled */
- public Expectation toEnable() {
- return toEnable(true);
- }
+ 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, "");
+ });
+ }
- /** Expect that given unit is already enabled */
- public Expectation isEnabled() {
- return toEnable(false);
- }
+ 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, "");
+ });
+ }
- 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;
+ public class Expectation {
+ private final Consumer<Boolean> converger;
+ public Expectation(Consumer<Boolean> converger) {
+ this.converger = converger;
}
- 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;
+ /** 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;
}
-
}
}
diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTesterTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTesterTest.java
new file mode 100644
index 00000000000..1c3a6f6a412
--- /dev/null
+++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/systemd/SystemCtlTesterTest.java
@@ -0,0 +1,52 @@
+// Copyright 2020 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.component.TestTaskContext;
+import com.yahoo.vespa.hosted.node.admin.task.util.process.TestTerminal;
+import org.junit.Test;
+
+import java.util.List;
+import java.util.function.Function;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @author freva
+ */
+public class SystemCtlTesterTest {
+
+ private static final String unit = "my-unit";
+ private final TestTerminal terminal = new TestTerminal();
+ private final SystemCtlTester systemCtl = new SystemCtlTester(terminal);
+ private final TestTaskContext context = new TestTaskContext();
+
+ @Test
+ public void return_expectations() {
+ assertSystemCtlMethod(sct -> sct.expectEnable(unit), sc -> sc.enable(unit).converge(context));
+ assertSystemCtlMethod(sct -> sct.expectDisable(unit), sc -> sc.disable(unit).converge(context));
+ assertSystemCtlMethod(sct -> sct.expectStart(unit), sc -> sc.start(unit).converge(context));
+ assertSystemCtlMethod(sct -> sct.expectStop(unit), sc -> sc.stop(unit).converge(context));
+ assertSystemCtlMethod(sct -> sct.expectServiceExists(unit), sc -> sc.serviceExists(context, unit));
+ assertSystemCtlMethod(sct -> sct.expectIsActive(unit), sc -> sc.isActive(context, unit));
+ }
+
+ @Test
+ public void void_tests() {
+ systemCtl.expectRestart(unit);
+ systemCtl.restart(unit).converge(context);
+ terminal.verifyAllCommandsExecuted();
+
+ systemCtl.expectDaemonReload();
+ systemCtl.daemonReload(context);
+ terminal.verifyAllCommandsExecuted();
+ }
+
+ private void assertSystemCtlMethod(Function<SystemCtlTester, SystemCtlTester.Expectation> systemCtlTesterExpectationFunction,
+ Function<SystemCtl, Boolean> systemCtlFunction) {
+ List.of(true, false).forEach(wantedReturnValue -> {
+ systemCtlTesterExpectationFunction.apply(systemCtl).andReturn(wantedReturnValue);
+ assertEquals(wantedReturnValue, systemCtlFunction.apply(systemCtl));
+ terminal.verifyAllCommandsExecuted();
+ });
+ }
+} \ No newline at end of file