]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: add ActivationPolicy= unit tests
authorDan Streetman <ddstreet@canonical.com>
Thu, 18 Jun 2020 22:31:18 +0000 (18:31 -0400)
committerDan Streetman <ddstreet@canonical.com>
Thu, 21 Jan 2021 17:05:17 +0000 (12:05 -0500)
test/test-network/conf/25-activation-policy.network [new file with mode: 0644]
test/test-network/conf/25-activation-policy.network.d/always-down.conf [new file with mode: 0644]
test/test-network/conf/25-activation-policy.network.d/always-up.conf [new file with mode: 0644]
test/test-network/conf/25-activation-policy.network.d/down.conf [new file with mode: 0644]
test/test-network/conf/25-activation-policy.network.d/manual.conf [new file with mode: 0644]
test/test-network/conf/25-activation-policy.network.d/up.conf [new file with mode: 0644]
test/test-network/systemd-networkd-tests.py

diff --git a/test/test-network/conf/25-activation-policy.network b/test/test-network/conf/25-activation-policy.network
new file mode 100644 (file)
index 0000000..c53e091
--- /dev/null
@@ -0,0 +1,6 @@
+[Match]
+Name=test1
+
+[Network]
+Address=192.168.10.30/24
+Gateway=192.168.10.1
diff --git a/test/test-network/conf/25-activation-policy.network.d/always-down.conf b/test/test-network/conf/25-activation-policy.network.d/always-down.conf
new file mode 100644 (file)
index 0000000..edfd12e
--- /dev/null
@@ -0,0 +1,2 @@
+[Link]
+ActivationPolicy=always-down
diff --git a/test/test-network/conf/25-activation-policy.network.d/always-up.conf b/test/test-network/conf/25-activation-policy.network.d/always-up.conf
new file mode 100644 (file)
index 0000000..b8e0fff
--- /dev/null
@@ -0,0 +1,2 @@
+[Link]
+ActivationPolicy=always-up
diff --git a/test/test-network/conf/25-activation-policy.network.d/down.conf b/test/test-network/conf/25-activation-policy.network.d/down.conf
new file mode 100644 (file)
index 0000000..65af49f
--- /dev/null
@@ -0,0 +1,2 @@
+[Link]
+ActivationPolicy=down
diff --git a/test/test-network/conf/25-activation-policy.network.d/manual.conf b/test/test-network/conf/25-activation-policy.network.d/manual.conf
new file mode 100644 (file)
index 0000000..8b81ccc
--- /dev/null
@@ -0,0 +1,2 @@
+[Link]
+ActivationPolicy=manual
diff --git a/test/test-network/conf/25-activation-policy.network.d/up.conf b/test/test-network/conf/25-activation-policy.network.d/up.conf
new file mode 100644 (file)
index 0000000..537380b
--- /dev/null
@@ -0,0 +1,2 @@
+[Link]
+ActivationPolicy=up
index b50da26f1053447e3b268c1bb3e91a89a8ae35a6..da81d48ecbc848fcba706215cc1664a8261b2584 100755 (executable)
@@ -1755,6 +1755,7 @@ class NetworkdNetworkTests(unittest.TestCase, Utilities):
         '25-address-peer-ipv4.network',
         '25-address-preferred-lifetime-zero.network',
         '25-address-static.network',
+        '25-activation-policy.network',
         '25-bind-carrier.network',
         '25-bond-active-backup-slave.netdev',
         '25-fibrule-invert.network',
@@ -2711,6 +2712,53 @@ class NetworkdNetworkTests(unittest.TestCase, Utilities):
         self.assertRegex(output, 'inet 192.168.10.30/24 brd 192.168.10.255 scope global test1')
         self.wait_operstate('test1', 'routable')
 
+    def _test_activation_policy(self, test):
+        self.setUp()
+        conffile = '25-activation-policy.network'
+        if test:
+            conffile = f'{conffile}.d/{test}.conf'
+        copy_unit_to_networkd_unit_path('11-dummy.netdev', conffile, dropins=False)
+        start_networkd()
+
+        always = test.startswith('always')
+        if test == 'manual':
+            initial_up = 'UP' in check_output('ip link show test1')
+        else:
+            initial_up = not test.endswith('down') # note: default is up
+        expect_up = initial_up
+        next_up = not expect_up
+
+        # if initial expected state is down, must wait for setup_state to reach configuring
+        # so systemd-networkd considers it 'activated'
+        setup_state = None if initial_up else 'configuring'
+
+        for iteration in range(4):
+            with self.subTest(iteration=iteration, expect_up=expect_up):
+                operstate = 'routable' if expect_up else 'off'
+                self.wait_operstate('test1', operstate, setup_state=setup_state, setup_timeout=20)
+                setup_state = None
+
+                if expect_up:
+                    self.assertIn('UP', check_output('ip link show test1'))
+                    self.assertIn('192.168.10.30/24', check_output('ip address show test1'))
+                    self.assertIn('default via 192.168.10.1', check_output('ip route show'))
+                else:
+                    self.assertIn('DOWN', check_output('ip link show test1'))
+
+            if next_up:
+                check_output('ip link set dev test1 up')
+            else:
+                check_output('ip link set dev test1 down')
+            expect_up = initial_up if always else next_up
+            next_up = not next_up
+
+        self.tearDown()
+
+    def test_activation_policy(self):
+        for test in ['up', 'always-up', 'manual', 'always-down', 'down', '']:
+            with self.subTest(test=test):
+                self._test_activation_policy(test)
+
     def test_domain(self):
         copy_unit_to_networkd_unit_path('12-dummy.netdev', '24-search-domain.network')
         start_networkd()