mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-22 12:50:22 -07:00
Migrate Linux CI roles to test targets. (#17997)
This commit is contained in:
parent
374e4348e4
commit
75e4645ee7
263 changed files with 53 additions and 53 deletions
|
@ -0,0 +1,10 @@
|
|||
description "ansible test daemon"
|
||||
|
||||
start on runlevel [345]
|
||||
stop on runlevel [!345]
|
||||
|
||||
expect daemon
|
||||
|
||||
exec ansible_test_service
|
||||
|
||||
manual
|
10
test/integration/targets/service/files/ansible.systemd
Normal file
10
test/integration/targets/service/files/ansible.systemd
Normal file
|
@ -0,0 +1,10 @@
|
|||
[Unit]
|
||||
Description=Ansible Test Service
|
||||
|
||||
[Service]
|
||||
ExecStart=/usr/sbin/ansible_test_service "Test\nthat newlines in scripts\nwork"
|
||||
ExecReload=/bin/true
|
||||
Type=forking
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
134
test/integration/targets/service/files/ansible.sysv
Executable file
134
test/integration/targets/service/files/ansible.sysv
Executable file
|
@ -0,0 +1,134 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
|
||||
# LSB header
|
||||
|
||||
### BEGIN INIT INFO
|
||||
# Provides: ansible-test
|
||||
# Default-Start: 3 4 5
|
||||
# Default-Stop: 0 1 2 6
|
||||
# Short-Description: test daemon for ansible
|
||||
# Description: This is a test daemon used by ansible for testing only
|
||||
### END INIT INFO
|
||||
|
||||
# chkconfig header
|
||||
|
||||
# chkconfig: 345 99 99
|
||||
# description: This is a test daemon used by ansible for testing only
|
||||
#
|
||||
# processname: /usr/sbin/ansible_test_service
|
||||
|
||||
# Sanity checks.
|
||||
[ -x /usr/sbin/ansible_test_service ] || exit 0
|
||||
|
||||
DEBIAN_VERSION=/etc/debian_version
|
||||
SUSE_RELEASE=/etc/SuSE-release
|
||||
# Source function library.
|
||||
if [ -f $DEBIAN_VERSION ]; then
|
||||
. /lib/lsb/init-functions
|
||||
elif [ -f $SUSE_RELEASE -a -r /etc/rc.status ]; then
|
||||
. /etc/rc.status
|
||||
else
|
||||
. /etc/rc.d/init.d/functions
|
||||
fi
|
||||
|
||||
SERVICE=ansible_test_service
|
||||
PROCESS=ansible_test_service
|
||||
CONFIG_ARGS=" "
|
||||
if [ -f $DEBIAN_VERSION ]; then
|
||||
LOCKFILE=/var/lock/$SERVICE
|
||||
else
|
||||
LOCKFILE=/var/lock/subsys/$SERVICE
|
||||
fi
|
||||
|
||||
RETVAL=0
|
||||
|
||||
start() {
|
||||
echo -n "Starting ansible test daemon: "
|
||||
if [ -f $SUSE_RELEASE ]; then
|
||||
startproc -p /var/run/${SERVICE}.pid -f /usr/sbin/ansible_test_service
|
||||
rc_status -v
|
||||
elif [ -e $DEBIAN_VERSION ]; then
|
||||
if [ -f $LOCKFILE ]; then
|
||||
echo -n "already started, lock file found"
|
||||
RETVAL=1
|
||||
elif /bin/python /usr/sbin/ansible_test_service; then
|
||||
echo -n "OK"
|
||||
RETVAL=0
|
||||
fi
|
||||
else
|
||||
daemon --check $SERVICE $PROCESS --daemonize $CONFIG_ARGS
|
||||
fi
|
||||
RETVAL=$?
|
||||
echo
|
||||
[ $RETVAL -eq 0 ] && touch $LOCKFILE
|
||||
return $RETVAL
|
||||
}
|
||||
|
||||
stop() {
|
||||
echo -n "Stopping ansible test daemon: "
|
||||
if [ -f $SUSE_RELEASE ]; then
|
||||
killproc -TERM /usr/sbin/ansible_test_service
|
||||
rc_status -v
|
||||
elif [ -f $DEBIAN_VERSION ]; then
|
||||
# Added this since Debian's start-stop-daemon doesn't support spawned processes
|
||||
if ps -ef | grep "/bin/python /usr/sbin/ansible_test_service" | grep -v grep | awk '{print $2}' | xargs kill &> /dev/null; then
|
||||
echo -n "OK"
|
||||
RETVAL=0
|
||||
else
|
||||
echo -n "Daemon is not started"
|
||||
RETVAL=1
|
||||
fi
|
||||
else
|
||||
killproc -p /var/run/${SERVICE}.pid
|
||||
fi
|
||||
RETVAL=$?
|
||||
echo
|
||||
if [ $RETVAL -eq 0 ]; then
|
||||
rm -f $LOCKFILE
|
||||
rm -f /var/run/$SERVICE.pid
|
||||
fi
|
||||
}
|
||||
|
||||
restart() {
|
||||
stop
|
||||
start
|
||||
}
|
||||
|
||||
# See how we were called.
|
||||
case "$1" in
|
||||
start|stop|restart)
|
||||
$1
|
||||
;;
|
||||
status)
|
||||
if [ -f $SUSE_RELEASE ]; then
|
||||
echo -n "Checking for ansible test service "
|
||||
checkproc /usr/sbin/ansible_test_service
|
||||
rc_status -v
|
||||
elif [ -f $DEBIAN_VERSION ]; then
|
||||
if [ -f $LOCKFILE ]; then
|
||||
RETVAL=0
|
||||
echo "ansible test is running."
|
||||
else
|
||||
RETVAL=1
|
||||
echo "ansible test is stopped."
|
||||
fi
|
||||
else
|
||||
status $PROCESS
|
||||
RETVAL=$?
|
||||
fi
|
||||
;;
|
||||
condrestart)
|
||||
[ -f $LOCKFILE ] && restart || :
|
||||
;;
|
||||
reload)
|
||||
echo "ok"
|
||||
RETVAL=0
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|status|restart|condrestart|reload}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
exit $RETVAL
|
||||
|
9
test/integration/targets/service/files/ansible.upstart
Normal file
9
test/integration/targets/service/files/ansible.upstart
Normal file
|
@ -0,0 +1,9 @@
|
|||
description "ansible test daemon"
|
||||
|
||||
start on runlevel [345]
|
||||
stop on runlevel [!345]
|
||||
|
||||
expect daemon
|
||||
|
||||
exec ansible_test_service
|
||||
|
67
test/integration/targets/service/files/ansible_test_service
Executable file
67
test/integration/targets/service/files/ansible_test_service
Executable file
|
@ -0,0 +1,67 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
# this is mostly based off of the code found here:
|
||||
# http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
|
||||
|
||||
import os
|
||||
import resource
|
||||
import sys
|
||||
import time
|
||||
|
||||
UMASK = 0
|
||||
WORKDIR = "/"
|
||||
MAXFD = 1024
|
||||
|
||||
if (hasattr(os, "devnull")):
|
||||
REDIRECT_TO = os.devnull
|
||||
else:
|
||||
REDIRECT_TO = "/dev/null"
|
||||
|
||||
def createDaemon():
|
||||
try:
|
||||
pid = os.fork()
|
||||
except OSError as e:
|
||||
raise Exception, "%s [%d]" % (e.strerror, e.errno)
|
||||
|
||||
if (pid == 0):
|
||||
os.setsid()
|
||||
|
||||
try:
|
||||
pid = os.fork()
|
||||
except OSError as e:
|
||||
raise Exception, "%s [%d]" % (e.strerror, e.errno)
|
||||
|
||||
if (pid == 0):
|
||||
os.chdir(WORKDIR)
|
||||
os.umask(UMASK)
|
||||
else:
|
||||
f = open('/var/run/ansible_test_service.pid', 'w')
|
||||
f.write("%d\n" % pid)
|
||||
f.close()
|
||||
os._exit(0)
|
||||
else:
|
||||
os._exit(0)
|
||||
|
||||
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
|
||||
if (maxfd == resource.RLIM_INFINITY):
|
||||
maxfd = MAXFD
|
||||
|
||||
for fd in range(0, maxfd):
|
||||
try:
|
||||
os.close(fd)
|
||||
except OSError: # ERROR, fd wasn't open to begin with (ignored)
|
||||
pass
|
||||
|
||||
os.open(REDIRECT_TO, os.O_RDWR)
|
||||
os.dup2(0, 1)
|
||||
os.dup2(0, 2)
|
||||
|
||||
return(0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
retCode = createDaemon()
|
||||
|
||||
while True:
|
||||
time.sleep(1000)
|
||||
|
20
test/integration/targets/service/meta/main.yml
Normal file
20
test/integration/targets/service/meta/main.yml
Normal file
|
@ -0,0 +1,20 @@
|
|||
# test code for the service module
|
||||
# (c) 2014, James Cammarata <jcammarata@ansible.com>
|
||||
|
||||
# This file is part of Ansible
|
||||
#
|
||||
# Ansible is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Ansible 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 General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
dependencies:
|
||||
- prepare_tests
|
155
test/integration/targets/service/tasks/main.yml
Normal file
155
test/integration/targets/service/tasks/main.yml
Normal file
|
@ -0,0 +1,155 @@
|
|||
- name: install the test daemon script
|
||||
copy: src=ansible_test_service dest=/usr/sbin/ansible_test_service mode=755
|
||||
register: install_result
|
||||
|
||||
- name: assert that the daemon script was installed
|
||||
assert:
|
||||
that:
|
||||
- "install_result.dest == '/usr/sbin/ansible_test_service'"
|
||||
- "install_result.checksum == '4e0164ceb9a5aeab76b38483ffd27fe791baa9f4'"
|
||||
- "install_result.state == 'file'"
|
||||
- "install_result.mode == '0755'"
|
||||
|
||||
# determine init system is in use
|
||||
- name: detect sysv init system
|
||||
set_fact: service_type=sysv
|
||||
when: ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux'] and (ansible_distribution_version|version_compare('6', '>=') and ansible_distribution_version|version_compare('7', '<'))
|
||||
- name: detect systemd init system
|
||||
set_fact: service_type=systemd
|
||||
when: (ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux'] and (ansible_distribution_version|version_compare('7', '>=') and ansible_distribution_version|version_compare('8', '<'))) or ansible_distribution == 'Fedora' or (ansible_distribution == 'Ubuntu' and ansible_distribution_version|version_compare('15.04', '>=')) or (ansible_distribution == 'Debian' and ansible_distribution_version|version_compare('8', '>=')) or ansible_os_family == 'Suse'
|
||||
- name: detect upstart init system
|
||||
set_fact: service_type=upstart
|
||||
when: ansible_distribution == 'Ubuntu' and ansible_distribution_version|version_compare('15.04', '<')
|
||||
|
||||
# setup test service script
|
||||
- include: 'sysv_setup.yml'
|
||||
when: service_type == "sysv"
|
||||
- include: 'systemd_setup.yml'
|
||||
when: service_type == "systemd"
|
||||
- include: 'upstart_setup.yml'
|
||||
when: service_type == "upstart"
|
||||
|
||||
- name: disable the ansible test service
|
||||
service: name=ansible_test enabled=no
|
||||
|
||||
- name: (check mode run) enable the ansible test service
|
||||
service: name=ansible_test enabled=yes
|
||||
register: enable_in_check_mode_result
|
||||
check_mode: yes
|
||||
|
||||
- name: assert that changes reported for check mode run
|
||||
assert:
|
||||
that:
|
||||
- "enable_in_check_mode_result.changed == true"
|
||||
|
||||
- name: enable the ansible test service
|
||||
service: name=ansible_test enabled=yes
|
||||
register: enable_result
|
||||
|
||||
- name: assert that the service was enabled and changes reported
|
||||
assert:
|
||||
that:
|
||||
- "enable_result.enabled == true"
|
||||
- "enable_result.changed == true"
|
||||
|
||||
- name: start the ansible test service
|
||||
service: name=ansible_test state=started
|
||||
register: start_result
|
||||
|
||||
- name: assert that the service was started
|
||||
assert:
|
||||
that:
|
||||
- "start_result.state == 'started'"
|
||||
|
||||
- name: find the service with a pattern
|
||||
service: name=ansible_test pattern="ansible_test_ser*" state=started
|
||||
register: start2_result
|
||||
# don't enable this check yet on systems with systemd because of https://github.com/ansible/ansible/issues/16694
|
||||
when: service_type != "systemd"
|
||||
|
||||
- name: assert that the service was started via the pattern
|
||||
assert:
|
||||
that:
|
||||
- "start2_result.name == 'ansible_test'"
|
||||
- "start2_result.state == 'started'"
|
||||
when: service_type != "systemd"
|
||||
|
||||
- name: restart the ansible test service
|
||||
service: name=ansible_test state=restarted
|
||||
register: restart_result
|
||||
|
||||
- name: assert that the service was restarted
|
||||
assert:
|
||||
that:
|
||||
- "restart_result.state == 'started'"
|
||||
|
||||
- name: restart the ansible test service with a sleep
|
||||
service: name=ansible_test state=restarted sleep=2
|
||||
register: restart_sleep_result
|
||||
# don't enable this check yet on systems with systemd because of https://github.com/ansible/ansible/issues/16694
|
||||
when: service_type != "systemd"
|
||||
|
||||
- name: assert that the service was restarted with a sleep
|
||||
assert:
|
||||
that:
|
||||
- "restart_sleep_result.state == 'started'"
|
||||
when: service_type != "systemd"
|
||||
|
||||
- name: reload the ansible test service
|
||||
service: name=ansible_test state=reloaded
|
||||
register: reload_result
|
||||
# don't do this on systems with systemd because it triggers error:
|
||||
# Unable to reload service ansible_test: ansible_test.service is not active, cannot reload.
|
||||
when: service_type != "systemd"
|
||||
|
||||
- name: assert that the service was reloaded
|
||||
assert:
|
||||
that:
|
||||
- "reload_result.state == 'started'"
|
||||
when: service_type != "systemd"
|
||||
|
||||
- name: stop the ansible test service
|
||||
service: name=ansible_test state=stopped
|
||||
register: stop_result
|
||||
|
||||
- name: assert that the service was stopped
|
||||
assert:
|
||||
that:
|
||||
- "stop_result.state == 'stopped'"
|
||||
|
||||
- name: disable the ansible test service
|
||||
service: name=ansible_test enabled=no
|
||||
register: disable_result
|
||||
|
||||
- name: assert that the service was disabled
|
||||
assert:
|
||||
that:
|
||||
- "disable_result.enabled == false"
|
||||
|
||||
- name: try to enable a broken service
|
||||
service: name=ansible_broken_test enabled=yes
|
||||
register: broken_enable_result
|
||||
ignore_errors: True
|
||||
|
||||
- name: assert that the broken test failed
|
||||
assert:
|
||||
that:
|
||||
- "broken_enable_result|failed"
|
||||
|
||||
- name: remove the test daemon script
|
||||
file: path=/usr/sbin/ansible_test_service state=absent
|
||||
register: remove_result
|
||||
|
||||
- name: assert that the test daemon script was removed
|
||||
assert:
|
||||
that:
|
||||
- "remove_result.path == '/usr/sbin/ansible_test_service'"
|
||||
- "remove_result.state == 'absent'"
|
||||
|
||||
# cleaning up changes made by this playbook
|
||||
- include: 'sysv_cleanup.yml'
|
||||
when: service_type == "sysv"
|
||||
- include: 'systemd_cleanup.yml'
|
||||
when: service_type == "systemd"
|
||||
- include: 'upstart_cleanup.yml'
|
||||
when: service_type == "upstart"
|
26
test/integration/targets/service/tasks/systemd_cleanup.yml
Normal file
26
test/integration/targets/service/tasks/systemd_cleanup.yml
Normal file
|
@ -0,0 +1,26 @@
|
|||
- name: remove the systemd unit file
|
||||
file: path=/usr/lib/systemd/system/ansible_test.service state=absent
|
||||
register: remove_systemd_result
|
||||
|
||||
- name: remove the systemd unit file
|
||||
file: path=/usr/lib/systemd/system/ansible_test_broken.service state=absent
|
||||
register: remove_systemd_broken_result
|
||||
|
||||
- debug: var=remove_systemd_broken_result
|
||||
- name: assert that the systemd unit file was removed
|
||||
assert:
|
||||
that:
|
||||
- "remove_systemd_result.path == '/usr/lib/systemd/system/ansible_test.service'"
|
||||
- "remove_systemd_result.state == 'absent'"
|
||||
- "remove_systemd_broken_result.path == '/usr/lib/systemd/system/ansible_test_broken.service'"
|
||||
- "remove_systemd_broken_result.state == 'absent'"
|
||||
|
||||
- name: make sure systemd is reloaded
|
||||
shell: systemctl daemon-reload
|
||||
register: restart_systemd_result
|
||||
|
||||
- name: assert that systemd was reloaded
|
||||
assert:
|
||||
that:
|
||||
- "restart_systemd_result.rc == 0"
|
||||
|
17
test/integration/targets/service/tasks/systemd_setup.yml
Normal file
17
test/integration/targets/service/tasks/systemd_setup.yml
Normal file
|
@ -0,0 +1,17 @@
|
|||
- name: install the systemd unit file
|
||||
copy: src=ansible.systemd dest=/etc/systemd/system/ansible_test.service
|
||||
register: install_systemd_result
|
||||
|
||||
- name: install a broken systemd unit file
|
||||
file: src=ansible_test.service path=/etc/systemd/system/ansible_test_broken.service state=link
|
||||
register: install_broken_systemd_result
|
||||
|
||||
- name: assert that the systemd unit file was installed
|
||||
assert:
|
||||
that:
|
||||
- "install_systemd_result.dest == '/etc/systemd/system/ansible_test.service'"
|
||||
- "install_systemd_result.state == 'file'"
|
||||
- "install_systemd_result.mode == '0644'"
|
||||
- "install_systemd_result.checksum == '6b5f2b9318524a387c77c550cef4dd411a471acf'"
|
||||
- "install_broken_systemd_result.dest == '/etc/systemd/system/ansible_test_broken.service'"
|
||||
- "install_broken_systemd_result.state == 'link'"
|
10
test/integration/targets/service/tasks/sysv_cleanup.yml
Normal file
10
test/integration/targets/service/tasks/sysv_cleanup.yml
Normal file
|
@ -0,0 +1,10 @@
|
|||
- name: remove the sysV init file
|
||||
file: path=/etc/init.d/ansible_test state=absent
|
||||
register: remove_sysv_result
|
||||
|
||||
- name: assert that the sysV init file was removed
|
||||
assert:
|
||||
that:
|
||||
- "remove_sysv_result.path == '/etc/init.d/ansible_test'"
|
||||
- "remove_sysv_result.state == 'absent'"
|
||||
|
12
test/integration/targets/service/tasks/sysv_setup.yml
Normal file
12
test/integration/targets/service/tasks/sysv_setup.yml
Normal file
|
@ -0,0 +1,12 @@
|
|||
- name: install the sysV init file
|
||||
copy: src=ansible.sysv dest=/etc/init.d/ansible_test mode=0755
|
||||
register: install_sysv_result
|
||||
|
||||
- name: assert that the sysV init file was installed
|
||||
assert:
|
||||
that:
|
||||
- "install_sysv_result.dest == '/etc/init.d/ansible_test'"
|
||||
- "install_sysv_result.state == 'file'"
|
||||
- "install_sysv_result.mode == '0755'"
|
||||
- "install_sysv_result.checksum == '174fa255735064b420600e4c8637ea0eff28d0c1'"
|
||||
|
15
test/integration/targets/service/tasks/upstart_cleanup.yml
Normal file
15
test/integration/targets/service/tasks/upstart_cleanup.yml
Normal file
|
@ -0,0 +1,15 @@
|
|||
- name: remove the upstart init file
|
||||
file: path=/etc/init/ansible_test.conf state=absent
|
||||
register: remove_upstart_result
|
||||
|
||||
- name: remove the upstart init file
|
||||
file: path=/etc/init/ansible_test_broken.conf state=absent
|
||||
register: remove_upstart_broken_result
|
||||
|
||||
- name: assert that the upstart init file was removed
|
||||
assert:
|
||||
that:
|
||||
- "remove_upstart_result.path == '/etc/init/ansible_test.conf'"
|
||||
- "remove_upstart_result.state == 'absent'"
|
||||
- "remove_upstart_broken_result.path == '/etc/init/ansible_test_broken.conf'"
|
||||
- "remove_upstart_broken_result.state == 'absent'"
|
19
test/integration/targets/service/tasks/upstart_setup.yml
Normal file
19
test/integration/targets/service/tasks/upstart_setup.yml
Normal file
|
@ -0,0 +1,19 @@
|
|||
- name: install the upstart init file
|
||||
copy: src=ansible.upstart dest=/etc/init/ansible_test.conf mode=0644
|
||||
register: install_upstart_result
|
||||
|
||||
- name: install an upstart init file that will fail (manual in .conf)
|
||||
copy: src=ansible-broken.upstart dest=/etc/init/ansible_broken_test.conf mode=0644
|
||||
register: install_upstart_broken_result
|
||||
|
||||
- name: assert that the upstart init file was installed
|
||||
assert:
|
||||
that:
|
||||
- "install_upstart_result.dest == '/etc/init/ansible_test.conf'"
|
||||
- "install_upstart_result.state == 'file'"
|
||||
- "install_upstart_result.mode == '0644'"
|
||||
- "install_upstart_result.checksum == '5c314837b6c4dd6c68d1809653a2974e9078e02a'"
|
||||
- "install_upstart_broken_result.dest == '/etc/init/ansible_broken_test.conf'"
|
||||
- "install_upstart_broken_result.state == 'file'"
|
||||
- "install_upstart_broken_result.mode == '0644'"
|
||||
- "install_upstart_broken_result.checksum == 'e66497894f2b2bf71e1380a196cc26089cc24a10'"
|
0
test/integration/targets/service/templates/main.yml
Normal file
0
test/integration/targets/service/templates/main.yml
Normal file
Loading…
Add table
Add a link
Reference in a new issue