Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cronvar - use correct binary name #63279

Merged
merged 4 commits into from Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/cronvar-correct-binary-name.yaml
@@ -0,0 +1,2 @@
bugfixes:
- cronvar - use correct binary name (https://github.com/ansible/ansible/issues/63274)
2 changes: 1 addition & 1 deletion lib/ansible/modules/system/cronvar.py
Expand Up @@ -126,7 +126,7 @@ def __init__(self, module, user=None, cron_file=None):
self.user = user
self.lines = None
self.wordchars = ''.join(chr(x) for x in range(128) if chr(x) not in ('=', "'", '"',))
self.cron_cmd = self.module.get_bin_path('cronvar', required=True)
self.cron_cmd = self.module.get_bin_path('crontab', required=True)

if cron_file:
self.cron_file = ""
Expand Down
1 change: 0 additions & 1 deletion test/integration/targets/cron/defaults/main.yml
@@ -1,2 +1 @@
---
faketime_pkg: libfaketime
2 changes: 2 additions & 0 deletions test/integration/targets/cron/meta/main.yml
@@ -0,0 +1,2 @@
dependencies:
- setup_cron
274 changes: 101 additions & 173 deletions test/integration/targets/cron/tasks/main.yml
@@ -1,173 +1,101 @@
- 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
- 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
3 changes: 3 additions & 0 deletions test/integration/targets/cronvar/aliases
@@ -0,0 +1,3 @@
destructive
shippable/posix/group4
skip/osx
1 change: 1 addition & 0 deletions test/integration/targets/cronvar/defaults/main.yml
@@ -0,0 +1 @@
cron_config_path: /etc/cron.d
2 changes: 2 additions & 0 deletions test/integration/targets/cronvar/meta/main.yml
@@ -0,0 +1,2 @@
dependencies:
- setup_cron