diff options
author | Matt Clay <matt@mystile.com> | 2019-03-27 19:36:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-27 19:36:07 +0100 |
commit | 7dea316c81f1662aec5da3377834925076f2f9ad (patch) | |
tree | 35452d14342fce14cee94a0b264582c05290b9f0 /test | |
parent | Fix virtualenv activation in tests. (diff) | |
download | ansible-7dea316c81f1662aec5da3377834925076f2f9ad.tar.xz ansible-7dea316c81f1662aec5da3377834925076f2f9ad.zip |
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.
Diffstat (limited to 'test')
6 files changed, 91 insertions, 88 deletions
diff --git a/test/integration/targets/service/files/ansible.sysv b/test/integration/targets/service/files/ansible.sysv index 372ea6e5b9..1df0423d22 100755 --- a/test/integration/targets/service/files/ansible.sysv +++ b/test/integration/targets/service/files/ansible.sysv @@ -13,7 +13,7 @@ # chkconfig header -# chkconfig: 345 99 99 +# chkconfig: 345 99 99 # description: This is a test daemon used by ansible for testing only # # processname: /usr/sbin/ansible_test_service @@ -50,9 +50,9 @@ start() { rc_status -v elif [ -e $DEBIAN_VERSION ]; then if [ -f $LOCKFILE ]; then - echo -n "already started, lock file found" + echo -n "already started, lock file found" RETVAL=1 - elif /bin/python /usr/sbin/ansible_test_service; then + elif /usr/sbin/ansible_test_service; then echo -n "OK" RETVAL=0 fi @@ -72,7 +72,7 @@ stop() { 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 + if ps -ef | grep "/usr/sbin/ansible_test_service" | grep -v grep | awk '{print $2}' | xargs kill &> /dev/null; then echo -n "OK" RETVAL=0 else diff --git a/test/integration/targets/service/files/ansible_test_service b/test/integration/targets/service/files/ansible_test_service deleted file mode 100755 index 598bc1a4c9..0000000000 --- a/test/integration/targets/service/files/ansible_test_service +++ /dev/null @@ -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) - diff --git a/test/integration/targets/service/files/ansible_test_service.py b/test/integration/targets/service/files/ansible_test_service.py new file mode 100644 index 0000000000..c4feed8502 --- /dev/null +++ b/test/integration/targets/service/files/ansible_test_service.py @@ -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) diff --git a/test/integration/targets/service/tasks/main.yml b/test/integration/targets/service/tasks/main.yml index 26b4c85dca..450487b240 100644 --- a/test/integration/targets/service/tasks/main.yml +++ b/test/integration/targets/service/tasks/main.yml @@ -1,15 +1,17 @@ - name: install the test daemon script - copy: src=ansible_test_service dest=/usr/sbin/ansible_test_service mode=755 - register: install_result + copy: + src: ansible_test_service.py + dest: /usr/sbin/ansible_test_service + mode: '755' -- 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'" +- 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: # determine init system is in use - name: detect sysv init system set_fact: diff --git a/test/integration/targets/service/tasks/sysv_setup.yml b/test/integration/targets/service/tasks/sysv_setup.yml index c8bbbbc219..7b648c2427 100644 --- a/test/integration/targets/service/tasks/sysv_setup.yml +++ b/test/integration/targets/service/tasks/sysv_setup.yml @@ -8,4 +8,4 @@ - "install_sysv_result.dest == '/etc/init.d/ansible_test'" - "install_sysv_result.state == 'file'" - "install_sysv_result.mode == '0755'" - - "install_sysv_result.checksum == '174fa255735064b420600e4c8637ea0eff28d0c1'" + - "install_sysv_result.checksum == '362899814c47d9aad6e93b2f64e39edd24e38797'" diff --git a/test/integration/targets/service/tasks/tests.yml b/test/integration/targets/service/tasks/tests.yml index d7e4d7f655..05f69e7de0 100644 --- a/test/integration/targets/service/tasks/tests.yml +++ b/test/integration/targets/service/tasks/tests.yml @@ -34,7 +34,7 @@ - name: check that the service was started shell: 'cat /proc/$(cat /var/run/ansible_test_service.pid)/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 service: name=ansible_test pattern="ansible_test_ser" state=started @@ -72,7 +72,7 @@ - name: check that service is started command: 'cat /proc/{{ pid_after_restart.stdout }}/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 service: name=ansible_test state=restarted sleep=2 @@ -116,7 +116,7 @@ - name: check that service is started command: 'cat /proc/{{ ansible_test_pid.stdout }}/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 service: name=ansible_test state=stopped @@ -125,7 +125,7 @@ - name: check that the service is stopped command: 'cat /proc/{{ ansible_test_pid.stdout }}/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 assert: |