Add integration tests for test mode onchanges/prereq

Conflicts:
  - tests/unit/states/test_file.py
This commit is contained in:
Erik Johnson 2018-09-28 21:30:06 -05:00 committed by Ch3LL
parent ed214c4cdb
commit 4bc5fd008e
No known key found for this signature in database
GPG key ID: 132B55A7C13EFA73
5 changed files with 222 additions and 1 deletions

View file

@ -0,0 +1,22 @@
one:
file.managed:
- name: {{ pillar['file1'] }}
- source: {{ pillar['source'] }}
# This should run because there were changes
two:
test.succeed_without_changes:
- {{ pillar['req'] }}:
- file: one
# Run the same state as "one" again, this should not cause changes
three:
file.managed:
- name: {{ pillar['file2'] }}
- source: {{ pillar['source'] }}
# This should not run because there should be no changes
four:
test.succeed_without_changes:
- {{ pillar['req'] }}:
- file: three

View file

@ -0,0 +1,3 @@
{{ salt['runtests_helpers.get_salt_temp_dir_for_path']('orch.req_test') }}:
file.managed:
- contents: 'Hello world!'

View file

@ -643,3 +643,119 @@ class OrchEventTest(ShellCase):
self.assertTrue(received)
del listener
signal.alarm(0)
def test_orchestration_onchanges_and_prereq(self):
'''
Test to confirm that the parallel state requisite works in orch
we do this by running 10 test.sleep's of 10 seconds, and insure it only takes roughly 10s
'''
self.write_conf({
'fileserver_backend': ['roots'],
'file_roots': {
'base': [self.base_env],
},
})
orch_sls = os.path.join(self.base_env, 'orch.sls')
with salt.utils.files.fopen(orch_sls, 'w') as fp_:
fp_.write(textwrap.dedent('''
manage_a_file:
salt.state:
- tgt: minion
- sls:
- orch.req_test
do_onchanges:
salt.function:
- tgt: minion
- name: test.ping
- onchanges:
- salt: manage_a_file
do_prereq:
salt.function:
- tgt: minion
- name: test.ping
- prereq:
- salt: manage_a_file
'''))
listener = salt.utils.event.get_event(
'master',
sock_dir=self.master_opts['sock_dir'],
transport=self.master_opts['transport'],
opts=self.master_opts)
try:
jid1 = self.run_run_plus(
'state.orchestrate',
'orch',
test=True,
__reload_config=True).get('jid')
# Run for real to create the file
self.run_run_plus(
'state.orchestrate',
'orch',
__reload_config=True).get('jid')
# Run again in test mode. Since there were no changes, the
# requisites should not fire.
jid2 = self.run_run_plus(
'state.orchestrate',
'orch',
test=True,
__reload_config=True).get('jid')
finally:
try:
os.remove(os.path.join(TMP, 'orch.req_test'))
except OSError:
pass
assert jid1 is not None
assert jid2 is not None
tags = {'salt/run/{0}/ret'.format(x): x for x in (jid1, jid2)}
ret = {}
signal.signal(signal.SIGALRM, self.alarm_handler)
signal.alarm(self.timeout)
try:
while True:
event = listener.get_event(full=True)
if event is None:
continue
if event['tag'] in tags:
ret[tags.pop(event['tag'])] = self.repack_state_returns(
event['data']['return']['data']['master']
)
if not tags:
# If tags is empty, we've grabbed all the returns we
# wanted, so let's stop listening to the event bus.
break
finally:
del listener
signal.alarm(0)
for sls_id in ('manage_a_file', 'do_onchanges', 'do_prereq'):
# The first time through, all three states should have a None
# result, while the second time through, they should all have a
# True result.
assert ret[jid1][sls_id]['result'] is None, \
'result of {0} ({1}) is not None'.format(
sls_id,
ret[jid1][sls_id]['result'])
assert ret[jid2][sls_id]['result'] is True, \
'result of {0} ({1}) is not True'.format(
sls_id,
ret[jid2][sls_id]['result'])
# The file.managed state should have shown changes in the test mode
# return data.
assert ret[jid1]['manage_a_file']['changes']
# After the file was created, running again in test mode should have
# shown no changes.
assert not ret[jid2]['manage_a_file']['changes'], \
ret[jid2]['manage_a_file']['changes']

View file

@ -816,6 +816,87 @@ class FileTest(ModuleCase, SaltReturnAssertsMixin):
result = self.run_function('cp.is_cached', [source, saltenv])
assert result == '', 'File is still cached at {0}'.format(result)
@with_tempfile(create=False)
@with_tempfile(create=False)
def test_file_managed_onchanges(self, file1, file2):
'''
Test file.managed state with onchanges
'''
pillar = {'file1': file1,
'file2': file2,
'source': 'salt://testfile',
'req': 'onchanges'}
# Lay down the file used in the below SLS to ensure that when it is
# run, there are no changes.
self.run_state(
'file.managed',
name=pillar['file2'],
source=pillar['source'])
ret = self.repack_state_returns(
self.run_function(
'state.apply',
mods='onchanges_prereq',
pillar=pillar,
test=True,
)
)
# The file states should both exit with None
assert ret['one']['result'] is None, ret['one']['result']
assert ret['three']['result'] is True, ret['three']['result']
# The first file state should have changes, since a new file was
# created. The other one should not, since we already created that file
# before applying the SLS file.
assert ret['one']['changes']
assert not ret['three']['changes'], ret['three']['changes']
# The state watching 'one' should have been run due to changes
assert ret['two']['comment'] == 'Success!', ret['two']['comment']
# The state watching 'three' should not have been run
assert ret['four']['comment'] == \
'State was not run because none of the onchanges reqs changed', \
ret['four']['comment']
@with_tempfile(create=False)
@with_tempfile(create=False)
def test_file_managed_prereq(self, file1, file2):
'''
Test file.managed state with prereq
'''
pillar = {'file1': file1,
'file2': file2,
'source': 'salt://testfile',
'req': 'prereq'}
# Lay down the file used in the below SLS to ensure that when it is
# run, there are no changes.
self.run_state(
'file.managed',
name=pillar['file2'],
source=pillar['source'])
ret = self.repack_state_returns(
self.run_function(
'state.apply',
mods='onchanges_prereq',
pillar=pillar,
test=True,
)
)
# The file states should both exit with None
assert ret['one']['result'] is None, ret['one']['result']
assert ret['three']['result'] is True, ret['three']['result']
# The first file state should have changes, since a new file was
# created. The other one should not, since we already created that file
# before applying the SLS file.
assert ret['one']['changes']
assert not ret['three']['changes'], ret['three']['changes']
# The state watching 'one' should have been run due to changes
assert ret['two']['comment'] == 'Success!', ret['two']['comment']
# The state watching 'three' should not have been run
assert ret['four']['comment'] == 'No changes detected', \
ret['four']['comment']
def test_directory(self):
'''
file.directory

View file

@ -839,7 +839,6 @@ class TestFileState(TestCase, LoaderModuleMockMixin):
ret = {'name': name,
'result': False,
'comment': '',
'changes': {},
'changes': {}}
comt = ('Must provide name to file.directory')