Files
helmfile/pkg/state/envvals_loader_test.go
T
DominicandYusuke Kuoka 789af92c09 Adds feature to fetch release values and secret values from remote (#47)
* Adds feature to fetch environment values from remote

The releases and environment section allow for values files on the local
disk.
This enhancement allows for referencing remote (go-getter) files to be
fetched, cached and referenced.

In addition when fetching a remote git source with a ssh key the ssh key
will not be part of the caching folder name. This avoids two problems:
1. Don't leak sensitive information in the name of the caching folder
2. Base64 encoded SSH keys are very long. On some file systems the max
lenght of the directory name is hit when using the full base64
information in the path name.

The sshkey informations are reducted. Because of this fixed string
there is a change of colloding cache names. The likelihood of this
collision is very low. The git repo and git reference need to be the
same, but the sshkey can change. This will result in the same source to
be checkout out and referenced.

Signed-off-by: Lüchinger Dominic <dev@snowgarden.ch>

* Update pkg/state/storage.go

Co-authored-by: Yusuke Kuoka <ykuoka@gmail.com>
2022-06-05 15:08:38 +09:00

140 lines
3.3 KiB
Go

package state
import (
"os"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/helmfile/helmfile/pkg/remote"
"go.uber.org/zap"
)
func newLoader() *EnvironmentValuesLoader {
log, err := zap.NewDevelopment(zap.AddStacktrace(zap.DebugLevel))
if err != nil {
panic(err)
}
sugar := log.Sugar()
storage := &Storage{
FilePath: "./helmfile.yaml",
basePath: ".",
glob: filepath.Glob,
logger: sugar,
}
readFile := func(s string) ([]byte, error) { return []byte{}, nil }
dirExists := func(d string) bool { return false }
fileExists := func(f string) bool { return false }
return NewEnvironmentValuesLoader(storage, os.ReadFile, sugar, remote.NewRemote(sugar, "/tmp", readFile, dirExists, fileExists))
}
// See https://github.com/roboll/helmfile/pull/1169
func TestEnvValsLoad_SingleValuesFile(t *testing.T) {
l := newLoader()
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.5.yaml"}, nil)
if err != nil {
t.Fatal(err)
}
expected := map[string]interface{}{
"affinity": map[string]interface{}{},
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}
}
// Fetch Environment values from remote
func TestEnvValsLoad_SingleValuesFileRemote(t *testing.T) {
l := newLoader()
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"git::https://github.com/helm/helm.git@cmd/helm/testdata/output/values.yaml?ref=v3.8.0"}, nil)
if err != nil {
t.Fatal(err)
}
expected := map[string]interface{}{
"name": string("value"),
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}
}
// See https://github.com/roboll/helmfile/issues/1150
func TestEnvValsLoad_OverwriteNilValue_Issue1150(t *testing.T) {
l := newLoader()
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.1.yaml", "testdata/values.2.yaml"}, nil)
if err != nil {
t.Fatal(err)
}
expected := map[string]interface{}{
"components": map[string]interface{}{
"etcd-operator": map[string]interface{}{
"version": "0.10.3",
},
},
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}
}
// See https://github.com/roboll/helmfile/issues/1154
func TestEnvValsLoad_OverwriteWithNilValue_Issue1154(t *testing.T) {
l := newLoader()
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/values.3.yaml", "testdata/values.4.yaml"}, nil)
if err != nil {
t.Fatal(err)
}
expected := map[string]interface{}{
"components": map[string]interface{}{
"etcd-operator": map[string]interface{}{
"version": "0.10.3",
},
"prometheus": nil,
},
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}
}
// See https://github.com/roboll/helmfile/issues/1168
func TestEnvValsLoad_OverwriteEmptyValue_Issue1168(t *testing.T) {
l := newLoader()
actual, err := l.LoadEnvironmentValues(nil, []interface{}{"testdata/issues/1168/addons.yaml", "testdata/issues/1168/addons2.yaml"}, nil)
if err != nil {
t.Fatal(err)
}
expected := map[string]interface{}{
"addons": map[string]interface{}{
"mychart": map[string]interface{}{
"skip": false,
"name": "mychart",
"namespace": "kube-system",
"chart": "stable/mychart",
"version": "1.0.0",
},
},
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}
}