Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yanksyoon committed May 8, 2024
1 parent 2bce917 commit 1a34a67
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tests/integration/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import jenkinsapi.jenkins
from juju.application import Application
from juju.model import Model
from juju.unit import Unit

from charm_state import APT_PACKAGES_CONFIG

from .conftest import NUM_AGENT_UNITS, assert_job_success

Expand Down Expand Up @@ -63,3 +66,21 @@ async def test_agent_relation(
assert len(nodes.values()) == NUM_AGENT_UNITS + 1

assert_job_success(jenkins_client, jenkins_agent_application.name, "machine")


async def test_agent_packages(
model: Model,
jenkins_agent_application: Application,
):
"""
arrange: given a jenkins agent application.
act: when the apt-packages configuration is set.
assert: the defined packages are installed.
"""
await jenkins_agent_application.set_config({APT_PACKAGES_CONFIG: "bzr, iputils-ping"})
await model.wait_for_idle()

unit: Unit = jenkins_agent_application.units[0]

assert "/usr/bin/bzr" == await unit.ssh(["which", "bzr"])
assert "/usr/bin/ping" == await unit.ssh(["which", "ping"])
34 changes: 34 additions & 0 deletions tests/unit/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import service
from charm import JenkinsAgentCharm
from charm_state import AGENT_RELATION
from service import JenkinsAgentService, PackageInstallError


@pytest.mark.parametrize(
Expand Down Expand Up @@ -72,6 +73,39 @@ def test_on_install(harness: ops.testing.Harness, monkeypatch: pytest.MonkeyPatc
assert harness.charm.unit.status.name == ops.BlockedStatus.name


def test_on_install_packages_fail(monkeypatch: pytest.MonkeyPatch):
"""
arrange: Given a monkeypatched apt lib that raises an error.
act: when install_apt_packages is called.
assert: PackageInstallError is raised.
"""
monkeypatch.setattr(apt, "add_package", MagicMock(side_effect=apt.PackageNotFoundError))

with pytest.raises(PackageInstallError):
JenkinsAgentService.install_apt_packages(["hello", "world"])


@pytest.mark.parametrize(
"packages",
[
pytest.param(tuple(), id="No packages"),
pytest.param(tuple("hello", "world"), id="Has packages"),
],
)
def test_on_install_packages(monkeypatch: pytest.MonkeyPatch, packages: tuple[str, ...]):
"""
arrange: Given a monkeypatched apt lib and list of packages to install.
act: when install_apt_packages is called.
assert: package install call is made.
"""
monkeypatch.setattr(apt, "add_package", (apt_mock := MagicMock()))

JenkinsAgentService.install_apt_packages(packages)

if packages:
apt_mock.assert_has_calls()


def test_restart_service(
harness: ops.testing.Harness,
monkeypatch: pytest.MonkeyPatch,
Expand Down

0 comments on commit 1a34a67

Please sign in to comment.