|
6 | 6 | from collections import defaultdict
|
7 | 7 | from unittest.mock import patch
|
8 | 8 |
|
9 |
| -from generic_config_updater.services_validator import vlan_validator |
| 9 | +from generic_config_updater.services_validator import vlan_validator, rsyslog_validator |
10 | 10 | import generic_config_updater.gu_common
|
11 | 11 |
|
12 | 12 |
|
13 | 13 | # Mimics os.system call
|
14 | 14 | #
|
15 |
| -os_system_expected_cmd = "" |
| 15 | +os_system_calls = [] |
| 16 | +os_system_call_index = 0 |
16 | 17 | msg = ""
|
17 | 18 |
|
18 |
| -def os_system_cfggen(cmd): |
19 |
| - assert cmd == os_system_expected_cmd, msg |
20 |
| - return 0 |
| 19 | +def mock_os_system_call(cmd): |
| 20 | + global os_system_calls, os_system_call_index |
| 21 | + |
| 22 | + assert os_system_call_index < len(os_system_calls) |
| 23 | + entry = os_system_calls[os_system_call_index] |
| 24 | + os_system_call_index += 1 |
| 25 | + |
| 26 | + assert cmd == entry["cmd"], msg |
| 27 | + return entry["rc"] |
21 | 28 |
|
22 | 29 |
|
23 | 30 | test_data = [
|
@@ -53,19 +60,41 @@ def os_system_cfggen(cmd):
|
53 | 60 | }
|
54 | 61 | ]
|
55 | 62 |
|
| 63 | +test_rsyslog_fail = [ |
| 64 | + # Fail the calls, to get the entire fail path calls invoked |
| 65 | + # |
| 66 | + { "cmd": "/usr/bin/rsyslog-config.sh", "rc": 1 }, # config update; fails |
| 67 | + { "cmd": "systemctl restart rsyslog", "rc": 1 }, # rsyslog restart; fails |
| 68 | + { "cmd": "systemctl reset-failed rsyslog", "rc": 1 }, # reset; failure here just logs |
| 69 | + { "cmd": "systemctl restart rsyslog", "rc": 1 }, # restart again; fails |
| 70 | + { "cmd": "sleep 10s", "rc": 0 }, # sleep; rc ignored |
| 71 | + { "cmd": "systemctl restart rsyslog", "rc": 1 }, # restart again; fails |
| 72 | + ] |
| 73 | + |
| 74 | + |
56 | 75 | class TestServiceValidator(unittest.TestCase):
|
57 | 76 |
|
58 | 77 | @patch("generic_config_updater.change_applier.os.system")
|
59 | 78 | def test_change_apply(self, mock_os_sys):
|
60 | 79 | global os_system_expected_cmd
|
| 80 | + global os_system_calls, os_system_call_index |
61 | 81 |
|
62 |
| - mock_os_sys.side_effect = os_system_cfggen |
| 82 | + mock_os_sys.side_effect = mock_os_system_call |
63 | 83 |
|
64 | 84 | i = 0
|
65 | 85 | for entry in test_data:
|
66 |
| - os_system_expected_cmd = entry["cmd"] |
| 86 | + if entry["cmd"]: |
| 87 | + os_system_calls.append({"cmd": entry["cmd"], "rc": 0 }) |
67 | 88 | msg = "case failed: {}".format(str(entry))
|
68 | 89 |
|
69 | 90 | vlan_validator(entry["old"], entry["upd"], None)
|
70 | 91 |
|
71 | 92 |
|
| 93 | + # Test failure case |
| 94 | + # |
| 95 | + os_system_calls = test_rsyslog_fail |
| 96 | + os_system_call_index = 0 |
| 97 | + |
| 98 | + rc = rsyslog_validator("", "", "") |
| 99 | + assert not rc, "rsyslog_validator expected to fail" |
| 100 | + |
0 commit comments