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
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()
})
}
}

@ -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()
})
}
}

@ -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

@ -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) {

Loading…
Cancel
Save