From de02ad26945f1a0785fa95f3d6622e1180e8625e Mon Sep 17 00:00:00 2001 From: paologallinaharbur Date: Mon, 13 Jun 2022 14:34:31 +0200 Subject: [PATCH 1/2] fix(test): deploment was not installing due to missing selector Signed-off-by: paologallinaharbur --- .../mutating-deployment-selector/templates/deployment.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/chart/test_charts/mutating-deployment-selector/templates/deployment.yaml b/pkg/chart/test_charts/mutating-deployment-selector/templates/deployment.yaml index 5f3f9345..c928016e 100644 --- a/pkg/chart/test_charts/mutating-deployment-selector/templates/deployment.yaml +++ b/pkg/chart/test_charts/mutating-deployment-selector/templates/deployment.yaml @@ -12,6 +12,11 @@ metadata: revision: {{ .Release.Revision | quote }} spec: replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + app.kubernetes.io/name: {{ include "nginx.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + revision: {{ .Release.Revision | quote }} template: metadata: labels: From b615abf0583ed77b936a3d4c9edd1a8b489c0afc Mon Sep 17 00:00:00 2001 From: paologallinaharbur Date: Mon, 13 Jun 2022 14:36:17 +0200 Subject: [PATCH 2/2] feat(test): tesi if chart is upgraded and error is triggered Signed-off-by: paologallinaharbur --- pkg/chart/chart_test.go | 4 ++- pkg/chart/integration_test.go | 6 ++++ .../Chart.yaml | 5 +++ .../README.md | 4 +++ .../templates/_helpers.tpl | 32 +++++++++++++++++++ .../templates/deployment.yaml | 31 ++++++++++++++++++ .../values.yaml | 11 +++++++ .../test_charts/simple-deployment/Chart.yaml | 5 +++ .../test_charts/simple-deployment/README.md | 4 +++ .../simple-deployment/templates/_helpers.tpl | 32 +++++++++++++++++++ .../templates/deployment.yaml | 29 +++++++++++++++++ .../test_charts/simple-deployment/values.yaml | 11 +++++++ 12 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 pkg/chart/test_charts/simple-deployment-different-selector/Chart.yaml create mode 100644 pkg/chart/test_charts/simple-deployment-different-selector/README.md create mode 100644 pkg/chart/test_charts/simple-deployment-different-selector/templates/_helpers.tpl create mode 100644 pkg/chart/test_charts/simple-deployment-different-selector/templates/deployment.yaml create mode 100644 pkg/chart/test_charts/simple-deployment-different-selector/values.yaml create mode 100644 pkg/chart/test_charts/simple-deployment/Chart.yaml create mode 100644 pkg/chart/test_charts/simple-deployment/README.md create mode 100644 pkg/chart/test_charts/simple-deployment/templates/_helpers.tpl create mode 100644 pkg/chart/test_charts/simple-deployment/templates/deployment.yaml create mode 100644 pkg/chart/test_charts/simple-deployment/values.yaml diff --git a/pkg/chart/chart_test.go b/pkg/chart/chart_test.go index adef684d..2f05a311 100644 --- a/pkg/chart/chart_test.go +++ b/pkg/chart/chart_test.go @@ -184,13 +184,15 @@ func TestReadAllChartDirectories(t *testing.T) { "test_charts/bar", "test_charts/must-pass-upgrade-install", "test_charts/mutating-deployment-selector", + "test_charts/simple-deployment", + "test_charts/simple-deployment-different-selector", "test_charts/mutating-sfs-volumeclaim", "test_chart_at_root", } for _, chart := range actual { assert.Contains(t, expected, chart) } - assert.Len(t, actual, 6) + assert.Len(t, actual, 8) assert.Nil(t, err) } diff --git a/pkg/chart/integration_test.go b/pkg/chart/integration_test.go index c3b24221..63fa5d62 100644 --- a/pkg/chart/integration_test.go +++ b/pkg/chart/integration_test.go @@ -141,6 +141,12 @@ func TestUpgradeChart(t *testing.T) { "test_charts/mutating-sfs-volumeclaim", processError, }, + { + "change immutable deployment.spec.selector.matchLabels field", + "test_charts/simple-deployment", + "test_charts/simple-deployment-different-selector", + processError, + }, } for _, tc := range cases { diff --git a/pkg/chart/test_charts/simple-deployment-different-selector/Chart.yaml b/pkg/chart/test_charts/simple-deployment-different-selector/Chart.yaml new file mode 100644 index 00000000..603a58ed --- /dev/null +++ b/pkg/chart/test_charts/simple-deployment-different-selector/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +appVersion: "1.0" +description: A Helm chart for Kubernetes +name: nginx +version: 0.1.0 diff --git a/pkg/chart/test_charts/simple-deployment-different-selector/README.md b/pkg/chart/test_charts/simple-deployment-different-selector/README.md new file mode 100644 index 00000000..8d70bbb1 --- /dev/null +++ b/pkg/chart/test_charts/simple-deployment-different-selector/README.md @@ -0,0 +1,4 @@ +Simple chart with a Deployment having a different selector. + +The integration test will install first simple-deployment and then try to upgrade +to simple-deployment-different-selector failing as expected diff --git a/pkg/chart/test_charts/simple-deployment-different-selector/templates/_helpers.tpl b/pkg/chart/test_charts/simple-deployment-different-selector/templates/_helpers.tpl new file mode 100644 index 00000000..165d01e7 --- /dev/null +++ b/pkg/chart/test_charts/simple-deployment-different-selector/templates/_helpers.tpl @@ -0,0 +1,32 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "nginx.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 "nginx.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 "nginx.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} diff --git a/pkg/chart/test_charts/simple-deployment-different-selector/templates/deployment.yaml b/pkg/chart/test_charts/simple-deployment-different-selector/templates/deployment.yaml new file mode 100644 index 00000000..74d8e3d7 --- /dev/null +++ b/pkg/chart/test_charts/simple-deployment-different-selector/templates/deployment.yaml @@ -0,0 +1,31 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "nginx.fullname" . }} + labels: + app.kubernetes.io/name: {{ include "nginx.name" . }} + helm.sh/chart: {{ include "nginx.chart" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/managed-by: {{ .Release.Service }} +spec: + replicas: 1 + selector: + matchLabels: + app.kubernetes.io/name: {{ include "nginx.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + extra: label + template: + metadata: + labels: + app.kubernetes.io/name: {{ include "nginx.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + extra: label + spec: + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP diff --git a/pkg/chart/test_charts/simple-deployment-different-selector/values.yaml b/pkg/chart/test_charts/simple-deployment-different-selector/values.yaml new file mode 100644 index 00000000..cefd3a0d --- /dev/null +++ b/pkg/chart/test_charts/simple-deployment-different-selector/values.yaml @@ -0,0 +1,11 @@ +# Default values for nginx. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent + +nameOverride: "" +fullnameOverride: "" diff --git a/pkg/chart/test_charts/simple-deployment/Chart.yaml b/pkg/chart/test_charts/simple-deployment/Chart.yaml new file mode 100644 index 00000000..603a58ed --- /dev/null +++ b/pkg/chart/test_charts/simple-deployment/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +appVersion: "1.0" +description: A Helm chart for Kubernetes +name: nginx +version: 0.1.0 diff --git a/pkg/chart/test_charts/simple-deployment/README.md b/pkg/chart/test_charts/simple-deployment/README.md new file mode 100644 index 00000000..280f67ff --- /dev/null +++ b/pkg/chart/test_charts/simple-deployment/README.md @@ -0,0 +1,4 @@ +Simple chart with a Deployment. + +The integration test will install first simple-deployment and then try to upgrade +to simple-deployment-different-selector failing as expected diff --git a/pkg/chart/test_charts/simple-deployment/templates/_helpers.tpl b/pkg/chart/test_charts/simple-deployment/templates/_helpers.tpl new file mode 100644 index 00000000..165d01e7 --- /dev/null +++ b/pkg/chart/test_charts/simple-deployment/templates/_helpers.tpl @@ -0,0 +1,32 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "nginx.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 "nginx.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 "nginx.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} diff --git a/pkg/chart/test_charts/simple-deployment/templates/deployment.yaml b/pkg/chart/test_charts/simple-deployment/templates/deployment.yaml new file mode 100644 index 00000000..ac64e0d3 --- /dev/null +++ b/pkg/chart/test_charts/simple-deployment/templates/deployment.yaml @@ -0,0 +1,29 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "nginx.fullname" . }} + labels: + app.kubernetes.io/name: {{ include "nginx.name" . }} + helm.sh/chart: {{ include "nginx.chart" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/managed-by: {{ .Release.Service }} +spec: + replicas: 1 + selector: + matchLabels: + app.kubernetes.io/name: {{ include "nginx.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + template: + metadata: + labels: + app.kubernetes.io/name: {{ include "nginx.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + spec: + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: 80 + protocol: TCP diff --git a/pkg/chart/test_charts/simple-deployment/values.yaml b/pkg/chart/test_charts/simple-deployment/values.yaml new file mode 100644 index 00000000..cefd3a0d --- /dev/null +++ b/pkg/chart/test_charts/simple-deployment/values.yaml @@ -0,0 +1,11 @@ +# Default values for nginx. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +image: + repository: nginx + tag: stable + pullPolicy: IfNotPresent + +nameOverride: "" +fullnameOverride: ""