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

Fix docker image grains pillar #54166

Merged
merged 3 commits into from
Dec 20, 2019
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
2 changes: 2 additions & 0 deletions salt/modules/dockermod.py
Original file line number Diff line number Diff line change
Expand Up @@ -6853,6 +6853,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