tests(helm): add unit tests for setting HELM_HOME

pull/2476/head
Adam Reese 8 years ago
parent 9b7d500fe0
commit faf560c552
No known key found for this signature in database
GPG Key ID: 06F35E60A7A18DD6

@ -19,7 +19,6 @@ package main // import "k8s.io/helm/cmd/helm"
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -45,9 +44,7 @@ var (
tlsKeyFile string // path to TLS key file tlsKeyFile string // path to TLS key file
tlsVerify bool // enable TLS and verify remote certificates tlsVerify bool // enable TLS and verify remote certificates
tlsEnable bool // enable TLS tlsEnable bool // enable TLS
)
var (
kubeContext string kubeContext string
tillerTunnel *kube.Tunnel tillerTunnel *kube.Tunnel
settings helm_env.EnvSettings settings helm_env.EnvSettings
@ -114,7 +111,7 @@ func initRootFlags(cmd *cobra.Command) {
tlsKeyFile = os.ExpandEnv(tlsKeyFile) tlsKeyFile = os.ExpandEnv(tlsKeyFile)
} }
func newRootCmd(out io.Writer) *cobra.Command { func newRootCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "helm", Use: "helm",
Short: "The Helm package manager for Kubernetes.", Short: "The Helm package manager for Kubernetes.",
@ -123,11 +120,12 @@ func newRootCmd(out io.Writer) *cobra.Command {
PersistentPreRun: func(cmd *cobra.Command, _ []string) { PersistentPreRun: func(cmd *cobra.Command, _ []string) {
initRootFlags(cmd) initRootFlags(cmd)
}, },
PersistentPostRun: func(_ *cobra.Command, _ []string) { PersistentPostRun: func(*cobra.Command, []string) {
teardown() teardown()
}, },
} }
addRootFlags(cmd) addRootFlags(cmd)
out := cmd.OutOrStdout()
cmd.AddCommand( cmd.AddCommand(
// chart commands // chart commands
@ -157,7 +155,7 @@ func newRootCmd(out io.Writer) *cobra.Command {
addFlagsTLS(newVersionCmd(nil, out)), addFlagsTLS(newVersionCmd(nil, out)),
newCompletionCmd(out), newCompletionCmd(out),
newHomeCmd(out), newHomeCmd(),
newInitCmd(out), newInitCmd(out),
newPluginCmd(out), newPluginCmd(out),
@ -180,7 +178,7 @@ func init() {
} }
func main() { func main() {
cmd := newRootCmd(os.Stdout) cmd := newRootCmd()
if err := cmd.Execute(); err != nil { if err := cmd.Execute(); err != nil {
os.Exit(1) os.Exit(1)
} }

@ -23,6 +23,7 @@ import (
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"os" "os"
"path/filepath"
"regexp" "regexp"
"sync" "sync"
"testing" "testing"
@ -337,3 +338,77 @@ func ensureTestHome(home helmpath.Home, t *testing.T) error {
t.Logf("$HELM_HOME has been configured at %s.\n", settings.Home.String()) t.Logf("$HELM_HOME has been configured at %s.\n", settings.Home.String())
return nil return nil
} }
func TestRootCmd(t *testing.T) {
oldhome := os.Getenv("HELM_HOME")
defer os.Setenv("HELM_HOME", oldhome)
tests := []struct {
name string
args []string
envars map[string]string
home string
}{
{
name: "defaults",
home: filepath.Join(os.Getenv("HOME"), "/.helm"),
},
{
name: "with --home set",
args: []string{"--home", "/foo"},
home: "/foo",
},
{
name: "subcommands with --home set",
args: []string{"home", "--home", "/foo"},
home: "/foo",
},
{
name: "with $HELM_HOME set",
envars: map[string]string{"HELM_HOME": "/bar"},
home: "/bar",
},
{
name: "subcommands with $HELM_HOME set",
args: []string{"home"},
envars: map[string]string{"HELM_HOME": "/bar"},
home: "/bar",
},
{
name: "with $HELM_HOME and --home set",
args: []string{"home", "--home", "/foo"},
envars: map[string]string{"HELM_HOME": "/bar"},
home: "/foo",
},
}
// ensure not set locally
os.Unsetenv("HELM_HOME")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer os.Unsetenv("HELM_HOME")
for k, v := range tt.envars {
os.Setenv(k, v)
}
cmd := newRootCmd()
cmd.SetOutput(ioutil.Discard)
cmd.SetArgs(tt.args)
cmd.Run = func(*cobra.Command, []string) {}
if err := cmd.Execute(); err != nil {
t.Errorf("unexpected error: %s", err)
}
if settings.Home.String() != tt.home {
t.Errorf("expected home %q, got %q", tt.home, settings.Home)
}
homeFlag := cmd.Flag("home").Value.String()
homeFlag = os.ExpandEnv(homeFlag)
if homeFlag != tt.home {
t.Errorf("expected home %q, got %q", tt.home, homeFlag)
}
})
}
}

@ -17,9 +17,6 @@ limitations under the License.
package main package main
import ( import (
"fmt"
"io"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -28,25 +25,24 @@ This command displays the location of HELM_HOME. This is where
any helm configuration files live. any helm configuration files live.
` `
func newHomeCmd(out io.Writer) *cobra.Command { func newHomeCmd() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "home", Use: "home",
Short: "displays the location of HELM_HOME", Short: "displays the location of HELM_HOME",
Long: longHomeHelp, Long: longHomeHelp,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
h := settings.Home h := settings.Home
fmt.Fprintf(out, "%s\n", h) cmd.Println(h)
if settings.Debug { if settings.Debug {
fmt.Fprintf(out, "Repository: %s\n", h.Repository()) cmd.Printf("Repository: %s\n", h.Repository())
fmt.Fprintf(out, "RepositoryFile: %s\n", h.RepositoryFile()) cmd.Printf("RepositoryFile: %s\n", h.RepositoryFile())
fmt.Fprintf(out, "Cache: %s\n", h.Cache()) cmd.Printf("Cache: %s\n", h.Cache())
fmt.Fprintf(out, "Stable CacheIndex: %s\n", h.CacheIndex("stable")) cmd.Printf("Stable CacheIndex: %s\n", h.CacheIndex("stable"))
fmt.Fprintf(out, "Starters: %s\n", h.Starters()) cmd.Printf("Starters: %s\n", h.Starters())
fmt.Fprintf(out, "LocalRepository: %s\n", h.LocalRepository()) cmd.Printf("LocalRepository: %s\n", h.LocalRepository())
fmt.Fprintf(out, "Plugins: %s\n", h.Plugins()) cmd.Printf("Plugins: %s\n", h.Plugins())
} }
}, },
} }
return cmd return cmd
} }

Loading…
Cancel
Save