Apply fix from #38705 to 2016.3 branch (#39077)

* Apply fix from #38705 to 2016.3 branch

This is a better fix and covers more use cases than the sudo_user one.

* Remove saltenv param from internal state func call

This was probably redundant in the first place, but since state.sls,
state.highstate, etc. accept a saltenv param and the actual state
functions do not, this results in multiple values passed for the saltenv
param.

Remove this argument and let file.get_managed reference __env__
internally.

* Update archive tests to match 2016.11 branch
This commit is contained in:
Erik Johnson 2017-02-03 10:35:49 -06:00 committed by Nicole Thomas
parent da3053ea9b
commit 9de08af950
4 changed files with 54 additions and 56 deletions

View File

@ -100,24 +100,15 @@ def _additions_install_opensuse(**kwargs):
kernel_type = re.sub(
r'^(\d|\.|-)*', '', __grains__.get('kernelrelease', ''))
kernel_devel = 'kernel-{0}-devel'.format(kernel_type)
ret = __salt__['state.single']('pkg.installed', 'devel packages',
pkgs=['make', 'gcc', kernel_devel],
concurrent=bool(__opts__.get('sudo_user')))
return ret
return __states__['pkg.installed'](None, pkgs=['make', 'gcc', kernel_devel])
def _additions_install_ubuntu(**kwargs):
ret = __salt__['state.single']('pkg.installed', 'devel packages',
pkgs=['dkms', ],
concurrent=bool(__opts__.get('sudo_user')))
return ret
return __states__['pkg.installed'](None, pkgs=['dkms', ])
def _additions_install_fedora(**kwargs):
ret = __salt__['state.single']('pkg.installed', 'devel packages',
pkgs=['dkms', 'gcc'],
concurrent=bool(__opts__.get('sudo_user')))
return ret
return __states__['pkg.installed'](None, pkgs=['dkms', 'gcc'])
def _additions_install_linux(mount_point, **kwargs):

View File

@ -343,7 +343,6 @@ def extracted(name,
__env__,
'{0}.{1}'.format(re.sub('[:/\\\\]', '_', if_missing), archive_format))
concurrent = bool(__opts__.get('sudo_user'))
if not source_is_local and not os.path.isfile(filename):
if __opts__['test']:
ret['result'] = None
@ -357,15 +356,12 @@ def extracted(name,
log.debug('%s is not in cache, downloading it', source_match)
file_result = __salt__['state.single']('file.managed',
filename,
source=source_match,
source_hash=source_hash,
makedirs=True,
skip_verify=skip_verify,
saltenv=__env__,
source_hash_name=source_hash_name,
concurrent=concurrent)
file_result = __states__['file.managed'](filename,
source=source_match,
source_hash=source_hash,
makedirs=True,
skip_verify=skip_verify,
source_hash_name=source_hash_name)
log.debug('file.managed: {0}'.format(file_result))
# get value of first key
try:
@ -525,12 +521,10 @@ def extracted(name,
recurse.append('user')
if group:
recurse.append('group')
dir_result = __salt__['state.single']('file.directory',
if_missing,
user=user,
group=group,
recurse=recurse,
concurrent=concurrent)
dir_result = __states__['file.directory'](if_missing,
user=user,
group=group,
recurse=recurse)
log.debug('file.directory: %s', dir_result)
elif os.path.isfile(if_missing):
log.debug('if_missing (%s) is a file, not enforcing user/group '

View File

@ -504,11 +504,9 @@ def loaded(name, tag='latest', source=None, source_hash='', force=False):
return _ret_status(name=name, comment=comment)
tmp_filename = salt.utils.mkstemp()
__salt__['state.single']('file.managed',
name=tmp_filename,
source=source,
source_hash=source_hash,
concurrent=bool(__opts__.get('sudo_user')))
__states__['file.managed'](name=tmp_filename,
source=source,
source_hash=source_hash)
changes = {}
if image_infos['status']:

View File

@ -4,14 +4,15 @@ Tests for the archive state
'''
# Import python libs
from __future__ import absolute_import
import errno
import logging
import os
import shutil
import socket
import threading
import tornado.ioloop
import tornado.web
# Import Salt Testing libs
from salttesting import TestCase
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
@ -19,6 +20,9 @@ ensure_in_syspath('../../')
import integration
import salt.utils
# Setup logging
log = logging.getLogger(__name__)
STATE_DIR = os.path.join(integration.FILES, 'file', 'base')
if salt.utils.is_windows():
ARCHIVE_DIR = os.path.join("c:/", "tmp")
@ -32,11 +36,10 @@ ARCHIVE_TAR_HASH = 'md5=7643861ac07c30fe7d2310e9f25ca514'
STATE_DIR = os.path.join(integration.FILES, 'file', 'base')
class SetupWebServer(TestCase):
class ArchiveTest(integration.ModuleCase,
integration.SaltReturnAssertsMixIn):
'''
Setup and Teardown of Web Server
Only need to set this up once not
before all tests
Validate the archive state
'''
@classmethod
def webserver(cls):
@ -51,34 +54,46 @@ class SetupWebServer(TestCase):
@classmethod
def setUpClass(cls):
'''
start tornado app on thread
and wait till its running
'''
cls.server_thread = threading.Thread(target=cls.webserver)
cls.server_thread.daemon = True
cls.server_thread.start()
# check if tornado app is up
port_closed = True
while port_closed:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1', PORT))
if result == 0:
port_closed = False
@classmethod
def tearDownClass(cls):
tornado.ioloop.IOLoop.instance().stop()
cls.server_thread.join()
def setUp(self):
self._clear_archive_dir()
class ArchiveTest(SetupWebServer,
integration.ModuleCase,
integration.SaltReturnAssertsMixIn):
'''
Validate the archive state
'''
def _check_ext_remove(self, dir, file):
def tearDown(self):
self._clear_archive_dir()
@staticmethod
def _clear_archive_dir():
try:
salt.utils.rm_rf(ARCHIVE_DIR)
except OSError as exc:
if exc.errno != errno.ENOENT:
raise
def _check_extracted(self, path):
'''
function to check if file was extracted
and remove the directory.
'''
# check to see if it extracted
check_dir = os.path.isfile(file)
self.assertTrue(check_dir)
# wipe away dir. Can't do this in teardown
# because it needs to be wiped before each test
shutil.rmtree(dir)
log.debug('Checking for extracted file: %s', path)
self.assertTrue(os.path.isfile(path))
def test_archive_extracted_skip_verify(self):
'''
@ -89,7 +104,7 @@ class ArchiveTest(SetupWebServer,
skip_verify=True)
self.assertSaltTrueReturn(ret)
self._check_ext_remove(ARCHIVE_DIR, UNTAR_FILE)
self._check_extracted(UNTAR_FILE)
def test_archive_extracted_with_source_hash(self):
'''
@ -102,7 +117,7 @@ class ArchiveTest(SetupWebServer,
source_hash=ARCHIVE_TAR_HASH)
self.assertSaltTrueReturn(ret)
self._check_ext_remove(ARCHIVE_DIR, UNTAR_FILE)
self._check_extracted(UNTAR_FILE)
if __name__ == '__main__':