Skip to content

Commit

Permalink
Update pillars and grains
Browse files Browse the repository at this point in the history
Without this, the thin client that's passed to the docker container
won't contain the proper grain/pillar information.
  • Loading branch information
waynew committed Nov 14, 2019
1 parent 920c7d9 commit ea21006
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions salt/modules/dockermod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
80 changes: 80 additions & 0 deletions tests/unit/modules/test_dockermod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit ea21006

Please sign in to comment.