Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: use is_subset to validate loaded data #2480

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ repos:
hooks:
- id: flake8
additional_dependencies:
- flake8-absolute-import
- flake8-black>=0.1.1
- flake8-mypy
language_version: python3
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.18.0
rev: v1.20.0
hooks:
- id: yamllint
files: \.(yaml|yml)$
Expand Down
2 changes: 1 addition & 1 deletion molecule/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

from .shell import main
from molecule.shell import main

if __name__ == '__main__':
main()
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
2 changes: 1 addition & 1 deletion molecule/test/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from molecule import logger
from molecule import util

from ..conftest import change_dir_to
from molecule.test.conftest import change_dir_to

LOG = logger.get_logger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion molecule/test/functional/docker/test_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from molecule import util

from ..conftest import change_dir_to
from molecule.test.conftest import change_dir_to


@pytest.fixture
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 molecule.test.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 molecule.test.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