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

Backport #51677 to master #55637

Merged
merged 11 commits into from
Dec 14, 2019
Prev Previous commit
Next Next commit
Add new tests for updated config.option function
  • Loading branch information
terminalmage authored and Erik Johnson committed Dec 13, 2019
commit a4cdcade940f1ab066ee69f9aa5b157066eeca37
136 changes: 117 additions & 19 deletions tests/unit/modules/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Import Python libs
from __future__ import absolute_import, print_function, unicode_literals
import fnmatch

# Import Salt Testing libs
from tests.support.mixins import LoaderModuleMockMixin
Expand All @@ -12,55 +13,152 @@
import salt.modules.config as config

DEFAULTS = {
'test.option.all': 'value of test.option.all in DEFAULTS',
'test.option': 'value of test.option in DEFAULTS'
'test.option.foo': 'value of test.option.foo in DEFAULTS',
'test.option.bar': 'value of test.option.bar in DEFAULTS',
'test.option.baz': 'value of test.option.baz in DEFAULTS',
'test.option': 'value of test.option in DEFAULTS',
}


@skipIf(NO_MOCK, NO_MOCK_REASON)
class TestModulesConfig(TestCase, LoaderModuleMockMixin):

no_match = 'test.option.nope'
opt_name = 'test.option.foo'
wildcard_opt_name = 'test.option.b*'

def setup_loader_modules(self):
return {
config: {
'__opts__': {
'test.option.all': 'value of test.option.all in __opts__'
'test.option.foo': 'value of test.option.foo in __opts__',
'test.option.bar': 'value of test.option.bar in __opts__',
'test.option.baz': 'value of test.option.baz in __opts__',
},
'__pillar__': {
'test.option.all': 'value of test.option.all in __pillar__',
'test.option.foo': 'value of test.option.foo in __pillar__',
'test.option.bar': 'value of test.option.bar in __pillar__',
'test.option.baz': 'value of test.option.baz in __pillar__',
'master': {
'test.option.all': 'value of test.option.all in master'
'test.option.foo': 'value of test.option.foo in master',
'test.option.bar': 'value of test.option.bar in master',
'test.option.baz': 'value of test.option.baz in master',
}
},
'__grains__': {
'test.option.foo': 'value of test.option.foo in __grains__',
'test.option.bar': 'value of test.option.bar in __grains__',
'test.option.baz': 'value of test.option.baz in __grains__',
}
}
}

def _wildcard_match(self, data):
return {x: data[x] for x in fnmatch.filter(data, self.wildcard_opt_name)}

def test_defaults_only_name(self):
with patch.dict(config.DEFAULTS, DEFAULTS):
opt_name = 'test.option'
opt = config.option(opt_name)
self.assertEqual(opt, config.DEFAULTS[opt_name])

def test_no_match(self):
'''
Make sure that the defa
'''
with patch.dict(config.DEFAULTS, DEFAULTS):
ret = config.option(self.no_match)
assert ret == '', ret

default = 'wat'
ret = config.option(self.no_match, default=default)
assert ret == default, ret

ret = config.option(self.no_match, wildcard=True)
assert ret == {}, ret

default = {'foo': 'bar'}
ret = config.option(self.no_match, default=default, wildcard=True)
assert ret == default, ret

# Should be no match since wildcard=False
ret = config.option(self.wildcard_opt_name)
assert ret == '', ret


def test_omits(self):
with patch.dict(config.DEFAULTS, DEFAULTS):
opt_name = 'test.option.all'
opt = config.option(opt_name,
omit_opts=False,
omit_master=True,
omit_pillar=True)

self.assertEqual(opt, config.__opts__[opt_name])
# ********** OMIT NOTHING **********

# Match should be in __opts__ dict
ret = config.option(self.opt_name)
assert ret == config.__opts__[self.opt_name], ret

# Wildcard match
ret = config.option(self.wildcard_opt_name, wildcard=True)
assert ret == self._wildcard_match(config.__opts__), ret

# ********** OMIT __opts__ **********

opt = config.option(opt_name,
# Match should be in __grains__ dict
ret = config.option(self.opt_name,
omit_opts=True)
assert ret == config.__grains__[self.opt_name], ret

# Wildcard match
ret = config.option(self.wildcard_opt_name,
omit_opts=True,
omit_master=True,
omit_pillar=False)
wildcard=True)
assert ret == self._wildcard_match(config.__grains__), ret

# ********** OMIT __opts__, __grains__ **********

# Match should be in __pillar__ dict
ret = config.option(self.opt_name,
omit_opts=True,
omit_grains=True)
assert ret == config.__pillar__[self.opt_name], ret

# Wildcard match
ret = config.option(self.wildcard_opt_name,
omit_opts=True,
omit_grains=True,
wildcard=True)
assert ret == self._wildcard_match(config.__pillar__), ret

# ********** OMIT __opts__, __grains__, __pillar__ **********

self.assertEqual(opt, config.__pillar__[opt_name])
opt = config.option(opt_name,
# Match should be in master opts
ret = config.option(self.opt_name,
omit_opts=True,
omit_master=False,
omit_grains=True,
omit_pillar=True)
assert ret == config.__pillar__['master'][self.opt_name], ret

self.assertEqual(
opt, config.__pillar__['master'][opt_name])
# Wildcard match
ret = config.option(self.wildcard_opt_name,
omit_opts=True,
omit_grains=True,
omit_pillar=True,
wildcard=True)
assert ret == self._wildcard_match(config.__pillar__['master']), ret

# ********** OMIT ALL THE THINGS **********

# Match should be in master opts
ret = config.option(self.opt_name,
omit_opts=True,
omit_grains=True,
omit_pillar=True,
omit_master=True)
assert ret == config.DEFAULTS[self.opt_name], ret

# Wildcard match
ret = config.option(self.wildcard_opt_name,
omit_opts=True,
omit_grains=True,
omit_pillar=True,
omit_master=True,
wildcard=True)
assert ret == self._wildcard_match(config.DEFAULTS), ret