2012-05-13 04:31:58 +00:00
|
|
|
'''
|
|
|
|
Tests for the file state
|
|
|
|
'''
|
|
|
|
import os
|
2012-08-05 18:23:12 +00:00
|
|
|
import shutil
|
2012-05-13 04:31:58 +00:00
|
|
|
import integration
|
|
|
|
|
2012-05-29 16:40:20 +00:00
|
|
|
|
2012-05-13 04:31:58 +00:00
|
|
|
class FileTest(integration.ModuleCase):
|
|
|
|
'''
|
|
|
|
Validate the file state
|
|
|
|
'''
|
2012-08-05 18:23:12 +00:00
|
|
|
|
2012-05-13 04:31:58 +00:00
|
|
|
def test_symlink(self):
|
|
|
|
'''
|
|
|
|
file.symlink
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'symlink')
|
|
|
|
tgt = os.path.join(integration.TMP, 'target')
|
|
|
|
ret = self.run_state('file.symlink', name=name, target=tgt)
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 04:31:58 +00:00
|
|
|
self.assertTrue(result)
|
2012-05-13 04:41:47 +00:00
|
|
|
|
|
|
|
def test_test_symlink(self):
|
|
|
|
'''
|
|
|
|
file.symlink test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'symlink')
|
|
|
|
tgt = os.path.join(integration.TMP, 'target')
|
|
|
|
ret = self.run_state('file.symlink', test=True, name=name, target=tgt)
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 04:41:47 +00:00
|
|
|
self.assertIsNone(result)
|
|
|
|
|
2012-05-13 04:55:28 +00:00
|
|
|
def test_absent_file(self):
|
|
|
|
'''
|
|
|
|
file.absent
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'file_to_kill')
|
|
|
|
with open(name, 'w+') as fp_:
|
|
|
|
fp_.write('killme')
|
|
|
|
ret = self.run_state('file.absent', name=name)
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 04:55:28 +00:00
|
|
|
self.assertTrue(result)
|
|
|
|
self.assertFalse(os.path.isfile(name))
|
|
|
|
|
|
|
|
def test_absent_dir(self):
|
|
|
|
'''
|
|
|
|
file.absent
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'dir_to_kill')
|
2012-08-25 13:36:30 +00:00
|
|
|
if not os.path.isdir(name):
|
|
|
|
# left behind... Don't fail because of this!
|
|
|
|
os.makedirs(name)
|
2012-05-13 04:55:28 +00:00
|
|
|
ret = self.run_state('file.absent', name=name)
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 04:55:28 +00:00
|
|
|
self.assertTrue(result)
|
|
|
|
self.assertFalse(os.path.isdir(name))
|
2012-05-13 14:18:05 +00:00
|
|
|
|
2012-05-13 04:55:28 +00:00
|
|
|
def test_absent_link(self):
|
|
|
|
'''
|
|
|
|
file.absent
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'link_to_kill')
|
Reverted back the `--key-logfile` deprecation.
On the parsers cleanup branch, I though about making the available options as equal as possible. Since the log_file setting for the master and the minion could be separately set, I made the logfile option for the key also log_file. Yet, since there's no key specific config file, it shares master, one could only specify the log_file settings from the cli, it could not be hardcoded on the config file, unless, we made a separate config file just for key.
So, in order to reduce required changes, and keep it all as backwards compatible as possible, the key log file options is now, once again, `--key-logfile` which will allow us to hardcode it in the masters config file as `key_logfile`.
This will also, hopefully make travis behave better too.
2012-08-08 00:08:38 +00:00
|
|
|
if not os.path.islink('{0}.tgt'.format(name)):
|
|
|
|
os.symlink(name, '{0}.tgt'.format(name))
|
2012-05-13 04:55:28 +00:00
|
|
|
ret = self.run_state('file.absent', name=name)
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 04:55:28 +00:00
|
|
|
self.assertTrue(result)
|
|
|
|
self.assertFalse(os.path.islink(name))
|
Reverted back the `--key-logfile` deprecation.
On the parsers cleanup branch, I though about making the available options as equal as possible. Since the log_file setting for the master and the minion could be separately set, I made the logfile option for the key also log_file. Yet, since there's no key specific config file, it shares master, one could only specify the log_file settings from the cli, it could not be hardcoded on the config file, unless, we made a separate config file just for key.
So, in order to reduce required changes, and keep it all as backwards compatible as possible, the key log file options is now, once again, `--key-logfile` which will allow us to hardcode it in the masters config file as `key_logfile`.
This will also, hopefully make travis behave better too.
2012-08-08 00:08:38 +00:00
|
|
|
if os.path.islink('{0}.tgt'.format(name)):
|
|
|
|
os.unlink('{0}.tgt'.format(name))
|
2012-05-13 04:58:26 +00:00
|
|
|
|
2012-05-13 05:19:39 +00:00
|
|
|
def test_test_absent(self):
|
2012-05-13 04:58:26 +00:00
|
|
|
'''
|
|
|
|
file.absent test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'file_to_kill')
|
|
|
|
with open(name, 'w+') as fp_:
|
|
|
|
fp_.write('killme')
|
|
|
|
ret = self.run_state('file.absent', test=True, name=name)
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 04:58:26 +00:00
|
|
|
self.assertIsNone(result)
|
|
|
|
self.assertTrue(os.path.isfile(name))
|
2012-08-05 18:23:12 +00:00
|
|
|
os.remove(name)
|
2012-05-13 05:19:39 +00:00
|
|
|
|
|
|
|
def test_managed(self):
|
|
|
|
'''
|
|
|
|
file.managed
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'grail_scene33')
|
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.managed', name=name, source='salt://grail/scene33'
|
|
|
|
)
|
2012-05-13 05:19:39 +00:00
|
|
|
src = os.path.join(
|
2012-08-05 18:23:12 +00:00
|
|
|
integration.FILES, 'file', 'base', 'grail', 'scene33'
|
|
|
|
)
|
2012-05-13 05:19:39 +00:00
|
|
|
with open(src, 'r') as fp_:
|
|
|
|
master_data = fp_.read()
|
|
|
|
with open(name, 'r') as fp_:
|
|
|
|
minion_data = fp_.read()
|
|
|
|
self.assertEqual(master_data, minion_data)
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 05:19:39 +00:00
|
|
|
self.assertTrue(result)
|
|
|
|
|
2012-05-13 05:27:00 +00:00
|
|
|
def test_test_managed(self):
|
|
|
|
'''
|
|
|
|
file.managed test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'grail_not_scene33')
|
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.managed', test=True, name=name, source='salt://grail/scene33'
|
|
|
|
)
|
2012-05-13 05:27:00 +00:00
|
|
|
self.assertFalse(os.path.isfile(name))
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 05:27:00 +00:00
|
|
|
self.assertIsNone(result)
|
2012-05-13 05:38:24 +00:00
|
|
|
|
|
|
|
def test_directory(self):
|
|
|
|
'''
|
|
|
|
file.directory
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'a_new_dir')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.directory', name=name)
|
2012-05-13 05:38:24 +00:00
|
|
|
self.assertTrue(os.path.isdir(name))
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 05:38:24 +00:00
|
|
|
self.assertTrue(result)
|
|
|
|
|
2012-05-13 06:27:00 +00:00
|
|
|
def test_test_directory(self):
|
|
|
|
'''
|
|
|
|
file.directory
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'a_not_dir')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.directory', test=True, name=name)
|
2012-05-13 06:27:00 +00:00
|
|
|
self.assertFalse(os.path.isdir(name))
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 06:27:00 +00:00
|
|
|
self.assertIsNone(result)
|
2012-05-13 06:54:28 +00:00
|
|
|
|
|
|
|
def test_recurse(self):
|
|
|
|
'''
|
|
|
|
file.recurse
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'recurse_dir')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.recurse', name=name, source='salt://grail')
|
2012-05-13 06:54:28 +00:00
|
|
|
self.assertTrue(os.path.isfile(os.path.join(name, '36', 'scene')))
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 06:54:28 +00:00
|
|
|
self.assertTrue(result)
|
2012-08-05 18:23:12 +00:00
|
|
|
shutil.rmtree(name, ignore_errors=True)
|
2012-05-13 06:54:28 +00:00
|
|
|
|
2012-05-13 07:02:05 +00:00
|
|
|
def test_test_recurse(self):
|
|
|
|
'''
|
|
|
|
file.recurse test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'recurse_test_dir')
|
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.recurse', test=True, name=name, source='salt://grail',
|
|
|
|
)
|
2012-05-13 07:02:05 +00:00
|
|
|
self.assertFalse(os.path.isfile(os.path.join(name, '36', 'scene')))
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 07:02:05 +00:00
|
|
|
self.assertIsNone(result)
|
2012-08-05 18:23:12 +00:00
|
|
|
os.removedirs(name)
|
2012-05-13 07:02:05 +00:00
|
|
|
|
2012-05-13 07:20:28 +00:00
|
|
|
def test_sed(self):
|
|
|
|
'''
|
|
|
|
file.sed
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'sed_test')
|
|
|
|
with open(name, 'w+') as fp_:
|
|
|
|
fp_.write('change_me')
|
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.sed', name=name, before='change', after='salt'
|
|
|
|
)
|
2012-05-13 07:20:28 +00:00
|
|
|
with open(name, 'r') as fp_:
|
|
|
|
self.assertIn('salt', fp_.read())
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 07:20:28 +00:00
|
|
|
self.assertTrue(result)
|
2012-08-05 18:23:12 +00:00
|
|
|
os.remove(name)
|
2012-05-13 07:24:26 +00:00
|
|
|
|
|
|
|
def test_test_sed(self):
|
|
|
|
'''
|
|
|
|
file.sed test integration
|
|
|
|
'''
|
2012-05-13 07:35:01 +00:00
|
|
|
name = os.path.join(integration.TMP, 'sed_test_test')
|
2012-05-13 07:24:26 +00:00
|
|
|
with open(name, 'w+') as fp_:
|
|
|
|
fp_.write('change_me')
|
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.sed', test=True, name=name, before='change', after='salt'
|
|
|
|
)
|
2012-05-13 07:24:26 +00:00
|
|
|
with open(name, 'r') as fp_:
|
|
|
|
self.assertIn('change', fp_.read())
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 07:24:26 +00:00
|
|
|
self.assertIsNone(result)
|
2012-08-05 18:23:12 +00:00
|
|
|
os.remove(name)
|
2012-05-13 07:35:01 +00:00
|
|
|
|
|
|
|
def test_comment(self):
|
|
|
|
'''
|
|
|
|
file.comment
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'comment_test')
|
2012-05-25 06:38:57 +00:00
|
|
|
# write a line to file
|
2012-05-13 07:35:01 +00:00
|
|
|
with open(name, 'w+') as fp_:
|
|
|
|
fp_.write('comment_me')
|
2012-05-25 06:38:57 +00:00
|
|
|
# comment once
|
|
|
|
_ret = self.run_state('file.comment', name=name, regex='^comment')
|
|
|
|
# line is commented
|
2012-05-13 07:35:01 +00:00
|
|
|
with open(name, 'r') as fp_:
|
2012-05-25 06:38:57 +00:00
|
|
|
self.assertTrue(fp_.read().startswith('#comment'))
|
|
|
|
# result is positive
|
2012-06-04 22:40:34 +00:00
|
|
|
ret = list(_ret.values())[0]
|
2012-05-25 06:38:57 +00:00
|
|
|
self.assertTrue(ret['result'], ret)
|
|
|
|
# comment twice
|
|
|
|
_ret = self.run_state('file.comment', name=name, regex='^comment')
|
|
|
|
# line is still commented
|
|
|
|
with open(name, 'r') as fp_:
|
|
|
|
self.assertTrue(fp_.read().startswith('#comment'))
|
|
|
|
# result is still positive
|
2012-06-04 22:40:34 +00:00
|
|
|
ret = list(_ret.values())[0]
|
2012-05-25 06:38:57 +00:00
|
|
|
self.assertTrue(ret['result'], ret)
|
2012-08-05 18:23:12 +00:00
|
|
|
os.remove(name)
|
2012-05-13 07:38:31 +00:00
|
|
|
|
|
|
|
def test_test_comment(self):
|
|
|
|
'''
|
|
|
|
file.comment test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'comment_test_test')
|
|
|
|
with open(name, 'w+') as fp_:
|
|
|
|
fp_.write('comment_me')
|
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.comment', test=True, name=name, regex='.*comment.*',
|
|
|
|
)
|
2012-05-13 07:38:31 +00:00
|
|
|
with open(name, 'r') as fp_:
|
|
|
|
self.assertNotIn('#comment', fp_.read())
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 07:38:31 +00:00
|
|
|
self.assertIsNone(result)
|
2012-08-05 18:23:12 +00:00
|
|
|
os.remove(name)
|
2012-05-13 07:54:20 +00:00
|
|
|
|
|
|
|
def test_uncomment(self):
|
|
|
|
'''
|
|
|
|
file.uncomment
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'uncomment_test')
|
|
|
|
with open(name, 'w+') as fp_:
|
|
|
|
fp_.write('#comment_me')
|
2012-05-25 06:38:57 +00:00
|
|
|
ret = self.run_state('file.uncomment', name=name, regex='^comment')
|
2012-05-13 07:54:20 +00:00
|
|
|
with open(name, 'r') as fp_:
|
|
|
|
self.assertNotIn('#comment', fp_.read())
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 07:54:20 +00:00
|
|
|
self.assertTrue(result)
|
2012-08-05 18:23:12 +00:00
|
|
|
os.remove(name)
|
2012-05-13 07:54:20 +00:00
|
|
|
|
|
|
|
def test_test_uncomment(self):
|
|
|
|
'''
|
|
|
|
file.comment test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'uncomment_test_test')
|
|
|
|
with open(name, 'w+') as fp_:
|
|
|
|
fp_.write('#comment_me')
|
|
|
|
ret = self.run_state(
|
2012-08-05 18:23:12 +00:00
|
|
|
'file.uncomment', test=True, name=name, regex='^comment.*'
|
|
|
|
)
|
2012-05-13 07:54:20 +00:00
|
|
|
with open(name, 'r') as fp_:
|
|
|
|
self.assertIn('#comment', fp_.read())
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 07:54:20 +00:00
|
|
|
self.assertIsNone(result)
|
2012-08-05 18:23:12 +00:00
|
|
|
os.remove(name)
|
2012-05-13 08:00:01 +00:00
|
|
|
|
|
|
|
def test_append(self):
|
|
|
|
'''
|
|
|
|
file.append
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'append_test')
|
|
|
|
with open(name, 'w+') as fp_:
|
|
|
|
fp_.write('#salty!')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.append', name=name, text='cheese')
|
2012-05-13 08:00:01 +00:00
|
|
|
with open(name, 'r') as fp_:
|
|
|
|
self.assertIn('cheese', fp_.read())
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 08:00:01 +00:00
|
|
|
self.assertTrue(result)
|
2012-08-05 18:23:12 +00:00
|
|
|
os.remove(name)
|
2012-05-13 08:00:01 +00:00
|
|
|
|
2012-05-13 08:02:20 +00:00
|
|
|
def test_test_append(self):
|
|
|
|
'''
|
|
|
|
file.append test interface
|
|
|
|
'''
|
2012-05-13 08:06:46 +00:00
|
|
|
name = os.path.join(integration.TMP, 'append_test_test')
|
2012-05-13 08:02:20 +00:00
|
|
|
with open(name, 'w+') as fp_:
|
|
|
|
fp_.write('#salty!')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.append', test=True, name=name, text='cheese')
|
2012-05-13 08:02:20 +00:00
|
|
|
with open(name, 'r') as fp_:
|
|
|
|
self.assertNotIn('cheese', fp_.read())
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 08:02:20 +00:00
|
|
|
self.assertIsNone(result)
|
2012-08-05 18:23:12 +00:00
|
|
|
os.remove(name)
|
2012-05-13 08:06:46 +00:00
|
|
|
|
|
|
|
def test_touch(self):
|
|
|
|
'''
|
|
|
|
file.touch
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'touch_test')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.touch', name=name)
|
2012-05-13 08:06:46 +00:00
|
|
|
self.assertTrue(os.path.isfile(name))
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 08:06:46 +00:00
|
|
|
self.assertTrue(result)
|
2012-08-05 18:23:12 +00:00
|
|
|
os.remove(name)
|
2012-05-13 08:06:46 +00:00
|
|
|
|
2012-05-13 08:07:54 +00:00
|
|
|
def test_test_touch(self):
|
|
|
|
'''
|
|
|
|
file.touch test interface
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'touch_test')
|
2012-08-05 18:23:12 +00:00
|
|
|
ret = self.run_state('file.touch', test=True, name=name)
|
2012-05-13 08:07:54 +00:00
|
|
|
self.assertFalse(os.path.isfile(name))
|
2012-06-01 18:03:17 +00:00
|
|
|
result = ret[next(iter(ret))]['result']
|
2012-05-13 08:07:54 +00:00
|
|
|
self.assertIsNone(result)
|
2012-05-25 06:38:57 +00:00
|
|
|
|
2012-08-05 18:23:12 +00:00
|
|
|
def test_touch_directory(self):
|
|
|
|
'''
|
|
|
|
file.touch a directory
|
|
|
|
'''
|
|
|
|
name = os.path.join(integration.TMP, 'touch_test_dir')
|
|
|
|
try:
|
2012-08-25 13:21:38 +00:00
|
|
|
if not os.path.isdir(name):
|
|
|
|
# left behind... Don't fail because of this!
|
|
|
|
os.makedirs(name)
|
2012-08-05 18:23:12 +00:00
|
|
|
except OSError:
|
2012-08-05 18:31:13 +00:00
|
|
|
self.skipTest("Failed to create directory {0}".format(name))
|
2012-08-05 18:23:12 +00:00
|
|
|
|
|
|
|
self.assertTrue(os.path.isdir(name))
|
|
|
|
ret = self.run_state('file.touch', name=name)
|
|
|
|
result = ret[next(iter(ret))]['result']
|
|
|
|
self.assertTrue(result)
|
|
|
|
self.assertTrue(os.path.isdir(name))
|
|
|
|
os.removedirs(name)
|
|
|
|
|
2012-05-25 06:38:57 +00:00
|
|
|
|
2012-07-20 06:21:01 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(FileTest)
|