From 0061f66d3794e45b2be20f9fd1ece3569c1d13f7 Mon Sep 17 00:00:00 2001 From: itaispiegel Date: Mon, 30 Aug 2021 22:56:26 +0300 Subject: [PATCH] Print warning instead of fail when accessing non-included file Signed-off-by: itaispiegel --- pkg/action/install_test.go | 25 ++++++++++++++++++++----- pkg/action/upgrade_test.go | 24 ++++++++++++++++++++---- pkg/engine/files.go | 18 ++++++++---------- pkg/engine/files_test.go | 24 ++++++++++++++++-------- 4 files changed, 64 insertions(+), 27 deletions(-) diff --git a/pkg/action/install_test.go b/pkg/action/install_test.go index 1fde091a3..f5e842076 100644 --- a/pkg/action/install_test.go +++ b/pkg/action/install_test.go @@ -17,9 +17,11 @@ limitations under the License. package action import ( + "bytes" "context" "fmt" "io/ioutil" + "log" "os" "path/filepath" "regexp" @@ -731,7 +733,7 @@ func TestNameAndChartGenerateName(t *testing.T) { } } -func TestInstallFailsWhenWrongPathsIncluded(t *testing.T) { +func TestInstallUsesEmptyContentWrongPathsIncluded(t *testing.T) { is := assert.New(t) vals := map[string]interface{}{} @@ -763,14 +765,27 @@ func TestInstallFailsWhenWrongPathsIncluded(t *testing.T) { }, } + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { + log.SetOutput(os.Stderr) + }() + for _, tc := range tests { t.Run(tc.Name, func(t *testing.T) { instAction := installAction(t) - instAction.ExternalPaths = append(instAction.ExternalPaths, tc.IncludedFilePath) + if tc.IncludedFilePath != "" { + instAction.ExternalPaths = append(instAction.ExternalPaths, tc.IncludedFilePath) + } - _, err := instAction.Run(buildChart(withExternalFileTemplate(tc.ExternalPath)), vals) - expectedErr := fmt.Sprintf("<.Files.Get>: error calling Get: file %s not included", tc.ExternalPath) - is.Error(err, expectedErr) + rel, err := instAction.Run(buildChart(withExternalFileTemplate(tc.ExternalPath)), vals) + is.Contains(buf.String(), "not included") + is.NoError(err) + is.Equal( + rel.Manifest, + "---\n# Source: hello/templates/hello\nhello: world\n---\n# Source: hello/templates/with-external-paths\ndata:\n", + ) + buf.Reset() }) } } diff --git a/pkg/action/upgrade_test.go b/pkg/action/upgrade_test.go index b64145f51..6ef058555 100644 --- a/pkg/action/upgrade_test.go +++ b/pkg/action/upgrade_test.go @@ -17,8 +17,11 @@ limitations under the License. package action import ( + "bytes" "context" "fmt" + "log" + "os" "testing" "time" @@ -421,19 +424,32 @@ func TestUpgradeFailsWhenWrongPathsIncluded(t *testing.T) { }, } + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { + log.SetOutput(os.Stderr) + }() + for _, tc := range tests { t.Run(tc.Name, func(t *testing.T) { upAction := upgradeAction(t) - upAction.ExternalPaths = append(upAction.ExternalPaths, tc.IncludedFilePath) + if tc.IncludedFilePath != "" { + upAction.ExternalPaths = append(upAction.ExternalPaths, tc.IncludedFilePath) + } rel := releaseStub() rel.Name = "test" rel.Info.Status = release.StatusDeployed upAction.cfg.Releases.Create(rel) - _, err := upAction.Run(rel.Name, buildChart(withExternalFileTemplate(tc.ExternalPath)), vals) - expectedErr := fmt.Sprintf("<.Files.Get>: error calling Get: file %s not included", tc.ExternalPath) - is.Error(err, expectedErr) + rel, err := upAction.Run(rel.Name, buildChart(withExternalFileTemplate(tc.ExternalPath)), vals) + is.Contains(buf.String(), "not included") + is.NoError(err) + is.Equal( + rel.Manifest, + "---\n# Source: hello/templates/hello\nhello: world\n---\n# Source: hello/templates/with-external-paths\ndata:\n", + ) + buf.Reset() }) } } diff --git a/pkg/engine/files.go b/pkg/engine/files.go index ed9ad757e..cd45d7a07 100644 --- a/pkg/engine/files.go +++ b/pkg/engine/files.go @@ -19,6 +19,7 @@ package engine import ( "encoding/base64" "fmt" + "log" "path" "strings" @@ -47,11 +48,12 @@ func newFiles(from []*chart.File) files { // // This is intended to be accessed from within a template, so a missed key returns // an empty []byte. -func (f files) GetBytes(name string) ([]byte, error) { +func (f files) GetBytes(name string) []byte { if v, ok := f[name]; ok { - return v, nil + return v } - return []byte{}, fmt.Errorf("file %s not included", name) + log.Printf("WARNING: %s not included", name) + return nil } // Get returns a string representation of the given file. @@ -60,13 +62,9 @@ func (f files) GetBytes(name string) ([]byte, error) { // template. // // {{.Files.Get "foo"}} -func (f files) Get(name string) (string, error) { - content, err := f.GetBytes(name) - if err != nil { - return "", err - } - - return string(content), nil +func (f files) Get(name string) string { + content := f.GetBytes(name) + return string(content) } // Glob takes a glob pattern and returns another files object only containing diff --git a/pkg/engine/files_test.go b/pkg/engine/files_test.go index debb3089e..17142d25f 100644 --- a/pkg/engine/files_test.go +++ b/pkg/engine/files_test.go @@ -16,6 +16,9 @@ limitations under the License. package engine import ( + "bytes" + "log" + "os" "testing" "github.com/stretchr/testify/assert" @@ -48,15 +51,15 @@ func TestNewFiles(t *testing.T) { } for i, f := range cases { - gotBytes, err := files.GetBytes(f.path) + gotBytes := files.GetBytes(f.path) got := string(gotBytes) - if err != nil || got != f.data { + if got != f.data { t.Errorf("%d: expected %q, got %q", i, f.data, got) } - gotBytes, err = files.GetBytes(f.path) + gotBytes = files.GetBytes(f.path) got = string(gotBytes) - if err != nil || got != f.data { + if got != f.data { t.Errorf("%d: expected %q, got %q", i, f.data, got) } } @@ -67,9 +70,15 @@ func TestGetNonExistingFile(t *testing.T) { f := getTestFiles() - content, err := f.Get(NonExistingFileName) + var buf bytes.Buffer + log.SetOutput(&buf) + defer func() { + log.SetOutput(os.Stderr) + }() + + content := f.Get(NonExistingFileName) as.Empty(content) - as.Error(err, "not included") + as.Contains(buf.String(), "not included") } func TestFileGlob(t *testing.T) { @@ -81,9 +90,8 @@ func TestFileGlob(t *testing.T) { as.Len(matched, 2, "Should be two files in glob story/**") - content, err := matched.Get("story/author.txt") + content := matched.Get("story/author.txt") as.Equal("Joseph Conrad", content) - as.NoError(err) } func TestToConfig(t *testing.T) {