Skip to content

Commit

Permalink
Add platform tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pete Wall <pete.wall@grafana.com>
  • Loading branch information
petewall committed Oct 28, 2024
1 parent 880032f commit 3a8f374
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 37 deletions.
79 changes: 79 additions & 0 deletions .github/workflows/platform-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
name: Platform Test
# yamllint disable-line rule:truthy
on:
push:
branches: ["main"]
paths:
- 'charts/**'
- '!charts/k8s-monitoring-v1/**'
pull_request:
paths:
- 'charts/**'
- '!charts/k8s-monitoring-v1/**'

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
list-tests:
name: List tests
runs-on: ubuntu-latest
outputs:
tests: ${{ steps.list_tests.outputs.tests }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: List tests
id: list_tests
run: |
contains(github.event.pull_request.labels.*.name, 'example-label')
if [ "${{ github.event_name }}" == "pull_request" ] && [ "${{contains(github.event.pull_request.labels.*.name, 'run-platform-tests')}}" != "true" ]; then
echo "Skipping platform tests"
exit 0
fi
tests=$(ls charts/k8s-monitoring/tests/platform)
echo "Tests: ${tests}"
echo "tests=$(echo "${tests}" | jq --raw-input --slurp --compact-output 'split("\n") | map(select(. != ""))')" >> "${GITHUB_OUTPUT}"
run-tests:
name: Platform Test
needs: list-tests
runs-on: ubuntu-latest
strategy:
matrix:
test: ${{ fromJson(needs.list-tests.outputs.tests) }}
fail-fast: false
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Helm
uses: azure/setup-helm@v4

- name: Check for cluster config
id: check-cluster-config
run: |
if [ -f "charts/k8s-monitoring/tests/integration/${{ matrix.test }}/cluster.yaml" ]; then
echo "has-cluster-config=true" >> "${GITHUB_OUTPUT}"
else
echo "has-cluster-config=false" >> "${GITHUB_OUTPUT}"
fi
- name: Create kind cluster
if: ${{ steps.check-cluster-config.outputs.has-cluster-config == 'false' }}
uses: helm/kind-action@v1

- name: Create kind cluster with special config
if: ${{ steps.check-cluster-config.outputs.has-cluster-config == 'true' }}
uses: helm/kind-action@v1
with:
config: charts/k8s-monitoring/tests/integration/${{ matrix.test }}/cluster.yaml

- name: Run test
env:
CREATE_CLUSTER: "false"
DEPLOY_GRAFANA: "false"
run: ./scripts/run-integration-test.sh "charts/k8s-monitoring/tests/platform/${{ matrix.test }}"
28 changes: 5 additions & 23 deletions charts/k8s-monitoring-test/.helmignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
docs
schema-mods
Makefile
README.md
README.md.gotmpl
3 changes: 1 addition & 2 deletions charts/k8s-monitoring-test/templates/configmap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ metadata:
data:
queries.json: |-
{
"queries":
{{- index $test "queries" | toPrettyJson | nindent 6 }}
"queries": {{ index $test "queries" | toPrettyJson | nindent 6 }}
}
{{- end }}
2 changes: 2 additions & 0 deletions charts/k8s-monitoring-test/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{{- range $i, $test := .Values.tests }}
{{- if hasKey $test "env" }}
---
apiVersion: v1
kind: Secret
Expand All @@ -10,3 +11,4 @@ stringData:
{{ $key }}: {{ $value | quote }}
{{- end }}
{{- end }}
{{- end }}
3 changes: 3 additions & 0 deletions charts/k8s-monitoring-test/templates/tests/test-pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ spec:
done
exit 1
envFrom:
{{ (index $test "envFrom") | toYaml | indent 8 }}
{{- if hasKey $test "env" }}
- secretRef:
name: {{ include "k8s-monitoring-test.fullname" $ }}-{{ $i }}
{{- end }}
volumeMounts:
- name: queries
mountPath: /etc/test
Expand Down
5 changes: 5 additions & 0 deletions charts/k8s-monitoring/tests/platform/remote-config/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export GRAFANA_CLOUD_FLEET_MGMT_USER=$(op --account grafana.1password.com read "op://Kubernetes Monitoring/helmchart Fleet Management/username")
export GRAFANA_CLOUD_FLEET_MGMT_TOKEN=$(op --account grafana.1password.com read "op://Kubernetes Monitoring/helmchart Fleet Management/password")
export GRAFANA_CLOUD_METRICS_USERNAME=$(op --account grafana.1password.com read "op://Kubernetes Monitoring/helmchart Prometheus/username")
export GRAFANA_CLOUD_LOGS_USERNAME=$(op --account grafana.1password.com read "op://Kubernetes Monitoring/helmchart Loki/username")
export GRAFANA_CLOUD_RW_POLICY_TOKEN=$(op --account grafana.1password.com read "op://Kubernetes Monitoring/helmchart Loki/password")
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: v1
kind: Secret
metadata:
name: grafana-cloud-credentials
stringData:
GRAFANA_CLOUD_FLEET_MGMT_USER: "$GRAFANA_CLOUD_FLEET_MGMT_USER"
GRAFANA_CLOUD_FLEET_MGMT_TOKEN: "$GRAFANA_CLOUD_FLEET_MGMT_TOKEN"
PROMETHEUS_URL: "https://prometheus-prod-13-prod-us-east-0.grafana.net/api/prom/api/v1/query"
PROMETHEUS_USER: "$GRAFANA_CLOUD_METRICS_USERNAME"
PROMETHEUS_PASS: "$GRAFANA_CLOUD_RW_POLICY_TOKEN"
LOKI_USER: "$GRAFANA_CLOUD_LOGS_USERNAME"
LOKI_PASS: "$GRAFANA_CLOUD_RW_POLICY_TOKEN"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
prerequisites:
- type: manifest
name: grafana-cloud-credentials
file: charts/k8s-monitoring/tests/platform/remote-config/grafana-cloud-credentials.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
tests:
- envFrom:
- secretRef:
name: grafana-cloud-credentials
queries:
- query: alloy_build_info{cluster="remote-config-platform-test"}
type: promql
18 changes: 18 additions & 0 deletions charts/k8s-monitoring/tests/platform/remote-config/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
cluster:
name: remote-config-platform-test

alloy-metrics:
enabled: true
remoteConfig:
enabled: true
url: https://fleet-management-prod-008.grafana.net
auth:
type: basic
usernameFrom: env("GRAFANA_CLOUD_FLEET_MGMT_USER")
passwordFrom: env("GRAFANA_CLOUD_FLEET_MGMT_TOKEN")
alloy:
stabilityLevel: public-preview
envFrom:
- secretRef:
name: grafana-cloud-credentials
19 changes: 7 additions & 12 deletions scripts/run-integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ cleanup() {
}
trap cleanup EXIT

runAndEcho() {
echo "$@"
eval "$@"
}

if [ "${CREATE_CLUSTER}" == "true" ]; then
echo "Creating cluster..."
if [ ! -f "${clusterConfig}" ]; then
Expand Down Expand Up @@ -91,9 +86,9 @@ for ((i=0; i<prerequisiteCount; i++)); do
prereqUrl=$(yq -r ".prerequisites[$i].url // \"\"" "${testManifest}")
prereqFile=$(yq -r ".prerequisites[$i].file // \"\"" "${testManifest}")
if [ -n "${prereqUrl}" ]; then
runAndEcho kubectl apply -f "${prereqUrl}" "${namespaceArg}"
kubectl apply -f "${prereqUrl}" ${namespaceArg}
elif [ -n "${prereqFile}" ]; then
runAndEcho kubectl apply -f "${PARENT_DIR}/${prereqFile}" "${namespaceArg}"
envsubst < "${PARENT_DIR}/${prereqFile}" | kubectl apply ${namespaceArg} -f -
else
echo "No URL or file specified for manifest prerequisite"
exit 1
Expand All @@ -105,13 +100,13 @@ for ((i=0; i<prerequisiteCount; i++)); do
prereqValuesFile="$(yq -r ".prerequisites[$i].valuesFile // \"\"" "${testManifest}")"

if [ -n "${prereqValuesFile}" ]; then
runAndEcho helm upgrade --install "${prereqName}" "${namespaceArg}" --create-namespace --repo "${prereqRepo}" "${prereqChart}" -f "${PARENT_DIR}/${prereqValuesFile}" --hide-notes --wait
helm upgrade --install "${prereqName}" ${namespaceArg} --create-namespace --repo "${prereqRepo}" "${prereqChart}" -f "${PARENT_DIR}/${prereqValuesFile}" --hide-notes --wait
elif [ -n "${prereqChart}" ]; then
echo "${prereqValues}" > temp-values.yaml
runAndEcho helm upgrade --install "${prereqName}" "${namespaceArg}" --create-namespace --repo "${prereqRepo}" "${prereqChart}" -f temp-values.yaml --hide-notes --wait
helm upgrade --install "${prereqName}" ${namespaceArg} --create-namespace --repo "${prereqRepo}" "${prereqChart}" -f temp-values.yaml --hide-notes --wait
rm temp-values.yaml
else
runAndEcho helm upgrade --install "${prereqName}" "${namespaceArg}" --create-namespace --repo "${prereqRepo}" "${prereqChart}" --hide-notes --wait
helm upgrade --install "${prereqName}" ${namespaceArg} --create-namespace --repo "${prereqRepo}" "${prereqChart}" --hide-notes --wait
fi
else
echo "Unknown prerequisite type: ${prereqType}"
Expand All @@ -124,6 +119,6 @@ helm upgrade --install k8smon "${PARENT_DIR}/charts/k8s-monitoring" -f "${values

if [ -f "${testValuesFile}" ]; then
echo "Deploying test chart..."
runAndEcho helm upgrade --install k8smon-test "${PARENT_DIR}/charts/k8s-monitoring-test" -f "${testValuesFile}" --wait
runAndEcho helm test k8smon-test --logs
helm upgrade --install k8smon-test "${PARENT_DIR}/charts/k8s-monitoring-test" -f "${testValuesFile}" --wait
helm test k8smon-test --logs
fi

0 comments on commit 3a8f374

Please sign in to comment.