From 0d4849ff989d5168a140f2c2704cd3331930bc83 Mon Sep 17 00:00:00 2001 From: Kazuki Suda Date: Sun, 24 Sep 2023 15:54:27 +0900 Subject: [PATCH] Add a test case for issue 4905 ``` ===== ACTUAL BEGIN ======================================== apiVersion: v1 data: config.yaml: "null" kind: ConfigMap metadata: name: issue4905 ===== ACTUAL END ========================================== EXPECTED ACTUAL -------- ------ apiVersion: v1 apiVersion: v1 data: data: X config.yaml: |- config.yaml: "null" X item1: 1 kind: ConfigMap X item2: 2 metadata: X kind: ConfigMap name: issue4905 X metadata: X name: issue4905 hasgett.go:22: Expected not equal to actual --- FAIL: TestHelmChartInflationGeneratorIssue4905 (0.24s) ``` Signed-off-by: Kazuki Suda --- .../HelmChartInflationGenerator_test.go | 47 ++++++++++++++ .../helmchartinflationgenerator/go.mod | 3 +- .../helmchartinflationgenerator/go.sum | 6 ++ .../testdata/charts/issue4905/Chart.yaml | 24 +++++++ .../charts/issue4905/templates/_helpers.tpl | 62 +++++++++++++++++++ .../charts/issue4905/templates/configmap.yaml | 7 +++ .../testdata/charts/issue4905/values.yaml | 1 + 7 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/Chart.yaml create mode 100644 plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/templates/_helpers.tpl create mode 100644 plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/templates/configmap.yaml create mode 100644 plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/values.yaml diff --git a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go index 0c2b4932c..95ba98c61 100644 --- a/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go +++ b/plugin/builtin/helmchartinflationgenerator/HelmChartInflationGenerator_test.go @@ -9,7 +9,9 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/require" kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" + "sigs.k8s.io/kustomize/kyaml/copyutil" ) func TestHelmChartInflationGenerator(t *testing.T) { @@ -579,3 +581,48 @@ valuesInline: `) th.AssertActualEqualsExpected(rm, "") } + +func TestHelmChartInflationGeneratorIssue4905(t *testing.T) { + th := kusttest_test.MakeEnhancedHarnessWithTmpRoot(t). + PrepBuiltin("HelmChartInflationGenerator") + defer th.Reset() + if err := th.ErrIfNoHelm(); err != nil { + t.Skip("skipping: " + err.Error()) + } + + copyTestChartsIntoHarness(t, th) + + rm := th.LoadAndRunGenerator(` +apiVersion: builtin +kind: HelmChartInflationGenerator +metadata: + name: issue4905 +name: issue4905 +releaseName: issue4905 +chartHome: ./charts +valuesInline: + config: + item1: 1 + item2: 2 +`) + + th.AssertActualEqualsExpected(rm, ` +apiVersion: v1 +data: + config.yaml: |- + item1: 1 + item2: 2 +kind: ConfigMap +metadata: + name: issue4905 +`) +} + +func copyTestChartsIntoHarness(t *testing.T, th *kusttest_test.HarnessEnhanced) { + t.Helper() + + thDir := filepath.Join(th.GetRoot(), "charts") + chartDir := "testdata/charts" + + require.NoError(t, copyutil.CopyDir(th.GetFSys(), chartDir, thDir)) +} diff --git a/plugin/builtin/helmchartinflationgenerator/go.mod b/plugin/builtin/helmchartinflationgenerator/go.mod index ef9b9e260..402baa418 100644 --- a/plugin/builtin/helmchartinflationgenerator/go.mod +++ b/plugin/builtin/helmchartinflationgenerator/go.mod @@ -4,6 +4,7 @@ go 1.20 require ( github.com/imdario/mergo v0.3.6 + github.com/stretchr/testify v1.8.1 sigs.k8s.io/kustomize/api v0.14.0 sigs.k8s.io/kustomize/kyaml v0.14.3 sigs.k8s.io/yaml v1.3.0 @@ -25,7 +26,7 @@ require ( github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/testify v1.8.1 // indirect + github.com/sergi/go-diff v1.1.0 // indirect github.com/xlab/treeprint v1.2.0 // indirect go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect golang.org/x/sys v0.8.0 // indirect diff --git a/plugin/builtin/helmchartinflationgenerator/go.sum b/plugin/builtin/helmchartinflationgenerator/go.sum index 93c2d9924..6269dd8a3 100644 --- a/plugin/builtin/helmchartinflationgenerator/go.sum +++ b/plugin/builtin/helmchartinflationgenerator/go.sum @@ -29,6 +29,7 @@ github.com/imdario/mergo v0.3.6 h1:xTNEAn+kxVO7dTZGu0CegyqKZmoWFI0rF8UxjlB2d28= github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -45,10 +46,12 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= @@ -67,10 +70,13 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng= google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/evanphx/json-patch.v5 v5.6.0 h1:BMT6KIwBD9CaU91PJCZIe46bDmBWa9ynTQgJIOpfQBk= gopkg.in/evanphx/json-patch.v5 v5.6.0/go.mod h1:/kvTRh1TVm5wuM6OkHxqXtE/1nUZZpihg29RtuIyfvk= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/Chart.yaml b/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/Chart.yaml new file mode 100644 index 000000000..3721f591c --- /dev/null +++ b/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: issue4905 +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" diff --git a/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/templates/_helpers.tpl b/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/templates/_helpers.tpl new file mode 100644 index 000000000..3b76a3e64 --- /dev/null +++ b/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "issue4905.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 "issue4905.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 "issue4905.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "issue4905.labels" -}} +helm.sh/chart: {{ include "issue4905.chart" . }} +{{ include "issue4905.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "issue4905.selectorLabels" -}} +app.kubernetes.io/name: {{ include "issue4905.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "issue4905.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "issue4905.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/templates/configmap.yaml b/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/templates/configmap.yaml new file mode 100644 index 000000000..275594bff --- /dev/null +++ b/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/templates/configmap.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "issue4905.fullname" . }} +data: + config.yaml: |- + {{- .Values.config | toYaml | nindent 4 }} diff --git a/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/values.yaml b/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/values.yaml new file mode 100644 index 000000000..f9294d30b --- /dev/null +++ b/plugin/builtin/helmchartinflationgenerator/testdata/charts/issue4905/values.yaml @@ -0,0 +1 @@ +config: # null