Helm resolves its cache, config and data homes from `$HOME`, but dokku runs `scheduler-k3s:initialize` as root and every other subcommand as the dokku user, so those paths are not always readable by the user a command ends up as. Helm treats a repositories file it cannot read as a fatal error rather than an absent one, which made chart resolution fail outright even though dokku never reads an entry from that file. Each call now points helm at a private scratch directory that is removed once the chart has been located.
227 lines
5.7 KiB
Go
227 lines
5.7 KiB
Go
package scheduler_k3s
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"helm.sh/helm/v3/pkg/action"
|
|
"helm.sh/helm/v3/pkg/helmpath"
|
|
)
|
|
|
|
func TestApplyChartPathOptions(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
options action.ChartPathOptions
|
|
input ChartInput
|
|
wantRepoURL string
|
|
wantVersion string
|
|
}{
|
|
{
|
|
name: "pins both the repository and the version",
|
|
input: ChartInput{RepoURL: "https://helm.traefik.io/traefik", Version: "26.0.0"},
|
|
wantRepoURL: "https://helm.traefik.io/traefik",
|
|
wantVersion: "26.0.0",
|
|
},
|
|
{
|
|
name: "an empty version leaves the field unset",
|
|
input: ChartInput{RepoURL: "https://helm.traefik.io/traefik"},
|
|
wantRepoURL: "https://helm.traefik.io/traefik",
|
|
wantVersion: "",
|
|
},
|
|
{
|
|
name: "an empty repository leaves the field unset",
|
|
input: ChartInput{Version: "26.0.0"},
|
|
wantRepoURL: "",
|
|
wantVersion: "26.0.0",
|
|
},
|
|
{
|
|
name: "an empty input leaves both fields unset",
|
|
input: ChartInput{},
|
|
wantRepoURL: "",
|
|
wantVersion: "",
|
|
},
|
|
{
|
|
name: "existing values are preserved when the input is empty",
|
|
options: action.ChartPathOptions{RepoURL: "https://charts.jetstack.io", Version: "v1.13.3"},
|
|
input: ChartInput{},
|
|
wantRepoURL: "https://charts.jetstack.io",
|
|
wantVersion: "v1.13.3",
|
|
},
|
|
{
|
|
name: "existing values are overridden by the input",
|
|
options: action.ChartPathOptions{RepoURL: "https://charts.jetstack.io", Version: "v1.13.3"},
|
|
input: ChartInput{RepoURL: "https://helm.traefik.io/traefik", Version: "26.0.0"},
|
|
wantRepoURL: "https://helm.traefik.io/traefik",
|
|
wantVersion: "26.0.0",
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
options := tc.options
|
|
applyChartPathOptions(&options, tc.input)
|
|
|
|
if options.RepoURL != tc.wantRepoURL {
|
|
t.Errorf("RepoURL = %q, want %q", options.RepoURL, tc.wantRepoURL)
|
|
}
|
|
if options.Version != tc.wantVersion {
|
|
t.Errorf("Version = %q, want %q", options.Version, tc.wantVersion)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func makeRevisions(revisions ...int) []Release {
|
|
out := make([]Release, 0, len(revisions))
|
|
for _, revision := range revisions {
|
|
out = append(out, Release{Name: "traefik", Revision: revision})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func revisionNumbers(releases []Release) []int {
|
|
out := make([]int, 0, len(releases))
|
|
for _, release := range releases {
|
|
out = append(out, release.Revision)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func equalInts(a, b []int) bool {
|
|
if len(a) != len(b) {
|
|
return false
|
|
}
|
|
for i := range a {
|
|
if a[i] != b[i] {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func TestSelectRevisions(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
releases []Release
|
|
max int
|
|
want []int
|
|
}{
|
|
{
|
|
name: "sorts an unordered ledger ascending",
|
|
releases: makeRevisions(3, 1, 2),
|
|
max: 0,
|
|
want: []int{1, 2, 3},
|
|
},
|
|
{
|
|
name: "a max of one keeps the newest revision",
|
|
releases: makeRevisions(3, 1, 2),
|
|
max: 1,
|
|
want: []int{3},
|
|
},
|
|
{
|
|
name: "a max below the ledger size keeps the newest revisions",
|
|
releases: makeRevisions(4, 1, 3, 2),
|
|
max: 2,
|
|
want: []int{3, 4},
|
|
},
|
|
{
|
|
name: "a max above the ledger size keeps everything",
|
|
releases: makeRevisions(2, 1),
|
|
max: 10,
|
|
want: []int{1, 2},
|
|
},
|
|
{
|
|
name: "an empty ledger stays empty",
|
|
releases: []Release{},
|
|
max: 1,
|
|
want: []int{},
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
got := revisionNumbers(selectRevisions(tc.releases, tc.max))
|
|
if !equalInts(got, tc.want) {
|
|
t.Errorf("selectRevisions() = %v, want %v", got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewHelmSettings(t *testing.T) {
|
|
sentinel := map[string]string{
|
|
helmpath.CacheHomeEnvVar: "/sentinel/cache",
|
|
helmpath.ConfigHomeEnvVar: "/sentinel/config",
|
|
helmpath.DataHomeEnvVar: "/sentinel/data",
|
|
}
|
|
for envVar, value := range sentinel {
|
|
t.Setenv(envVar, value)
|
|
}
|
|
|
|
settings, cleanup, err := newHelmSettings()
|
|
if err != nil {
|
|
t.Fatalf("newHelmSettings() returned an error: %v", err)
|
|
}
|
|
|
|
directory := os.Getenv(helmpath.CacheHomeEnvVar)
|
|
if directory == sentinel[helmpath.CacheHomeEnvVar] {
|
|
t.Fatalf("%s was not overridden", helmpath.CacheHomeEnvVar)
|
|
}
|
|
scratch := filepath.Dir(directory)
|
|
|
|
if _, err := os.Stat(scratch); err != nil {
|
|
t.Fatalf("scratch directory %q is not usable: %v", scratch, err)
|
|
}
|
|
|
|
for envVar, subdirectory := range helmHomeEnvVars {
|
|
want := filepath.Join(scratch, subdirectory)
|
|
if got := os.Getenv(envVar); got != want {
|
|
t.Errorf("%s = %q, want %q", envVar, got, want)
|
|
}
|
|
}
|
|
|
|
paths := map[string]string{
|
|
"RepositoryConfig": settings.RepositoryConfig,
|
|
"RepositoryCache": settings.RepositoryCache,
|
|
"RegistryConfig": settings.RegistryConfig,
|
|
}
|
|
for name, path := range paths {
|
|
if !strings.HasPrefix(path, scratch+string(os.PathSeparator)) {
|
|
t.Errorf("%s = %q, want a path under %q", name, path, scratch)
|
|
}
|
|
}
|
|
|
|
cleanup()
|
|
|
|
if _, err := os.Stat(scratch); !os.IsNotExist(err) {
|
|
t.Errorf("scratch directory %q still exists after cleanup", scratch)
|
|
}
|
|
|
|
for envVar, want := range sentinel {
|
|
if got := os.Getenv(envVar); got != want {
|
|
t.Errorf("%s = %q after cleanup, want the prior value %q", envVar, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNewHelmSettingsUnsetsVariablesThatWereNotSet(t *testing.T) {
|
|
for envVar := range helmHomeEnvVars {
|
|
t.Setenv(envVar, "")
|
|
os.Unsetenv(envVar) // nolint: errcheck
|
|
}
|
|
|
|
_, cleanup, err := newHelmSettings()
|
|
if err != nil {
|
|
t.Fatalf("newHelmSettings() returned an error: %v", err)
|
|
}
|
|
cleanup()
|
|
|
|
for envVar := range helmHomeEnvVars {
|
|
if value, ok := os.LookupEnv(envVar); ok {
|
|
t.Errorf("%s = %q after cleanup, want it to be unset", envVar, value)
|
|
}
|
|
}
|
|
}
|