mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Move the service module and state integration tests to be functional tests
This commit is contained in:
parent
b1fe70ed09
commit
ed299d9d70
5 changed files with 279 additions and 330 deletions
|
@ -30,8 +30,8 @@ salt/modules/(aix_group|groupadd|mac_group|pw_group|solaris_group|win_groupadd)\
|
|||
|
||||
salt/modules/(debian_service|freebsdservice|gentoo_service|launchctl_service|mac_service|netbsdservice|openbsdrcctl_service|openbsdservice|rh_service|runit|linux_service|smf_service|systemd_service|upstart_service|win_service)\.py:
|
||||
- pytests.unit.states.test_service
|
||||
- pytests.integration.modules.test_service
|
||||
- pytests.integration.states.test_service
|
||||
- pytests.functional.modules.test_service
|
||||
- pytests.functional.states.test_service
|
||||
|
||||
|
||||
salt/modules/ansiblegate.py:
|
||||
|
|
157
tests/pytests/functional/modules/test_service.py
Normal file
157
tests/pytests/functional/modules/test_service.py
Normal file
|
@ -0,0 +1,157 @@
|
|||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.utils.path
|
||||
import salt.utils.platform
|
||||
import salt.utils.systemd
|
||||
from salt.exceptions import CommandExecutionError
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.windows_whitelisted,
|
||||
pytest.mark.destructive_test,
|
||||
pytest.mark.slow_test,
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service_name(grains, modules):
|
||||
# For local testing purposes
|
||||
env_name = os.environ.get("SALT_FUNCTIONAL_TEST_SERVICE_NAME")
|
||||
if env_name is not None:
|
||||
return env_name
|
||||
|
||||
service_name = "cron"
|
||||
cmd_name = "crontab"
|
||||
os_family = grains.get("os_family")
|
||||
is_systemd = grains.get("systemd")
|
||||
if os_family == "RedHat":
|
||||
service_name = "crond"
|
||||
elif os_family == "Arch":
|
||||
service_name = "sshd"
|
||||
cmd_name = "systemctl"
|
||||
elif os_family == "MacOS":
|
||||
service_name = "com.apple.AirPlayXPCHelper"
|
||||
elif os_family == "Windows":
|
||||
service_name = "Spooler"
|
||||
|
||||
if os_family != "Windows" and salt.utils.path.which(cmd_name) is None:
|
||||
pytest.skip("{} is not installed".format(cmd_name))
|
||||
|
||||
if is_systemd and modules.service.offline():
|
||||
pytest.skip("systemd is OFFLINE")
|
||||
|
||||
return service_name
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_service(service_name, modules):
|
||||
pre_srv_status = modules.service.status(service_name)
|
||||
pre_srv_enabled = service_name in modules.service.get_enabled()
|
||||
|
||||
yield pre_srv_status
|
||||
|
||||
post_srv_status = modules.service.status(service_name)
|
||||
post_srv_enabled = service_name in modules.service.get_enabled()
|
||||
|
||||
if post_srv_status != pre_srv_status:
|
||||
if pre_srv_status:
|
||||
modules.service.enable(service_name)
|
||||
else:
|
||||
modules.service.disable(service_name)
|
||||
|
||||
if post_srv_enabled != pre_srv_enabled:
|
||||
if pre_srv_enabled:
|
||||
modules.service.enable(service_name)
|
||||
else:
|
||||
modules.service.disable(service_name)
|
||||
|
||||
|
||||
@pytest.mark.flaky(max_runs=4)
|
||||
def test_service_status_running(modules, service_name):
|
||||
"""
|
||||
test service.status execution module
|
||||
when service is running
|
||||
"""
|
||||
modules.service.start(service_name)
|
||||
check_service = modules.service.status(service_name)
|
||||
assert check_service
|
||||
|
||||
|
||||
def test_service_status_dead(modules, service_name):
|
||||
"""
|
||||
test service.status execution module
|
||||
when service is dead
|
||||
"""
|
||||
modules.service.stop(service_name)
|
||||
check_service = modules.service.status(service_name)
|
||||
assert not check_service
|
||||
|
||||
|
||||
def test_service_restart(modules, service_name):
|
||||
"""
|
||||
test service.restart
|
||||
"""
|
||||
assert modules.service.stop(service_name)
|
||||
|
||||
|
||||
def test_service_enable(modules, service_name):
|
||||
"""
|
||||
test service.get_enabled and service.enable module
|
||||
"""
|
||||
# disable service before test
|
||||
assert modules.service.disable(service_name)
|
||||
|
||||
assert modules.service.enable(service_name)
|
||||
assert service_name in modules.service.get_enabled()
|
||||
|
||||
|
||||
def test_service_disable(modules, service_name):
|
||||
"""
|
||||
test service.get_disabled and service.disable module
|
||||
"""
|
||||
# enable service before test
|
||||
assert modules.service.enable(service_name)
|
||||
|
||||
assert modules.service.disable(service_name)
|
||||
if salt.utils.platform.is_darwin():
|
||||
assert modules.service.disabled(service_name)
|
||||
else:
|
||||
assert service_name in modules.service.get_disabled()
|
||||
|
||||
|
||||
def test_service_disable_doesnot_exist(modules):
|
||||
"""
|
||||
test service.get_disabled and service.disable module
|
||||
when service name does not exist
|
||||
"""
|
||||
# enable service before test
|
||||
srv_name = "doesnotexist"
|
||||
try:
|
||||
enable = modules.service.enable(srv_name)
|
||||
assert not enable
|
||||
except CommandExecutionError as exc:
|
||||
assert srv_name in exc.error
|
||||
|
||||
try:
|
||||
disable = modules.service.disable(srv_name)
|
||||
assert not disable
|
||||
except CommandExecutionError as exc:
|
||||
assert srv_name in exc.error
|
||||
|
||||
if salt.utils.platform.is_darwin():
|
||||
with pytest.raises(
|
||||
CommandExecutionError, match=f"Service not found: {srv_name}"
|
||||
):
|
||||
modules.service.disabled(srv_name)
|
||||
else:
|
||||
assert srv_name not in modules.service.get_disabled()
|
||||
|
||||
|
||||
@pytest.mark.skip_unless_on_windows
|
||||
def test_service_get_service_name(modules, service_name):
|
||||
"""
|
||||
test service.get_service_name
|
||||
"""
|
||||
ret = modules.service.get_service_name()
|
||||
assert service_name in ret.data.values()
|
120
tests/pytests/functional/states/test_service.py
Normal file
120
tests/pytests/functional/states/test_service.py
Normal file
|
@ -0,0 +1,120 @@
|
|||
"""
|
||||
Tests for the service state
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.utils.path
|
||||
import salt.utils.platform
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.windows_whitelisted,
|
||||
pytest.mark.destructive_test,
|
||||
pytest.mark.slow_test,
|
||||
]
|
||||
|
||||
|
||||
INIT_DELAY = 5
|
||||
STOPPED = False
|
||||
RUNNING = True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service_name(grains, modules):
|
||||
# For local testing purposes
|
||||
env_name = os.environ.get("SALT_FUNCTIONAL_TEST_SERVICE_NAME")
|
||||
if env_name is not None:
|
||||
return env_name
|
||||
|
||||
service_name = "cron"
|
||||
cmd_name = "crontab"
|
||||
os_family = grains.get("os_family")
|
||||
is_systemd = grains.get("systemd")
|
||||
if os_family == "RedHat":
|
||||
service_name = "crond"
|
||||
elif os_family == "Arch":
|
||||
service_name = "sshd"
|
||||
cmd_name = "systemctl"
|
||||
elif os_family == "MacOS":
|
||||
service_name = "com.apple.AirPlayXPCHelper"
|
||||
elif os_family == "Windows":
|
||||
service_name = "Spooler"
|
||||
|
||||
if os_family != "Windows" and salt.utils.path.which(cmd_name) is None:
|
||||
pytest.skip("{} is not installed".format(cmd_name))
|
||||
|
||||
if is_systemd and modules.service.offline():
|
||||
pytest.skip("systemd is OFFLINE")
|
||||
|
||||
return service_name
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_service(service_name, modules):
|
||||
pre_srv_enabled = True if service_name in modules.service.get_enabled() else False
|
||||
post_srv_disable = False
|
||||
if not pre_srv_enabled:
|
||||
modules.service.enable(service_name)
|
||||
post_srv_disable = True
|
||||
yield post_srv_disable
|
||||
if post_srv_disable:
|
||||
modules.service.disable(service_name)
|
||||
|
||||
|
||||
def check_service_status(exp_return, modules, service_name):
|
||||
"""
|
||||
helper method to check status of service
|
||||
"""
|
||||
check_status = modules.service.status(service_name)
|
||||
|
||||
if check_status is not exp_return:
|
||||
pytest.fail("status of service is not returning correctly")
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_service_running(service_name, modules, states):
|
||||
"""
|
||||
test service.running state module
|
||||
"""
|
||||
if modules.service.status(service_name):
|
||||
stop_service = modules.service.stop(service_name)
|
||||
assert stop_service is True
|
||||
check_service_status(STOPPED, modules, service_name)
|
||||
|
||||
if salt.utils.platform.is_darwin():
|
||||
# make sure the service is enabled on macosx
|
||||
enable = modules.service.enable(service_name)
|
||||
|
||||
start_service = states.service.running(service_name)
|
||||
assert start_service.full_return["result"] is True
|
||||
check_service_status(RUNNING, modules, service_name)
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_service_dead(service_name, modules, states):
|
||||
"""
|
||||
test service.dead state module
|
||||
"""
|
||||
start_service = states.service.running(service_name)
|
||||
assert start_service.full_return["result"] is True
|
||||
check_service_status(RUNNING, modules, service_name)
|
||||
|
||||
ret = states.service.dead(service_name)
|
||||
assert ret.full_return["result"] is True
|
||||
check_service_status(STOPPED, modules, service_name)
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_service_dead_init_delay(service_name, modules, states):
|
||||
"""
|
||||
test service.dead state module
|
||||
"""
|
||||
start_service = states.service.running(service_name)
|
||||
assert start_service.full_return["result"] is True
|
||||
check_service_status(RUNNING, modules, service_name)
|
||||
|
||||
ret = states.service.dead(service_name, init_delay=INIT_DELAY)
|
||||
assert ret.full_return["result"] is True
|
||||
check_service_status(STOPPED, modules, service_name)
|
|
@ -1,185 +0,0 @@
|
|||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.utils.path
|
||||
import salt.utils.platform
|
||||
import salt.utils.systemd
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.windows_whitelisted,
|
||||
pytest.mark.destructive_test,
|
||||
pytest.mark.slow_test,
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service_name(grains, salt_cli, salt_minion):
|
||||
# For local testing purposes
|
||||
env_name = os.environ.get("SALT_INTEGRATION_TEST_SERVICE_NAME")
|
||||
if env_name is not None:
|
||||
return env_name
|
||||
|
||||
service_name = "cron"
|
||||
cmd_name = "crontab"
|
||||
os_family = grains.get("os_family")
|
||||
is_systemd = grains.get("systemd")
|
||||
if os_family == "RedHat":
|
||||
service_name = "crond"
|
||||
elif os_family == "Arch":
|
||||
service_name = "sshd"
|
||||
cmd_name = "systemctl"
|
||||
elif os_family == "MacOS":
|
||||
service_name = "com.apple.AirPlayXPCHelper"
|
||||
elif os_family == "Windows":
|
||||
service_name = "Spooler"
|
||||
|
||||
if os_family != "Windows" and salt.utils.path.which(cmd_name) is None:
|
||||
pytest.skip("{} is not installed".format(cmd_name))
|
||||
|
||||
if is_systemd and salt_cli.run("service.offline", minion_tgt=salt_minion.id):
|
||||
pytest.skip("systemd is OFFLINE")
|
||||
|
||||
return service_name
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_service(service_name, salt_cli, salt_minion):
|
||||
pre_srv_status = salt_cli.run("service.status", minion_tgt=salt_minion.id).data
|
||||
pre_srv_enabled = (
|
||||
service_name
|
||||
in salt_cli.run("service.get_enabled", minion_tgt=salt_minion.id).data
|
||||
)
|
||||
|
||||
yield pre_srv_status
|
||||
|
||||
post_srv_status = salt_cli.run("service.status", minion_tgt=salt_minion.id).data
|
||||
post_srv_enabled = (
|
||||
service_name
|
||||
in salt_cli.run("service.get_enabled", minion_tgt=salt_minion.id).data
|
||||
)
|
||||
|
||||
if post_srv_status != pre_srv_status:
|
||||
if pre_srv_status:
|
||||
salt_cli.run("service.enable", service_name, minion_tgt=salt_minion.id)
|
||||
else:
|
||||
salt_cli.run("service.disable", service_name, minion_tgt=salt_minion.id)
|
||||
|
||||
if post_srv_enabled != pre_srv_enabled:
|
||||
if pre_srv_enabled:
|
||||
salt_cli.run("service.enable", service_name, minion_tgt=salt_minion.id)
|
||||
else:
|
||||
salt_cli.run("service.disable", service_name, minion_tgt=salt_minion.id)
|
||||
|
||||
|
||||
@pytest.mark.flaky(max_runs=4)
|
||||
def test_service_status_running(salt_cli, salt_minion, service_name):
|
||||
"""
|
||||
test service.status execution module
|
||||
when service is running
|
||||
"""
|
||||
salt_cli.run("service.start", service_name, minion_tgt=salt_minion.id)
|
||||
check_service = salt_cli.run(
|
||||
"service.status", service_name, minion_tgt=salt_minion.id
|
||||
).data
|
||||
assert check_service
|
||||
|
||||
|
||||
def test_service_status_dead(salt_cli, salt_minion, service_name):
|
||||
"""
|
||||
test service.status execution module
|
||||
when service is dead
|
||||
"""
|
||||
salt_cli.run("service.stop", service_name, minion_tgt=salt_minion.id)
|
||||
check_service = salt_cli.run(
|
||||
"service.status", service_name, minion_tgt=salt_minion.id
|
||||
).data
|
||||
assert not check_service
|
||||
|
||||
|
||||
def test_service_restart(salt_cli, salt_minion, service_name):
|
||||
"""
|
||||
test service.restart
|
||||
"""
|
||||
assert salt_cli.run("service.stop", service_name, minion_tgt=salt_minion.id).data
|
||||
|
||||
|
||||
def test_service_enable(salt_cli, salt_minion, service_name):
|
||||
"""
|
||||
test service.get_enabled and service.enable module
|
||||
"""
|
||||
# disable service before test
|
||||
assert salt_cli.run("service.disable", service_name, minion_tgt=salt_minion.id).data
|
||||
|
||||
assert salt_cli.run("service.enable", service_name, minion_tgt=salt_minion.id).data
|
||||
assert (
|
||||
service_name
|
||||
in salt_cli.run("service.get_enabled", minion_tgt=salt_minion.id).data
|
||||
)
|
||||
|
||||
|
||||
def test_service_disable(salt_cli, salt_minion, service_name):
|
||||
"""
|
||||
test service.get_disabled and service.disable module
|
||||
"""
|
||||
# enable service before test
|
||||
assert salt_cli.run("service.enable", service_name, minion_tgt=salt_minion.id).data
|
||||
|
||||
assert salt_cli.run("service.disable", service_name, minion_tgt=salt_minion.id).data
|
||||
if salt.utils.platform.is_darwin():
|
||||
assert salt_cli.run(
|
||||
"service.disabled", service_name, minion_tgt=salt_minion.id
|
||||
).data
|
||||
else:
|
||||
assert (
|
||||
service_name
|
||||
in salt_cli.run("service.get_disabled", minion_tgt=salt_minion.id).data
|
||||
)
|
||||
|
||||
|
||||
def test_service_disable_doesnot_exist(salt_cli, salt_minion):
|
||||
"""
|
||||
test service.get_disabled and service.disable module
|
||||
when service name does not exist
|
||||
"""
|
||||
# enable service before test
|
||||
srv_name = "doesnotexist"
|
||||
enable = salt_cli.run("service.enable", srv_name, minion_tgt=salt_minion.id).data
|
||||
systemd = salt.utils.systemd.booted()
|
||||
|
||||
# check service was not enabled
|
||||
try:
|
||||
assert not enable
|
||||
except AssertionError:
|
||||
assert "error" in enable.lower()
|
||||
|
||||
else:
|
||||
try:
|
||||
disable = salt_cli.run(
|
||||
"service.disable", srv_name, minion_tgt=salt_minion.id
|
||||
).data
|
||||
assert not disable
|
||||
except AssertionError:
|
||||
assert "error" in disable.lower()
|
||||
|
||||
if salt.utils.platform.is_darwin():
|
||||
assert (
|
||||
"ERROR: Service not found: {}".format(srv_name)
|
||||
in salt_cli.run(
|
||||
"service.disabled", srv_name, minion_tgt=salt_minion.id
|
||||
).stdout
|
||||
)
|
||||
else:
|
||||
assert (
|
||||
srv_name
|
||||
not in salt_cli.run("service.get_disabled", minion_tgt=salt_minion.id).data
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skip_unless_on_windows
|
||||
def test_service_get_service_name(salt_cli, salt_minion, service_name):
|
||||
"""
|
||||
test service.get_service_name
|
||||
"""
|
||||
ret = salt_cli.run("service.get_service_name", minion_tgt=salt_minion.id).data
|
||||
assert service_name in ret.data.values()
|
|
@ -1,143 +0,0 @@
|
|||
"""
|
||||
Tests for the service state
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
import salt.utils.path
|
||||
import salt.utils.platform
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.windows_whitelisted,
|
||||
pytest.mark.destructive_test,
|
||||
pytest.mark.slow_test,
|
||||
]
|
||||
|
||||
|
||||
INIT_DELAY = 5
|
||||
STOPPED = False
|
||||
RUNNING = True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service_name(grains, salt_cli, salt_minion):
|
||||
# For local testing purposes
|
||||
env_name = os.environ.get("SALT_INTEGRATION_TEST_SERVICE_NAME")
|
||||
if env_name is not None:
|
||||
return env_name
|
||||
|
||||
service_name = "cron"
|
||||
cmd_name = "crontab"
|
||||
os_family = grains.get("os_family")
|
||||
is_systemd = grains.get("systemd")
|
||||
if os_family == "RedHat":
|
||||
service_name = "crond"
|
||||
elif os_family == "Arch":
|
||||
service_name = "sshd"
|
||||
cmd_name = "systemctl"
|
||||
elif os_family == "MacOS":
|
||||
service_name = "com.apple.AirPlayXPCHelper"
|
||||
elif os_family == "Windows":
|
||||
service_name = "Spooler"
|
||||
|
||||
if os_family != "Windows" and salt.utils.path.which(cmd_name) is None:
|
||||
pytest.skip("{} is not installed".format(cmd_name))
|
||||
|
||||
if is_systemd and salt_cli.run("service.offline", minion_tgt=salt_minion.id):
|
||||
pytest.skip("systemd is OFFLINE")
|
||||
|
||||
return service_name
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_service(service_name, salt_cli, salt_minion):
|
||||
pre_srv_enabled = (
|
||||
True
|
||||
if service_name
|
||||
in salt_cli.run("service.get_enabled", minion_tgt=salt_minion.id).stdout
|
||||
else False
|
||||
)
|
||||
post_srv_disable = False
|
||||
if not pre_srv_enabled:
|
||||
salt_cli.run("service.enable", service_name, minion_tgt=salt_minion.id)
|
||||
post_srv_disable = True
|
||||
yield post_srv_disable
|
||||
if post_srv_disable:
|
||||
salt_cli.run("service.disable", service_name, minion_tgt=salt_minion.id)
|
||||
|
||||
|
||||
def check_service_status(exp_return, salt_cli, salt_minion, service_name):
|
||||
"""
|
||||
helper method to check status of service
|
||||
"""
|
||||
check_status = salt_cli.run(
|
||||
"service.status", service_name, minion_tgt=salt_minion.id
|
||||
)
|
||||
|
||||
if check_status.data is not exp_return:
|
||||
pytest.fail("status of service is not returning correctly")
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_service_running(service_name, salt_minion, salt_cli):
|
||||
"""
|
||||
test service.running state module
|
||||
"""
|
||||
if salt_cli.run("service.status", service_name, minion_tgt=salt_minion.id):
|
||||
stop_service = salt_cli.run(
|
||||
"service.stop", service_name, minion_tgt=salt_minion.id
|
||||
)
|
||||
assert stop_service.data is True
|
||||
check_service_status(STOPPED, salt_cli, salt_minion, service_name)
|
||||
|
||||
if salt.utils.platform.is_darwin():
|
||||
# make sure the service is enabled on macosx
|
||||
enable = salt_cli.run("service.enable", service_name, minion_tgt=salt_minion.id)
|
||||
|
||||
start_service = salt_cli.run(
|
||||
"state.single", "service.running", service_name, minion_tgt=salt_minion.id
|
||||
)
|
||||
assert next(iter(start_service.data.values()))["result"] is True
|
||||
check_service_status(RUNNING, salt_cli, salt_minion, service_name)
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_service_dead(service_name, salt_cli, salt_minion):
|
||||
"""
|
||||
test service.dead state module
|
||||
"""
|
||||
start_service = salt_cli.run(
|
||||
"state.single", "service.running", service_name, minion_tgt=salt_minion.id
|
||||
)
|
||||
assert next(iter(start_service.data.values()))["result"] is True
|
||||
check_service_status(RUNNING, salt_cli, salt_minion, service_name)
|
||||
|
||||
ret = salt_cli.run(
|
||||
"state.single", "service.dead", service_name, minion_tgt=salt_minion.id
|
||||
)
|
||||
assert next(iter(ret.data.values()))["result"] is True
|
||||
check_service_status(STOPPED, salt_cli, salt_minion, service_name)
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_service_dead_init_delay(service_name, salt_cli, salt_minion):
|
||||
"""
|
||||
test service.dead state module with init_delay arg
|
||||
"""
|
||||
start_service = salt_cli.run(
|
||||
"state.single", "service.running", service_name, minion_tgt=salt_minion.id
|
||||
)
|
||||
assert next(iter(start_service.data.values()))["result"] is True
|
||||
check_service_status(RUNNING, salt_cli, salt_minion, service_name)
|
||||
|
||||
ret = salt_cli.run(
|
||||
"state.single",
|
||||
"service.dead",
|
||||
service_name,
|
||||
init_delay=INIT_DELAY,
|
||||
minion_tgt=salt_minion.id,
|
||||
)
|
||||
assert next(iter(ret.data.values()))["result"] is True
|
||||
check_service_status(STOPPED, salt_cli, salt_minion, service_name)
|
Loading…
Add table
Reference in a new issue