add tests for backup cmd

This commit is contained in:
Matt Jaffee 2017-03-16 11:54:52 -05:00
parent a1d29537db
commit 9aac0d143f
2 changed files with 54 additions and 0 deletions

39
cmd/backup_test.go Normal file
View file

@ -0,0 +1,39 @@
package cmd_test
import (
"strings"
"testing"
"github.com/pilosa/pilosa/cmd"
)
func TestBackupHelp(t *testing.T) {
output, err := ExecNewRootCommand(t, "backup", "--help")
if !strings.Contains(output, "Usage:") ||
!strings.Contains(output, "Flags:") ||
!strings.Contains(output, "pilosa backup") || err != nil {
t.Fatalf("Command 'backup --help' not working, err: '%v', output: '%s'", err, output)
}
}
func TestBackupConfig(t *testing.T) {
tests := []commandTest{
{
args: []string{"backup", "--output-file", "/somefile"},
env: map[string]string{"PILOSA_HOST": "localhost:12345"},
cfgFileContent: `
database = "mydb"
frame = "f1"
`,
validation: func() error {
v := validator{}
v.Check(cmd.Backuper.Host, "localhost:12345")
v.Check(cmd.Backuper.Database, "mydb")
v.Check(cmd.Backuper.Frame, "f1")
v.Check(cmd.Backuper.Path, "/somefile")
return v.Error()
},
},
}
executeDry(t, tests)
}

View file

@ -112,6 +112,21 @@ func (ct *commandTest) reset() {
}
}
func executeDry(t *testing.T, tests []commandTest) {
for i, test := range tests {
test.args = append(test.args[:1], append([]string{"--dry-run"}, test.args[1:]...)...)
com := test.setupCommand(t)
err := com.Execute()
if err.Error() != "dry run" {
t.Fatalf("Problem with test %d, err: '%v'", i, err)
}
if err := test.validation(); err != nil {
t.Fatalf("Failed test %d due to: %v", i, err)
}
test.reset()
}
}
func TestRootCommand(t *testing.T) {
outStr, err := ExecNewRootCommand(t, "--help")
if !strings.Contains(outStr, "Usage:") ||