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

Simplify helm tests #957

Merged
merged 1 commit into from
Sep 8, 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: 2 additions & 0 deletions pkg/skaffold/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"os/exec"
"path/filepath"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -89,6 +90,7 @@ func (h *HelmDeployer) Dependencies() ([]string, error) {
return nil
})
}
sort.Strings(deps)
return deps, nil
}

Expand Down
104 changes: 35 additions & 69 deletions pkg/skaffold/deploy/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"sort"
"strings"
"testing"

Expand Down Expand Up @@ -355,6 +352,7 @@ func TestHelmDeploy(t *testing.T) {
util.DefaultExecCommand = tt.cmd

_, err := tt.deployer.Deploy(context.Background(), &bytes.Buffer{}, tt.builds)

testutil.CheckError(t, tt.shouldErr, err)
})
}
Expand Down Expand Up @@ -435,20 +433,18 @@ func TestParseHelmRelease(t *testing.T) {
shouldErr bool
}{
{
name: "parse valid deployment yaml",
yaml: []byte(validDeployYaml),
shouldErr: false,
name: "parse valid deployment yaml",
yaml: []byte(validDeployYaml),
},
{
name: "parse valid service yaml",
yaml: []byte(validServiceYaml),
},
{
name: "parse invalid deployment yaml",
yaml: []byte(invalidDeployYaml),
shouldErr: true,
},
{
name: "parse valid service yaml",
yaml: []byte(validServiceYaml),
shouldErr: false,
},
}

for _, tt := range tests {
Expand All @@ -460,72 +456,34 @@ func TestParseHelmRelease(t *testing.T) {
}

func TestExtractChartFilename(t *testing.T) {
testCases := map[string]struct {
input string
tmp string
output string
shouldErr bool
}{
"1": {
input: "Successfully packaged chart and saved it to: /var/folders/gm/rrs_712142x8vymmd7xq7h340000gn/T/foo-1.2.3-dirty.tgz\n",
tmp: "/var/folders/gm/rrs_712142x8vymmd7xq7h340000gn/T/",
output: "foo-1.2.3-dirty.tgz",
shouldErr: false,
},
}
out, err := extractChartFilename(
"Successfully packaged chart and saved it to: /var/folders/gm/rrs_712142x8vymmd7xq7h340000gn/T/foo-1.2.3-dirty.tgz\n",
"/var/folders/gm/rrs_712142x8vymmd7xq7h340000gn/T/",
)

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
out, err := extractChartFilename(tc.input, tc.tmp)
testutil.CheckError(t, tc.shouldErr, err)
if out != tc.output {
t.Errorf("Expected output to be %q but got %q", tc.output, out)
}
})
}
}

func testDeployConfigWithPaths(chartPath string, valuesFilePath string) *v1alpha2.HelmDeploy {
return &v1alpha2.HelmDeploy{
Releases: []v1alpha2.HelmRelease{
{
Name: "skaffold-helm",
ChartPath: chartPath,
ValuesFilePath: valuesFilePath,
Values: map[string]string{
"image": "skaffold-helm",
},
Overrides: map[string]interface{}{
"foo": "bar",
},
SetValues: map[string]string{
"some.key": "somevalue",
},
},
},
}
testutil.CheckErrorAndDeepEqual(t, false, err, "foo-1.2.3-dirty.tgz", out)
}

func TestHelmDependencies(t *testing.T) {
var tests = []struct {
description string
files []string
valuesFilePath string
output func(folder *testutil.TempDir) []string
expected func(folder *testutil.TempDir) []string
}{
{
description: "charts dir is excluded",
files: []string{"Chart.yaml", "charts/xyz.tar", "templates/deploy.yaml"},
output: func(folder *testutil.TempDir) []string {
return []string{filepath.Join(folder.Root(), "Chart.yaml"), filepath.Join(folder.Root(), "templates/deploy.yaml")}
expected: func(folder *testutil.TempDir) []string {
return []string{folder.Path("Chart.yaml"), folder.Path("templates/deploy.yaml")}
},
},
{
description: "values file is included",
files: []string{"Chart.yaml"},
valuesFilePath: "/tmp/values.yaml",
output: func(folder *testutil.TempDir) []string {
return []string{filepath.Join(folder.Root(), "Chart.yaml"), "/tmp/values.yaml"}
valuesFilePath: "/folder/values.yaml",
expected: func(folder *testutil.TempDir) []string {
return []string{"/folder/values.yaml", folder.Path("Chart.yaml")}
},
},
}
Expand All @@ -534,18 +492,26 @@ func TestHelmDependencies(t *testing.T) {
t.Run(tt.description, func(t *testing.T) {
folder, cleanup := testutil.NewTempDir(t)
defer cleanup()
deployer := NewHelmDeployer(testDeployConfigWithPaths(folder.Root(), tt.valuesFilePath), testKubeContext, testNamespace)
for _, file := range tt.files {
folder.Write(file, "test")
folder.Write(file, "")
}

deps, _ := deployer.Dependencies()
output := tt.output(folder)
sort.Strings(output)
sort.Strings(deps)
if !reflect.DeepEqual(deps, output) {
t.Errorf("Expected chart dependencies to be %q but got %q", output, deps)
}
deployer := NewHelmDeployer(&v1alpha2.HelmDeploy{
Releases: []v1alpha2.HelmRelease{
{
Name: "skaffold-helm",
ChartPath: folder.Root(),
ValuesFilePath: tt.valuesFilePath,
Values: map[string]string{"image": "skaffold-helm"},
Overrides: map[string]interface{}{"foo": "bar"},
SetValues: map[string]string{"some.key": "somevalue"},
},
},
}, testKubeContext, testNamespace)

deps, err := deployer.Dependencies()

testutil.CheckErrorAndDeepEqual(t, false, err, tt.expected(folder), deps)
})
}
}