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