From ea210065e52371d749ae81e8a174fd3878d0e2ee Mon Sep 17 00:00:00 2001 From: Wayne Werner Date: Tue, 27 Aug 2019 12:13:02 -0500 Subject: [PATCH] Update pillars and grains Without this, the thin client that's passed to the docker container won't contain the proper grain/pillar information. --- salt/modules/dockermod.py | 2 + tests/unit/modules/test_dockermod.py | 80 ++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/salt/modules/dockermod.py b/salt/modules/dockermod.py index 1bdc98dc6289..067c1a7013e7 100644 --- a/salt/modules/dockermod.py +++ b/salt/modules/dockermod.py @@ -6824,6 +6824,8 @@ def sls(name, mods=None, **kwargs): if pillar_override and isinstance(pillar_override, dict): pillar.update(pillar_override) + sls_opts['grains'].update(grains) + sls_opts['pillar'].update(pillar) trans_tar = _prepare_trans_tar( name, sls_opts, diff --git a/tests/unit/modules/test_dockermod.py b/tests/unit/modules/test_dockermod.py index 168ab3125cff..27f4d8f866af 100644 --- a/tests/unit/modules/test_dockermod.py +++ b/tests/unit/modules/test_dockermod.py @@ -37,6 +37,86 @@ def _docker_py_version(): return (0,) +@skipIf(NO_MOCK, NO_MOCK_REASON) +class DockerUnitTestCase(TestCase, LoaderModuleMockMixin): + def fake_run(self, *args, **kwargs): + print(args, kwargs) + return '{}' + + def setup_loader_modules(self): + return { + docker_mod: { + '__utils__': { + 'state.get_sls_opts': MagicMock(return_value={ + 'pillarenv': MagicMock(), + 'pillar': {}, + 'grains': {}, + }), + 'args.clean_kwargs': lambda **x: x, + }, + '__salt__': { + 'config.option': MagicMock(return_value=None), + 'cmd.run': self.fake_run, + }, + '__opts__': { + 'id': 'dockermod-unit-test', + }, + }, + } + + def test_trans_tar_should_have_grains_in_sls_opts_including_pillar_override(self): + container_name = 'fnord' + expected_grains = { + 'roscivs': 'bottia', + 'fnord': 'dronf', + 'salt': 'NaCl', + } + expected_pillars = { + 'this': { + 'is': { + 'my': { + 'pillar': 'data', + }, + }, + }, + } + extra_pillar_data = {'some': 'extras'} + fake_trans_tar = MagicMock(return_value=b'hi') + patch_trans_tar = patch( + 'salt.modules.dockermod._prepare_trans_tar', + fake_trans_tar, + ) + patch_call = patch( + 'salt.modules.dockermod.call', + MagicMock(return_value=expected_grains), + ) + fake_get_pillar = MagicMock() + fake_get_pillar.compile_pillar.return_value = expected_pillars + patch_pillar = patch( + 'salt.modules.dockermod.salt.pillar.get_pillar', + MagicMock(return_value=fake_get_pillar), + ) + patch_run_all = patch( + 'salt.modules.dockermod.run_all', + MagicMock(return_value={'retcode': 1, 'stderr': 'early exit test'}), + ) + with patch_trans_tar, patch_call, patch_pillar, patch_run_all: + docker_mod.sls(container_name, pillar=extra_pillar_data) + # TODO: It would be fine if we could make this test require less magic numbers -W. Werner, 2019-08-27 + actual_sls_opts = fake_trans_tar.call_args[0][1] + self.assertDictContainsSubset( + expected_grains, + actual_sls_opts['grains'], + 'Docker container grains not provided to thin client creation', + ) + expected_pillars.update(extra_pillar_data) + self.assertDictContainsSubset( + expected_pillars, + actual_sls_opts['pillar'], + 'Docker container pillar not provided to thin client creation', + ) + + @skipIf(NO_MOCK, NO_MOCK_REASON) @skipIf(docker_mod.HAS_DOCKER_PY is False, 'docker-py must be installed to run these tests. Skipping.') class DockerTestCase(TestCase, LoaderModuleMockMixin):