2012-04-04 05:15:37 +00:00
|
|
|
# Import python libs
|
|
|
|
import os
|
2012-04-04 18:00:52 +00:00
|
|
|
import hashlib
|
2012-11-18 18:57:10 +00:00
|
|
|
|
2013-06-27 10:54:59 +00:00
|
|
|
# Import Salt Testing libs
|
|
|
|
from salttesting.helpers import ensure_in_syspath
|
|
|
|
ensure_in_syspath('../../')
|
|
|
|
|
2012-11-18 18:57:10 +00:00
|
|
|
# Import salt libs
|
2013-06-27 10:54:59 +00:00
|
|
|
import integration
|
2012-11-18 18:57:10 +00:00
|
|
|
import salt.utils
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2012-04-04 05:15:37 +00:00
|
|
|
|
|
|
|
class CPModuleTest(integration.ModuleCase):
|
|
|
|
'''
|
|
|
|
Validate the test module
|
|
|
|
'''
|
|
|
|
def test_get_file(self):
|
|
|
|
'''
|
|
|
|
cp.get_file
|
|
|
|
'''
|
|
|
|
tgt = os.path.join(integration.TMP, 'scene33')
|
|
|
|
self.run_function(
|
|
|
|
'cp.get_file',
|
|
|
|
[
|
|
|
|
'salt://grail/scene33',
|
|
|
|
tgt,
|
|
|
|
])
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(tgt, 'r') as scene:
|
2012-04-04 05:15:37 +00:00
|
|
|
data = scene.read()
|
|
|
|
self.assertIn('KNIGHT: They\'re nervous, sire.', data)
|
2012-04-04 05:31:53 +00:00
|
|
|
self.assertNotIn('bacon', data)
|
|
|
|
|
2012-11-07 03:05:59 +00:00
|
|
|
def test_get_file_templated_paths(self):
|
2012-11-07 02:36:10 +00:00
|
|
|
'''
|
|
|
|
cp.get_file
|
|
|
|
'''
|
|
|
|
tgt = os.path.join(integration.TMP, 'cheese')
|
|
|
|
self.run_function(
|
|
|
|
'cp.get_file',
|
|
|
|
[
|
2012-11-07 03:05:59 +00:00
|
|
|
'salt://{{grains.test_grain}}',
|
|
|
|
tgt.replace('cheese', '{{grains.test_grain}}')
|
|
|
|
],
|
|
|
|
template='jinja'
|
|
|
|
)
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(tgt, 'r') as cheese:
|
2012-11-07 02:36:10 +00:00
|
|
|
data = cheese.read()
|
|
|
|
self.assertIn('Gromit', data)
|
|
|
|
self.assertNotIn('bacon', data)
|
|
|
|
|
2012-11-09 00:09:38 +00:00
|
|
|
def test_get_file_gzipped(self):
|
|
|
|
'''
|
|
|
|
cp.get_file
|
|
|
|
'''
|
|
|
|
tgt = os.path.join(integration.TMP, 'file.big')
|
|
|
|
src = os.path.join(integration.FILES, 'file/base/file.big')
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(src, 'r') as fp_:
|
2012-11-09 00:09:38 +00:00
|
|
|
hash = hashlib.md5(fp_.read()).hexdigest()
|
|
|
|
|
|
|
|
self.run_function(
|
|
|
|
'cp.get_file',
|
|
|
|
[
|
|
|
|
'salt://file.big',
|
|
|
|
tgt,
|
|
|
|
],
|
2012-11-15 02:06:53 +00:00
|
|
|
gzip=5
|
2012-11-09 00:09:38 +00:00
|
|
|
)
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(tgt, 'r') as scene:
|
2012-11-09 00:09:38 +00:00
|
|
|
data = scene.read()
|
|
|
|
self.assertIn('KNIGHT: They\'re nervous, sire.', data)
|
|
|
|
self.assertNotIn('bacon', data)
|
|
|
|
self.assertEqual(hash, hashlib.md5(data).hexdigest())
|
|
|
|
|
2012-11-15 02:06:53 +00:00
|
|
|
def test_get_file_makedirs(self):
|
2012-11-15 00:48:26 +00:00
|
|
|
'''
|
|
|
|
cp.get_file
|
|
|
|
'''
|
|
|
|
tgt = os.path.join(integration.TMP, 'make/dirs/scene33')
|
|
|
|
self.run_function(
|
|
|
|
'cp.get_file',
|
|
|
|
[
|
|
|
|
'salt://grail/scene33',
|
|
|
|
tgt,
|
|
|
|
],
|
2012-11-15 02:06:53 +00:00
|
|
|
makedirs=True
|
2012-11-15 00:48:26 +00:00
|
|
|
)
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(tgt, 'r') as scene:
|
2012-11-15 00:48:26 +00:00
|
|
|
data = scene.read()
|
|
|
|
self.assertIn('KNIGHT: They\'re nervous, sire.', data)
|
|
|
|
self.assertNotIn('bacon', data)
|
|
|
|
|
2012-04-04 05:31:53 +00:00
|
|
|
def test_get_template(self):
|
|
|
|
'''
|
|
|
|
cp.get_template
|
|
|
|
'''
|
|
|
|
tgt = os.path.join(integration.TMP, 'scene33')
|
|
|
|
self.run_function(
|
|
|
|
'cp.get_template',
|
|
|
|
[
|
|
|
|
'salt://grail/scene33',
|
|
|
|
tgt,
|
|
|
|
'spam=bacon',
|
|
|
|
])
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(tgt, 'r') as scene:
|
2012-04-04 05:31:53 +00:00
|
|
|
data = scene.read()
|
|
|
|
self.assertIn('bacon', data)
|
|
|
|
self.assertNotIn('spam', data)
|
2012-04-04 05:46:45 +00:00
|
|
|
|
|
|
|
def test_get_dir(self):
|
|
|
|
'''
|
|
|
|
cp.get_dir
|
|
|
|
'''
|
|
|
|
tgt = os.path.join(integration.TMP, 'many')
|
|
|
|
self.run_function(
|
|
|
|
'cp.get_dir',
|
|
|
|
[
|
|
|
|
'salt://grail',
|
|
|
|
tgt
|
|
|
|
])
|
|
|
|
self.assertIn('grail', os.listdir(tgt))
|
|
|
|
self.assertIn('36', os.listdir(os.path.join(tgt, 'grail')))
|
2012-11-15 00:48:26 +00:00
|
|
|
self.assertIn('empty', os.listdir(os.path.join(tgt, 'grail')))
|
|
|
|
self.assertIn('scene', os.listdir(os.path.join(tgt, 'grail', '36')))
|
|
|
|
|
|
|
|
def test_get_dir_templated_paths(self):
|
|
|
|
'''
|
|
|
|
cp.get_dir
|
|
|
|
'''
|
|
|
|
tgt = os.path.join(integration.TMP, 'many')
|
|
|
|
self.run_function(
|
|
|
|
'cp.get_dir',
|
|
|
|
[
|
|
|
|
'salt://{{grains.script}}',
|
|
|
|
tgt.replace('many', '{{grains.alot}}')
|
|
|
|
]
|
|
|
|
)
|
|
|
|
self.assertIn('grail', os.listdir(tgt))
|
|
|
|
self.assertIn('36', os.listdir(os.path.join(tgt, 'grail')))
|
2012-04-04 05:46:45 +00:00
|
|
|
self.assertIn('empty', os.listdir(os.path.join(tgt, 'grail')))
|
|
|
|
self.assertIn('scene', os.listdir(os.path.join(tgt, 'grail', '36')))
|
2012-04-04 05:52:56 +00:00
|
|
|
|
|
|
|
def test_get_url(self):
|
|
|
|
'''
|
|
|
|
cp.get_url
|
|
|
|
'''
|
2013-06-27 10:54:59 +00:00
|
|
|
# We should add a 'if the internet works download some files'
|
2012-04-04 05:52:56 +00:00
|
|
|
tgt = os.path.join(integration.TMP, 'scene33')
|
|
|
|
self.run_function(
|
|
|
|
'cp.get_url',
|
|
|
|
[
|
|
|
|
'salt://grail/scene33',
|
|
|
|
tgt,
|
|
|
|
])
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(tgt, 'r') as scene:
|
2012-04-04 05:52:56 +00:00
|
|
|
data = scene.read()
|
|
|
|
self.assertIn('KNIGHT: They\'re nervous, sire.', data)
|
|
|
|
self.assertNotIn('bacon', data)
|
|
|
|
|
2012-04-04 05:58:34 +00:00
|
|
|
def test_cache_file(self):
|
|
|
|
'''
|
|
|
|
cp.cache_file
|
|
|
|
'''
|
|
|
|
ret = self.run_function(
|
|
|
|
'cp.cache_file',
|
|
|
|
[
|
|
|
|
'salt://grail/scene33',
|
|
|
|
])
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(ret, 'r') as scene:
|
2012-04-04 05:58:34 +00:00
|
|
|
data = scene.read()
|
|
|
|
self.assertIn('KNIGHT: They\'re nervous, sire.', data)
|
|
|
|
self.assertNotIn('bacon', data)
|
2012-04-04 06:06:41 +00:00
|
|
|
|
|
|
|
def test_cache_files(self):
|
|
|
|
'''
|
|
|
|
cp.cache_files
|
|
|
|
'''
|
|
|
|
ret = self.run_function(
|
|
|
|
'cp.cache_files',
|
|
|
|
[
|
2012-05-29 16:40:20 +00:00
|
|
|
['salt://grail/scene33', 'salt://grail/36/scene'],
|
2012-04-04 06:06:41 +00:00
|
|
|
])
|
|
|
|
for path in ret:
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(path, 'r') as scene:
|
2012-04-04 06:06:41 +00:00
|
|
|
data = scene.read()
|
|
|
|
self.assertIn('ARTHUR:', data)
|
|
|
|
self.assertNotIn('bacon', data)
|
2012-04-04 16:13:29 +00:00
|
|
|
|
|
|
|
def test_cache_master(self):
|
|
|
|
'''
|
|
|
|
cp.cache_master
|
|
|
|
'''
|
|
|
|
ret = self.run_function(
|
|
|
|
'cp.cache_master',
|
|
|
|
)
|
|
|
|
for path in ret:
|
|
|
|
self.assertTrue(os.path.exists(path))
|
|
|
|
|
2012-04-04 16:25:02 +00:00
|
|
|
def test_cache_local_file(self):
|
|
|
|
'''
|
|
|
|
cp.cache_local_file
|
|
|
|
'''
|
|
|
|
src = os.path.join(integration.TMP, 'random')
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(src, 'w+') as fn_:
|
2012-04-04 16:25:02 +00:00
|
|
|
fn_.write('foo')
|
|
|
|
ret = self.run_function(
|
|
|
|
'cp.cache_local_file',
|
|
|
|
[src])
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(ret, 'r') as cp_:
|
2012-04-04 16:25:02 +00:00
|
|
|
self.assertEqual(cp_.read(), 'foo')
|
2012-04-04 16:28:57 +00:00
|
|
|
|
|
|
|
def test_list_states(self):
|
|
|
|
'''
|
|
|
|
cp.list_states
|
|
|
|
'''
|
|
|
|
ret = self.run_function(
|
|
|
|
'cp.list_states',
|
|
|
|
)
|
|
|
|
self.assertIn('core', ret)
|
|
|
|
self.assertIn('top', ret)
|
2012-04-04 16:39:45 +00:00
|
|
|
|
|
|
|
def test_list_minion(self):
|
|
|
|
'''
|
|
|
|
cp.list_minion
|
|
|
|
'''
|
|
|
|
self.run_function(
|
|
|
|
'cp.cache_file',
|
|
|
|
[
|
|
|
|
'salt://grail/scene33',
|
|
|
|
])
|
|
|
|
ret = self.run_function('cp.list_minion')
|
|
|
|
found = False
|
|
|
|
for path in ret:
|
|
|
|
if 'grail/scene33' in path:
|
|
|
|
found = True
|
|
|
|
self.assertTrue(found)
|
2012-04-04 16:46:01 +00:00
|
|
|
|
|
|
|
def test_is_cached(self):
|
|
|
|
'''
|
|
|
|
cp.is_cached
|
|
|
|
'''
|
|
|
|
self.run_function(
|
|
|
|
'cp.cache_file',
|
|
|
|
[
|
|
|
|
'salt://grail/scene33',
|
|
|
|
])
|
|
|
|
ret1 = self.run_function(
|
|
|
|
'cp.is_cached',
|
|
|
|
[
|
|
|
|
'salt://grail/scene33',
|
|
|
|
])
|
|
|
|
self.assertTrue(ret1)
|
|
|
|
ret2 = self.run_function(
|
|
|
|
'cp.is_cached',
|
|
|
|
[
|
|
|
|
'salt://fasldkgj/poicxzbn',
|
|
|
|
])
|
|
|
|
self.assertFalse(ret2)
|
2012-04-04 18:00:52 +00:00
|
|
|
|
|
|
|
def test_hash_file(self):
|
|
|
|
'''
|
|
|
|
cp.hash_file
|
|
|
|
'''
|
|
|
|
md5_hash = self.run_function(
|
|
|
|
'cp.hash_file',
|
|
|
|
[
|
|
|
|
'salt://grail/scene33',
|
|
|
|
])
|
|
|
|
path = self.run_function(
|
|
|
|
'cp.cache_file',
|
|
|
|
[
|
|
|
|
'salt://grail/scene33',
|
|
|
|
])
|
2012-11-18 18:57:10 +00:00
|
|
|
with salt.utils.fopen(path, 'r') as fn_:
|
2012-04-04 18:00:52 +00:00
|
|
|
self.assertEqual(
|
|
|
|
md5_hash['hsum'],
|
|
|
|
hashlib.md5(fn_.read()).hexdigest()
|
|
|
|
)
|
2012-05-05 14:09:23 +00:00
|
|
|
|
2013-11-01 19:36:09 +00:00
|
|
|
def test_get_file_from_env_predifined(self):
|
2013-11-01 12:44:56 +00:00
|
|
|
'''
|
|
|
|
cp.get_file
|
|
|
|
'''
|
|
|
|
tgt = os.path.join(integration.TMP, 'cheese')
|
2013-11-01 19:36:09 +00:00
|
|
|
try:
|
|
|
|
self.run_function('cp.get_file', ['salt://cheese', tgt])
|
|
|
|
with salt.utils.fopen(tgt, 'r') as cheese:
|
|
|
|
data = cheese.read()
|
|
|
|
self.assertIn('Gromit', data)
|
|
|
|
self.assertNotIn('Comte', data)
|
|
|
|
finally:
|
|
|
|
os.unlink(tgt)
|
2013-11-01 12:44:56 +00:00
|
|
|
|
2013-11-01 19:36:09 +00:00
|
|
|
def test_get_file_from_env_in_url(self):
|
|
|
|
tgt = os.path.join(integration.TMP, 'cheese')
|
|
|
|
try:
|
|
|
|
self.run_function('cp.get_file', ['salt://cheese?env=prod', tgt])
|
|
|
|
with salt.utils.fopen(tgt, 'r') as cheese:
|
|
|
|
data = cheese.read()
|
|
|
|
self.assertIn('Gromit', data)
|
|
|
|
self.assertIn('Comte', data)
|
|
|
|
finally:
|
|
|
|
os.unlink(tgt)
|
2013-11-01 12:44:56 +00:00
|
|
|
|
2012-07-20 06:21:01 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
from integration import run_tests
|
|
|
|
run_tests(CPModuleTest)
|