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