Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
holmanb committed Jan 8, 2025
1 parent 12b7730 commit 033cecf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
19 changes: 10 additions & 9 deletions cloudinit/signal_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

LOG = logging.getLogger(__name__)

SIG_MESSAGE: Final = "Cloud-init %s received %s, exiting\n"
SIG_MESSAGE: Final = "Cloud-init {} received {}, exiting\n"
BACK_FRAME_TRACE_DEPTH: Final = 3
SIGNALS: Final[Dict[int, str]] = {
signal.SIGINT: "Cloud-init %(version)s received SIGINT, exiting",
Expand All @@ -35,7 +35,7 @@ class ExitBehavior(NamedTuple):

SIGNAL_EXIT_BEHAVIOR_CRASH: Final = ExitBehavior(1, logging.ERROR)
SIGNAL_EXIT_BEHAVIOR_QUIET: Final = ExitBehavior(0, logging.INFO)
SIGNAL_EXIT_BEHAVIOR = SIGNAL_EXIT_BEHAVIOR_CRASH
_SIGNAL_EXIT_BEHAVIOR = SIGNAL_EXIT_BEHAVIOR_CRASH


def inspect_handler(sig: Union[int, Callable, None]) -> None:
Expand Down Expand Up @@ -73,13 +73,14 @@ def _pprint_frame(frame, depth, max_depth, contents):


def _handle_exit(signum, frame):
msg = SIG_MESSAGE
contents = StringIO(msg.format(vr.version_string(), signum.name))
# in practice we always receive a Signals object but int is possible
name = signum.name if isinstance(signum, signal.Signals) else signum
contents = StringIO(SIG_MESSAGE.format(vr.version_string(), name))
_pprint_frame(frame, 1, BACK_FRAME_TRACE_DEPTH, contents)
log_util.multi_log(
contents.getvalue(), log=LOG, log_level=SIGNAL_EXIT_BEHAVIOR.log_level
contents.getvalue(), log=LOG, log_level=_SIGNAL_EXIT_BEHAVIOR.log_level
)
sys.exit(SIGNAL_EXIT_BEHAVIOR.exit_code)
sys.exit(_SIGNAL_EXIT_BEHAVIOR.exit_code)


def attach_handlers():
Expand All @@ -99,7 +100,7 @@ def suspend_crash():
call stack is still printed if signal is received during this context, but
the return code is 0 and no traceback is printed.
"""
global SIGNAL_EXIT_BEHAVIOR
SIGNAL_EXIT_BEHAVIOR = SIGNAL_EXIT_BEHAVIOR_QUIET
global _SIGNAL_EXIT_BEHAVIOR
_SIGNAL_EXIT_BEHAVIOR = SIGNAL_EXIT_BEHAVIOR_QUIET
yield
SIGNAL_EXIT_BEHAVIOR = SIGNAL_EXIT_BEHAVIOR_CRASH
_SIGNAL_EXIT_BEHAVIOR = SIGNAL_EXIT_BEHAVIOR_CRASH
2 changes: 1 addition & 1 deletion tests/integration_tests/integration_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
# Install from a PPA. It MUST start with 'ppa:'
# <file path>
# A path to a valid package to be uploaded and installed
CLOUD_INIT_SOURCE = "NONE"
CLOUD_INIT_SOURCE = "IN_PLACE"

# cloud-init metapackage to install
# Examples: cloud-init, cloud-init-base, cloud-init-smart-os
Expand Down
27 changes: 27 additions & 0 deletions tests/unittests/test_signal_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""cloudinit.signal_handler tests"""

import inspect
import signal
from unittest.mock import Mock, patch

import pytest

from cloudinit import signal_handler


@patch.object(signal_handler.sys, "exit", Mock())
class TestSignalHandler:

@pytest.mark.parametrize(
"m_args",
[
(signal.SIGINT, inspect.currentframe()),
(9, None),
(signal.SIGTERM, None),
(1, inspect.currentframe()),
],
)
def test_suspend_signal(self, m_args):
sig, frame = m_args
with signal_handler.suspend_crash():
signal_handler._handle_exit(sig, frame)

0 comments on commit 033cecf

Please sign in to comment.