anitazha / rpms / systemd

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