command/auth: tests work wihtout vault installed
This commit is contained in:
parent
47a293579f
commit
e40d0874e1
|
@ -13,12 +13,19 @@ import (
|
|||
// DefaultPath is the default path where the Vault token is stored.
|
||||
const DefaultPath = "~/.vault-token"
|
||||
|
||||
type Command struct{}
|
||||
type Command struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
func (c *Command) Run(args []string) int {
|
||||
var path string
|
||||
pathDefault := DefaultPath
|
||||
if c.Path != "" {
|
||||
pathDefault = c.Path
|
||||
}
|
||||
|
||||
f := flag.NewFlagSet("token-disk", flag.ContinueOnError)
|
||||
f.StringVar(&path, "path", DefaultPath, "")
|
||||
f.StringVar(&path, "path", pathDefault, "")
|
||||
f.Usage = func() { fmt.Fprintf(os.Stderr, c.Help()+"\n") }
|
||||
if err := f.Parse(args); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "\n%s\n", err)
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
tokenDisk "github.com/hashicorp/vault/builtin/token/disk"
|
||||
"github.com/hashicorp/vault/command/token"
|
||||
"github.com/mitchellh/cli"
|
||||
)
|
||||
|
||||
|
@ -80,5 +84,15 @@ func testAuthInit(t *testing.T) {
|
|||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Set the HOME env var so we get that right
|
||||
os.Setenv("HOME", td)
|
||||
|
||||
// Write a .vault config to use our custom token helper
|
||||
config := fmt.Sprintf(
|
||||
"token_helper = \"%s\"\n", token.TestProcessPath(t))
|
||||
ioutil.WriteFile(filepath.Join(td, ".vault"), []byte(config), 0644)
|
||||
}
|
||||
|
||||
func TestHelperProcess(t *testing.T) {
|
||||
token.TestHelperProcessCLI(t, &tokenDisk.Command{})
|
||||
}
|
||||
|
|
|
@ -44,25 +44,22 @@ func Test(t *testing.T, path string) {
|
|||
// helper process. For this to work, the TestHelperProcess function must
|
||||
// exist.
|
||||
func TestProcess(t *testing.T, s ...string) {
|
||||
// Build the path to the CLI to execute
|
||||
cs := []string{"-test.run=TestHelperProcess", "--"}
|
||||
Test(t, TestProcessPath(t, s...))
|
||||
}
|
||||
|
||||
// TestProcessPath returns the path to the test process.
|
||||
func TestProcessPath(t *testing.T, s ...string) string {
|
||||
cs := []string{"-test.run=TestHelperProcess", "--", "GO_WANT_HELPER_PROCESS"}
|
||||
cs = append(cs, s...)
|
||||
path := fmt.Sprintf(
|
||||
"GO_WANT_HELPER_PROCESS=1 %s %s",
|
||||
return fmt.Sprintf(
|
||||
"%s %s",
|
||||
os.Args[0],
|
||||
strings.Join(cs, " "))
|
||||
|
||||
// Run the tests
|
||||
Test(t, path)
|
||||
}
|
||||
|
||||
// TestHelperProcessCLI can be called to implement TestHelperProcess
|
||||
// for TestProcess that just executes a CLI command.
|
||||
func TestHelperProcessCLI(t *testing.T, cmd cli.Command) {
|
||||
if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
|
||||
return
|
||||
}
|
||||
|
||||
args := os.Args
|
||||
for len(args) > 0 {
|
||||
if args[0] == "--" {
|
||||
|
@ -72,6 +69,10 @@ func TestHelperProcessCLI(t *testing.T, cmd cli.Command) {
|
|||
|
||||
args = args[1:]
|
||||
}
|
||||
if len(args) == 0 || args[0] != "GO_WANT_HELPER_PROCESS" {
|
||||
return
|
||||
}
|
||||
args = args[1:]
|
||||
|
||||
os.Exit(cmd.Run(args))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue