anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone
dd65c9
From 675af6905b424f2a927e4737a53f9b844e0cd9cc Mon Sep 17 00:00:00 2001
dd65c9
From: Michal Sekletar <msekleta@redhat.com>
dd65c9
Date: Wed, 15 Feb 2017 12:40:52 +0100
dd65c9
Subject: [PATCH] tests: add new test for issue #518
dd65c9
dd65c9
(cherry picked from commit 123d672e85d0c52ff7cf81997d4910990da409c1)
dd65c9
dd65c9
Related: #1404657, #1471230
dd65c9
---
dd65c9
 Makefile.am                       |   3 +-
23b3cf
 test/test-exec-deserialization.py | 192 ++++++++++++++++++++++++++++++
dd65c9
 2 files changed, 194 insertions(+), 1 deletion(-)
dd65c9
 create mode 100755 test/test-exec-deserialization.py
dd65c9
dd65c9
diff --git a/Makefile.am b/Makefile.am
c62b8e
index f06bc29c25..c4a96e1fd1 100644
dd65c9
--- a/Makefile.am
dd65c9
+++ b/Makefile.am
dd65c9
@@ -5711,7 +5711,8 @@ EXTRA_DIST += \
dd65c9
 	src/network/networkd-network-gperf.gperf \
dd65c9
 	src/network/networkd-netdev-gperf.gperf \
dd65c9
 	units/systemd-networkd.service.in \
dd65c9
-	units/systemd-networkd-wait-online.service.in
dd65c9
+	units/systemd-networkd-wait-online.service.in \
dd65c9
+	test/test-exec-deserialization.py
dd65c9
 
dd65c9
 CLEANFILES += \
dd65c9
 	src/network/networkd-network-gperf.c \
