Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/tasks/run_role_with_clear_facts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
# Task file: clear_facts, run linux-system-roles.systemd.
# Include this with include_tasks or import_tasks
# Input:
# - __sr_tasks_from: tasks_from to run - same as tasks_from in include_role
# - __sr_public: export private vars from role - same as public in include_role
# - __sr_failed_when: set to false to ignore role errors - same as failed_when in include_role
- name: Clear facts
meta: clear_facts

# note that you can use failed_when with import_role but not with include_role
# so this simulates the __sr_failed_when false case
# Q: Why do we need a separate task to run the role normally? Why not just
# run the role in the block and rethrow the error in the rescue block?
# A: Because you cannot rethrow the error in exactly the same way as the role does.
# It might be possible to exactly reconstruct ansible_failed_result but it's not worth the effort.
- name: Run the role with __sr_failed_when false
when:
- __sr_failed_when is defined
- not __sr_failed_when
block:
- name: Run the role
include_role:
name: linux-system-roles.systemd
tasks_from: "{{ __sr_tasks_from | default('main') }}"
public: "{{ __sr_public | default(false) }}"
rescue:
- name: Ignore the failure when __sr_failed_when is false
debug:
msg: Ignoring failure when __sr_failed_when is false

- name: Run the role normally
include_role:
name: linux-system-roles.systemd
tasks_from: "{{ __sr_tasks_from | default('main') }}"
public: "{{ __sr_public | default(false) }}"
when: __sr_failed_when | d(true)
60 changes: 23 additions & 37 deletions tests/tests_basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@
---
- name: Ensure that deploying unit files work correctly
hosts: all
gather_facts: false
vars:
systemd_unit_file_templates:
- nested/dir/templates/foo.service.j2
roles:
- linux-system-roles.systemd
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
- name: Verify that unit file was deployed
stat:
path: /etc/systemd/system/foo.service

- name: Ensure that deploying unit file dropins work correctly
hosts: all
gather_facts: false
vars:
systemd_dropins:
- nested/dir/templates/foo.service.conf.j2
roles:
- linux-system-roles.systemd
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
- name: Stat the dropin directory path
stat:
path: /etc/systemd/system/foo.service.d
Expand All @@ -38,13 +36,12 @@

- name: Ensure that we can start units using the role
hosts: all
gather_facts: false
vars:
systemd_started_units:
- foo.service
roles:
- linux-system-roles.systemd
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
- name: Make sure that foo.service main state is active
assert:
that:
Expand All @@ -59,19 +56,18 @@

- name: Ensure that we can restart units using the role
hosts: all
gather_facts: false
vars:
systemd_restarted_units:
- foo.service
roles:
- linux-system-roles.systemd
pre_tasks:
- name: Save MainPID of foo.service before restart
# noqa command-instead-of-module
command: systemctl show -p MainPID foo.service
register: pid_before_restart
changed_when: false
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
- name: Get MainPID of foo.service
# noqa command-instead-of-module
command: systemctl show -p MainPID foo.service
Expand All @@ -85,37 +81,35 @@

- name: Ensure that we can reload units
hosts: all
gather_facts: false
vars:
systemd_reloaded_units:
- foo.service
roles:
- linux-system-roles.systemd
pre_tasks:
- name: Delete reload flag file
file:
path: /tmp/foo-service-reloaded
state: absent
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
- name: Reload operation should created flag file
stat:
path: /tmp/foo-service-reloaded

- name: Ensure that we can enable unit files
hosts: all
gather_facts: false
vars:
systemd_enabled_units:
- foo.service
roles:
- linux-system-roles.systemd
pre_tasks:
- name: Save UnitFileState before calling role
# noqa command-instead-of-module
command: systemctl show -p UnitFileState foo.service
register: unit_file_state_before
changed_when: false
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
- name: Get UnitFileState=
# noqa command-instead-of-module
command: systemctl show -p UnitFileState foo.service
Expand All @@ -130,13 +124,12 @@

- name: Ensure that we can disable unit files
hosts: all
gather_facts: false
vars:
systemd_disabled_units:
- foo.service
roles:
- linux-system-roles.systemd
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
- name: Get UnitFileState= - 2
# noqa command-instead-of-module
command: systemctl show -p UnitFileState foo.service
Expand All @@ -150,20 +143,19 @@

