Skip to content

Commit

Permalink
Add tests for yamls and helm releases
Browse files Browse the repository at this point in the history
arnarg committed Aug 3, 2024

Verified

This commit was signed with the committer’s verified signature.
djc Dirkjan Ochtman
1 parent 08f0764 commit 2de8fe2
Showing 10 changed files with 383 additions and 1 deletion.
2 changes: 1 addition & 1 deletion modules/applications/helm.nix
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ in {
description = "Namespace for the release.";
};
chart = mkOption {
type = types.package;
type = with types; either package path;
description = ''
Derivation containing the helm chart for the release.
'';
2 changes: 2 additions & 0 deletions tests/default.nix
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@
./sync-options.nix
./compare-options.nix
./create-namespace.nix
./yamls.nix
./helm
];
};
}
24 changes: 24 additions & 0 deletions tests/helm/chart/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: chart
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: "1.16.0"
62 changes: 62 additions & 0 deletions tests/helm/chart/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{{/*
Expand the name of the chart.
*/}}
{{- define "chart.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 "chart.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 "chart.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "chart.labels" -}}
helm.sh/chart: {{ include "chart.chart" . }}
{{ include "chart.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "chart.selectorLabels" -}}
app.kubernetes.io/name: {{ include "chart.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "chart.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "chart.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}
23 changes: 23 additions & 0 deletions tests/helm/chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "chart.fullname" . }}
labels:
{{- include "chart.labels" . | nindent 4 }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
{{- include "chart.selectorLabels" . | nindent 6 }}
template:
metadata:
labels:
{{- include "chart.labels" . | nindent 8 }}
spec:
containers:
- name: {{ .Chart.Name }}
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
ports:
- name: http
containerPort: {{ .Values.service.port }}
protocol: TCP
11 changes: 11 additions & 0 deletions tests/helm/chart/templates/issuer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{{ if .Values.issuer.create }}
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: ca-issuer
labels:
{{- include "chart.labels" . | nindent 4 }}
spec:
ca:
secretName: ca-key-pair
{{ end }}
15 changes: 15 additions & 0 deletions tests/helm/chart/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "chart.fullname" . }}
labels:
{{- include "chart.labels" . | nindent 4 }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
{{- include "chart.selectorLabels" . | nindent 4 }}
12 changes: 12 additions & 0 deletions tests/helm/chart/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
replicaCount: 1

image:
repository: nginx
tag: latest

service:
type: ClusterIP
port: 80

issuer:
create: false
149 changes: 149 additions & 0 deletions tests/helm/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
lib,
config,
...
}: let
apps = config.applications;
in {
applications = {
# Create an application with a helm chart
test1.helm.releases.test1 = {
chart = ./chart;
};

# Create an application with a helm chart
# and overriding the values and using a
# transformer
test2.helm.releases.test2 = {
chart = ./chart;
values = {
replicaCount = 3;
image.tag = "1.0.0";
service.port = 8080;
issuer.create = true;
};

transformer = map (lib.kube.removeLabels [
"app.kubernetes.io/version"
"helm.sh/chart"
]);
};
};

test = with lib; {
name = "helm charts";
description = "Check that helm charts are rendered correctly.";
assertions = [
{
description = "Helm chart with no values override should render deployment correctly.";
expression = apps.test1.resources.apps.v1.Deployment.test1-chart;
assertion = depl: let
container = head depl.spec.template.spec.containers;
in
depl.kind
== "Deployment"
&& depl.metadata.name == "test1-chart"
&& depl.metadata.namespace == "test1"
&& depl.metadata.labels
== {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
"app.kubernetes.io/version" = "1.16.0";
"helm.sh/chart" = "chart-0.1.0";
}
&& depl.spec.replicas == 1
&& container.name == "chart"
&& container.image == "nginx:latest";
}

{
description = "Helm chart with no values override should render service correctly.";
expression = apps.test1.resources.core.v1.Service.test1-chart;
assertion = svc: let
port = head svc.spec.ports;
in
svc.kind
== "Service"
&& svc.metadata.name == "test1-chart"
&& svc.metadata.namespace == "test1"
&& svc.metadata.labels
== {
"app.kubernetes.io/instance" = "test1";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
"app.kubernetes.io/version" = "1.16.0";
"helm.sh/chart" = "chart-0.1.0";
}
&& svc.spec.type == "ClusterIP"
&& port.port == 80
&& port.targetPort == "http"
&& port.protocol == "TCP"
&& port.name == "http";
}

{
description = "Helm chart with values override should render deployment correctly.";
expression = apps.test2.resources.apps.v1.Deployment.test2-chart;
assertion = depl: let
container = head depl.spec.template.spec.containers;
in
depl.kind
== "Deployment"
&& depl.metadata.name == "test2-chart"
&& depl.metadata.namespace == "test2"
&& depl.metadata.labels
== {
"app.kubernetes.io/instance" = "test2";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
}
&& depl.spec.replicas == 3
&& container.name == "chart"
&& container.image == "nginx:1.0.0";
}

{
description = "Helm chart with values override should render service correctly.";
expression = apps.test2.resources.core.v1.Service.test2-chart;
assertion = svc: let
port = head svc.spec.ports;
in
svc.kind
== "Service"
&& svc.metadata.name == "test2-chart"
&& svc.metadata.namespace == "test2"
&& svc.metadata.labels
== {
"app.kubernetes.io/instance" = "test2";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
}
&& svc.spec.type == "ClusterIP"
&& port.port == 8080
&& port.targetPort == "http"
&& port.protocol == "TCP"
&& port.name == "http";
}

{
description = "Helm chart with unsupported resource types should still output the resource.";
expression = apps.test2.objects;
assertion = any (
obj:
obj.apiVersion
== "cert-manager.io/v1"
&& obj.kind == "Issuer"
&& obj.metadata.name == "ca-issuer"
&& obj.metadata.labels
== {
"app.kubernetes.io/instance" = "test2";
"app.kubernetes.io/managed-by" = "Helm";
"app.kubernetes.io/name" = "chart";
}
&& obj.spec.ca.secretName == "ca-key-pair"
);
}
];
};
}
84 changes: 84 additions & 0 deletions tests/yamls.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{
lib,
config,
...
}: let
apps = config.applications;
in {
applications = {
# Create an application with yamls
test1.yamls = [
# One Service object which always has resource
# types for
''
apiVersion: v1
kind: Service
metadata:
name: my-svc
labels:
name: my-svc
spec:
type: ClusterIP
ports:
- port: 80
targetPort: http
protocol: TCP
name: http
selector:
name: my-svc
''

# One Issuer which is does not have resource
# types defined
''
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: ca-issuer
spec:
ca:
secretName: ca-key-pair
''
];
};

test = with lib; {
name = "custom yamls";
description = "Check that extra custom yamls are parsed an output with resources.";
assertions = [
{
description = "Service should be parsed correctly.";
expression = apps.test1.resources.core.v1.Service.my-svc;
assertion = svc: let
port = head svc.spec.ports;
in
svc.kind
== "Service"
&& svc.metadata.name == "my-svc"
&& svc.metadata.namespace == "test1"
&& svc.metadata.labels
== {
name = "my-svc";
}
&& svc.spec.type == "ClusterIP"
&& port.port == 80
&& port.targetPort == "http"
&& port.protocol == "TCP"
&& port.name == "http";
}

{
description = "Custom yaml of unsupported resource types should still be in the resources output.";
expression = apps.test1.objects;
assertion = any (
obj:
obj.apiVersion
== "cert-manager.io/v1"
&& obj.kind == "Issuer"
&& obj.metadata.name == "ca-issuer"
&& obj.spec.ca.secretName == "ca-key-pair"
);
}
];
};
}

0 comments on commit 2de8fe2

Please sign in to comment.