Closed watsonb closed 3 years ago
Thanks for raising and sharing your thoughts @watsonb!
When working with the provisioner section of molecule.yml for a scenario and feeding TestInfra tests, I notice that I have to violate DRY and explicitly define the values of role variables (e.g. those defined within defaults/main.yml) within the provisioner.inventory.group_vars/host_vars section of molecule.yml for the values to be picked up within TestInfra tests.
As you have said, this is a testinfra
thing and I think we should fix it there. I raised this at testinfra
IIUC your issue: https://github.com/philpep/testinfra/issues/345. I would recommend you weigh in there as well and bring your use case and motivations. There's no clear solution yet ...
Moreover, when defining the values of these vars within molecule.yml, I cannot reference the values of other variables using the "{{ }}" Ansible syntax. The double-curly braces seem to be interpreted as string literals and the value is not being substituted.
That sounds like something we should look into.
If we're in agreement about pushing out 1 to https://github.com/philpep/testinfra/issues/345, then let's rename this issue to describe the incorrect role referencing and focus on that. We might start by adding a https://github.com/ansible/molecule/tree/master/test/scenarios to test that it fails the way it should and then dive into the code ... (if anyone is feeling motivate ...)
BTW, we're working on getting the latest testinfra integrated (see #2034) and this utilises a new Ansible runner API. You may want to investigate this for the purposes of this issue ...
I found myself in this situation as well. You can create a pytest fixture to limit repetition.
@pytest.fixture(scope='module')
def AnsibleRoleDefaults(host):
return host.ansible(
'include_vars', '../../defaults/main.yml'
}['ansible_facts']
def test_thing(host, AnsibleRoleDefaults):
var = AnsibleRoleDefaults['var']
var2 = host.ansible.get_variables()['var']
Due to the way testifnra creates the host fixture, using include_vars
will work like it does within a play. So for some cases you might want to use debug msg={{ lookup('template','../../vars/main.yml') | from_yaml }}"
this will get you the variables without bothering the variable space.
Would it be out of scope for molecule to provide these?
Thanks for that update! Molecule shouldn't be providing these fixtures, we need to push for a solution in testinfra that handles this more easily. That's why I opened up https://github.com/philpep/testinfra/issues/345. However, we could think about documenting this for now, as a stop-gap.
Here is a pytest plugin that exposes ansible variables and facts, gopass secrets and the ansible python api as pytest fixtures in testinfra tests for molecule: testaid
Issue Type
Molecule and Ansible details
Molecule installation method (one of):
Ansible installation method (one of):
Desired Behavior
Warning: this could be considered two issues/features.
When working with the
provisioner
section ofmolecule.yml
for a scenario and feeding TestInfra tests, I notice that I have to violate DRY and explicitly define the values of role variables (e.g. those defined withindefaults/main.yml
) within theprovisioner.inventory.group_vars/host_vars
section ofmolecule.yml
for the values to be picked up within TestInfra tests.Moreover, when defining the values of these vars within molecule.yml, I cannot reference the values of other variables using the
"{{ }}"
Ansible syntax. The double-curly braces seem to be interpreted as string literals and the value is not being substituted.Examples:
defaults/main.yml
has the following:If my
molecule.yml
does NOT also define this, then my TestInfra test:Fails with:
However, if I do something like this (repeat myself) in my
molecule.yml
:Then it works as expected:
molecule verify
and NOTmolecule converge
.For example:
Given my
molecule.yml
contains:And my TestInfra test looks like:
You can see the double-curly braces coming through seemingly as a string literal in the informational output:
What's more concerning, it is not properly reporting
changed
and the test is passing. If I hard-code an invalid file path (like I think I should):I see an error that is apparently swallowed resulting in a passed test:
But if I hard-code the path to handle the backslashes correctly for TestInfra:
I get this:
These appear to be TestInfra issues and/or Molecule feeding TestInfra, and they could be specific to my Windows-based roles under test, I'm not sure. I know there is a discussion about Molecule Verification in general (e.g. using a verify.yml Ansible playbook to do it). I kind of prefer the notion of a separate tool verifying Ansible rather than using Ansible to verify itself (this recent Boeing/FAA thing comes to mind).
Anyhow, my primary request is the whole not having to repeat yourself in
molecule.yml
, but we'll see how this shakes out pending future verification discussions.