From b76154404363ea83f647eb153cf321734172a192 Mon Sep 17 00:00:00 2001 From: Emilien Macchi Date: Thu, 23 Jul 2020 15:53:13 -0400 Subject: [PATCH] [train-only] container_systemd: clean and test requires for systemd Clean some old functions and test the healthcheck dependencies on their units. This adds a test case in molecule where we: - Stop a service and check its healthcheck is also stopped by systemd. - Start a service and check its healthcheck is also started by systemd. Change-Id: I5e759d1fed4fe1f7bc2b542d0981e5ce9531d463 --- .../action/container_systemd.py | 34 +++++--------- .../molecule/default/playbook.yml | 46 +++++++++++++++++++ 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/tripleo_ansible/ansible_plugins/action/container_systemd.py b/tripleo_ansible/ansible_plugins/action/container_systemd.py index 99b389fa3..696699bec 100644 --- a/tripleo_ansible/ansible_plugins/action/container_systemd.py +++ b/tripleo_ansible/ansible_plugins/action/container_systemd.py @@ -266,27 +266,25 @@ class ActionModule(ActionBase): if results.get('changed', False): self.changed = True - def _add_systemd_requires(self, services, task_vars): + def _add_systemd_requires(self, services): """Add systemd dependencies for healthchecks. :param services: List for service names. - :param task_vars: Dictionary of Ansible task variables. """ for name in services: service = 'tripleo_{}'.format(name) - if self.debug: - DISPLAY.display('Adding systemd dependency for ' - '{}'.format(service)) - command = ('systemctl add-requires {}.service '.format(service) + '{}_healthcheck.timer'.format(service)) - results = self._execute_module( - module_name='command', - module_args=dict(cmd=command), - task_vars=task_vars + if self.debug: + DISPLAY.display('Adding systemd dependency for ' + '{} with command: ' + '{}'.format(service, command)) + results = self._low_level_execute_command( + command, + executable='/bin/bash' ) - if results.get('changed', False): - self.changed = True + if results.get('rc', 0) != 0: + raise AnsibleActionFail('Failed to run {}'.format(command)) @tenacity.retry( reraise=True, @@ -347,15 +345,6 @@ class ActionModule(ActionBase): self._manage_service(name=name, state='started', extension=extension, task_vars=task_vars) - def _add_requires(self, services, task_vars): - """Add systemd requires for healthchecks. - - :param services: List of services to manage. - """ - for s in services: - # TODO - pass - def run(self, tmp=None, task_vars=None): self.changed = False self.restarted = [] @@ -414,8 +403,7 @@ class ActionModule(ActionBase): self._ensure_started(service_names=h_already_created, extension='timer', task_vars=task_vars) - requires_healthchecks = changed_healthchecks + h_already_created - self._add_systemd_requires(requires_healthchecks, task_vars) + self._add_systemd_requires(all_healthchecks) result['changed'] = self.changed result['restarted'] = self.restarted diff --git a/tripleo_ansible/roles/tripleo-container-manage/molecule/default/playbook.yml b/tripleo_ansible/roles/tripleo-container-manage/molecule/default/playbook.yml index e1400b20e..4a10de44f 100644 --- a/tripleo_ansible/roles/tripleo-container-manage/molecule/default/playbook.yml +++ b/tripleo_ansible/roles/tripleo-container-manage/molecule/default/playbook.yml @@ -107,6 +107,52 @@ fail_msg: 'fedora_three container has wrong image' success_msg: 'fedora_three container has the right image' +- name: Test systemd healthcheck dependency on service + become: true + hosts: all + gather_facts: false + tasks: + - name: Stop systemd service for tripleo_fedora + systemd: + name: tripleo_fedora.service + state: stopped + enabled: true + - name: Check if tripleo_fedora_healthcheck timer is active + command: systemctl is-active --quiet tripleo_fedora_healthcheck.timer + register: tripleo_fedora_healthcheck_active_result + failed_when: false + when: + - not ansible_check_mode|bool + - name: Assert that tripleo_fedora_healthcheck timer service is not active + assert: + that: + - tripleo_fedora_healthcheck_active_result.rc == 3 + fail_msg: 'tripleo_fedora systemd service is active' + success_msg: 'tripleo_fedora systemd service is not active' + when: + - not ansible_check_mode|bool + - name: Start systemd service for tripleo_fedora + systemd: + name: tripleo_fedora.service + state: started + enabled: true + - name: Check if tripleo_fedora_healthcheck timer is active + command: systemctl is-active --quiet tripleo_fedora_healthcheck.timer + register: tripleo_fedora_healthcheck_active_result + until: tripleo_fedora_healthcheck_active_result.rc == 0 + retries: 5 + delay: 5 + when: + - not ansible_check_mode|bool + - name: Assert that tripleo_fedora_healthcheck timer service is active + assert: + that: + - tripleo_fedora_healthcheck_active_result.rc == 0 + fail_msg: 'tripleo_fedora systemd service is not active' + success_msg: 'tripleo_fedora systemd service is active' + when: + - not ansible_check_mode|bool + - name: Test idempotency on fedora container become: true hosts: all