Recommended Solution
The recommended way to address this is using multiple Molecule Scenarios. You could use your install
scenario as the default, and then add a second uninstall
scenario that just runs and tests the uninstall steps.
When setting this up, simply create a second scenario directory in your role's molecule
folder (copy the default
one), and then make some changes:
- (Edit: this step was necessary for molecule < 3.0.
scenario.name
was removed in later versions) In the molecule.yml
file change the scenario.name
attribute to uninstall
.
- In the same file, use the
default
scenario's playbook.yml
file as the playbook for the prepare
step:
provisioner:
name: ansible
playbooks:
prepare: ../default/playbook.yml
converge: playbook.yml
- Adjust the tests for the
uninstall
scenario to verify the uninstall steps.
This will ensure that the same steps are used for installing the software as in the install/default scenario, and you can focus on the uninstall steps.
To run the scenarios you can either run all of them or a single one:
# Run all scenarios
molecule test --all
# Run only the uninstall scenario
molecule test --scenario-name uninstall
This should get you pretty close to what you want to do, without replicating any code.
If you want to try some other things, here are a couple of other thoughts:
Alternatives
I would keep a scenario for the install only that would play all the needed tests (lint, idempotency, check, verify....) and create an install_uninstall
specific scenario.
Playing install_uninstall
will never be idempotent. So this scenario should disable the idempotency tests that will never pass. You might as well want to disable the check test that is played in your other scenario, lint... This can be done in molecule.yml
by adjusting the parameters for scenario.test_sequence
:
scenario:
name: install_uninstall
test_sequence:
- destroy
- create
- prepare
- converge
- verify
- destroy
Of course you can adjust to your real needs (like dropping verify also if you have no testinfra tests for this case).
Once this is done you only have to add two plays in your scenario playbook:
---
- name: install
hosts: all
roles:
- role: my_role
install: true
- name: uninstall
hosts: all
roles:
- role: my_role
install: false
And you should be ready to test with:
molecule test -s install_uninstall
Edit:
An other option would be to keep only your current install scenario but launch the individual molecule commands rather than a full test. Assuming your current working scenario is in default
# Check and lint my files
molecule lint
# Make sure no box/container is on the way
molecule destroy
# Create my box/container for tests
molecule create
# Play my default playbook
molecule converge
# Idempotency check
molecule idempotence
# Verify we can correctly use check mode
molecule check
# Play testinfra tests
molecule verify
# Now play the uninstall
molecule converge -- -e install=false
## add more tests you can think off ##
# and finally cleanup
molecule destroy.
Unfortunately, unless this feature was very recently added to molecule, it is not possible to call idempotency
and check
with extra variables
../default/playbook.yml
in theprepare
phase. Thus I don't have to replicate the install steps, and can focus on just the uninstall steps in the uninstallplaybook.yml
. This works rather nicely - exactly what I wanted. – Hawsehole