Skip to content

Commit

Permalink
refactor: use mungectl generate rather than generating own bytes
Browse files Browse the repository at this point in the history
Signed-off-by: Jason C. Nucciarone <[email protected]>
  • Loading branch information
NucciTheBoss committed Jul 2, 2024
1 parent 9568ad8 commit 60f0c13
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
10 changes: 6 additions & 4 deletions src/slurmhelpers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import base64
import logging
import os
import subprocess
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional

Expand Down Expand Up @@ -165,10 +165,12 @@ def generate_key(self) -> None:
Replicates the daemon autostart feature from the munge Debian package.
"""
logging.info("Generating new secret file for service `munged`.")
if not self.secret_file.exists():
self.secret_file.touch(0o600)
try:
subprocess.check_output(["mungectl", "generate"])
except subprocess.CalledProcessError:
logging.fatal("Failed to generate a new munge key")
raise

self.secret_file.write_bytes(os.urandom(1024))
self._needs_restart(["munged"])

def update_config(self, config: Dict[str, str]) -> None:
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
from unittest.mock import MagicMock, PropertyMock

import pytest
from slurmhelpers.models import Munge, Slurm, Slurmd, Slurmdbd, Slurmrestd
from snaphelpers import Snap, SnapConfig, SnapConfigOptions, SnapServices
from snaphelpers._ctl import ServiceInfo

from slurmhelpers.models import Munge, Slurm, Slurmd, Slurmdbd, Slurmrestd


@pytest.fixture
def env():
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class TestHooks:

def test_install_hook(self, mocker, snap) -> None:
"""Test `install` hook."""
mocker.patch("subprocess.check_output")
mocker.patch("pathlib.Path.chmod")
mocker.patch("pathlib.Path.mkdir")
mocker.patch("pathlib.Path.touch")
Expand Down
32 changes: 17 additions & 15 deletions tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@

"""Test models that wrap the configuration for the bundled daemons."""

import subprocess

import pytest
from slurmhelpers.models import (
_process_down_nodes,
_process_frontend_nodes,
_process_node_sets,
_process_nodes,
_process_partitions,
)
from slurmutils.models import (
DownNodes,
DownNodesList,
Expand All @@ -31,14 +40,6 @@
SlurmdbdConfig,
)

from slurmhelpers.models import (
_process_down_nodes,
_process_frontend_nodes,
_process_node_sets,
_process_nodes,
_process_partitions,
)


class TestBaseModel:
"""Test the `_BaseModel` parent class for data models."""
Expand Down Expand Up @@ -114,15 +115,16 @@ def test_max_thread_count(self, mocker, munge) -> None:
def test_generate_key(self, mocker, munge) -> None:
"""Test `generate_key` method."""
# Generate key when `munge.key` file does not yet exist.
mocker.patch("pathlib.Path.exists", return_value=False)
mocker.patch("pathlib.Path.touch")
mocker.patch("pathlib.Path.write_bytes")
mocker.patch("subprocess.check_output")
munge.generate_key()

# Generate key when `munge.key` file exists.
mocker.patch("pathlib.Path.exists", return_value=True)
mocker.patch("pathlib.Path.write_bytes")
munge.generate_key()
# Fail to generate new `munge.key`.
mocker.patch(
"subprocess.check_output",
side_effect=subprocess.CalledProcessError(0, ["mungectl", "generate"]),
)
with pytest.raises(subprocess.CalledProcessError):
munge.generate_key()

def test_update_config(self, mocker, munge) -> None:
"""Test `update_config` method."""
Expand Down

0 comments on commit 60f0c13

Please sign in to comment.