Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove systemd config on relation departed #22

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/agent_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _on_agent_relation_changed(self, _: ops.RelationChangedEvent) -> None:
def _on_agent_relation_departed(self, _: ops.RelationDepartedEvent) -> None:
"""Handle agent relation departed event."""
try:
self.jenkins_agent_service.stop()
self.jenkins_agent_service.reset()
except service.ServiceStopError:
self.charm.unit.status = ops.BlockedStatus("Error stopping the agent service")
return
Expand Down
6 changes: 4 additions & 2 deletions src/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ def restart(self) -> None:
if not self._startup_check():
raise ServiceRestartError("Error waiting for the agent service to start")

def stop(self) -> None:
"""Stop the agent service.
def reset(self) -> None:
"""Stop the agent service and clear its configuration file.

Raises:
ServiceStopError: if systemctl stop returns a non-zero exit code.
Expand All @@ -175,6 +175,8 @@ def stop(self) -> None:
except systemd.SystemdError as exc:
logger.error("service %s failed to stop", AGENT_SERVICE_NAME)
raise ServiceStopError(f"service {AGENT_SERVICE_NAME} failed to stop") from exc
config_file = Path(f"{SYSTEMD_SERVICE_CONF_DIR}/override.conf")
config_file.unlink(missing_ok=True)

def _startup_check(self) -> bool:
"""Check whether the service was correctly started.
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

"""Test for agent relations."""

import pathlib
from unittest.mock import MagicMock, PropertyMock

import ops.testing
Expand Down Expand Up @@ -141,6 +142,8 @@ def test_agent_relation_departed(
assert: The charm falls into BlockedStatus with the correct message.
"""
monkeypatch.setattr(systemd, "service_stop", MagicMock())
path_unlink_mock = MagicMock()
monkeypatch.setattr(pathlib.Path, "unlink", path_unlink_mock)

harness = harness_with_agent_relation
harness.begin()
Expand All @@ -152,3 +155,4 @@ def test_agent_relation_departed(
charm: JenkinsAgentCharm = harness.charm
assert charm.unit.status.name == ops.BlockedStatus.name
assert charm.unit.status.message == "Waiting for config/relation."
path_unlink_mock.assert_called_once()
Loading