mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Fix service integration test python selection. (#54449)
* Fix service integration test python selection. * Clean up source in ansible_test_service. * Rename script to include in python tests. * Make shebang templating sanity friendly. * Fix checksum. * Use realpath of python to avoid selinux issues.
This commit is contained in:
parent
ec83f52fa8
commit
7dea316c81
6 changed files with 91 additions and 88 deletions
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
# chkconfig header
|
# chkconfig header
|
||||||
|
|
||||||
# chkconfig: 345 99 99
|
# chkconfig: 345 99 99
|
||||||
# description: This is a test daemon used by ansible for testing only
|
# description: This is a test daemon used by ansible for testing only
|
||||||
#
|
#
|
||||||
# processname: /usr/sbin/ansible_test_service
|
# processname: /usr/sbin/ansible_test_service
|
||||||
|
@ -50,9 +50,9 @@ start() {
|
||||||
rc_status -v
|
rc_status -v
|
||||||
elif [ -e $DEBIAN_VERSION ]; then
|
elif [ -e $DEBIAN_VERSION ]; then
|
||||||
if [ -f $LOCKFILE ]; then
|
if [ -f $LOCKFILE ]; then
|
||||||
echo -n "already started, lock file found"
|
echo -n "already started, lock file found"
|
||||||
RETVAL=1
|
RETVAL=1
|
||||||
elif /bin/python /usr/sbin/ansible_test_service; then
|
elif /usr/sbin/ansible_test_service; then
|
||||||
echo -n "OK"
|
echo -n "OK"
|
||||||
RETVAL=0
|
RETVAL=0
|
||||||
fi
|
fi
|
||||||
|
@ -72,7 +72,7 @@ stop() {
|
||||||
rc_status -v
|
rc_status -v
|
||||||
elif [ -f $DEBIAN_VERSION ]; then
|
elif [ -f $DEBIAN_VERSION ]; then
|
||||||
# Added this since Debian's start-stop-daemon doesn't support spawned processes
|
# 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
|
if ps -ef | grep "/usr/sbin/ansible_test_service" | grep -v grep | awk '{print $2}' | xargs kill &> /dev/null; then
|
||||||
echo -n "OK"
|
echo -n "OK"
|
||||||
RETVAL=0
|
RETVAL=0
|
||||||
else
|
else
|
||||||
|
|
|
@ -1,70 +0,0 @@
|
||||||
#!/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 signal
|
|
||||||
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__":
|
|
||||||
|
|
||||||
signal.signal(signal.SIGHUP, signal.SIG_IGN)
|
|
||||||
|
|
||||||
retCode = createDaemon()
|
|
||||||
|
|
||||||
while True:
|
|
||||||
time.sleep(1000)
|
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
#!/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 signal
|
||||||
|
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__":
|
||||||
|
|
||||||
|
signal.signal(signal.SIGHUP, signal.SIG_IGN)
|
||||||
|
|
||||||
|
retCode = createDaemon()
|
||||||
|
|
||||||
|
while True:
|
||||||
|
time.sleep(1000)
|
|
@ -1,15 +1,17 @@
|
||||||
- name: install the test daemon script
|
- name: install the test daemon script
|
||||||
copy: src=ansible_test_service dest=/usr/sbin/ansible_test_service mode=755
|
copy:
|
||||||
register: install_result
|
src: ansible_test_service.py
|
||||||
|
dest: /usr/sbin/ansible_test_service
|
||||||
|
mode: '755'
|
||||||
|
|
||||||
|
- name: rewrite shebang in the test daemon script
|
||||||
|
lineinfile:
|
||||||
|
path: /usr/sbin/ansible_test_service
|
||||||
|
line: "#!{{ ansible_python_interpreter | realpath }}"
|
||||||
|
insertbefore: BOF
|
||||||
|
firstmatch: yes
|
||||||
|
|
||||||
- block:
|
- block:
|
||||||
- name: assert that the daemon script was installed
|
|
||||||
assert:
|
|
||||||
that:
|
|
||||||
- "install_result.dest == '/usr/sbin/ansible_test_service'"
|
|
||||||
- "install_result.state == 'file'"
|
|
||||||
- "install_result.mode == '0755'"
|
|
||||||
|
|
||||||
# determine init system is in use
|
# determine init system is in use
|
||||||
- name: detect sysv init system
|
- name: detect sysv init system
|
||||||
set_fact:
|
set_fact:
|
||||||
|
|
|
@ -8,4 +8,4 @@
|
||||||
- "install_sysv_result.dest == '/etc/init.d/ansible_test'"
|
- "install_sysv_result.dest == '/etc/init.d/ansible_test'"
|
||||||
- "install_sysv_result.state == 'file'"
|
- "install_sysv_result.state == 'file'"
|
||||||
- "install_sysv_result.mode == '0755'"
|
- "install_sysv_result.mode == '0755'"
|
||||||
- "install_sysv_result.checksum == '174fa255735064b420600e4c8637ea0eff28d0c1'"
|
- "install_sysv_result.checksum == '362899814c47d9aad6e93b2f64e39edd24e38797'"
|
||||||
|
|
|
@ -34,7 +34,7 @@
|
||||||
- name: check that the service was started
|
- name: check that the service was started
|
||||||
shell: 'cat /proc/$(cat /var/run/ansible_test_service.pid)/cmdline'
|
shell: 'cat /proc/$(cat /var/run/ansible_test_service.pid)/cmdline'
|
||||||
register: cmdline
|
register: cmdline
|
||||||
failed_when: cmdline is failed or not cmdline.stdout.startswith('python\0/usr/sbin/ansible_test_service\0')
|
failed_when: cmdline is failed or '\0/usr/sbin/ansible_test_service\0' not in cmdline.stdout
|
||||||
|
|
||||||
- name: find the service with a pattern
|
- name: find the service with a pattern
|
||||||
service: name=ansible_test pattern="ansible_test_ser" state=started
|
service: name=ansible_test pattern="ansible_test_ser" state=started
|
||||||
|
@ -72,7 +72,7 @@
|
||||||
- name: check that service is started
|
- name: check that service is started
|
||||||
command: 'cat /proc/{{ pid_after_restart.stdout }}/cmdline'
|
command: 'cat /proc/{{ pid_after_restart.stdout }}/cmdline'
|
||||||
register: cmdline
|
register: cmdline
|
||||||
failed_when: cmdline is failed or not cmdline.stdout.startswith('python\0/usr/sbin/ansible_test_service\0')
|
failed_when: cmdline is failed or '\0/usr/sbin/ansible_test_service\0' not in cmdline.stdout
|
||||||
|
|
||||||
- name: restart the ansible test service with a sleep
|
- name: restart the ansible test service with a sleep
|
||||||
service: name=ansible_test state=restarted sleep=2
|
service: name=ansible_test state=restarted sleep=2
|
||||||
|
@ -116,7 +116,7 @@
|
||||||
- name: check that service is started
|
- name: check that service is started
|
||||||
command: 'cat /proc/{{ ansible_test_pid.stdout }}/cmdline'
|
command: 'cat /proc/{{ ansible_test_pid.stdout }}/cmdline'
|
||||||
register: cmdline
|
register: cmdline
|
||||||
failed_when: cmdline is failed or not cmdline.stdout.startswith('python\0/usr/sbin/ansible_test_service\0')
|
failed_when: cmdline is failed or '\0/usr/sbin/ansible_test_service\0' not in cmdline.stdout
|
||||||
|
|
||||||
- name: stop the ansible test service
|
- name: stop the ansible test service
|
||||||
service: name=ansible_test state=stopped
|
service: name=ansible_test state=stopped
|
||||||
|
@ -125,7 +125,7 @@
|
||||||
- name: check that the service is stopped
|
- name: check that the service is stopped
|
||||||
command: 'cat /proc/{{ ansible_test_pid.stdout }}/cmdline'
|
command: 'cat /proc/{{ ansible_test_pid.stdout }}/cmdline'
|
||||||
register: cmdline
|
register: cmdline
|
||||||
failed_when: cmdline is not failed or cmdline.stdout.startswith('python\0/usr/sbin/ansible_test_service\0')
|
failed_when: cmdline is not failed or '\0/usr/sbin/ansible_test_service\0' in cmdline.stdout
|
||||||
|
|
||||||
- name: assert that the service was stopped
|
- name: assert that the service was stopped
|
||||||
assert:
|
assert:
|
||||||
|
|
Loading…
Reference in a new issue