mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Salt api honors --log-file correctly
Make sure we verify the proper path when the --log-file cli options is passed via the commandline. Fixes #59880
This commit is contained in:
parent
3f3010415d
commit
94492d8538
4 changed files with 40 additions and 33 deletions
1
changelog/59880.fixed
Normal file
1
changelog/59880.fixed
Normal file
|
@ -0,0 +1 @@
|
|||
Salt api verifies proper log file path when providing '--log-file' from the cli
|
|
@ -1,4 +1,3 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
salt.cli.api
|
||||
~~~~~~~~~~~~~
|
||||
|
@ -7,12 +6,9 @@
|
|||
|
||||
"""
|
||||
|
||||
# Import Python libs
|
||||
from __future__ import absolute_import, print_function, unicode_literals
|
||||
|
||||
import logging
|
||||
|
||||
# Import Salt libs
|
||||
import salt.client.netapi
|
||||
import salt.utils.files
|
||||
import salt.utils.parsers as parsers
|
||||
|
@ -34,11 +30,11 @@ class SaltAPI(parsers.SaltAPIParser):
|
|||
|
||||
super(YourSubClass, self).prepare()
|
||||
"""
|
||||
super(SaltAPI, self).prepare()
|
||||
super().prepare()
|
||||
|
||||
try:
|
||||
if self.config["verify_env"]:
|
||||
logfile = self.config["log_file"]
|
||||
logfile = self.options.api_logfile
|
||||
if logfile is not None:
|
||||
# Logfile is not using Syslog, verify
|
||||
with salt.utils.files.set_umask(0o027):
|
||||
|
@ -64,7 +60,7 @@ class SaltAPI(parsers.SaltAPIParser):
|
|||
|
||||
NOTE: Run any required code before calling `super()`.
|
||||
"""
|
||||
super(SaltAPI, self).start()
|
||||
super().start()
|
||||
if check_user(self.config["user"]):
|
||||
log.info("The salt-api is starting up")
|
||||
self.api.run()
|
||||
|
@ -79,7 +75,7 @@ class SaltAPI(parsers.SaltAPIParser):
|
|||
exitmsg = msg + exitmsg
|
||||
else:
|
||||
exitmsg = msg.strip()
|
||||
super(SaltAPI, self).shutdown(exitcode, exitmsg)
|
||||
super().shutdown(exitcode, exitmsg)
|
||||
|
||||
def _handle_signals(self, signum, sigframe): # pylint: disable=unused-argument
|
||||
# escalate signal to the process manager processes
|
||||
|
@ -87,4 +83,4 @@ class SaltAPI(parsers.SaltAPIParser):
|
|||
self.api.process_manager.send_signal_to_processes(signum)
|
||||
# kill any remaining processes
|
||||
self.api.process_manager.kill_children()
|
||||
super(SaltAPI, self)._handle_signals(signum, sigframe)
|
||||
super()._handle_signals(signum, sigframe)
|
||||
|
|
34
tests/pytests/functional/cli/test_api.py
Normal file
34
tests/pytests/functional/cli/test_api.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
import pytest
|
||||
from salt.cli.api import SaltAPI
|
||||
from tests.support.mock import patch
|
||||
|
||||
|
||||
@pytest.mark.slow_test
|
||||
def test_start_shutdown():
|
||||
pidfile = tempfile.mktemp()
|
||||
logfile = tempfile.mktemp()
|
||||
api = SaltAPI()
|
||||
with pytest.raises(SystemExit):
|
||||
try:
|
||||
# testing environment will fail if we use default pidfile
|
||||
# overwrite sys.argv so salt-api does not use testing args
|
||||
with patch.object(
|
||||
sys, "argv", [sys.argv[0], "--pid-file", pidfile, "--log-file", logfile]
|
||||
):
|
||||
api.start()
|
||||
assert os.path.isfile(pidfile)
|
||||
assert os.path.isfile(logfile)
|
||||
api.shutdown()
|
||||
finally:
|
||||
try:
|
||||
os.remove(pidfile)
|
||||
except OSError:
|
||||
pass
|
||||
try:
|
||||
os.remove(logfile)
|
||||
except OSError:
|
||||
pass
|
|
@ -1,24 +0,0 @@
|
|||
import os
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from salt.cli.api import SaltAPI
|
||||
from tests.support.mock import patch
|
||||
from tests.support.unit import TestCase
|
||||
|
||||
|
||||
class SaltAPITestCase(TestCase):
|
||||
@pytest.mark.slow_test
|
||||
def test_start_shutdown(self):
|
||||
api = SaltAPI()
|
||||
try:
|
||||
# testing environment will fail if we use default pidfile
|
||||
# overwrite sys.argv so salt-api does not use testing args
|
||||
with patch.object(
|
||||
sys, "argv", [sys.argv[0], "--pid-file", "salt-api-test.pid"]
|
||||
):
|
||||
api.start()
|
||||
self.assertTrue(os.path.isfile("salt-api-test.pid"))
|
||||
os.remove("salt-api-test.pid")
|
||||
finally:
|
||||
self.assertRaises(SystemExit, api.shutdown)
|
Loading…
Add table
Reference in a new issue