diff --git a/plugins/modules/monit.py b/plugins/modules/monit.py index 63c83741b5c..09cedcf1bf2 100644 --- a/plugins/modules/monit.py +++ b/plugins/modules/monit.py @@ -151,8 +151,11 @@ def get_status(self, validate=False): """ monit_command = "validate" if validate else "status" check_rc = False if validate else True # 'validate' always has rc = 1 + + time.sleep(1) command = [self.monit_bin_path, monit_command] + self.command_args + [self.process_name] rc, out, err = self.module.run_command(command, check_rc=check_rc) + # raise Exception('HH') return self._parse_status(out, err) def _parse_status(self, output, err): @@ -171,9 +174,12 @@ def _parse_status(self, output, err): if ' - ' not in status_val: status_val = status_val.replace(' ', '_') try: + # raise Exception(f'II1A {getattr(Status, status_val)=}') + # raise Exception(f'II1 {status_val=} {dir(Status)=}') return getattr(Status, status_val) except AttributeError: self.module.warn("Unknown monit status '%s', treating as execution failed" % status_val) + raise Exception('II2') return Status.EXECUTION_FAILED else: status_val, substatus = status_val.split(' - ') @@ -185,6 +191,7 @@ def _parse_status(self, output, err): if state == 'pending': status = status.pending() + raise Exception('II3') return status def is_process_present(self): @@ -201,9 +208,11 @@ def run_command(self, command): def wait_for_status_change(self, current_status): running_status = self.get_status() + raise Exception('JJ') if running_status.value != current_status.value or current_status.value == StatusValue.EXECUTION_FAILED: return running_status + raise Exception('GG') loop_count = 0 while running_status.value == current_status.value: if loop_count >= self._status_change_retry_count: @@ -213,6 +222,7 @@ def wait_for_status_change(self, current_status): time.sleep(0.5) validate = loop_count % 2 == 0 # force recheck of status every second try running_status = self.get_status(validate) + raise Exception('FF') return running_status def wait_for_monit_to_stop_pending(self, current_status=None): @@ -255,17 +265,28 @@ def present(self): def change_state(self, state, expected_status, invert_expected=None): current_status = self.get_status() self.run_command(STATE_COMMAND_MAP[state]) + # if state == 'stopped': + # raise Exception('EE') status = self.wait_for_status_change(current_status) + if state == 'stopped': + raise Exception('DD') status = self.wait_for_monit_to_stop_pending(status) status_match = status.value == expected_status.value + if state == 'stopped': + raise Exception('CC') if invert_expected: status_match = not status_match + if state == 'stopped': + raise Exception('BB') if status_match: self.exit_success(state=state) + if state == 'stopped': + raise Exception('AA') self.exit_fail('%s process not %s' % (self.process_name, state), status) def stop(self): self.change_state('stopped', Status.NOT_MONITORED) + raise Exception('INSIDE-AFTER: Failed to stop process') def unmonitor(self): self.change_state('unmonitored', Status.NOT_MONITORED) @@ -323,6 +344,7 @@ def exit_if_check_mode(): if running and state == 'stopped': exit_if_check_mode() monit.stop() + raise Exception('OUTSIDE-AFTER: Failed to stop process') if running and state == 'unmonitored': exit_if_check_mode() diff --git a/tests/integration/targets/monit/aliases b/tests/integration/targets/monit/aliases index c78104339ce..ed99f5aeb6b 100644 --- a/tests/integration/targets/monit/aliases +++ b/tests/integration/targets/monit/aliases @@ -10,4 +10,4 @@ skip/macos skip/freebsd skip/aix skip/rhel # FIXME -unstable # TODO: the tests fail a lot; 'unstable' only requires them to pass when the module itself has been modified +# unstable # TODO: the tests fail a lot; 'unstable' only requires them to pass when the module itself has been modified diff --git a/tests/integration/targets/monit/templates/monitrc.j2 b/tests/integration/targets/monit/files/monitrc similarity index 59% rename from tests/integration/targets/monit/templates/monitrc.j2 rename to tests/integration/targets/monit/files/monitrc index 4f1a6e24734..ec53b596c57 100644 --- a/tests/integration/targets/monit/templates/monitrc.j2 +++ b/tests/integration/targets/monit/files/monitrc @@ -1,8 +1,6 @@ -{# -Copyright (c) Ansible Project -GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -SPDX-License-Identifier: GPL-3.0-or-later -#} +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later set daemon 2 set logfile /var/log/monit.log diff --git a/tests/integration/targets/monit/tasks/main.yml b/tests/integration/targets/monit/tasks/main.yml index 518e997c329..ade9875b178 100644 --- a/tests/integration/targets/monit/tasks/main.yml +++ b/tests/integration/targets/monit/tasks/main.yml @@ -41,9 +41,10 @@ - name: monit config become: true - template: - src: "monitrc.j2" + copy: + src: "monitrc" dest: "{{ monitrc }}" + mode: '0700' - name: copy process file become: true @@ -65,12 +66,9 @@ - name: install dependencies pip: - name: "{{ item }}" + name: python-daemon virtualenv: "{{ process_venv }}" extra_args: "-c {{ remote_constraints }}" - loop: - - setuptools==44 - - python-daemon - name: restart monit become: true diff --git a/tests/integration/targets/monit/tasks/test_state.yml b/tests/integration/targets/monit/tasks/test_state.yml index 33a70c196aa..4d2991ea4f9 100644 --- a/tests/integration/targets/monit/tasks/test_state.yml +++ b/tests/integration/targets/monit/tasks/test_state.yml @@ -13,6 +13,7 @@ name: httpd_echo state: "{{ state }}" register: result + failed_when: result.rc | default(0) is not in [0, -15] - name: check that state changed assert: