Skip to content

Commit

Permalink
test: postbuildscript (#44)
Browse files Browse the repository at this point in the history
* test: postbuildscript

* fix: linting

* fix: bandit tmp dir usage
  • Loading branch information
yanksyoon authored Oct 31, 2023
1 parent 342db0f commit cb27456
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tests/integration/files/postbuildscript_plugin_job_xml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<project>
<description />
<keepDependencies>false</keepDependencies>
<properties />
<scm class="hudson.scm.NullSCM" />
<canRoam>true</canRoam>
<disabled>false</disabled>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<triggers />
<concurrentBuild>false</concurrentBuild>
<builders>
<hudson.tasks.Shell>
<command>echo "hello world!"</command>
<configuredLocalRules />
</hudson.tasks.Shell>
</builders>
<publishers>
<org.jenkinsci.plugins.postbuildscript.PostBuildScript
plugin="postbuildscript@{{ postbuildscript_plugin_version }}">
<config>
<scriptFiles />
<groovyScripts />
<buildSteps>
<org.jenkinsci.plugins.postbuildscript.model.PostBuildStep>
<results>
<string>SUCCESS</string>
</results>
<role>BOTH</role>
<buildSteps>
<hudson.tasks.Shell>
<command>{{ postbuildscript_command }}</command>
<configuredLocalRules />
</hudson.tasks.Shell>
</buildSteps>
<stopOnFailure>false</stopOnFailure>
</org.jenkinsci.plugins.postbuildscript.model.PostBuildStep>
</buildSteps>
<markBuildUnstable>false</markBuildUnstable>
</config>
</org.jenkinsci.plugins.postbuildscript.PostBuildScript>
</publishers>
<buildWrappers />
</project>
37 changes: 37 additions & 0 deletions tests/integration/test_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import jenkinsapi.plugin
import pytest
from jinja2 import Environment, FileSystemLoader
from juju.application import Application
from juju.unit import Unit
from pytest_operator.plugin import OpsTest

from .constants import ALLOWED_PLUGINS, INSTALLED_PLUGINS, REMOVED_PLUGINS
Expand Down Expand Up @@ -182,3 +184,38 @@ async def test_matrix_combinations_parameter_plugin(
assert (
"Configuration Matrix" in test_page
), f"Configuration matrix table not found, {test_page}"


@pytest.mark.usefixtures("app_k8s_agent_related")
async def test_postbuildscript_plugin(
ops_test: OpsTest, unit_web_client: UnitWebClient, jenkins_k8s_agents: Application
):
"""
arrange: given a jenkins charm with postbuildscript plugin installed and related to an agent.
act: when a postbuildscript job that writes a file to a /tmp folder is dispatched.
assert: the file is written on the /tmp folder of the job host.
"""
await install_plugins(
ops_test, unit_web_client.unit, unit_web_client.client, ("postbuildscript",)
)
postbuildscript_plugin: jenkinsapi.plugin.Plugin = unit_web_client.client.plugins[
"postbuildscript"
]
environment = Environment(loader=FileSystemLoader("tests/integration/files/"), autoescape=True)
template = environment.get_template("postbuildscript_plugin_job_xml.j2")
# tmp directory is fine to use for testing purposes since TemporaryFile cannot be used here.
test_output_path = "/tmp/postbuildscript_test.txt" # nosec
test_output = "postbuildscript test"
job_xml = template.render(
postbuildscript_plugin_version=postbuildscript_plugin.version,
postbuildscript_command=f'echo -n "{test_output}" > {test_output_path}',
)
job = unit_web_client.client.create_job("postbuildscript-test-k8s", job_xml)
job.invoke().block_until_complete()

unit: Unit = next(iter(jenkins_k8s_agents.units))
ret, stdout, stderr = await ops_test.juju(
"ssh", "--container", "jenkins-k8s-agent", unit.name, "cat", test_output_path
)
assert ret == 0, f"Failed to scp test output file, {stderr}"
assert stdout == test_output

0 comments on commit cb27456

Please sign in to comment.