Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for helm deployment with `packaged' option #696

Merged
merged 2 commits into from
Jun 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/skaffold/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Deployer interface {
Cleanup(context.Context, io.Writer) error
}

func JoinTagsToBuildResult(builds []build.Artifact, params map[string]string) (map[string]build.Artifact, error) {
func joinTagsToBuildResult(builds []build.Artifact, params map[string]string) (map[string]build.Artifact, error) {
imageToBuildResult := map[string]build.Artifact{}
for _, build := range builds {
imageToBuildResult[build.ImageName] = build
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (h *HelmDeployer) deployRelease(out io.Writer, r v1alpha2.HelmRelease, buil
fmt.Fprintf(out, "Helm release %s not installed. Installing...\n", releaseName)
isInstalled = false
}
params, err := JoinTagsToBuildResult(builds, r.Values)
params, err := joinTagsToBuildResult(builds, r.Values)
if err != nil {
return nil, errors.Wrap(err, "matching build results to chart values")
}
Expand Down
76 changes: 76 additions & 0 deletions pkg/skaffold/deploy/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"testing"

"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
Expand All @@ -36,6 +41,13 @@ var testBuilds = []build.Artifact{
},
}

var testBuildsFoo = []build.Artifact{
{
ImageName: "foo",
Tag: "foo:3605e7bc17cf46e53f4d81c4cbc24e5b4c495184",
},
}

var testDeployConfig = &v1alpha2.DeployConfig{
DeployType: v1alpha2.DeployType{
HelmDeploy: &v1alpha2.HelmDeploy{
Expand Down Expand Up @@ -74,6 +86,26 @@ var testDeployConfigParameterUnmatched = &v1alpha2.DeployConfig{
},
}

var testDeployFooWithPackaged = &v1alpha2.DeployConfig{
DeployType: v1alpha2.DeployType{
HelmDeploy: &v1alpha2.HelmDeploy{
Releases: []v1alpha2.HelmRelease{
{
Name: "foo",
ChartPath: "testdata/foo",
Values: map[string]string{
"image.tag": "foo",
},
Packaged: &v1alpha2.HelmPackaged{
Version: "0.1.2",
AppVersion: "1.2.3",
},
},
},
},
},
}

var testNamespace = "testNamespace"

var validDeployYaml = `
Expand Down Expand Up @@ -154,6 +186,12 @@ HOOKS:
MANIFEST:
`

// TestMain disables logrus output before running tests.
func TestMain(m *testing.M) {
logrus.SetOutput(ioutil.Discard)
os.Exit(m.Run())
}

func TestHelmDeploy(t *testing.T) {
var tests = []struct {
description string
Expand Down Expand Up @@ -214,6 +252,34 @@ func TestHelmDeploy(t *testing.T) {
deployer: NewHelmDeployer(testDeployConfig, testKubeContext, testNamespace),
builds: testBuilds,
},
{
description: "should package chart and deploy",
cmd: &MockHelm{
t: t,
packageOut: bytes.NewBufferString("Packaged to " + os.TempDir() + "foo-0.1.2.tgz"),
},
shouldErr: false,
deployer: NewHelmDeployer(
testDeployFooWithPackaged,
testKubeContext,
testNamespace,
),
builds: testBuildsFoo,
},
{
description: "should fail to deploy when packaging fails",
cmd: &MockHelm{
t: t,
packageResult: fmt.Errorf("packaging failed"),
},
shouldErr: true,
deployer: NewHelmDeployer(
testDeployFooWithPackaged,
testKubeContext,
testNamespace,
),
builds: testBuildsFoo,
},
}

for _, tt := range tests {
Expand All @@ -234,6 +300,9 @@ type MockHelm struct {
installResult error
upgradeResult error
depResult error

packageOut io.Reader
packageResult error
}

func (m *MockHelm) RunCmdOut(c *exec.Cmd) ([]byte, error) {
Expand All @@ -259,6 +328,13 @@ func (m *MockHelm) RunCmd(c *exec.Cmd) error {
return m.upgradeResult
case "dep":
return m.depResult
case "package":
if m.packageOut != nil {
if _, err := io.Copy(c.Stdout, m.packageOut); err != nil {
m.t.Errorf("Failed to copy stdout")
}
}
return m.packageResult
default:
m.t.Errorf("Unknown helm command: %+v", c)
return nil
Expand Down
21 changes: 21 additions & 0 deletions pkg/skaffold/deploy/testdata/foo/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# 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
*~
# Various IDEs
.project
.idea/
*.tmproj
5 changes: 5 additions & 0 deletions pkg/skaffold/deploy/testdata/foo/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: foo
version: 0.1.0
19 changes: 19 additions & 0 deletions pkg/skaffold/deploy/testdata/foo/templates/NOTES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
1. Get the application URL by running these commands:
{{- if .Values.ingress.enabled }}
{{- range .Values.ingress.hosts }}
http{{ if $.Values.ingress.tls }}s{{ end }}://{{ . }}{{ $.Values.ingress.path }}
{{- end }}
{{- else if contains "NodePort" .Values.service.type }}
export NODE_PORT=$(kubectl get --namespace {{ .Release.Namespace }} -o jsonpath="{.spec.ports[0].nodePort}" services {{ template "foo.fullname" . }})
export NODE_IP=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")
echo http://$NODE_IP:$NODE_PORT
{{- else if contains "LoadBalancer" .Values.service.type }}
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
You can watch the status of by running 'kubectl get svc -w {{ template "foo.fullname" . }}'
export SERVICE_IP=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "foo.fullname" . }} -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
echo http://$SERVICE_IP:{{ .Values.service.port }}
{{- else if contains "ClusterIP" .Values.service.type }}
export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "foo.name" . }},release={{ .Release.Name }}" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl port-forward $POD_NAME 8080:80
{{- end }}
32 changes: 32 additions & 0 deletions pkg/skaffold/deploy/testdata/foo/templates/_helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "foo.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 "foo.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 "foo.chart" -}}
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
{{- end -}}
51 changes: 51 additions & 0 deletions pkg/skaffold/deploy/testdata/foo/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: {{ template "foo.fullname" . }}
labels:
app: {{ template "foo.name" . }}
chart: {{ template "foo.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
replicas: {{ .Values.replicaCount }}
selector:
matchLabels:
app: {{ template "foo.name" . }}
release: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ template "foo.name" . }}
release: {{ .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
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
resources:
{{ toYaml .Values.resources | indent 12 }}
{{- with .Values.nodeSelector }}
nodeSelector:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.affinity }}
affinity:
{{ toYaml . | indent 8 }}
{{- end }}
{{- with .Values.tolerations }}
tolerations:
{{ toYaml . | indent 8 }}
{{- end }}
39 changes: 39 additions & 0 deletions pkg/skaffold/deploy/testdata/foo/templates/ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{{- if .Values.ingress.enabled -}}
{{- $fullName := include "foo.fullname" . -}}
{{- $servicePort := .Values.service.port -}}
{{- $ingressPath := .Values.ingress.path -}}
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: {{ $fullName }}
labels:
app: {{ template "foo.name" . }}
chart: {{ template "foo.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
{{- with .Values.ingress.annotations }}
annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
{{- if .Values.ingress.tls }}
tls:
{{- range .Values.ingress.tls }}
- hosts:
{{- range .hosts }}
- {{ . }}
{{- end }}
secretName: {{ .secretName }}
{{- end }}
{{- end }}
rules:
{{- range .Values.ingress.hosts }}
- host: {{ . }}
http:
paths:
- path: {{ $ingressPath }}
backend:
serviceName: {{ $fullName }}
servicePort: http
{{- end }}
{{- end }}
19 changes: 19 additions & 0 deletions pkg/skaffold/deploy/testdata/foo/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Service
metadata:
name: {{ template "foo.fullname" . }}
labels:
app: {{ template "foo.name" . }}
chart: {{ template "foo.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
selector:
app: {{ template "foo.name" . }}
release: {{ .Release.Name }}
45 changes: 45 additions & 0 deletions pkg/skaffold/deploy/testdata/foo/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Default values for foo.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.

replicaCount: 1

image:
repository: nginx
tag: stable
pullPolicy: IfNotPresent

service:
type: ClusterIP
port: 80

ingress:
enabled: false
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
path: /
hosts:
- chart-example.local
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local

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

nodeSelector: {}

tolerations: []

affinity: {}