| 1 |
# Copyright (c) 2023 Vlad Glagolev <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from unittest.mock import patch |
| 8 |
|
| 9 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 10 |
AnsibleFailJson, |
| 11 |
ModuleTestCase, |
| 12 |
set_module_args, |
| 13 |
) |
| 14 |
|
| 15 |
from ansible_collections.community.general.plugins.modules.simpleinit_msb import SimpleinitMSB, build_module |
| 16 |
|
| 17 |
_TELINIT_LIST = """ |
| 18 |
RUNLEVEL SCRIPT |
| 19 |
2 smgl-suspend-single |
| 20 |
3 crond |
| 21 |
3 fuse |
| 22 |
3 network |
| 23 |
3 nscd |
| 24 |
3 smgl-default-remote-fs |
| 25 |
3 smgl-misc |
| 26 |
3 sshd |
| 27 |
DEV coldplug |
| 28 |
DEV devices |
| 29 |
DEV udevd |
| 30 |
S hostname.sh |
| 31 |
S hwclock.sh |
| 32 |
S keymap.sh |
| 33 |
S modutils |
| 34 |
S mountall.sh |
| 35 |
S mountroot.sh |
| 36 |
S single |
| 37 |
S smgl-default-crypt-fs |
| 38 |
S smgl-metalog |
| 39 |
S smgl-sysctl |
| 40 |
S sysstat |
| 41 |
""" |
| 42 |
|
| 43 |
_TELINIT_LIST_ENABLED = """ |
| 44 |
smgl-suspend-single |
| 45 |
crond |
| 46 |
fuse |
| 47 |
network |
| 48 |
nscd |
| 49 |
smgl-default-remote-fs |
| 50 |
smgl-misc |
| 51 |
sshd |
| 52 |
coldplug |
| 53 |
devices |
| 54 |
udevd |
| 55 |
hostname.sh |
| 56 |
hwclock.sh |
| 57 |
keymap.sh |
| 58 |
modutils |
| 59 |
mountall.sh |
| 60 |
mountroot.sh |
| 61 |
single |
| 62 |
smgl-default-crypt-fs |
| 63 |
smgl-metalog |
| 64 |
smgl-sysctl |
| 65 |
""" |
| 66 |
|
| 67 |
_TELINIT_LIST_DISABLED = """ |
| 68 |
sysstat |
| 69 |
""" |
| 70 |
|
| 71 |
_TELINIT_ALREADY_ENABLED = """ |
| 72 |
Service smgl-suspend-single already enabled. |
| 73 |
""" |
| 74 |
|
| 75 |
_TELINIT_ALREADY_DISABLED = """ |
| 76 |
Service smgl-suspend-single already disabled. |
| 77 |
""" |
| 78 |
|
| 79 |
_TELINIT_STATUS_RUNNING = """ |
| 80 |
sshd is running with Process ID(s) 8510 8508 2195 |
| 81 |
""" |
| 82 |
|
| 83 |
_TELINIT_STATUS_RUNNING_NOT = """ |
| 84 |
/sbin/metalog is not running |
| 85 |
""" |
| 86 |
|
| 87 |
|
| 88 |
class TestSimpleinitMSB(ModuleTestCase): |
| 89 |
def setUp(self): |
| 90 |
super().setUp() |
| 91 |
|
| 92 |
def tearDown(self): |
| 93 |
super().tearDown() |
| 94 |
|
| 95 |
@patch("os.path.exists", return_value=True) |
| 96 |
@patch("ansible.module_utils.basic.AnsibleModule.get_bin_path", return_value="/sbin/telinit") |
| 97 |
def test_get_service_tools(self, *args, **kwargs): |
| 98 |
with set_module_args( |
| 99 |
{ |
| 100 |
"name": "smgl-suspend-single", |
| 101 |
"state": "running", |
| 102 |
} |
| 103 |
): |
| 104 |
simpleinit_msb = SimpleinitMSB(build_module()) |
| 105 |
|
| 106 |
simpleinit_msb.get_service_tools() |
| 107 |
|
| 108 |
self.assertEqual(simpleinit_msb.telinit_cmd, "/sbin/telinit") |
| 109 |
|
| 110 |
@patch("ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.execute_command") |
| 111 |
def test_service_exists(self, execute_command): |
| 112 |
with set_module_args( |
| 113 |
{ |
| 114 |
"name": "smgl-suspend-single", |
| 115 |
"state": "running", |
| 116 |
} |
| 117 |
): |
| 118 |
simpleinit_msb = SimpleinitMSB(build_module()) |
| 119 |
|
| 120 |
execute_command.return_value = (0, _TELINIT_LIST, "") |
| 121 |
|
| 122 |
simpleinit_msb.service_exists() |
| 123 |
|
| 124 |
@patch("ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.execute_command") |
| 125 |
def test_service_exists_not(self, execute_command): |
| 126 |
with set_module_args( |
| 127 |
{ |
| 128 |
"name": "ntp", |
| 129 |
"state": "running", |
| 130 |
} |
| 131 |
): |
| 132 |
simpleinit_msb = SimpleinitMSB(build_module()) |
| 133 |
|
| 134 |
execute_command.return_value = (0, _TELINIT_LIST, "") |
| 135 |
|
| 136 |
with self.assertRaises(AnsibleFailJson) as context: |
| 137 |
simpleinit_msb.service_exists() |
| 138 |
|
| 139 |
self.assertEqual("telinit could not find the requested service: ntp", context.exception.args[0]["msg"]) |
| 140 |
|
| 141 |
@patch("ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_exists") |
| 142 |
@patch("ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.execute_command") |
| 143 |
def test_check_service_enabled(self, execute_command, service_exists): |
| 144 |
with set_module_args( |
| 145 |
{ |
| 146 |
"name": "nscd", |
| 147 |
"state": "running", |
| 148 |
"enabled": "true", |
| 149 |
} |
| 150 |
): |
| 151 |
simpleinit_msb = SimpleinitMSB(build_module()) |
| 152 |
|
| 153 |
service_exists.return_value = True |
| 154 |
execute_command.return_value = (0, _TELINIT_LIST_ENABLED, "") |
| 155 |
|
| 156 |
self.assertTrue(simpleinit_msb.service_enabled()) |
| 157 |
|
| 158 |
# Race condition check |
| 159 |
with patch( |
| 160 |
"ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_enabled", |
| 161 |
return_value=False, |
| 162 |
): |
| 163 |
execute_command.return_value = (0, "", _TELINIT_ALREADY_ENABLED) |
| 164 |
|
| 165 |
simpleinit_msb.service_enable() |
| 166 |
|
| 167 |
self.assertFalse(simpleinit_msb.changed) |
| 168 |
|
| 169 |
@patch("ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_exists") |
| 170 |
@patch("ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.execute_command") |
| 171 |
def test_check_service_disabled(self, execute_command, service_exists): |
| 172 |
with set_module_args( |
| 173 |
{ |
| 174 |
"name": "sysstat", |
| 175 |
"state": "stopped", |
| 176 |
"enabled": "false", |
| 177 |
} |
| 178 |
): |
| 179 |
simpleinit_msb = SimpleinitMSB(build_module()) |
| 180 |
|
| 181 |
service_exists.return_value = True |
| 182 |
execute_command.return_value = (0, _TELINIT_LIST_DISABLED, "") |
| 183 |
|
| 184 |
self.assertFalse(simpleinit_msb.service_enabled()) |
| 185 |
|
| 186 |
# Race condition check |
| 187 |
with patch( |
| 188 |
"ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_enabled", |
| 189 |
return_value=True, |
| 190 |
): |
| 191 |
execute_command.return_value = (0, "", _TELINIT_ALREADY_DISABLED) |
| 192 |
|
| 193 |
simpleinit_msb.service_enable() |
| 194 |
|
| 195 |
self.assertFalse(simpleinit_msb.changed) |
| 196 |
|
| 197 |
@patch("ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_control") |
| 198 |
def test_check_service_running(self, service_control): |
| 199 |
with set_module_args( |
| 200 |
{ |
| 201 |
"name": "sshd", |
| 202 |
"state": "running", |
| 203 |
} |
| 204 |
): |
| 205 |
simpleinit_msb = SimpleinitMSB(build_module()) |
| 206 |
|
| 207 |
service_control.return_value = (0, _TELINIT_STATUS_RUNNING, "") |
| 208 |
|
| 209 |
self.assertFalse(simpleinit_msb.get_service_status()) |
| 210 |
|
| 211 |
@patch("ansible_collections.community.general.plugins.modules.simpleinit_msb.SimpleinitMSB.service_control") |
| 212 |
def test_check_service_running_not(self, service_control): |
| 213 |
with set_module_args( |
| 214 |
{ |
| 215 |
"name": "smgl-metalog", |
| 216 |
"state": "running", |
| 217 |
} |
| 218 |
): |
| 219 |
simpleinit_msb = SimpleinitMSB(build_module()) |
| 220 |
|
| 221 |
service_control.return_value = (0, _TELINIT_STATUS_RUNNING_NOT, "") |
| 222 |
|
| 223 |
self.assertFalse(simpleinit_msb.get_service_status()) |