diff --git a/metadata.yaml b/metadata.yaml index 12869e2c..e18d4654 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -33,10 +33,20 @@ tags: containers: jenkins: resource: jenkins-image + mounts: + - storage: jenkins-home + location: /var/lib/jenkins + resources: jenkins-image: type: oci-image description: OCI image for Jenkins + +storage: + jenkins-home: + type: filesystem + location: /var/lib/jenkins + requires: agent-deprecated: interface: jenkins-slave diff --git a/tests/integration/test_jenkins.py b/tests/integration/test_jenkins.py index aeab07b9..c7acbda7 100644 --- a/tests/integration/test_jenkins.py +++ b/tests/integration/test_jenkins.py @@ -4,12 +4,16 @@ """Integration tests for jenkins-k8s-operator charm.""" import typing +from secrets import token_hex import jenkinsapi import pytest +from juju.action import Action +from juju.application import Application +from juju.unit import Unit from pytest_operator.plugin import OpsTest -from .helpers import install_plugins +from .helpers import gen_test_job_xml, install_plugins from .substrings import assert_substrings_not_in_string from .types_ import UnitWebClient @@ -55,3 +59,41 @@ async def test_jenkins_automatic_update_out_of_range( assert unit_web_client.client.has_plugin( extra_plugin ), "additionally installed plugin cleanedup." + + +async def test_storage_mount( + application: Application, + jenkins_client: jenkinsapi.jenkins.Jenkins, +): + """ + arrange: a bare Jenkins charm. + act: Add a job, scale the charm to 0 unit and scale back to 1. + assert: The job configuration persists and is the same as the one used. + """ + test_job_name = token_hex(8) + job_configuration = gen_test_job_xml("built-in") + jenkins_client.create_job(test_job_name, job_configuration) + + await application.scale(scale=0) + await application.model.wait_for_idle( + apps=[application.name], + timeout=20 * 60, + idle_period=30, + wait_for_exact_units=0, + ) + await application.scale(scale=1) + await application.model.wait_for_idle( + apps=[application.name], + timeout=20 * 60, + idle_period=30, + wait_for_exact_units=1, + ) + + jenkins_unit: Unit = application.units[0] + assert jenkins_unit + command = f"cat /var/lib/jenkins/jobs/{test_job_name}/config.xml" + action: Action = await jenkins_unit.run(command=command, timeout=60) + await action.wait() + assert action.results.get("return-code") == 0 + # Remove leading and trailing newline since jenkins client autoformat config + assert job_configuration.strip("\n") in str(action.results.get("stdout"))