Skip to content

Commit

Permalink
Merge pull request #404 from gboutry/fix/check-clusterd-status
Browse files Browse the repository at this point in the history
Check if clusterd service is up before checking socket group
  • Loading branch information
hemanthnakkina authored Jan 13, 2025
2 parents f94cd6c + 44d0817 commit 11c767e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
26 changes: 23 additions & 3 deletions sunbeam-python/sunbeam/core/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

LOG = logging.getLogger(__name__)

CLUSTERD_SERVICE = "clusterd"


def run_preflight_checks(checks: Sequence["Check"], console: Console):
"""Run preflight checks sequentially.
Expand Down Expand Up @@ -235,11 +237,11 @@ class DaemonGroupCheck(Check):
"""Check if user is member of socket group."""

def __init__(self):
snap = Snap()
self.snap = Snap()

self.user = os.environ.get("USER")
self.group = snap.config.get("daemon.group")
self.clusterd_socket = Path(snap.paths.common / "state" / "control.socket")
self.group = self.snap.config.get("daemon.group")
self.clusterd_socket = Path(self.snap.paths.common / "state" / "control.socket")

super().__init__(
"Check for snap_daemon group membership",
Expand All @@ -252,6 +254,24 @@ def run(self) -> bool:
Checks:
- User has access to clusterd socket
"""
services = self.snap.services.list()
if CLUSTERD_SERVICE not in services:
self.message = f"{CLUSTERD_SERVICE} service not detected"
return False

if (clusterd := services[CLUSTERD_SERVICE]) and not clusterd.active:
self.message = (
f"{CLUSTERD_SERVICE.capitalize()} service is not active\n"
"Check status by running:\n"
"\n"
f" snap services {self.snap.name}\n"
"\n"
"Check logs by running:\n"
"\n"
f" sudo snap logs {self.snap.name}.clusterd"
)
return False

if not os.access(self.clusterd_socket, os.W_OK):
self.message = (
"Insufficient permissions to run sunbeam commands\n"
Expand Down
13 changes: 13 additions & 0 deletions sunbeam-python/tests/unit/sunbeam/core/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_run_missing_interface(self, mocker, snap):

class TestDaemonGroupCheck:
def test_run(self, mocker, snap):
snap.services.list.return_value = {"clusterd": Mock(active=True)}
mocker.patch.object(checks, "Snap", return_value=snap)
mocker.patch.object(os, "access", return_value=True)

Expand All @@ -59,6 +60,7 @@ def test_run(self, mocker, snap):
assert result is True

def test_run_no_daemon_socket_access(self, mocker, snap):
snap.services.list.return_value = {"clusterd": Mock(active=True)}
mocker.patch.object(checks, "Snap", return_value=snap)
mocker.patch.object(os, "access", return_value=False)

Expand All @@ -69,6 +71,17 @@ def test_run_no_daemon_socket_access(self, mocker, snap):
assert result is False
assert "Insufficient permissions" in check.message

def test_run_daemon_not_active(self, mocker, snap):
snap.services.list.return_value = {"clusterd": Mock(active=False)}
mocker.patch.object(checks, "Snap", return_value=snap)

check = checks.DaemonGroupCheck()

result = check.run()

assert result is False
assert "Clusterd service is not active" in check.message


class TestLocalShareCheck:
def test_run(self, mocker, snap):
Expand Down

0 comments on commit 11c767e

Please sign in to comment.