Skip to content

Instantly share code, notes, and snippets.

View jwillker's full-sized avatar
:octocat:
...

Jhonn W. Frazão jwillker

:octocat:
...
View GitHub Profile
func sum(x, y int) int {
return x + y
}
func Test_sum(t *testing.T) {
tests := []struct {
name string
x int
y int
want int
}{
{
name: "one plus two",
x: 1,
image: "{{ .Values.containers.image }}:{{ .Values.app.version }}"
imagePullPolicy: {{ .Values.containers.pullPolicy }}
{{- if .Values.containers.command }}
command:
{{- range .Values.containers.command }}
- {{ . }}
{{- end }}
{{- end }}
{{- if .Values.containers.args }}
args:
{{/*
Return the appropriate apiVersion for ingress.
*/}}
{{- define "ingress.apiVersion" -}}
{{- if and (.Capabilities.APIVersions.Has "networking.k8s.io/v1") (semverCompare ">= 1.19.x" (include "chart.kubeVersion" .)) -}}
{{- print "networking.k8s.io/v1" -}}
{{- else if .Capabilities.APIVersions.Has "networking.k8s.io/v1beta1" -}}
{{- print "networking.k8s.io/v1beta1" -}}
{{- else -}}
{{- print "extensions/v1beta1" -}}
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "helm-basic-example.fullname" . }}
namespace: {{ .Release.Namespace }}
labels:
# These labels are required by helm. You can read more about required labels in the chart best pracices guide:
# https://docs.helm.sh/chart_best_practices/#standard-labels
helm.sh/chart: {{ include "helm-basic-example.chart" . }}
app.kubernetes.io/name: {{ include "helm-basic-example.name" . }}
// An example of how to verify the rendered template object of a Helm Chart given various inputs.
func TestHelmBasicExampleTemplateRenderedDeployment(t *testing.T) {
t.Parallel()
// Path to the helm chart we will test
helmChartPath, err := filepath.Abs("../examples/helm-basic-example")
releaseName := "helm-basic"
require.NoError(t, err)
// Since we aren't deploying any resources, there is no need to setup kubectl authentication or helm home.
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "generic.fullname" .}}
labels: &labels
{{- include "generic.commonLabels" . | nindent 4 }}
{{- with .Values.app.labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
annotations: &annotations
{{- define "generic.fullname" -}}
{{- .Release.Name | trunc 6 -}}
{{- end }}
{{/*
Selector labels
*/}}
{{- define "generic.selectorLabels" -}}
app.kubernetes.io/name: {{ include "generic.fullname" .}}
app.kubernetes.io/instance: '{{ include "generic.fullname" . }}-{{ .Values.app.version }}'
func TestDeployTemplateRendersMetadata(t *testing.T) {
t.Parallel()
var (
// Default Helm values
// Work as values.yaml for tests purposes
deployDefaultValues = map[string]string{
"app.version": "0.1.0",
"app.replicas": "2",
"app.component": "api",
"app.env": "dev",
// Render receive helm parameters and render a k8s object
func Render(t *testing.T, helmChartPath, templateFile, release string, values map[string]string, k8sObject interface{}) error {
// Passing values to helm chart.
options := &helm.Options{
SetValues: values,
}
templateFile = fmt.Sprintf("templates/%s", templateFile)
output, err := helm.RenderTemplateE(t, options, helmChartPath, release, []string{templateFile})