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 state.show_states when sls file missing in top file #54785

Merged
merged 2 commits into from
Sep 30, 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
3 changes: 3 additions & 0 deletions salt/modules/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,9 @@ def show_states(queue=False, **kwargs):
raise Exception(result)

for s in result:
if not isinstance(s, dict):
_set_retcode(result)
return result
states[s['__sls__']] = True
finally:
st_.pop_active()
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,10 @@ def transplant_configs(cls, transport='zeromq'):
}
master_opts['file_roots'] = syndic_master_opts['file_roots'] = {
'base': [
os.path.join(FILES, 'file', 'base'),
# Let's support runtime created files that can be used like:
# salt://my-temp-file.txt
RUNTIME_VARS.TMP_STATE_TREE
RUNTIME_VARS.TMP_STATE_TREE,
os.path.join(FILES, 'file', 'base'),
],
# Alternate root to test __env__ choices
'prod': [
Expand All @@ -903,10 +903,10 @@ def transplant_configs(cls, transport='zeromq'):
}
minion_opts['file_roots'] = {
'base': [
os.path.join(FILES, 'file', 'base'),
# Let's support runtime created files that can be used like:
# salt://my-temp-file.txt
RUNTIME_VARS.TMP_STATE_TREE
RUNTIME_VARS.TMP_STATE_TREE,
os.path.join(FILES, 'file', 'base'),
],
# Alternate root to test __env__ choices
'prod': [
Expand Down
38 changes: 24 additions & 14 deletions tests/integration/modules/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from tests.support.case import ModuleCase
from tests.support.helpers import with_tempdir, flaky
from tests.support.unit import skipIf
from tests.support.paths import BASE_FILES, TMP, TMP_PILLAR_TREE
from tests.support.paths import BASE_FILES, TMP, TMP_PILLAR_TREE, TMP_STATE_TREE
from tests.support.mixins import SaltReturnAssertsMixin

# Import Salt libs
Expand Down Expand Up @@ -118,6 +118,21 @@ def test_show_states(self):
self.assertTrue(isinstance(states, list))
self.assertTrue(isinstance(states[0], six.string_types))

def test_show_states_missing_sls(self):
'''
Test state.show_states with a sls file
defined in a top file is missing
'''
with salt.utils.files.fopen(os.path.join(TMP_STATE_TREE, 'top.sls'), 'w') as top_file:
top_file.write(textwrap.dedent('''\
base:
'*':
- doesnotexist
'''))
states = self.run_function('state.show_states')
assert isinstance(states, list)
assert states == ["No matching sls found for 'doesnotexist' in env 'base'"]

def test_catch_recurse(self):
'''
state.show_sls used to catch a recursive ref
Expand Down Expand Up @@ -2065,26 +2080,21 @@ def test_state_sls_unicode_characters_cmd_output(self):
self.assertEqual(_expected, ret[key]['changes']['stdout'])

def tearDown(self):
nonbase_file = os.path.join(TMP, 'nonbase_env')
if os.path.isfile(nonbase_file):
os.remove(nonbase_file)
rm_files = [os.path.join(TMP, 'nonbase_env'),
os.path.join(TMP, 'testfile'),
os.path.join(TMP, 'test.txt'),
os.path.join(TMP_STATE_TREE, 'top.sls')]

for file_ in rm_files:
if os.path.isfile(file_):
os.remove(file_)

# remove old pillar data
for filename in os.listdir(TMP_PILLAR_TREE):
os.remove(os.path.join(TMP_PILLAR_TREE, filename))
self.run_function('saltutil.refresh_pillar')
self.run_function('test.sleep', [5])

# remove testfile added in core.sls state file
state_file = os.path.join(TMP, 'testfile')
if os.path.isfile(state_file):
os.remove(state_file)

# remove testfile added in issue-30161.sls state file
state_file = os.path.join(TMP, 'test.txt')
if os.path.isfile(state_file):
os.remove(state_file)

def test_state_sls_integer_name(self):
'''
This tests the case where the state file is named
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/modules/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,19 @@ def test_show_states(self):
self.assertEqual(state.show_low_sls("foo"), "A")
self.assertListEqual(state.show_states("foo"), ['abc'])

def test_show_states_missing_sls(self):
'''
Test state.show_states when a sls file defined
in a top.sls file is missing
'''
msg = ["No matching sls found for 'cloud' in evn 'base'"]
chunks_mock = MagicMock(side_effect=[msg])
mock = MagicMock(side_effect=["A", None])
with patch.object(state, '_check_queue', mock),\
patch('salt.state.HighState.compile_low_chunks', chunks_mock):
self.assertEqual(state.show_low_sls("foo"), "A")
self.assertListEqual(state.show_states("foo"), [msg[0]])

def test_sls_id(self):
'''
Test to call a single ID from the
Expand Down