Skip to content

Commit

Permalink
Merge pull request #54166 from waynew/fix-docker-image-grains-pillar
Browse files Browse the repository at this point in the history
Fix docker image grains pillar
  • Loading branch information
dwoz authored Dec 20, 2019
2 parents 0f2e289 + df46cb1 commit 0937a3a
Show file tree
Hide file tree
Showing 2 changed files with 81 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 @@ -6792,6 +6792,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
79 changes: 79 additions & 0 deletions tests/unit/modules/test_dockermod.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,85 @@ def _docker_py_version():
return (0,)


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(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 0937a3a

Please sign in to comment.