- name: Ensure that we can mask unit files
hosts: all
gather_facts: false
vars:
# It is not possible to mask admin units files in /etc/systemd/system.
systemd_masked_units:
- sshd.service
roles:
- linux-system-roles.systemd
pre_tasks:
- name: Save UnitFileState before calling role - 2
# noqa command-instead-of-module
command: systemctl show -p UnitFileState sshd.service
register: sshd_state_before
changed_when: false
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
- name: Get UnitFileState= - 3
# noqa command-instead-of-module
command: systemctl show -p UnitFileState sshd.service
Expand All @@ -180,13 +172,12 @@

- name: Ensure that we can unmask unit files
hosts: all
gather_facts: false
vars:
systemd_unmasked_units:
- sshd.service
roles:
- linux-system-roles.systemd
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
- name: Get UnitFileState= - 4
# noqa command-instead-of-module
command: systemctl show -p UnitFileState sshd.service
Expand All @@ -200,20 +191,18 @@

- name: Ensure that we can stop units
hosts: all
gather_facts: false
vars:
systemd_stopped_units:
- foo.service
roles:
- linux-system-roles.systemd
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
- name: Foo.service shouldn't be in systemd_units
fail:
when: ansible_facts['systemd_units']['foo.service'] is defined

- name: Test unmask and start
hosts: all
gather_facts: false
vars:
# we need a
# * system service provided in /usr/lib/systemd/system
Expand Down Expand Up @@ -247,8 +236,7 @@
test_unit: "{{ __find_test_unit.stdout | trim }}"

- name: Ensure test unit is running and unmasked
include_role:
name: linux-system-roles.systemd
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
systemd_started_units:
- "{{ test_unit }}"
Expand All @@ -269,8 +257,7 @@
- test_unit_state.stdout is search("SubState=running")

- name: Stop and mask test unit
include_role:
name: linux-system-roles.systemd
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
systemd_stopped_units:
- "{{ test_unit }}"
Expand All @@ -292,8 +279,7 @@
and test_unit_state.stdout is search('SubState=dead') }}"

- name: Ensure test unit is running and unmasked - 2
include_role:
name: linux-system-roles.systemd
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
systemd_started_units:
- "{{ test_unit }}"
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
---
- name: Ensure that the role runs with no arguments
hosts: all
gather_facts: false
roles:
- linux-system-roles.systemd
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
7 changes: 5 additions & 2 deletions tests/tests_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
---
- name: Ensure that basic features of role work correctly
hosts: all
roles:
- linux-system-roles.systemd
tasks:
- name: Run the role
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
__sr_public: true

- name: Print units facts
debug:
msg: "{{ systemd_units }}"
Expand Down
20 changes: 7 additions & 13 deletions tests/tests_user_units.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
---
- name: Ensure that the role works with a mix of user and system units
hosts: all
gather_facts: false
vars:
systemd_fail_if_too_old: false # allow test to pass on el7
__bar_service_name: bar.service
Expand Down Expand Up @@ -67,10 +66,9 @@
loop: "{{ __users }}"

- name: Run role to create and start units
include_role:
name: linux-system-roles.systemd
public: true
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
__sr_public: true
systemd_unit_files: "{{ __systemd_unit_files }}"
systemd_unit_file_templates: "{{ __systemd_unit_file_templates }}"
systemd_dropins: "{{ __systemd_dropins }}"
Expand Down Expand Up @@ -136,8 +134,7 @@
scope: "{{ '--system' if item.user == 'root' else '--user' }}"

- name: Run role to enable units
include_role:
name: linux-system-roles.systemd
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
systemd_enabled_units: "{{ __systemd_enabled_units }}"

Expand All @@ -156,8 +153,7 @@
scope: "{{ '--system' if item.user == 'root' else '--user' }}"

- name: Run role to disable units
include_role:
name: linux-system-roles.systemd
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
systemd_disabled_units: "{{ __systemd_disabled_units }}"

Expand All @@ -176,8 +172,7 @@
scope: "{{ '--system' if item.user == 'root' else '--user' }}"

- name: Run role to stop units
include_role:
name: linux-system-roles.systemd
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
systemd_stopped_units: "{{ __systemd_stopped_units }}"

Expand All @@ -187,10 +182,9 @@
loop: "{{ __all_units }}"

- name: Run role to remove unit files and dropins
include_role:
name: linux-system-roles.systemd
public: true
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
__sr_public: true
systemd_unit_files: "{{ __systemd_unit_files |
map('combine', __absent) | list }}"
systemd_unit_file_templates: "{{ __systemd_unit_file_templates |
Expand Down
Loading