Strategy on testing that play is correctly failing

33 views
Skip to first unread message

Alix Lourme

unread,
Jul 24, 2019, 7:47:57 AM7/24/19
to molecule-users
Hello,

Molecule is perfect to test that play is correctly executed (different inputs, ...), but is there a way to test that some executions are correctly failing ?

e.g: don't stop when converge is failed to execute at minimum verify phase, and perhaps check which task or message error content in verify phase ?

It could be very valuable to test when ansible role checks some prerequisites (like tools installation, ...).

Sample:

Consider this very valuable role with a "tasks/main.yml" :

- name: Verify prerequisites
  stat
:
    path
: /tmp/prerequisites.file
 
register: prerequisites_file
  failed_when
: not prerequisites_file.stat.exists
- name: Do job
  copy
:
    content
: Job done!
    dest
: /tmp/job.file

The correct behavior can be mocked with a "molecule/default/prepare.yml" like :

---
- name: Prepare
  hosts
: all
  tasks
:
 
- name: Create prerequisites
    copy
:
      content
: prerequisites
      dest
: /tmp/prerequisites.file

And tested with a "molecule/default/tests/test_default.py" containing:

def test_job(host):
    f
= host.file('/tmp/job.file')

   
assert f.exists
   
assert f.content_string == "Job done!"

Now, if I want test that my role fails correctly when prerequisites are not set/exist/... I can have a test scenario (named test-prerequisites) without any "prepare.yml" and with a "molecule/test-prerequisites/tests/test_default.py" file containing:

def test_prerequisites(host):
    f
= host.file('/tmp/job.file')

   
assert not f.exists

But when the converge phase of the scenario "test-prerequisites" is executed, the process is (logically) stopped because my play is failed, so verify phase is not executed (only the destroy phase for cleanup).

The ignore_errors is not a solution because in this case the "prerequisite check task" doesn't fail and next tasks are executed.

If anybody has achieve this use case, or have some documentation links about this kind of strategy, it will be really appreciable.
Many thanks.
Best regards

a.a....@gmail.com

unread,
Mar 31, 2020, 9:55:47 AM3/31/20
to molecule-users
HI! Came here with the same question.

I'd like to test some failures. For example, showing meaningful message on unsupported platforms like this:

- name: Check distribution
  fail:
    msg: "Only xyz is supported at the moment"
  when: not ansible_distribution == "xyz"


Maybe at this point somebody came up with some solution?

Reply all
Reply to author
Forward
0 new messages