diff --git a/pkg/deployments/deployments_test.go b/pkg/deployments/deployments_test.go index 579da3f6..9e2c0bdb 100644 --- a/pkg/deployments/deployments_test.go +++ b/pkg/deployments/deployments_test.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/Azure/draft/pkg/config" "github.com/Azure/draft/pkg/embedutils" + "github.com/Azure/draft/pkg/fixtures" "github.com/Azure/draft/pkg/templatewriter/writers" "io" "io/fs" @@ -40,6 +41,7 @@ func TestCreateDeployments(t *testing.T) { tempDirPath string tempFileName string tempPath string + fixturePath string cleanUp func() }{ { @@ -49,6 +51,7 @@ func TestCreateDeployments(t *testing.T) { tempDirPath: "charts/templates", tempFileName: "charts/templates/deployment.yaml", tempPath: "../../test/templates/helm/charts/templates/deployment.yaml", + fixturePath: "../fixtures/deployments/charts/templates/deployment.yaml", cleanUp: func() { os.Remove(".charts") }, @@ -60,6 +63,7 @@ func TestCreateDeployments(t *testing.T) { tempDirPath: "test/templates/unsupported", tempFileName: "test/templates/unsupported/deployment.yaml", tempPath: "test/templates/unsupported/deployment.yaml", + fixturePath: "../fixtures/deployments/test/templates/unsupported/deployment.yaml", cleanUp: func() { os.Remove("deployments") }, @@ -78,6 +82,16 @@ func TestCreateDeployments(t *testing.T) { assert.Error(t, err) } else { assert.NoError(t, err) + + generatedContent, err := os.ReadFile(tt.tempFileName) + assert.Nil(t, err) + + if _, err := os.Stat(tt.fixturePath); os.IsNotExist(err) { + t.Errorf("Fixture file does not exist at path: %s", tt.fixturePath) + } + + err = fixtures.ValidateContentAgainstFixture(generatedContent, tt.fixturePath) + assert.Nil(t, err) } tt.cleanUp() diff --git a/pkg/fixtures/deployments/charts/.helmignore b/pkg/fixtures/deployments/charts/.helmignore new file mode 100644 index 00000000..0e8a0eb3 --- /dev/null +++ b/pkg/fixtures/deployments/charts/.helmignore @@ -0,0 +1,23 @@ +# 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/ diff --git a/pkg/fixtures/deployments/charts/Chart.yaml b/pkg/fixtures/deployments/charts/Chart.yaml new file mode 100644 index 00000000..0462171f --- /dev/null +++ b/pkg/fixtures/deployments/charts/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: testapp +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/pkg/fixtures/deployments/charts/production.yaml b/pkg/fixtures/deployments/charts/production.yaml new file mode 100644 index 00000000..d3391779 --- /dev/null +++ b/pkg/fixtures/deployments/charts/production.yaml @@ -0,0 +1,8 @@ +image: + repository: "testapp" + pullPolicy: Always + tag: "latest" +service: + annotations: {} + type: LoadBalancer + port: "80" diff --git a/pkg/fixtures/deployments/charts/templates/_helpers.tpl b/pkg/fixtures/deployments/charts/templates/_helpers.tpl new file mode 100644 index 00000000..276b51f5 --- /dev/null +++ b/pkg/fixtures/deployments/charts/templates/_helpers.tpl @@ -0,0 +1,39 @@ + +{{- define "testapp.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + + +{{- define "testapp.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 }} + + +{{- define "testapp.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + + +{{- define "testapp.labels" -}} +helm.sh/chart: {{ include "testapp.chart" . }} +{{ include "testapp.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + + +{{- define "testapp.selectorLabels" -}} +app.kubernetes.io/name: {{ include "testapp.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} \ No newline at end of file diff --git a/pkg/fixtures/deployments/charts/templates/deployment.yaml b/pkg/fixtures/deployments/charts/templates/deployment.yaml new file mode 100644 index 00000000..ec61f72c --- /dev/null +++ b/pkg/fixtures/deployments/charts/templates/deployment.yaml @@ -0,0 +1,63 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "testapp.fullname" . }} + labels: + {{- include "testapp.labels" . | nindent 4 }} + kubernetes.azure.com/generator: {{ .Values.generatorLabel }} + namespace: {{ .Values.namespace }} +spec: + {{- if not .Values.autoscaling.enabled }} + replicas: {{ .Values.replicaCount }} + {{- end }} + selector: + matchLabels: + {{- include "testapp.selectorLabels" . | nindent 6 }} + template: + metadata: + {{- with .Values.podAnnotations }} + annotations: + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "testapp.selectorLabels" . | nindent 8 }} + namespace: {{ .Values.namespace }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - name: http + containerPort: {{ .Values.containerPort }} + protocol: TCP + livenessProbe: + httpGet: + path: / + port: http + readinessProbe: + httpGet: + path: / + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- 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/pkg/fixtures/deployments/charts/templates/service.yaml b/pkg/fixtures/deployments/charts/templates/service.yaml new file mode 100644 index 00000000..72b9c183 --- /dev/null +++ b/pkg/fixtures/deployments/charts/templates/service.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "testapp.fullname" . }} + labels: + {{- include "testapp.labels" . | nindent 4 }} + kubernetes.azure.com/generator: {{.Values.generatorLabel}} + annotations: + {{ toYaml .Values.service.annotations | nindent 4 }} + namespace: {{ .Values.namespace }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: {{ .Values.containerPort }} + protocol: TCP + name: svchttp + selector: + {{- include "testapp.selectorLabels" . | nindent 6 }} diff --git a/pkg/fixtures/deployments/charts/values.yaml b/pkg/fixtures/deployments/charts/values.yaml new file mode 100644 index 00000000..c22ff16e --- /dev/null +++ b/pkg/fixtures/deployments/charts/values.yaml @@ -0,0 +1,63 @@ +# Default values for testapp. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 + +namespace: default + +containerPort: 80 + +image: + repository: testimage + tag: latest + pullPolicy: Always + + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +podAnnotations: {} + +podSecurityContext: {} +# fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true +# runAsUser: 1000 + +service: + annotations: {} + type: LoadBalancer + port: 80 + +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 + +autoscaling: + enabled: false + minReplicas: 1 + maxReplicas: 100 + targetCPUUtilizationPercentage: 80 + # targetMemoryUtilizationPercentage: 80 + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +generatorLabel: draft \ No newline at end of file diff --git a/pkg/fixtures/deployments/test/templates/unsupported/deployment.yaml b/pkg/fixtures/deployments/test/templates/unsupported/deployment.yaml new file mode 100644 index 00000000..e69de29b diff --git a/pkg/fixtures/validatetemplate.go b/pkg/fixtures/validatetemplate.go index b6149a00..a2c4616a 100644 --- a/pkg/fixtures/validatetemplate.go +++ b/pkg/fixtures/validatetemplate.go @@ -12,6 +12,9 @@ import ( //go:embed pipelines/* var pipelines embed.FS +//go:embed deployments/* +var deployments embed.FS + func ValidateContentAgainstFixture(generatedContent []byte, fixturePath string) error { fullFixturePath := fmt.Sprintf("%s", fixturePath)