Skip to content

Commit

Permalink
tests: use is_subset to validate loaded data
Browse files Browse the repository at this point in the history
Fixes recurring problem with tests which failed for users that use
DOCKER_HOST to point to a remote docker. This happened because in those
cases the loaded configuration dictionary looks different as it has
extra parameters added to it.

We failed to catch these on CI because we do not use a remote docker
connection.

From now on we should only check specific content in loaded config.
  • Loading branch information
ssbarnea committed Jan 3, 2020
1 parent 12ed644 commit 583200a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
18 changes: 18 additions & 0 deletions molecule/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ def _rebake_command(cmd, env, out=LOG.out, err=LOG.error):
return cmd.bake(_env=env, _out=out, _err=err)


def is_subset(subset, superset):
# Checks if first dict is a subset of the second one
if isinstance(subset, dict):
return all(
key in superset and is_subset(val, superset[key])
for key, val in subset.items()
)

if isinstance(subset, list) or isinstance(subset, set):
return all(
any(is_subset(subitem, superitem) for superitem in superset)
for subitem in subset
)

# assume that subset is a plain value if none of the above match
return subset == superset


@pytest.fixture
def random_string(l=5):
return ''.join(random.choice(string.ascii_uppercase) for _ in range(l))
Expand Down
3 changes: 2 additions & 1 deletion molecule/test/unit/driver/test_delegated.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from molecule import config
from molecule.driver import delegated
from molecule.test.conftest import is_subset


@pytest.fixture
Expand Down Expand Up @@ -190,7 +191,7 @@ def test_login_options_when_managed(mocker, _instance):
def test_ansible_connection_options(_instance):
x = {'ansible_connection': 'docker'}

assert x == _instance.ansible_connection_options('foo')
assert is_subset(x, _instance.ansible_connection_options('foo'))


@pytest.mark.parametrize(
Expand Down
3 changes: 2 additions & 1 deletion molecule/test/unit/driver/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from molecule import config
from molecule.driver import docker
from ...conftest import is_subset


@pytest.fixture
Expand Down Expand Up @@ -96,7 +97,7 @@ def test_login_options(_instance):
def test_ansible_connection_options(_instance):
x = {'ansible_connection': 'docker'}

assert x == _instance.ansible_connection_options('foo')
assert is_subset(x, _instance.ansible_connection_options('foo'))


def test_instance_config_property(_instance):
Expand Down
9 changes: 5 additions & 4 deletions molecule/test/unit/provisioner/test_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import os

import pytest
from ...conftest import is_subset

from molecule import config
from molecule import util
Expand Down Expand Up @@ -374,7 +375,7 @@ def test_inventory_property(_instance):
},
}

assert x == _instance.inventory
assert is_subset(x, _instance.inventory)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -409,7 +410,7 @@ def test_inventory_property_handles_missing_groups(temp_dir, _instance):
},
}

assert x == _instance.inventory
assert is_subset(x, _instance.inventory)


def test_inventory_directory_property(_instance):
Expand Down Expand Up @@ -482,7 +483,7 @@ def test_playbooks_side_effect_property(_instance):
def test_connection_options(_instance):
x = {'ansible_connection': 'docker', 'foo': 'bar'}

assert x == _instance.connection_options('foo')
assert is_subset(x, _instance.connection_options('foo'))


def test_check(_instance, mocker, _patched_ansible_playbook):
Expand Down Expand Up @@ -799,7 +800,7 @@ def test_write_inventory(temp_dir, _instance):
},
}

assert x == data
assert is_subset(x, data)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 583200a

Please sign in to comment.