dd65c9
diff --git a/test/test-exec-deserialization.py b/test/test-exec-deserialization.py
dd65c9
new file mode 100755
c62b8e
index 0000000000..b974b1c133
dd65c9
--- /dev/null
dd65c9
+++ b/test/test-exec-deserialization.py
dd65c9
@@ -0,0 +1,192 @@
dd65c9
+#!/usr/bin/python3
dd65c9
+
dd65c9
+#
dd65c9
+#  Copyright 2017 Michal Sekletar <msekleta@redhat.com>
dd65c9
+#
dd65c9
+#  systemd is free software; you can redistribute it and/or modify it
dd65c9
+#  under the terms of the GNU Lesser General Public License as published by
dd65c9
+#  the Free Software Foundation; either version 2.1 of the License, or
dd65c9
+#  (at your option) any later version.
dd65c9
+#
dd65c9
+#  systemd is distributed in the hope that it will be useful, but
dd65c9
+#  WITHOUT ANY WARRANTY; without even the implied warranty of
dd65c9
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
dd65c9
+#  Lesser General Public License for more details.
dd65c9
+#
dd65c9
+#  You should have received a copy of the GNU Lesser General Public License
dd65c9
+#  along with systemd; If not, see <http://www.gnu.org/licenses/>.
dd65c9
+
dd65c9
+# ATTENTION: This uses the *installed* systemd, not the one from the built
dd65c9
+# source tree.
dd65c9
+
dd65c9
+import unittest
dd65c9
+import time
dd65c9
+import os
dd65c9
+import tempfile
dd65c9
+import subprocess
dd65c9
+
dd65c9
+from enum import Enum
dd65c9
+
dd65c9
+class UnitFileChange(Enum):
dd65c9
+    NO_CHANGE = 0
dd65c9
+    LINES_SWAPPED = 1
dd65c9
+    COMMAND_ADDED_BEFORE = 2
dd65c9
+    COMMAND_ADDED_AFTER = 3
dd65c9
+    COMMAND_INTERLEAVED = 4
dd65c9
+    REMOVAL = 5
dd65c9
+
dd65c9
+class ExecutionResumeTest(unittest.TestCase):
dd65c9
+    def setUp(self):
dd65c9
+        self.unit = 'test-issue-518.service'
dd65c9
+        self.unitfile_path = '/run/systemd/system/{0}'.format(self.unit)
dd65c9
+        self.output_file = tempfile.mktemp()
dd65c9
+        self.unit_files = {}
dd65c9
+
dd65c9
+        unit_file_content = '''
dd65c9
+        [Service]
dd65c9
+        Type=oneshot
dd65c9
+        ExecStart=/bin/sleep 2
dd65c9
+        ExecStart=/bin/bash -c "echo foo >> {0}"
dd65c9
+        '''.format(self.output_file)
dd65c9
+        self.unit_files[UnitFileChange.NO_CHANGE] = unit_file_content
dd65c9
+
dd65c9
+        unit_file_content = '''
dd65c9
+        [Service]
dd65c9
+        Type=oneshot
dd65c9
+        ExecStart=/bin/bash -c "echo foo >> {0}"
dd65c9
+        ExecStart=/bin/sleep 2
dd65c9
+        '''.format(self.output_file)
dd65c9
+        self.unit_files[UnitFileChange.LINES_SWAPPED] = unit_file_content
dd65c9
+
dd65c9
+        unit_file_content = '''
dd65c9
+        [Service]
dd65c9
+        Type=oneshot
dd65c9
+        ExecStart=/bin/bash -c "echo bar >> {0}"
dd65c9
+        ExecStart=/bin/sleep 2
dd65c9
+        ExecStart=/bin/bash -c "echo foo >> {0}"
dd65c9
+        '''.format(self.output_file)
dd65c9
+        self.unit_files[UnitFileChange.COMMAND_ADDED_BEFORE] = unit_file_content
dd65c9
+
dd65c9
+        unit_file_content = '''
dd65c9
+        [Service]
dd65c9
+        Type=oneshot
dd65c9
+        ExecStart=/bin/sleep 2
dd65c9
+        ExecStart=/bin/bash -c "echo foo >> {0}"
dd65c9
+        ExecStart=/bin/bash -c "echo bar >> {0}"
dd65c9
+        '''.format(self.output_file)
dd65c9
+        self.unit_files[UnitFileChange.COMMAND_ADDED_AFTER] = unit_file_content
dd65c9
+
dd65c9
+        unit_file_content = '''
dd65c9
+        [Service]
dd65c9
+        Type=oneshot
dd65c9
+        ExecStart=/bin/bash -c "echo baz >> {0}"
dd65c9
+        ExecStart=/bin/sleep 2
dd65c9
+        ExecStart=/bin/bash -c "echo foo >> {0}"
dd65c9
+        ExecStart=/bin/bash -c "echo bar >> {0}"
dd65c9
+        '''.format(self.output_file)
dd65c9
+        self.unit_files[UnitFileChange.COMMAND_INTERLEAVED] = unit_file_content
dd65c9
+
dd65c9
+        unit_file_content = '''
dd65c9
+        [Service]
dd65c9
+        Type=oneshot
dd65c9
+        ExecStart=/bin/bash -c "echo bar >> {0}"
dd65c9
+        ExecStart=/bin/bash -c "echo baz >> {0}"
dd65c9
+        '''.format(self.output_file)
dd65c9
+        self.unit_files[UnitFileChange.REMOVAL] = unit_file_content
dd65c9
+
dd65c9
+    def reload(self):
dd65c9
+        subprocess.check_call(['systemctl', 'daemon-reload'])
dd65c9
+
dd65c9
+    def write_unit_file(self, unit_file_change):
dd65c9
+        if not isinstance(unit_file_change, UnitFileChange):
dd65c9
+            raise ValueError('Unknown unit file change')
dd65c9
+
dd65c9
+        content = self.unit_files[unit_file_change]
dd65c9
+
dd65c9
+        with open(self.unitfile_path, 'w') as f:
dd65c9
+            f.write(content)
dd65c9
+
dd65c9
+        self.reload()
dd65c9
+
dd65c9
+    def check_output(self, expected_output):
dd65c9
+        try:
dd65c9
+            with open(self.output_file, 'r') as log:
dd65c9
+                output = log.read()
dd65c9
+        except IOError:
dd65c9
+            self.fail()
dd65c9
+
dd65c9
+        self.assertEqual(output, expected_output)
dd65c9
+
dd65c9
+    def setup_unit(self):
dd65c9
+        self.write_unit_file(UnitFileChange.NO_CHANGE)
dd65c9
+        subprocess.check_call(['systemctl', '--job-mode=replace', '--no-block', 'start', self.unit])
dd65c9
+
dd65c9
+    def test_no_change(self):
dd65c9
+        expected_output = 'foo\n'
dd65c9
+
dd65c9
+        self.setup_unit()
dd65c9
+        self.reload()
dd65c9
+        time.sleep(4)
dd65c9
+
dd65c9
+        self.check_output(expected_output)
dd65c9
+
dd65c9
+    def test_swapped(self):
dd65c9
+        expected_output = ''
dd65c9
+
dd65c9
+        self.setup_unit()
dd65c9
+        self.write_unit_file(UnitFileChange.LINES_SWAPPED)
dd65c9
+        self.reload()
dd65c9
+        time.sleep(4)
dd65c9
+
dd65c9
+        self.assertTrue(not os.path.exists(self.output_file))
dd65c9
+
dd65c9
+    def test_added_before(self):
dd65c9
+        expected_output = 'foo\n'
dd65c9
+
dd65c9
+        self.setup_unit()
dd65c9
+        self.write_unit_file(UnitFileChange.COMMAND_ADDED_BEFORE)
dd65c9
+        self.reload()
dd65c9
+        time.sleep(4)
dd65c9
+
dd65c9
+        self.check_output(expected_output)
dd65c9
+
dd65c9
+    def test_added_after(self):
dd65c9
+        expected_output = 'foo\nbar\n'
dd65c9
+
dd65c9
+        self.setup_unit()
dd65c9
+        self.write_unit_file(UnitFileChange.COMMAND_ADDED_AFTER)
dd65c9
+        self.reload()
dd65c9
+        time.sleep(4)
dd65c9
+
dd65c9
+        self.check_output(expected_output)
dd65c9
+
dd65c9
+    def test_interleaved(self):
dd65c9
+        expected_output = 'foo\nbar\n'
dd65c9
+
dd65c9
+        self.setup_unit()
dd65c9
+        self.write_unit_file(UnitFileChange.COMMAND_INTERLEAVED)
dd65c9
+        self.reload()
dd65c9
+        time.sleep(4)
dd65c9
+
dd65c9
+        self.check_output(expected_output)
dd65c9
+
dd65c9
+    def test_removal(self):
dd65c9
+        self.setup_unit()
dd65c9
+        self.write_unit_file(UnitFileChange.REMOVAL)
dd65c9
+        self.reload()
dd65c9
+        time.sleep(4)
dd65c9
+
dd65c9
+        self.assertTrue(not os.path.exists(self.output_file))
dd65c9
+
dd65c9
+    def tearDown(self):
dd65c9
+        for f in [self.output_file, self.unitfile_path]:
dd65c9
+            try:
dd65c9
+                os.remove(f)
dd65c9
+            except OSError:
dd65c9
+                # ignore error if log file doesn't exist
dd65c9
+                pass
dd65c9
+
dd65c9
+        self.reload()
dd65c9
+
dd65c9
+if __name__ == '__main__':
dd65c9
+    unittest.main()