question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

documentation: passing dynamic role variables to tests when runnning molecule verify

See original GitHub issue

I know this could be batted away as something that is related to testinfra only, however, I am finding that I would like to ask here - would you accept a documentation PR that outlines a strategy for including role variables that you passed to your converge playbook, into your unit tests?

Here’s what I am currently doing:

# molecule/default/playbook.yml
---
- name: Converge
  hosts: all
  vars_files:
    - testvars.yml
  roles:
    - role: my-role
      foobar: "{{ test_foobar }}"

Then in a molecule/default/testvars.yml, I have:

---
test_foobar: barfoo

Then, in my molecule/default/tests/conftest.py, I do the following:

import os

import pytest
from testinfra.utils.ansible_runner import AnsibleRunner

DEFAULT_HOST = 'all'

inventory = os.environ['MOLECULE_INVENTORY_FILE']
runner = AnsibleRunner(inventory)
runner.get_hosts(DEFAULT_HOST)


@pytest.fixture
def testvars(host):
    variables = runner.run(
        DEFAULT_HOST,
        'include_vars',
        'testvars.yml'
    )
    return variables['ansible_facts']

And in my tests, I can then:

def test_something(host, testvars):
    print(testvars['test_foobar'])

As far as I can see, this is the cleanest way (I’ve digged around in a lot of tickets) of matching up the dynamic variables that you pass to your role and your testinfra pytest tests.

If it isn’t, please someone tell me 😃

In any case, the main question remains - is there a place we can start to document this on the RTD setup?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:6
  • Comments:38 (17 by maintainers)

github_iconTop GitHub Comments

11reactions
dangoncalvescommented, Aug 2, 2018

OK thanks for the explaination. I was able to adapt your code to an usage with molecule. I just added the following code to my tests (it works like yours):

import pytest

@pytest.fixture
def get_vars(host):
    defaults_files = "file=../../defaults/main.yml name=role_defaults"
    vars_files = "file=../../vars/main.yml name=role_vars"

    ansible_vars = host.ansible(
        "include_vars",
        defaults_files)["ansible_facts"]["role_defaults"]

    ansible_vars.update(host.ansible(
        "include_vars",
        vars_files)["ansible_facts"]["role_vars"])

    return ansible_vars

Of course it could be very useful if this was integrated natively in Testinfra, but it could be not so easy because of the many use cases that users could encounter.

10reactions
camillehuotcommented, Oct 10, 2018

Also looking forward this capability 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

documentation: passing dynamic role variables to tests when ...
I'm not a huge fan of Molecule supporting this type of testing. Having a test which references a variable isn't really testing the...
Read more >
Configuration — Molecule Documentation
By default, Molecule will check whether the role name follows the name standard. ... Environment variables can be passed to the dependency.
Read more >
Developing and Testing Ansible Roles with Molecule and ...
Molecule is a complete testing framework that helps you develop and test Ansible roles, which allows you to focus on role content instead...
Read more >
Ansible Testing Using Molecule with Ansible as Verifier
This tutorial is a simple demonstration of how Ansible testing can be done with Molecule using Ansible as verifier.
Read more >
Testing Ansible roles with Molecule and VMware
The idempotence molecule test will validate whether my role can be re-applied without making changes. Let's skip the majority of the output ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found