From 3c8abc0b824977d1941a307273e865aed49a2993 Mon Sep 17 00:00:00 2001 From: Pilou Date: Thu, 26 Sep 2019 19:50:54 +0000 Subject: [PATCH] cron: add integration tests (#59830) * cron: add integration tests Use faketime in order to reduce waiting time. Using opensuse15 container: Tuesday 30 July 2019 23:03:19 +0000 (0:00:05.410) 0:01:28.291 ********** =============================================================================== cron : install cron and faketime packages ------------------------------ 78.65s cron : wait 70 seconds max ---------------------------------------------- 5.41s Gathering Facts --------------------------------------------------------- 0.97s cron : enable cron service ---------------------------------------------- 0.72s cron : Use faketime with cron service ----------------------------------- 0.71s cron : command ---------------------------------------------------------- 0.41s cron : create directory for cron drop-in file --------------------------- 0.41s cron : add cron task ---------------------------------------------------- 0.40s cron : command ---------------------------------------------------------- 0.25s cron : command ---------------------------------------------------------- 0.25s cron : include_vars ----------------------------------------------------- 0.06s * cron test, simplify: use Environment instead ExecStart Thanks to mscherer for the suggestion. * clean up comment: remove reference to outdated path $OUTPUT_DIR is a plain path which doesn't reference any other environment variables * cron test: display elapsed time * cron test: display some logs in case of failure * cron test: handle FreeBSD * cron tests: add checks --- test/integration/targets/cron/aliases | 3 + .../targets/cron/defaults/cron.debian.yml | 4 + .../targets/cron/defaults/cron.fedora.yml | 4 + .../targets/cron/defaults/cron.freebsd.yml | 4 + .../targets/cron/defaults/cron.redhat.yml | 5 + .../targets/cron/defaults/cron.suse.yml | 4 + .../targets/cron/defaults/main.yml | 2 + test/integration/targets/cron/tasks/main.yml | 173 ++++++++++++++++++ 8 files changed, 199 insertions(+) create mode 100644 test/integration/targets/cron/aliases create mode 100644 test/integration/targets/cron/defaults/cron.debian.yml create mode 100644 test/integration/targets/cron/defaults/cron.fedora.yml create mode 100644 test/integration/targets/cron/defaults/cron.freebsd.yml create mode 100644 test/integration/targets/cron/defaults/cron.redhat.yml create mode 100644 test/integration/targets/cron/defaults/cron.suse.yml create mode 100644 test/integration/targets/cron/defaults/main.yml create mode 100644 test/integration/targets/cron/tasks/main.yml diff --git a/test/integration/targets/cron/aliases b/test/integration/targets/cron/aliases new file mode 100644 index 00000000000..fe75653cadc --- /dev/null +++ b/test/integration/targets/cron/aliases @@ -0,0 +1,3 @@ +destructive +shippable/posix/group4 +skip/osx diff --git a/test/integration/targets/cron/defaults/cron.debian.yml b/test/integration/targets/cron/defaults/cron.debian.yml new file mode 100644 index 00000000000..49abf6995fb --- /dev/null +++ b/test/integration/targets/cron/defaults/cron.debian.yml @@ -0,0 +1,4 @@ +--- +cron_pkg: cron +cron_service: cron +list_pkg_files: dpkg -L diff --git a/test/integration/targets/cron/defaults/cron.fedora.yml b/test/integration/targets/cron/defaults/cron.fedora.yml new file mode 100644 index 00000000000..3507db46ac2 --- /dev/null +++ b/test/integration/targets/cron/defaults/cron.fedora.yml @@ -0,0 +1,4 @@ +--- +cron_pkg: cronie +cron_service: crond +list_pkg_files: rpm -ql diff --git a/test/integration/targets/cron/defaults/cron.freebsd.yml b/test/integration/targets/cron/defaults/cron.freebsd.yml new file mode 100644 index 00000000000..07063dc0faf --- /dev/null +++ b/test/integration/targets/cron/defaults/cron.freebsd.yml @@ -0,0 +1,4 @@ +--- +cron_pkg: +cron_service: cron +list_pkg_files: pkg info --list-files diff --git a/test/integration/targets/cron/defaults/cron.redhat.yml b/test/integration/targets/cron/defaults/cron.redhat.yml new file mode 100644 index 00000000000..40a79f37c3d --- /dev/null +++ b/test/integration/targets/cron/defaults/cron.redhat.yml @@ -0,0 +1,5 @@ +--- +cron_pkg: cronie +cron_service: crond +faketime_pkg: +list_pkg_files: rpm -ql diff --git a/test/integration/targets/cron/defaults/cron.suse.yml b/test/integration/targets/cron/defaults/cron.suse.yml new file mode 100644 index 00000000000..5e57a6e514c --- /dev/null +++ b/test/integration/targets/cron/defaults/cron.suse.yml @@ -0,0 +1,4 @@ +--- +cron_pkg: cron +cron_service: cron +list_pkg_files: rpm -ql diff --git a/test/integration/targets/cron/defaults/main.yml b/test/integration/targets/cron/defaults/main.yml new file mode 100644 index 00000000000..2abb0ebf5dc --- /dev/null +++ b/test/integration/targets/cron/defaults/main.yml @@ -0,0 +1,2 @@ +--- +faketime_pkg: libfaketime diff --git a/test/integration/targets/cron/tasks/main.yml b/test/integration/targets/cron/tasks/main.yml new file mode 100644 index 00000000000..fb7fd42d6ac --- /dev/null +++ b/test/integration/targets/cron/tasks/main.yml @@ -0,0 +1,173 @@ +- include_vars: "{{ lookup('first_found', search) }}" + vars: + search: + files: + - 'cron.{{ ansible_system | lower }}.yml' + - 'cron.{{ ansible_distribution | lower }}.yml' + - 'cron.{{ ansible_os_family | lower }}.yml' + paths: + - '../defaults/' + +- vars: + remote_dir: "{{ lookup('env', 'OUTPUT_DIR') }}" + block: + - name: install cron package + package: + name: '{{ cron_pkg }}' + when: cron_pkg|default(false, true) + register: cron_package_installed + until: cron_package_installed is success + + - when: faketime_pkg|default(false, true) + block: + - name: install cron and faketime packages + package: + name: '{{ faketime_pkg }}' + register: faketime_package_installed + until: faketime_package_installed is success + + - name: Find libfaketime path + shell: '{{ list_pkg_files }} {{ faketime_pkg }} | grep -F libfaketime.so.1' + args: + warn: false + register: libfaketime_path + + - when: ansible_service_mgr == 'systemd' + block: + - name: create directory for cron drop-in file + file: + path: '/etc/systemd/system/{{ cron_service }}.service.d' + state: directory + owner: root + group: root + mode: 0755 + + - name: Use faketime with cron service + copy: + content: |- + [Service] + Environment=LD_PRELOAD={{ libfaketime_path.stdout_lines[0].strip() }} + Environment="FAKETIME=+0y x10" + Environment=RANDOM_DELAY=0 + dest: '/etc/systemd/system/{{ cron_service }}.service.d/faketime.conf' + owner: root + group: root + mode: 0644 + + - when: ansible_system == 'FreeBSD' + name: Use faketime with cron service + copy: + content: |- + cron_env='LD_PRELOAD={{ libfaketime_path.stdout_lines[0].strip() }} FAKETIME="+0y x10"' + dest: '/etc/rc.conf.d/cron' + owner: root + group: wheel + mode: 0644 + + - name: enable cron service + service: + daemon-reload: "{{ (ansible_service_mgr == 'systemd')|ternary(true, omit) }}" + name: '{{ cron_service }}' + state: restarted + + - name: add cron task (check mode enabled, cron task not already created) + cron: + name: test cron task + job: 'date > {{ remote_dir }}/cron_canary1' + check_mode: yes + register: check_mode_enabled_state_present + + - assert: + that: check_mode_enabled_state_present is changed + + - name: add cron task (check mode disabled, task hasn't already been created) + cron: + name: test cron task + job: 'date > {{ remote_dir }}/cron_canary1' + register: add_cron_task + + - assert: + that: add_cron_task is changed + + - name: add cron task (check mode enabled, cron task already exists) + cron: + name: test cron task + job: 'date > {{ remote_dir }}/cron_canary1' + check_mode: yes + register: check_mode_enabled_state_present_cron_task_already_exists + + - assert: + that: check_mode_enabled_state_present_cron_task_already_exists is not changed + + - name: add cron task (check mode disabled, cron task already created) + cron: + name: test cron task + job: 'date > {{ remote_dir }}/cron_canary1' + register: cron_task_already_created + + - assert: + that: cron_task_already_created is not changed + + - block: + - name: wait for canary creation + wait_for: + path: '{{ remote_dir }}/cron_canary1' + timeout: '{{ 20 if faketime_pkg else 70 }}' + register: wait_canary + always: + - name: display some logs in case of failure + command: 'journalctl -u {{ cron_service }}' + when: wait_canary is failed and ansible_service_mgr == 'systemd' + + - debug: + msg: 'elapsed time waiting for canary: {{ wait_canary.elapsed }}' + + - name: Check check_mode + cron: + name: test cron task + job: 'date > {{ remote_dir }}/cron_canary1' + state: absent + check_mode: yes + register: check_check_mode + + - assert: + that: check_check_mode is changed + + - name: Remove a cron task + cron: + name: test cron task + job: 'date > {{ remote_dir }}/cron_canary1' + state: absent + register: remove_task + + - assert: + that: remove_task is changed + + - name: 'cron task missing: check idempotence (check mode enabled, state=absent)' + cron: + name: test cron task + job: 'date > {{ remote_dir }}/cron_canary1' + state: absent + register: check_mode_enabled_remove_task_idempotence + + - assert: + that: check_mode_enabled_remove_task_idempotence is not changed + + - name: 'cron task missing: check idempotence (check mode disabled, state=absent)' + cron: + name: test cron task + job: 'date > {{ remote_dir }}/cron_canary1' + state: absent + register: remove_task_idempotence + + - assert: + that: remove_task_idempotence is not changed + + - name: Check that removing a cron task with cron_file and without specifying an user is allowed (#58493) + cron: + cron_file: unexistent_cron_file + state: absent + register: remove_cron_file + + - assert: + that: remove_cron_file is not changed