Skip to content

Commit

Permalink
k8s-sidecar: add smoke test (chainguard-images#1994)
Browse files Browse the repository at this point in the history
* k8s-sidecar: add smoke test

Signed-off-by: Furkan Türkal <[email protected]>
Co-authored-by: Batuhan <[email protected]>

* fix typo

Signed-off-by: Batuhan Apaydin <[email protected]>

* fix the tests

Signed-off-by: Batuhan Apaydin <[email protected]>

---------

Signed-off-by: Furkan Türkal <[email protected]>
Signed-off-by: Batuhan Apaydin <[email protected]>
Co-authored-by: Batuhan <[email protected]>
  • Loading branch information
Dentrax and developer-guy authored Dec 27, 2023
1 parent e0713eb commit 1c2a816
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
26 changes: 26 additions & 0 deletions images/k8s-sidecar/tests/02-smoke.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

set -o errexit -o nounset -o errtrace -o pipefail -x

tmpdir=$(mktemp -d) && cd "$tmpdir"
curl -sLO https://raw.githubusercontent.com/fabianlee/blogcode/master/prometheus/pod-count-dashboard.yaml
sed -i'' -i 's/namespace: prom//g' pod-count-dashboard.yaml

kubectl apply -n ${NAMESPACE} -f pod-count-dashboard.yaml

PASSWORD=$(kubectl get secret ${NAME}-grafana -n ${NAMESPACE} -o jsonpath='{.data.admin-password}' | base64 --decode)

kubectl port-forward -n ${NAMESPACE} service/${NAME}-grafana 3000:80 &
pid=$!
trap "kill -9 $pid" EXIT

sleep 5

RESPONSE=$(curl -u admin:${PASSWORD} --retry 5 --retry-all-errors "http://localhost:3000/api/search?query=kubernetes%20pod%20count")

if [ "$(echo "$RESPONSE" | jq length)" -eq 0 ]; then
echo "Grafana should have at least one Dashboard, exiting..."
exit 1
fi

echo "$RESPONSE" | jq '.[].title' | grep -i "kubernetes pod count"
52 changes: 51 additions & 1 deletion images/k8s-sidecar/tests/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
terraform {
required_providers {
oci = { source = "chainguard-dev/oci" }
oci = { source = "chainguard-dev/oci" }
helm = { source = "hashicorp/helm" }
}
}

Expand All @@ -12,3 +13,52 @@ data "oci_exec_test" "runs" {
digest = var.digest
script = "${path.module}/01-runs.sh"
}

data "oci_string" "ref" { input = var.digest }

resource "random_pet" "suffix" {}

resource "helm_release" "k8s-sidecar" {
name = "prometheus-${random_pet.suffix.id}"
namespace = "prometheus-${random_pet.suffix.id}"
repository = "https://prometheus-community.github.io/helm-charts"
chart = "kube-prometheus-stack"
create_namespace = true
timeout = 600

set {
name = "grafana.sidecar.image.registry"
value = data.oci_string.ref.registry
}
set {
name = "grafana.sidecar.image.repository"
value = data.oci_string.ref.repo
}
set {
name = "grafana.sidecar.image.tag"
value = data.oci_string.ref.pseudo_tag
}
}

data "oci_exec_test" "smoke" {
digest = var.digest
script = "./02-smoke.sh"
working_dir = path.module
depends_on = [helm_release.k8s-sidecar]

env {
name = "NAME"
value = "prometheus-${random_pet.suffix.id}"
}
env {
name = "NAMESPACE"
value = "prometheus-${random_pet.suffix.id}"
}
}

module "helm_cleanup" {
depends_on = [data.oci_exec_test.smoke]
source = "../../../tflib/helm-cleanup"
name = helm_release.k8s-sidecar.id
namespace = helm_release.k8s-sidecar.namespace
}

0 comments on commit 1c2a816

Please sign in to comment.