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

Migrate jenkins to use imagetest_harness_k3s for tests #2499

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
47 changes: 47 additions & 0 deletions images/jenkins/tests/controller/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// default values - these get overridden when the resource is called
variable "values" {
type = any
default = {
namespace = "jenkins"
fullnameOverride = "jenkins"
nameOverride = "jenkins"
create_namespace = true

controller = {
javaOpts = ""
jenkinsOpts = ""
admin = {
createSecret = false
}
installPlugins = false
sidecars = {
configAutoReload = {
enabled = false
}
}
image = {
registry = "cgr.dev"
repository = "chainguard/jenkins"
tag = "latest"
}
}
}
}

module "helm" {
source = "../../../../tflib/imagetest/helm"

namespace = "jenkins"
chart = "jenkins"
repo = "https://charts.jenkins.io"

values = var.values
}

output "install_cmd" {
value = module.helm.install_cmd
}

output "release_name" {
value = module.helm.release_name
}
34 changes: 34 additions & 0 deletions images/jenkins/tests/hello-world.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

# set -euo pipefail

apk add curl openjdk-21-jre-base

export JAVA_HOME=/usr/lib/jvm/java-21-openjdk/

kubectl port-forward -n jenkins svc/jenkins 8080:8080&
sleep 5 # wait for port-forward to settle
curl -s -O "http://localhost:8080/jnlpJars/jenkins-cli.jar"
mv jenkins-cli.jar /tmp/

# define a job
cat /tests/hello-world.xml | \
$JAVA_HOME/bin/java \
-jar /tmp/jenkins-cli.jar \
-s http://127.0.0.1:8080 \
create-job "Hello world"

sleep 5 # wait for port-forward to settle

# run the job
$JAVA_HOME/bin/java \
-jar /tmp/jenkins-cli.jar \
-s http://127.0.0.1:8080 build "Hello world"

sleep 5 # wait for port-forward to settle

# check job success
STATUS=`$JAVA_HOME/bin/java \
-jar /tmp/jenkins-cli.jar \
-s http://127.0.0.1:8080 console "Hello world" |grep 'Finished: SUCCESS'`

20 changes: 20 additions & 0 deletions images/jenkins/tests/hello-world.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<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/>
<buildWrappers/>
</project>
73 changes: 66 additions & 7 deletions images/jenkins/tests/main.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,78 @@
terraform {
required_providers {
oci = { source = "chainguard-dev/oci" }
oci = { source = "chainguard-dev/oci" }
imagetest = { source = "chainguard-dev/imagetest" }
}
}

locals {
namespace = "jenkins"
}

variable "digest" {
description = "The image digest to run tests over."
}

data "oci_exec_test" "version" {
digest = var.digest
script = "docker run --rm $IMAGE_NAME --version"
data "oci_string" "ref" { input = var.digest }

data "imagetest_inventory" "this" {}

resource "imagetest_harness_k3s" "this" {
name = "jenkins"
inventory = data.imagetest_inventory.this

sandbox = {
mounts = [
{
source = path.module
destination = "/tests"
}
]
}
}

data "oci_exec_test" "run-tests" {
digest = var.digest
script = "${path.module}/run-tests.sh"
module "helm_controller" {
source = "./controller"
values = {
namespace = local.namespace
fullnameOverride = local.namespace
nameOverride = local.namespace
create_namespace = true

controller = {
javaOpts = ""
jenkinsOpts = ""
installPlugins = false
admin = {
createSecret = false
}
sidecars = {
configAutoReload = {
enabled = false
}
}
image = {
registry = data.oci_string.ref.registry
repository = data.oci_string.ref.repo
tag = data.oci_string.ref.pseudo_tag
}
}
}
}

resource "imagetest_feature" "this" {
harness = imagetest_harness_k3s.this
name = "jenkins"
description = "Test jenkins functionality"

steps = [
{
name = "Install controller",
cmd = module.helm_controller.install_cmd
},
{
name = "Test running a job",
cmd = "/tests/hello-world.sh"
},
]
}
78 changes: 0 additions & 78 deletions images/jenkins/tests/run-tests.sh

This file was deleted.

Loading