mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Test verify logs without a whole minion
This commit is contained in:
parent
a05018e089
commit
effa77de7f
6 changed files with 72 additions and 60 deletions
|
@ -15,7 +15,7 @@ import logging
|
|||
import salt.client.netapi
|
||||
import salt.utils.files
|
||||
import salt.utils.parsers as parsers
|
||||
from salt.utils.verify import check_user, verify_files, verify_log
|
||||
from salt.utils.verify import check_user, verify_log_files, verify_log
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
@ -38,12 +38,10 @@ class SaltAPI(parsers.SaltAPIParser):
|
|||
try:
|
||||
if self.config['verify_env']:
|
||||
logfile = self.config['log_file']
|
||||
if logfile is not None and not logfile.startswith(('tcp://',
|
||||
'udp://',
|
||||
'file://')):
|
||||
if logfile is not None:
|
||||
# Logfile is not using Syslog, verify
|
||||
with salt.utils.files.set_umask(0o027):
|
||||
verify_files([logfile], self.config['user'])
|
||||
verify_log_files([logfile], self.config['user'])
|
||||
except OSError as err:
|
||||
log.exception('Failed to prepare salt environment')
|
||||
self.shutdown(err.errno)
|
||||
|
|
|
@ -28,7 +28,7 @@ import salt.utils.cloud
|
|||
import salt.utils.parsers
|
||||
import salt.utils.user
|
||||
from salt.exceptions import SaltCloudException, SaltCloudSystemExit
|
||||
from salt.utils.verify import check_user, verify_env, verify_files, verify_log
|
||||
from salt.utils.verify import check_user, verify_env, verify_log_files, verify_log
|
||||
|
||||
# Import 3rd-party libs
|
||||
from salt.ext import six
|
||||
|
@ -70,11 +70,9 @@ class SaltCloud(salt.utils.parsers.SaltCloudParser):
|
|||
root_dir=self.config['root_dir'],
|
||||
)
|
||||
logfile = self.config['log_file']
|
||||
if logfile is not None and not logfile.startswith('tcp://') \
|
||||
and not logfile.startswith('udp://') \
|
||||
and not logfile.startswith('file://'):
|
||||
if logfile is not None:
|
||||
# Logfile is not using Syslog, verify
|
||||
verify_files([logfile], salt_master_user)
|
||||
verify_log_files([logfile], salt_master_user)
|
||||
except (IOError, OSError) as err:
|
||||
log.error('Error while verifying the environment: %s', err)
|
||||
sys.exit(err.errno)
|
||||
|
|
|
@ -43,7 +43,7 @@ import salt.utils.xdg
|
|||
import salt.utils.yaml
|
||||
from salt.defaults import DEFAULT_TARGET_DELIM
|
||||
from salt.utils.validate.path import is_writeable
|
||||
from salt.utils.verify import verify_files
|
||||
from salt.utils.verify import verify_log_files
|
||||
import salt.exceptions
|
||||
from salt.ext import six
|
||||
from salt.ext.six.moves import range # pylint: disable=import-error,redefined-builtin
|
||||
|
@ -723,10 +723,10 @@ class LogLevelMixIn(six.with_metaclass(MixInMeta, object)):
|
|||
if self.config['verify_env'] and self.config['log_level'] not in ('quiet', ):
|
||||
# Verify the logfile if it was explicitly set but do not try to
|
||||
# verify the default
|
||||
if logfile is not None and not logfile.startswith(('tcp://', 'udp://', 'file://')):
|
||||
if logfile is not None:
|
||||
# Logfile is not using Syslog, verify
|
||||
with salt.utils.files.set_umask(0o027):
|
||||
verify_files([logfile], self.config['user'])
|
||||
verify_log_files([logfile], self.config['user'])
|
||||
|
||||
if logfile is None:
|
||||
# Use the default setting if the logfile wasn't explicity set
|
||||
|
|
|
@ -35,7 +35,7 @@ import salt.utils.user
|
|||
log = logging.getLogger(__name__)
|
||||
|
||||
ROOT_DIR = 'c:\\salt' if salt.utils.platform.is_windows() else '/'
|
||||
|
||||
DEFAULT_SCHEMES = ['tcp://', 'udp://', 'file://',]
|
||||
|
||||
def zmq_version():
|
||||
'''
|
||||
|
@ -146,6 +146,28 @@ def verify_socket(interface, pub_port, ret_port):
|
|||
return True
|
||||
|
||||
|
||||
def verify_logs_filter(files):
|
||||
to_verify = []
|
||||
for filename in files:
|
||||
verify_file = True
|
||||
for scheme in DEFAULT_SCHEMES:
|
||||
if filename.startswith(scheme):
|
||||
verify_file = False
|
||||
break
|
||||
if verify_file:
|
||||
to_verify.append(filename)
|
||||
return to_verify
|
||||
|
||||
|
||||
def verify_log_files(files, user):
|
||||
'''
|
||||
Verify the log files exist and are owned by the named user. Filenames that
|
||||
begin with tcp:// and udp:// will be filtered out. Filenames that begin
|
||||
with file:// are handled correctly
|
||||
'''
|
||||
return verify_files(verify_logs_filter(files), user)
|
||||
|
||||
|
||||
def verify_files(files, user):
|
||||
'''
|
||||
Verify that the named files exist and are owned by the named user
|
||||
|
|
|
@ -49,51 +49,6 @@ class MinionTest(ShellCase, testprogram.TestProgramCase, ShellCaseCommonTestsMix
|
|||
'subminion',
|
||||
)
|
||||
|
||||
@skipIf(salt.utils.platform.is_darwin(), 'Test is flaky on macosx')
|
||||
@skipIf(salt.utils.platform.is_fedora(), 'Test is flaky on fedora')
|
||||
def test_issue_7754(self):
|
||||
old_cwd = os.getcwd()
|
||||
config_dir = os.path.join(TMP, 'issue-7754')
|
||||
if not os.path.isdir(config_dir):
|
||||
os.makedirs(config_dir)
|
||||
|
||||
os.chdir(config_dir)
|
||||
|
||||
config_file_name = 'minion'
|
||||
pid_path = os.path.join(config_dir, '{0}.pid'.format(config_file_name))
|
||||
with salt.utils.files.fopen(self.get_config_file_path(config_file_name), 'r') as fhr:
|
||||
config = salt.utils.yaml.safe_load(fhr)
|
||||
config['log_file'] = 'file:///tmp/log/LOG_LOCAL3'
|
||||
config['id'] = 'issue-7754'
|
||||
|
||||
with salt.utils.files.fopen(os.path.join(config_dir, config_file_name), 'w') as fhw:
|
||||
salt.utils.yaml.safe_dump(config, fhw, default_flow_style=False)
|
||||
|
||||
ret = self.run_script(
|
||||
self._call_binary_,
|
||||
'--disable-keepalive --config-dir {0} --pid-file {1} -l debug'.format(
|
||||
config_dir,
|
||||
pid_path
|
||||
),
|
||||
timeout=5,
|
||||
catch_stderr=True,
|
||||
with_retcode=True
|
||||
)
|
||||
|
||||
# Now kill it if still running
|
||||
if os.path.exists(pid_path):
|
||||
with salt.utils.files.fopen(pid_path) as fhr:
|
||||
try:
|
||||
os.kill(int(fhr.read()), signal.SIGKILL)
|
||||
except OSError:
|
||||
pass
|
||||
try:
|
||||
self.assertFalse(os.path.isdir(os.path.join(config_dir, 'file:')))
|
||||
finally:
|
||||
self.chdir(old_cwd)
|
||||
if os.path.isdir(config_dir):
|
||||
shutil.rmtree(config_dir)
|
||||
|
||||
def _run_initscript(
|
||||
self,
|
||||
init_script,
|
||||
|
|
|
@ -35,6 +35,7 @@ from tests.support.mock import (
|
|||
|
||||
# Import salt libs
|
||||
import salt.utils.files
|
||||
import salt.utils.platform
|
||||
from salt.utils.verify import (
|
||||
check_user,
|
||||
verify_env,
|
||||
|
@ -44,6 +45,8 @@ from salt.utils.verify import (
|
|||
valid_id,
|
||||
log,
|
||||
verify_log,
|
||||
verify_logs_filter,
|
||||
verify_log_files,
|
||||
)
|
||||
|
||||
# Import 3rd-party libs
|
||||
|
@ -108,7 +111,7 @@ class TestVerify(TestCase):
|
|||
# If there's a different error catch, write it to sys.stderr
|
||||
sys.stderr.write(writer.output)
|
||||
|
||||
@skipIf(sys.platform.startswith('win'), 'No verify_env Windows')
|
||||
@skipIf(salt.utils.platform.is_windows(), 'No verify_env Windows')
|
||||
def test_verify_env(self):
|
||||
root_dir = tempfile.mkdtemp(dir=TMP)
|
||||
var_dir = os.path.join(root_dir, 'var', 'log', 'salt')
|
||||
|
@ -284,3 +287,39 @@ class TestVerify(TestCase):
|
|||
with patch.object(log, 'warning', mock_info):
|
||||
verify_log({'log_level': 'info'})
|
||||
self.assertTrue(mock_info.call_count == 0)
|
||||
|
||||
|
||||
class TestVerifyLog(TestCase):
|
||||
def setUp(self):
|
||||
self.tmpdir = tempfile.mkdtemp()
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tmpdir)
|
||||
|
||||
def test_verify_logs_filter(self):
|
||||
filtered = verify_logs_filter(
|
||||
['udp://foo', 'tcp://bar', '/tmp/foo', 'file://tmp/bar']
|
||||
)
|
||||
assert filtered == ['/tmp/foo'], filtered
|
||||
|
||||
@skipIf(salt.utils.platform.is_windows(), 'Not applicable on Windows')
|
||||
def test_verify_log_files_udp_scheme(self):
|
||||
verify_log_files(['udp://foo'], getpass.getuser())
|
||||
self.assertFalse(os.path.isdir(os.path.join(os.getcwd(), 'udp:')))
|
||||
|
||||
@skipIf(salt.utils.platform.is_windows(), 'Not applicable on Windows')
|
||||
def test_verify_log_files_tcp_scheme(self):
|
||||
verify_log_files(['udp://foo'], getpass.getuser())
|
||||
self.assertFalse(os.path.isdir(os.path.join(os.getcwd(), 'tcp:')))
|
||||
|
||||
@skipIf(salt.utils.platform.is_windows(), 'Not applicable on Windows')
|
||||
def test_verify_log_files_file_scheme(self):
|
||||
verify_log_files(['file://{}'], getpass.getuser())
|
||||
self.assertFalse(os.path.isdir(os.path.join(os.getcwd(), 'file:')))
|
||||
|
||||
@skipIf(salt.utils.platform.is_windows(), 'Not applicable on Windows')
|
||||
def test_verify_log_files(self):
|
||||
path = os.path.join(self.tmpdir, 'foo', 'bar.log')
|
||||
self.assertFalse(os.path.exists(path))
|
||||
verify_log_files([path], getpass.getuser())
|
||||
self.assertTrue(os.path.exists(path))
|
||||
|
|
Loading…
Add table
Reference in a new issue