123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- # -*- coding: utf-8 -*-
- # Import python libs
- from __future__ import absolute_import, print_function, unicode_literals
- import os
- import sys
- import hashlib
- import logging
- # Import Salt Testing libs
- from tests.support.runtime import RUNTIME_VARS
- # Import 3rd party libs
- import pytest
- import salt.ext.six as six
- # Import salt libs
- import salt.utils.files
- import salt.utils.path
- import salt.utils.platform
- import salt.utils.stringutils
- log = logging.getLogger(__name__)
- SSL3_SUPPORT = sys.version_info >= (2, 7, 9)
- def test_get_file(modules):
- '''
- cp.get_file
- '''
- with pytest.helpers.temp_file('scene33') as path:
- ret = modules.cp.get_file('salt://grail/scene33', path)
- assert ret == path
- assert os.path.isfile(path)
- with salt.utils.files.fopen(path, 'r') as scene:
- data = salt.utils.stringutils.to_unicode(scene.read())
- assert 'KNIGHT: They\'re nervous, sire.' in data
- assert 'bacon' not in data
- def test_get_file_to_dir(modules):
- '''
- cp.get_file
- '''
- with pytest.helpers.temp_directory() as tgt:
- path = os.path.join(tgt, 'scene33')
- ret = modules.cp.get_file('salt://grail/scene33', tgt)
- assert ret == path
- assert os.path.isfile(path)
- with salt.utils.files.fopen(path, 'r') as scene:
- data = salt.utils.stringutils.to_unicode(scene.read())
- assert 'KNIGHT: They\'re nervous, sire.' in data
- assert 'bacon' not in data
- def test_get_file_templated_paths(modules):
- '''
- cp.get_file
- '''
- with pytest.helpers.temp_file('cheese') as path:
- ret = modules.cp.get_file('salt://{{grains.test_grain}}',
- path.replace('cheese', '{{grains.test_grain}}'),
- template='jinja')
- assert ret == path
- assert os.path.isfile(path)
- with salt.utils.files.fopen(path, 'r') as scene:
- data = salt.utils.stringutils.to_unicode(scene.read())
- assert 'Gromit' in data
- assert 'bacon' not in data
- def test_get_file_gzipped(modules):
- '''
- cp.get_file
- '''
- src = os.path.join(RUNTIME_VARS.FILES, 'file', 'base', 'file.big')
- with salt.utils.files.fopen(src, 'rb') as fp_:
- hash_str = hashlib.md5(fp_.read()).hexdigest()
- with pytest.helpers.temp_file('scene33') as path:
- ret = modules.cp.get_file('salt://file.big', path, gzip=5)
- assert ret == path
- with salt.utils.files.fopen(path, 'rb') as scene:
- data = scene.read()
- assert hashlib.md5(data).hexdigest() == hash_str
- data = salt.utils.stringutils.to_unicode(data)
- assert 'KNIGHT: They\'re nervous, sire.' in data
- assert 'bacon' not in data
- def test_get_file_makedirs(modules):
- '''
- cp.get_file
- '''
- with pytest.helpers.temp_directory('make') as path:
- tgt = os.path.join(path, 'dirs', 'scene33')
- ret = modules.cp.get_file('salt://grail/scene33', tgt, makedirs=True)
- assert ret == tgt
- assert os.path.isfile(tgt)
- with salt.utils.files.fopen(tgt, 'r') as scene:
- data = salt.utils.stringutils.to_unicode(scene.read())
- assert 'KNIGHT: They\'re nervous, sire.' in data
- assert 'bacon' not in data
- def test_get_template(modules):
- '''
- cp.get_template
- '''
- with pytest.helpers.temp_file('scene33') as path:
- ret = modules.cp.get_template('salt://grail/scene33', path, spam='bacon')
- assert ret == path
- assert os.path.isfile(path)
- with salt.utils.files.fopen(path, 'r') as scene:
- data = salt.utils.stringutils.to_unicode(scene.read())
- assert 'bacon' in data
- assert 'spam' not in data
- def test_get_dir(modules):
- '''
- cp.get_dir
- '''
- with pytest.helpers.temp_directory('many') as path:
- ret = modules.cp.get_dir('salt://grail', path)
- assert ret != []
- assert 'grail' in os.listdir(path)
- assert '36' in os.listdir(os.path.join(path, 'grail'))
- assert 'empty' in os.listdir(os.path.join(path, 'grail'))
- assert 'scene' in os.listdir(os.path.join(path, 'grail', '36'))
- def test_get_dir_templated_paths(modules):
- '''
- cp.get_dir
- '''
- with pytest.helpers.temp_directory('many') as path:
- ret = modules.cp.get_dir('salt://{{grains.script}}',
- path.replace('many', '{{grains.alot}}'),
- template='jinja')
- assert ret != []
- assert 'grail' in os.listdir(path)
- assert '36' in os.listdir(os.path.join(path, 'grail'))
- assert 'empty' in os.listdir(os.path.join(path, 'grail'))
- assert 'scene' in os.listdir(os.path.join(path, 'grail', '36'))
- # cp.get_url tests
- def test_get_url(modules):
- '''
- cp.get_url with salt:// source given
- '''
- with pytest.helpers.temp_file('scene33') as path:
- ret = modules.cp.get_url('salt://grail/scene33', path)
- assert ret == path
- assert os.path.isfile(path)
- with salt.utils.files.fopen(path, 'r') as scene:
- data = salt.utils.stringutils.to_unicode(scene.read())
- assert 'KNIGHT: They\'re nervous, sire.' in data
- assert 'bacon' not in data
- def test_get_url_makedirs(modules):
- '''
- cp.get_url
- '''
- with pytest.helpers.temp_directory('make') as path:
- tgt = os.path.join(path, 'dirs', 'scene33')
- ret = modules.cp.get_url('salt://grail/scene33', tgt, makedirs=True)
- assert ret == tgt
- assert os.path.isfile(tgt)
- with salt.utils.files.fopen(tgt, 'r') as scene:
- data = salt.utils.stringutils.to_unicode(scene.read())
- assert 'KNIGHT: They\'re nervous, sire.' in data
- assert 'bacon' not in data
- def test_get_url_dest_empty(modules):
- '''
- cp.get_url with salt:// source given and destination omitted.
- '''
- ret = modules.cp.get_url('salt://grail/scene33')
- try:
- assert os.path.isfile(ret)
- with salt.utils.files.fopen(ret, 'r') as scene:
- data = salt.utils.stringutils.to_unicode(scene.read())
- assert 'KNIGHT: They\'re nervous, sire.' in data
- assert 'bacon' not in data
- finally:
- os.unlink(ret)
- def test_get_url_no_dest(modules):
- '''
- cp.get_url with salt:// source given and destination set as None
- '''
- ret = modules.cp.get_url('salt://grail/scene33', None)
- assert isinstance(ret, six.text_type)
- assert 'KNIGHT: They\'re nervous, sire.' in ret
- assert 'bacon' not in ret
- def test_get_url_nonexistent_source(modules):
- '''
- cp.get_url with nonexistent salt:// source given
- '''
- ret = modules.cp.get_url('salt://grail/nonexistent_scene', None)
- assert ret is False
- ret = modules.cp.get_url('salt://grail/nonexistent_scene')
- assert ret is False
- def test_get_url_to_dir(modules):
- '''
- cp.get_url with salt:// source
- '''
- with pytest.helpers.temp_directory() as tgt:
- path = os.path.join(tgt, 'scene33')
- ret = modules.cp.get_url('salt://grail/scene33', tgt)
- assert ret == path
- assert os.path.isfile(path)
- with salt.utils.files.fopen(path, 'r') as scene:
- data = salt.utils.stringutils.to_unicode(scene.read())
- assert 'KNIGHT: They\'re nervous, sire.' in data
- assert 'bacon' not in data
- @pytest.mark.skipif(SSL3_SUPPORT is False, reason='Requires python with SSL3 support')
- @pytest.mark.requires_network
- def test_get_url_https(modules):
- '''
- cp.get_url with https:// source given
- '''
- with pytest.helpers.temp_file('index.html') as path:
- ret = modules.cp.get_url('https://repo.saltstack.com/index.html', path)
- assert ret == path
- with salt.utils.files.fopen(path, 'r') as instructions:
- data = salt.utils.stringutils.to_unicode(instructions.read())
- assert 'Bootstrap' in data
- assert 'Debian' in data
- assert 'Windows' in data
- assert 'AYBABTU' not in data
- @pytest.mark.skipif(SSL3_SUPPORT is False, reason='Requires python with SSL3 support')
- @pytest.mark.requires_network
- def test_get_url_https_empty_dest(modules):
- '''
- cp.get_url with https:// source given and destination omitted.
- '''
- path = modules.cp.get_url('https://repo.saltstack.com/index.html')
- try:
- with salt.utils.files.fopen(path, 'r') as instructions:
- data = salt.utils.stringutils.to_unicode(instructions.read())
- assert 'Bootstrap' in data
- assert 'Debian' in data
- assert 'Windows' in data
- assert 'AYBABTU' not in data
- finally:
- try:
- os.unlink(path)
- except OSError:
- pass
- @pytest.mark.skipif(SSL3_SUPPORT is False, reason='Requires python with SSL3 support')
- @pytest.mark.requires_network
- def test_get_url_https_no_dest(modules):
- '''
- cp.get_url with https:// source given and destination set as None
- '''
- data = modules.cp.get_url('https://repo.saltstack.com/index.html', None)
- assert 'Bootstrap' in data
- assert 'Debian' in data
- assert 'Windows' in data
- assert 'AYBABTU' not in data
- def test_get_url_file(modules):
- '''
- cp.get_url with file:// source given
- '''
- src = os.path.join('file://', RUNTIME_VARS.FILES, 'file', 'base', 'file.big')
- path = modules.cp.get_url(src)
- with salt.utils.files.fopen(path, 'r') as scene:
- data = salt.utils.stringutils.to_unicode(scene.read())
- assert 'KNIGHT: They\'re nervous, sire.' in data
- assert 'bacon' not in data
- def test_get_url_file_no_dest(modules):
- '''
- cp.get_url with file:// source given and destination set as None
- '''
- src = os.path.join('file://', RUNTIME_VARS.FILES, 'file', 'base', 'file.big')
- ret = modules.cp.get_url(src, None)
- assert 'KNIGHT: They\'re nervous, sire.' in ret
- assert 'bacon' not in ret
- # cp.get_file_str tests
- def test_get_file_str_salt(modules):
- '''
- cp.get_file_str with salt:// source given
- '''
- src = 'salt://grail/scene33'
- ret = modules.cp.get_file_str(src)
- assert 'KNIGHT: They\'re nervous, sire.' in ret
- def test_get_file_str_nonexistent_source(modules):
- '''
- cp.get_file_str with nonexistent salt:// source given
- '''
- src = 'salt://grail/nonexistent_scene'
- ret = modules.cp.get_file_str(src)
- assert ret is False
- @pytest.mark.skipif(SSL3_SUPPORT is False, reason='Requires python with SSL3 support')
- @pytest.mark.requires_network
- def test_get_file_str_https(modules):
- '''
- cp.get_file_str with https:// source given
- '''
- src = 'https://repo.saltstack.com/index.html'
- data = modules.cp.get_file_str(src)
- assert 'Bootstrap' in data
- assert 'Debian' in data
- assert 'Windows' in data
- assert 'AYBABTU' not in data
- def test_get_file_str_local(modules):
- '''
- cp.get_file_str with file:// source given
- '''
- src = os.path.join('file://', RUNTIME_VARS.FILES, 'file', 'base', 'file.big')
- ret = modules.cp.get_file_str(src)
- assert 'KNIGHT: They\'re nervous, sire.' in ret
- assert 'bacon' not in ret
- def test_list_states(modules):
- '''
- cp.list_states
- '''
- top_sls_contents = '''
- base:
- '*':
- - core
- '''
- core_sls_contents = '''
- # Goes nowhere, does nothing.
- gndn:
- test.succeed_with_changes
- '''
- with pytest.helpers.temp_state_file('top.sls', top_sls_contents):
- with pytest.helpers.temp_state_file('core.sls', core_sls_contents):
- ret = modules.cp.list_states()
- assert 'top' in ret
- assert 'core' in ret
- def test_envs(modules):
- assert sorted(modules.cp.envs()) == sorted(['base', 'prod'])
|