Print warning instead of fail when accessing non-included file

Signed-off-by: itaispiegel <itai.spiegel@gmail.com>
pull/10077/head
itaispiegel 4 years ago
parent 4a3deb8835
commit 0061f66d37

@ -17,9 +17,11 @@ limitations under the License.
package action package action
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -731,7 +733,7 @@ func TestNameAndChartGenerateName(t *testing.T) {
} }
} }
func TestInstallFailsWhenWrongPathsIncluded(t *testing.T) { func TestInstallUsesEmptyContentWrongPathsIncluded(t *testing.T) {
is := assert.New(t) is := assert.New(t)
vals := map[string]interface{}{} 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 { for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
instAction := installAction(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) rel, err := instAction.Run(buildChart(withExternalFileTemplate(tc.ExternalPath)), vals)
expectedErr := fmt.Sprintf("<.Files.Get>: error calling Get: file %s not included", tc.ExternalPath) is.Contains(buf.String(), "not included")
is.Error(err, expectedErr) 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()
}) })
} }
} }

@ -17,8 +17,11 @@ limitations under the License.
package action package action
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"log"
"os"
"testing" "testing"
"time" "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 { for _, tc := range tests {
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
upAction := upgradeAction(t) upAction := upgradeAction(t)
upAction.ExternalPaths = append(upAction.ExternalPaths, tc.IncludedFilePath) if tc.IncludedFilePath != "" {
upAction.ExternalPaths = append(upAction.ExternalPaths, tc.IncludedFilePath)
}
rel := releaseStub() rel := releaseStub()
rel.Name = "test" rel.Name = "test"
rel.Info.Status = release.StatusDeployed rel.Info.Status = release.StatusDeployed
upAction.cfg.Releases.Create(rel) upAction.cfg.Releases.Create(rel)
_, err := upAction.Run(rel.Name, buildChart(withExternalFileTemplate(tc.ExternalPath)), vals) 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.Contains(buf.String(), "not included")
is.Error(err, expectedErr) 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()
}) })
} }
} }

@ -19,6 +19,7 @@ package engine
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"log"
"path" "path"
"strings" "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 // This is intended to be accessed from within a template, so a missed key returns
// an empty []byte. // an empty []byte.
func (f files) GetBytes(name string) ([]byte, error) { func (f files) GetBytes(name string) []byte {
if v, ok := f[name]; ok { 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. // Get returns a string representation of the given file.
@ -60,13 +62,9 @@ func (f files) GetBytes(name string) ([]byte, error) {
// template. // template.
// //
// {{.Files.Get "foo"}} // {{.Files.Get "foo"}}
func (f files) Get(name string) (string, error) { func (f files) Get(name string) string {
content, err := f.GetBytes(name) content := f.GetBytes(name)
if err != nil { return string(content)
return "", err
}
return string(content), nil
} }
// Glob takes a glob pattern and returns another files object only containing // Glob takes a glob pattern and returns another files object only containing

@ -16,6 +16,9 @@ limitations under the License.
package engine package engine
import ( import (
"bytes"
"log"
"os"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -48,15 +51,15 @@ func TestNewFiles(t *testing.T) {
} }
for i, f := range cases { for i, f := range cases {
gotBytes, err := files.GetBytes(f.path) gotBytes := files.GetBytes(f.path)
got := string(gotBytes) got := string(gotBytes)
if err != nil || got != f.data { if got != f.data {
t.Errorf("%d: expected %q, got %q", i, f.data, got) 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) got = string(gotBytes)
if err != nil || got != f.data { if got != f.data {
t.Errorf("%d: expected %q, got %q", i, f.data, got) t.Errorf("%d: expected %q, got %q", i, f.data, got)
} }
} }
@ -67,9 +70,15 @@ func TestGetNonExistingFile(t *testing.T) {
f := getTestFiles() 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.Empty(content)
as.Error(err, "not included") as.Contains(buf.String(), "not included")
} }
func TestFileGlob(t *testing.T) { 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/**") 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.Equal("Joseph Conrad", content)
as.NoError(err)
} }
func TestToConfig(t *testing.T) { func TestToConfig(t *testing.T) {

Loading…
Cancel
Save