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:
Daniel A. Wozniak 2021-03-23 14:09:44 -07:00 committed by Megan Wilhite
parent 3f3010415d
commit 94492d8538
4 changed files with 40 additions and 33 deletions

1
changelog/59880.fixed Normal file
View file

@ -0,0 +1 @@
Salt api verifies proper log file path when providing '--log-file' from the cli

View file

@ -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)

View 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

View file

@ -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)