Skip to content

Commit

Permalink
added fixtures to deployment unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vidya2606 committed Sep 4, 2024
1 parent 350a9c4 commit ea8e9e1
Show file tree
Hide file tree
Showing 10 changed files with 256 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/deployments/deployments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -40,6 +41,7 @@ func TestCreateDeployments(t *testing.T) {
tempDirPath string
tempFileName string
tempPath string
fixturePath string
cleanUp func()
}{
{
Expand All @@ -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")
},
Expand All @@ -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")
},
Expand All @@ -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()
Expand Down
23 changes: 23 additions & 0 deletions pkg/fixtures/deployments/charts/.helmignore
Original file line number Diff line number Diff line change
@@ -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/
24 changes: 24 additions & 0 deletions pkg/fixtures/deployments/charts/Chart.yaml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions pkg/fixtures/deployments/charts/production.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
image:
repository: "testapp"
pullPolicy: Always
tag: "latest"
service:
annotations: {}
type: LoadBalancer
port: "80"
39 changes: 39 additions & 0 deletions pkg/fixtures/deployments/charts/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -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 }}
63 changes: 63 additions & 0 deletions pkg/fixtures/deployments/charts/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
19 changes: 19 additions & 0 deletions pkg/fixtures/deployments/charts/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -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 }}
63 changes: 63 additions & 0 deletions pkg/fixtures/deployments/charts/values.yaml
Original file line number Diff line number Diff line change
@@ -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
Empty file.
3 changes: 3 additions & 0 deletions pkg/fixtures/validatetemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit ea8e9e1

Please sign in to comment.