2016-07-07 20:42:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2016-08-30 21:43:28 +00:00
|
|
|
Tests for the archive state
|
2016-07-07 20:42:27 +00:00
|
|
|
'''
|
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
2016-12-15 03:57:14 +00:00
|
|
|
import errno
|
|
|
|
import logging
|
2016-07-07 20:42:27 +00:00
|
|
|
import os
|
2016-09-12 21:43:06 +00:00
|
|
|
import socket
|
2016-08-30 21:43:28 +00:00
|
|
|
import threading
|
2017-03-20 19:08:17 +00:00
|
|
|
import tornado.httpserver
|
2016-08-30 21:43:28 +00:00
|
|
|
import tornado.ioloop
|
|
|
|
import tornado.web
|
2016-07-07 20:42:27 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
2017-04-03 16:04:09 +00:00
|
|
|
from tests.support.case import ModuleCase
|
|
|
|
from tests.support.paths import FILES
|
2017-04-04 17:57:27 +00:00
|
|
|
from tests.support.helpers import get_unused_localhost_port, skip_if_not_root
|
2017-04-02 16:09:47 +00:00
|
|
|
from tests.support.mixins import SaltReturnAssertsMixin
|
2016-07-07 20:42:27 +00:00
|
|
|
|
|
|
|
# Import salt libs
|
|
|
|
import salt.utils
|
|
|
|
|
2016-12-15 03:57:14 +00:00
|
|
|
# Setup logging
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2016-07-22 23:51:41 +00:00
|
|
|
if salt.utils.is_windows():
|
2017-03-02 19:24:03 +00:00
|
|
|
ARCHIVE_DIR = os.path.join('c:/', 'tmp')
|
2016-07-22 23:51:41 +00:00
|
|
|
else:
|
2016-12-15 03:57:14 +00:00
|
|
|
ARCHIVE_DIR = '/tmp/archive'
|
2016-07-22 23:51:41 +00:00
|
|
|
|
2016-12-15 03:57:14 +00:00
|
|
|
UNTAR_FILE = os.path.join(ARCHIVE_DIR, 'custom/README')
|
2016-08-30 21:43:28 +00:00
|
|
|
ARCHIVE_TAR_HASH = 'md5=7643861ac07c30fe7d2310e9f25ca514'
|
2017-04-03 16:04:09 +00:00
|
|
|
STATE_DIR = os.path.join(FILES, 'file', 'base')
|
2017-01-18 18:03:55 +00:00
|
|
|
|
2016-08-30 21:43:28 +00:00
|
|
|
|
2017-04-03 16:04:09 +00:00
|
|
|
class ArchiveTest(ModuleCase, SaltReturnAssertsMixin):
|
2016-08-30 21:43:28 +00:00
|
|
|
'''
|
2016-09-12 21:43:06 +00:00
|
|
|
Validate the archive state
|
2016-08-30 21:43:28 +00:00
|
|
|
'''
|
|
|
|
@classmethod
|
|
|
|
def webserver(cls):
|
|
|
|
'''
|
|
|
|
method to start tornado
|
|
|
|
static web app
|
|
|
|
'''
|
2017-03-08 19:09:49 +00:00
|
|
|
cls._ioloop = tornado.ioloop.IOLoop()
|
|
|
|
cls._ioloop.make_current()
|
|
|
|
cls._application = tornado.web.Application([(r'/(.*)', tornado.web.StaticFileHandler,
|
|
|
|
{'path': STATE_DIR})])
|
2017-03-11 16:00:28 +00:00
|
|
|
cls._application.listen(cls.server_port)
|
2017-03-08 19:09:49 +00:00
|
|
|
cls._ioloop.start()
|
2016-07-07 20:42:27 +00:00
|
|
|
|
2016-08-30 21:43:28 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2016-09-12 21:43:06 +00:00
|
|
|
'''
|
|
|
|
start tornado app on thread
|
|
|
|
and wait till its running
|
|
|
|
'''
|
2017-03-12 18:05:30 +00:00
|
|
|
cls.server_port = get_unused_localhost_port()
|
2016-08-30 21:43:28 +00:00
|
|
|
cls.server_thread = threading.Thread(target=cls.webserver)
|
|
|
|
cls.server_thread.daemon = True
|
|
|
|
cls.server_thread.start()
|
2017-03-11 16:00:28 +00:00
|
|
|
cls.archive_tar_source = 'http://localhost:{0}/custom.tar.gz'.format(cls.server_port)
|
2016-09-12 21:43:06 +00:00
|
|
|
# check if tornado app is up
|
|
|
|
port_closed = True
|
|
|
|
while port_closed:
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2017-03-11 16:00:28 +00:00
|
|
|
result = sock.connect_ex(('127.0.0.1', cls.server_port))
|
2016-09-12 21:43:06 +00:00
|
|
|
if result == 0:
|
|
|
|
port_closed = False
|
2016-07-07 20:42:27 +00:00
|
|
|
|
2016-08-30 21:43:28 +00:00
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
2017-03-08 19:09:49 +00:00
|
|
|
cls._ioloop.add_callback(cls._ioloop.stop)
|
2016-08-30 21:43:28 +00:00
|
|
|
cls.server_thread.join()
|
2017-03-08 19:09:49 +00:00
|
|
|
for attrname in ('_ioloop', '_application', 'server_thread'):
|
|
|
|
try:
|
|
|
|
delattr(cls, attrname)
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
2016-08-30 21:43:28 +00:00
|
|
|
|
2016-12-15 03:57:14 +00:00
|
|
|
def setUp(self):
|
|
|
|
self._clear_archive_dir()
|
|
|
|
|
|
|
|
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):
|
2016-07-07 20:42:27 +00:00
|
|
|
'''
|
|
|
|
function to check if file was extracted
|
|
|
|
'''
|
2016-12-15 03:57:14 +00:00
|
|
|
log.debug('Checking for extracted file: %s', path)
|
|
|
|
self.assertTrue(os.path.isfile(path))
|
2016-07-07 20:42:27 +00:00
|
|
|
|
|
|
|
def test_archive_extracted_skip_verify(self):
|
|
|
|
'''
|
|
|
|
test archive.extracted with skip_verify
|
|
|
|
'''
|
|
|
|
ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
|
2017-03-11 16:00:28 +00:00
|
|
|
source=self.archive_tar_source, archive_format='tar',
|
2016-07-07 20:42:27 +00:00
|
|
|
skip_verify=True)
|
2016-12-15 03:57:14 +00:00
|
|
|
log.debug('ret = %s', ret)
|
2016-11-16 16:23:39 +00:00
|
|
|
if 'Timeout' in ret:
|
|
|
|
self.skipTest('Timeout talking to local tornado server.')
|
2016-07-07 20:42:27 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2016-12-15 03:57:14 +00:00
|
|
|
self._check_extracted(UNTAR_FILE)
|
2016-07-07 20:42:27 +00:00
|
|
|
|
|
|
|
def test_archive_extracted_with_source_hash(self):
|
|
|
|
'''
|
|
|
|
test archive.extracted without skip_verify
|
2016-07-22 23:51:41 +00:00
|
|
|
only external resources work to check to
|
|
|
|
ensure source_hash is verified correctly
|
2016-07-07 20:42:27 +00:00
|
|
|
'''
|
|
|
|
ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
|
2017-03-11 16:00:28 +00:00
|
|
|
source=self.archive_tar_source, archive_format='tar',
|
2016-07-07 20:42:27 +00:00
|
|
|
source_hash=ARCHIVE_TAR_HASH)
|
2016-12-15 03:57:14 +00:00
|
|
|
log.debug('ret = %s', ret)
|
2016-11-16 16:23:39 +00:00
|
|
|
if 'Timeout' in ret:
|
|
|
|
self.skipTest('Timeout talking to local tornado server.')
|
|
|
|
|
2016-07-07 20:42:27 +00:00
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2016-12-15 03:57:14 +00:00
|
|
|
self._check_extracted(UNTAR_FILE)
|
2016-07-07 20:42:27 +00:00
|
|
|
|
2017-04-04 17:57:27 +00:00
|
|
|
@skip_if_not_root
|
2016-11-30 19:11:41 +00:00
|
|
|
def test_archive_extracted_with_root_user_and_group(self):
|
|
|
|
'''
|
2016-12-15 03:57:14 +00:00
|
|
|
test archive.extracted with user and group set to "root"
|
2016-11-30 19:11:41 +00:00
|
|
|
'''
|
|
|
|
ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
|
2017-03-11 16:00:28 +00:00
|
|
|
source=self.archive_tar_source, archive_format='tar',
|
2016-11-30 19:11:41 +00:00
|
|
|
source_hash=ARCHIVE_TAR_HASH,
|
|
|
|
user='root', group='root')
|
2016-12-15 03:57:14 +00:00
|
|
|
log.debug('ret = %s', ret)
|
|
|
|
if 'Timeout' in ret:
|
|
|
|
self.skipTest('Timeout talking to local tornado server.')
|
|
|
|
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
|
|
|
self._check_extracted(UNTAR_FILE)
|
|
|
|
|
|
|
|
def test_archive_extracted_with_strip_in_options(self):
|
|
|
|
'''
|
|
|
|
test archive.extracted with --strip in options
|
|
|
|
'''
|
|
|
|
ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
|
2017-03-11 16:00:28 +00:00
|
|
|
source=self.archive_tar_source,
|
2016-12-15 03:57:14 +00:00
|
|
|
source_hash=ARCHIVE_TAR_HASH,
|
|
|
|
options='--strip=1',
|
|
|
|
enforce_toplevel=False)
|
|
|
|
log.debug('ret = %s', ret)
|
2016-11-30 19:11:41 +00:00
|
|
|
if 'Timeout' in ret:
|
|
|
|
self.skipTest('Timeout talking to local tornado server.')
|
|
|
|
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
2016-12-15 03:57:14 +00:00
|
|
|
self._check_extracted(os.path.join(ARCHIVE_DIR, 'README'))
|
|
|
|
|
|
|
|
def test_archive_extracted_with_strip_components_in_options(self):
|
|
|
|
'''
|
|
|
|
test archive.extracted with --strip-components in options
|
|
|
|
'''
|
|
|
|
ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
|
2017-03-11 16:00:28 +00:00
|
|
|
source=self.archive_tar_source,
|
2016-12-15 03:57:14 +00:00
|
|
|
source_hash=ARCHIVE_TAR_HASH,
|
|
|
|
options='--strip-components=1',
|
|
|
|
enforce_toplevel=False)
|
|
|
|
log.debug('ret = %s', ret)
|
|
|
|
if 'Timeout' in ret:
|
|
|
|
self.skipTest('Timeout talking to local tornado server.')
|
|
|
|
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
|
|
|
self._check_extracted(os.path.join(ARCHIVE_DIR, 'README'))
|
|
|
|
|
|
|
|
def test_archive_extracted_without_archive_format(self):
|
|
|
|
'''
|
|
|
|
test archive.extracted with no archive_format option
|
|
|
|
'''
|
|
|
|
ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
|
2017-03-11 16:00:28 +00:00
|
|
|
source=self.archive_tar_source,
|
2016-12-15 03:57:14 +00:00
|
|
|
source_hash=ARCHIVE_TAR_HASH)
|
|
|
|
log.debug('ret = %s', ret)
|
|
|
|
if 'Timeout' in ret:
|
|
|
|
self.skipTest('Timeout talking to local tornado server.')
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
|
|
|
self._check_extracted(UNTAR_FILE)
|
2016-11-30 19:11:41 +00:00
|
|
|
|
2017-03-27 16:32:27 +00:00
|
|
|
def test_archive_extracted_with_cmd_unzip_false(self):
|
|
|
|
'''
|
|
|
|
test archive.extracted using use_cmd_unzip argument as false
|
|
|
|
'''
|
|
|
|
|
|
|
|
ret = self.run_state('archive.extracted', name=ARCHIVE_DIR,
|
2017-03-29 15:11:46 +00:00
|
|
|
source=self.archive_tar_source,
|
2017-03-27 16:32:27 +00:00
|
|
|
source_hash=ARCHIVE_TAR_HASH,
|
|
|
|
use_cmd_unzip=False,
|
|
|
|
archive_format='tar')
|
|
|
|
log.debug('ret = %s', ret)
|
|
|
|
if 'Timeout' in ret:
|
|
|
|
self.skipTest('Timeout talking to local tornado server.')
|
|
|
|
self.assertSaltTrueReturn(ret)
|
|
|
|
|
|
|
|
self._check_extracted(UNTAR_FILE)
|