From b2a6231dcffef09d9d4045466fe3e953ca718f95 Mon Sep 17 00:00:00 2001 From: KUOKA Yusuke Date: Fri, 12 Jul 2019 22:37:54 +0900 Subject: [PATCH] fix: absolute chart path (#753) Resolves #743 --- pkg/app/app_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ pkg/state/state.go | 10 ---------- pkg/state/util.go | 12 ++++++++++++ pkg/state/util_test.go | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 10 deletions(-) diff --git a/pkg/app/app_test.go b/pkg/app/app_test.go index 2c132a9d..1702b7e6 100644 --- a/pkg/app/app_test.go +++ b/pkg/app/app_test.go @@ -1028,6 +1028,48 @@ x: } } +func TestVisitDesiredStatesWithReleasesFiltered_ChartAtAbsPath(t *testing.T) { + files := map[string]string{ + "/path/to/helmfile.yaml": ` +releases: +- name: myapp + chart: /path/to/mychart +`, + } + + actual := []state.ReleaseSpec{} + + collectReleases := func(st *state.HelmState, helm helmexec.Interface) []error { + for _, r := range st.Releases { + actual = append(actual, r) + } + return []error{} + } + app := appWithFs(&App{ + KubeContext: "default", + Logger: helmexec.NewLogger(os.Stderr, "debug"), + Reverse: false, + Namespace: "", + Env: "default", + Selectors: []string{}, + }, files) + err := app.VisitDesiredStatesWithReleasesFiltered( + "helmfile.yaml", collectReleases, + ) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(actual) != 1 { + t.Errorf("unexpected number of processed releases: expected=1, got=%d", len(actual)) + } + if actual[0].Name != "myapp" { + t.Errorf("unexpected name: expected=%s, got=%s", "myapp", actual[0].Name) + } + if actual[0].Chart != "/path/to/mychart" { + t.Errorf("unexpected chart: expected=%s, got=%s", "/path/to/mychart", actual[0].Chart) + } +} + func TestVisitDesiredStatesWithReleasesFiltered_RemoteTgzAsChart(t *testing.T) { testcases := []struct { expr, env, expected string diff --git a/pkg/state/state.go b/pkg/state/state.go index f47e1334..99818cdd 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -1092,16 +1092,6 @@ func (st *HelmState) BuildDeps(helm helmexec.Interface) []error { return nil } -// normalizeChart allows for the distinction between a file path reference and repository references. -// - Any single (or double character) followed by a `/` will be considered a local file reference and -// be constructed relative to the `base path`. -// - Everything else is assumed to be an absolute path or an actual / reference. -func normalizeChart(basePath, chart string) string { - if !isLocalChart(chart) { - return chart - } - return filepath.Join(basePath, chart) -} func pathExists(chart string) bool { _, err := os.Stat(chart) return err == nil diff --git a/pkg/state/util.go b/pkg/state/util.go index 03d565c5..eadb22ca 100644 --- a/pkg/state/util.go +++ b/pkg/state/util.go @@ -1,6 +1,7 @@ package state import ( + "path/filepath" "regexp" "strings" ) @@ -38,3 +39,14 @@ func resolveRemoteChart(repoAndChart string) (string, string, bool) { return repo, chart, true } + +// normalizeChart allows for the distinction between a file path reference and repository references. +// - Any single (or double character) followed by a `/` will be considered a local file reference and +// be constructed relative to the `base path`. +// - Everything else is assumed to be an absolute path or an actual / reference. +func normalizeChart(basePath, chart string) string { + if !isLocalChart(chart) || chart[0] == '/' { + return chart + } + return filepath.Join(basePath, chart) +} diff --git a/pkg/state/util_test.go b/pkg/state/util_test.go index 1d5d46d9..57d51f3a 100644 --- a/pkg/state/util_test.go +++ b/pkg/state/util_test.go @@ -27,6 +27,10 @@ func TestIsLocalChart(t *testing.T) { input: "./charts/mysubsystem/myapp", expected: true, }, + { + input: "/charts/mysubsystem/myapp", + expected: true, + }, { // Regression test case for: // * https://github.com/roboll/helmfile/issues/675 @@ -80,6 +84,10 @@ func TestResolveRemortChart(t *testing.T) { input: "./charts/mysubsystem/myapp", remote: false, }, + { + input: "/charts/mysubsystem/myapp", + remote: false, + }, { // Regression test case for: // * https://github.com/roboll/helmfile/issues/675 @@ -111,3 +119,29 @@ func TestResolveRemortChart(t *testing.T) { } } } + +func TestNormalizeChart(t *testing.T) { + testcases := []struct { + input string + expected string + }{ + { + input: "mychart", + expected: "/path/to/mychart", + }, + { + input: "/charts/mychart", + expected: "/charts/mychart", + }, + } + + for i := range testcases { + testcase := testcases[i] + + actual := normalizeChart("/path/to", testcase.input) + + if testcase.expected != actual { + t.Fatalf("unexpected result: normalizeChart(\"/path/to\", \"%s\"): expected=%v, got=%v", testcase.input, testcase.expected, actual) + } + } +}