From 674f6ad58939966d2e3a3251e57d46e907a85f9c Mon Sep 17 00:00:00 2001 From: Curtis Vogt Date: Wed, 6 Mar 2024 09:35:24 -0600 Subject: [PATCH] Add integration testing framework --- test/integration/Dockerfile | 22 ++++++ test/integration/Dockerfile.dockerignore | 10 +++ test/integration/chart/k8s-deputy/Chart.yaml | 25 ++++++ .../chart/k8s-deputy/templates/_helpers.tpl | 51 ++++++++++++ .../k8s-deputy/templates/deployment.yaml | 77 ++++++++++++++++++ test/integration/chart/k8s-deputy/values.yaml | 79 +++++++++++++++++++ test/integration/entrypoint.jl | 9 +++ test/integration/skaffold.yaml | 20 +++++ 8 files changed, 293 insertions(+) create mode 100644 test/integration/Dockerfile create mode 100644 test/integration/Dockerfile.dockerignore create mode 100644 test/integration/chart/k8s-deputy/Chart.yaml create mode 100644 test/integration/chart/k8s-deputy/templates/_helpers.tpl create mode 100644 test/integration/chart/k8s-deputy/templates/deployment.yaml create mode 100644 test/integration/chart/k8s-deputy/values.yaml create mode 100644 test/integration/entrypoint.jl create mode 100644 test/integration/skaffold.yaml diff --git a/test/integration/Dockerfile b/test/integration/Dockerfile new file mode 100644 index 0000000..259aef7 --- /dev/null +++ b/test/integration/Dockerfile @@ -0,0 +1,22 @@ +# Expected to be run from the context of the K8sDeputy package root: +# ``` +# docker build -f test/integration/Dockerfile . +# ``` + +ARG JULIA_VERSION=1.9.4 +FROM julia:${JULIA_VERSION}-bookworm + +# Install Julia package registries +RUN julia -e 'using Pkg; Pkg.Registry.add("General")' + +ENV JULIA_PROJECT=/root/K8sDeputy +COPY Project.toml Manifest.toml ${JULIA_PROJECT}/ +RUN julia -e 'using Pkg; Pkg.Registry.update(); Pkg.instantiate(); Pkg.build(); Pkg.precompile(strict=true, timing=true)' + +COPY . ${JULIA_PROJECT} +RUN mv ${JULIA_PROJECT}/test/integration/entrypoint.jl ${JULIA_PROJECT} && \ + rmdir ${JULIA_PROJECT}/test/integration +RUN julia -e 'using Pkg; Pkg.precompile(strict=true, timing=true)' + +WORKDIR ${JULIA_PROJECT} +ENTRYPOINT ["julia", "entrypoint.jl"] diff --git a/test/integration/Dockerfile.dockerignore b/test/integration/Dockerfile.dockerignore new file mode 100644 index 0000000..5b068ea --- /dev/null +++ b/test/integration/Dockerfile.dockerignore @@ -0,0 +1,10 @@ +# Using a .dockerignore file relative to the Dockerfile and not the build context: +# https://docs.docker.com/build/building/context/#filename-and-location + +**/*.md +.git +.github +.gitignore +docs +test/integration +!test/integration/entrypoint.jl diff --git a/test/integration/chart/k8s-deputy/Chart.yaml b/test/integration/chart/k8s-deputy/Chart.yaml new file mode 100644 index 0000000..c4ae6a9 --- /dev/null +++ b/test/integration/chart/k8s-deputy/Chart.yaml @@ -0,0 +1,25 @@ +--- +apiVersion: v2 +name: k8s-deputy +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "0.0.0" diff --git a/test/integration/chart/k8s-deputy/templates/_helpers.tpl b/test/integration/chart/k8s-deputy/templates/_helpers.tpl new file mode 100644 index 0000000..a475705 --- /dev/null +++ b/test/integration/chart/k8s-deputy/templates/_helpers.tpl @@ -0,0 +1,51 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "k8s-deputy.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "k8s-deputy.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "k8s-deputy.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "k8s-deputy.labels" -}} +helm.sh/chart: {{ include "k8s-deputy.chart" . }} +{{ include "k8s-deputy.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "k8s-deputy.selectorLabels" -}} +app.kubernetes.io/name: {{ include "k8s-deputy.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} diff --git a/test/integration/chart/k8s-deputy/templates/deployment.yaml b/test/integration/chart/k8s-deputy/templates/deployment.yaml new file mode 100644 index 0000000..c16a65d --- /dev/null +++ b/test/integration/chart/k8s-deputy/templates/deployment.yaml @@ -0,0 +1,77 @@ +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "k8s-deputy.fullname" . }} + labels: + {{- include "k8s-deputy.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "k8s-deputy.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "k8s-deputy.labels" . | nindent 8 }} + {{- with .Values.podLabels }} + {{- toYaml . | nindent 8 }} + {{- end }} + spec: + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + # Wait after Julia process exits to allow `preStop` to complete + command: ["/bin/sh", "-c", "julia entrypoint.jl; sleep 1"] + env: + - name: JULIA_NUM_THREADS + value: "{ .Values.threads }}" + - name: DEPUTY_HEALTH_CHECK_PORT + value: {{ .Values.probe.port | quote }} + - name: DEPUTY_IPC_DIR + value: /mnt/deputy-ipc + ports: + - name: probe + containerPort: {{ .Values.probe.port }} + protocol: TCP + livenessProbe: + httpGet: + path: /health/live + port: probe + timeoutSeconds: 5 + lifecycle: + preStop: + exec: + command: ["julia", "-e", "using K8sDeputy; graceful_terminate()"] + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.volumeMounts }} + volumeMounts: + {{- toYaml . | nindent 12 }} + {{- end }} + terminationGracePeriodSeconds: 30 + {{- with .Values.volumes }} + volumes: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/test/integration/chart/k8s-deputy/values.yaml b/test/integration/chart/k8s-deputy/values.yaml new file mode 100644 index 0000000..217ee6f --- /dev/null +++ b/test/integration/chart/k8s-deputy/values.yaml @@ -0,0 +1,79 @@ +# Default values for k8s-deputyprov. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +--- +replicaCount: 1 + +image: + repository: k8s-deputy + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +threads: "1,1" + +nameOverride: "" +fullnameOverride: "" + +podAnnotations: {} +podLabels: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: + allowPrivilegeEscalation: false + capabilities: + drop: + - all + readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +probe: + port: 8081 + +livenessProbe: + httpGet: + path: / + port: http +readinessProbe: + httpGet: + path: / + port: http + +# Additional volumes on the output Deployment definition. +volumes: + - name: deputy-ipc + emptyDir: + medium: Memory +# - name: foo +# secret: +# secretName: mysecret +# optional: false + +# Additional volumeMounts on the output Deployment definition. +volumeMounts: + - name: deputy-ipc + mountPath: /mnt/deputy-ipc +# - name: foo +# mountPath: "/etc/foo" +# readOnly: true + +nodeSelector: {} + +tolerations: [] + +affinity: {} diff --git a/test/integration/entrypoint.jl b/test/integration/entrypoint.jl new file mode 100644 index 0000000..39d8fe4 --- /dev/null +++ b/test/integration/entrypoint.jl @@ -0,0 +1,9 @@ +#!/usr/bin/env -S julia --color=no --startup-file=no + +using K8sDeputy + +deputy = Deputy() +server = K8sDeputy.serve!(deputy, "0.0.0.0") +graceful_terminator(() -> shutdown(deputy)) +readied(deputy) +wait(server) diff --git a/test/integration/skaffold.yaml b/test/integration/skaffold.yaml new file mode 100644 index 0000000..f2e4de5 --- /dev/null +++ b/test/integration/skaffold.yaml @@ -0,0 +1,20 @@ +--- +apiVersion: skaffold/v4beta2 +kind: Config + +build: + artifacts: + - image: k8s-deputy + context: ../.. + local: + useBuildkit: true + push: false + +deploy: + helm: + releases: + - name: k8s-deputy + chartPath: chart/k8s-deputy + setValueTemplates: + image.repository: "{{.IMAGE_REPO_k8s_deputy}}" + image.tag: "{{.IMAGE_TAG_k8s_deputy}}@{{.IMAGE_DIGEST_k8s_deputy}}"