about summary refs log tree commit diff
path: root/nixos/tests/switch-test.nix
diff options
context:
space:
mode:
authorJanne Heß <janne@hess.ooo>2022-02-17 12:46:00 +0100
committerJanne Heß <janne@hess.ooo>2022-02-17 12:49:45 +0100
commit3617ecb67f447f50f79ccd9b1bdaa5e151749ec7 (patch)
tree7f957678213d39d8fd8712c84f379ef32cb46bda /nixos/tests/switch-test.nix
parentb516e55d9d1509063d6ceea8ffa817f9f352c500 (diff)
nixos/switch-to-configuration: Fix backslashes in unit names
systemd needs this so special characters (like the ones in wireguard
units that appear because they are part of base64) can be escaped using
the \x syntax.

Root of the issue is that `glob()` handles the backslash internally
which is obviously not what we want here.

Also add a test case and fix some perlcritic issues in the subroutine.
Diffstat (limited to 'nixos/tests/switch-test.nix')
-rw-r--r--nixos/tests/switch-test.nix36
1 files changed, 36 insertions, 0 deletions
diff --git a/nixos/tests/switch-test.nix b/nixos/tests/switch-test.nix
index 3357d83342deb..4f297b6521d17 100644
--- a/nixos/tests/switch-test.nix
+++ b/nixos/tests/switch-test.nix
@@ -145,6 +145,23 @@ import ./make-test-python.nix ({ pkgs, ...} : {
           systemd.services.test.serviceConfig."X-Test" = "test";
         };
 
+        unitWithBackslash.configuration = {
+          systemd.services."escaped\\x2ddash" = {
+            wantedBy = [ "multi-user.target" ];
+            serviceConfig = {
+              Type = "oneshot";
+              RemainAfterExit = true;
+              ExecStart = "${pkgs.coreutils}/bin/true";
+              ExecReload = "${pkgs.coreutils}/bin/true";
+            };
+          };
+        };
+
+        unitWithBackslashModified.configuration = {
+          imports = [ unitWithBackslash.configuration ];
+          systemd.services."escaped\\x2ddash".serviceConfig.X-Test = "test";
+        };
+
         restart-and-reload-by-activation-script.configuration = {
           systemd.services = rec {
             simple-service = {
@@ -433,6 +450,25 @@ import ./make-test-python.nix ({ pkgs, ...} : {
         assert_lacks(out, "as well:")
         assert_contains(out, "would start the following units: test.service\n")
 
+        # Ensure \ works in unit names
+        out = switch_to_specialisation("${machine}", "unitWithBackslash")
+        assert_contains(out, "stopping the following units: test.service\n")
+        assert_lacks(out, "NOT restarting the following changed units:")
+        assert_lacks(out, "reloading the following units:")
+        assert_lacks(out, "\nrestarting the following units:")
+        assert_lacks(out, "\nstarting the following units:")
+        assert_contains(out, "the following new units were started: escaped\\x2ddash.service\n")
+        assert_lacks(out, "as well:")
+
+        out = switch_to_specialisation("${machine}", "unitWithBackslashModified")
+        assert_contains(out, "stopping the following units: escaped\\x2ddash.service\n")
+        assert_lacks(out, "NOT restarting the following changed units:")
+        assert_lacks(out, "reloading the following units:")
+        assert_lacks(out, "\nrestarting the following units:")
+        assert_contains(out, "\nstarting the following units: escaped\\x2ddash.service\n")
+        assert_lacks(out, "the following new units were started:")
+        assert_lacks(out, "as well:")
+
     with subtest("failing units"):
         # Let the simple service fail
         switch_to_specialisation("${machine}", "simpleServiceModified")