add tests for subcommands

This commit is contained in:
Matt Jaffee 2017-03-16 13:34:34 -05:00
parent 9aac0d143f
commit 74b79dd928
10 changed files with 211 additions and 3 deletions

40
cmd/bench_test.go Normal file
View file

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

22
cmd/check_test.go Normal file
View file

@ -0,0 +1,22 @@
package cmd_test
import (
"strings"
"testing"
)
func TestCheckHelp(t *testing.T) {
output, err := ExecNewRootCommand(t, "check", "--help")
if !strings.Contains(output, "Usage:") ||
!strings.Contains(output, "Flags:") ||
!strings.Contains(output, "pilosa check") || err != nil {
t.Fatalf("Command 'check --help' not working, err: '%v', output: '%s'", err, output)
}
}
func TestCheckNoPath(t *testing.T) {
output, err := ExecNewRootCommand(t, "check")
if !strings.Contains(err.Error(), "path required") {
t.Fatalf("Command 'check' without args should error but: err: '%v', output: '%v'", err, output)
}
}

39
cmd/export_test.go Normal file
View file

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

View file

@ -12,7 +12,7 @@ import (
var Importer *ctl.ImportCommand
func NewImportCommand(stdin io.Reader, stdout, stderr io.Writer) *cobra.Command {
Importer := ctl.NewImportCommand(stdin, stdout, stderr)
Importer = ctl.NewImportCommand(stdin, stdout, stderr)
importCmd := &cobra.Command{
Use: "import",
Short: "Bulk load data into pilosa.",

38
cmd/import_test.go Normal file
View file

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

39
cmd/restore_test.go Normal file
View file

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

View file

@ -123,7 +123,7 @@ func setAllConfig(v *viper.Viper, flags *flag.FlagSet, envPrefix string) error {
} else {
value = v.GetString(f.Name)
}
fmt.Printf("Visiting '%v' with value '%v', changed: '%v', new value: '%v'\n", f.Name, f.Value, f.Changed, value)
if f.Changed {
// If f.Changed is true, that means the value has already been set
// by a flag, and we don't need to ask viper for it since the flag

View file

@ -32,7 +32,6 @@ func tExec(t *testing.T, cmd *cobra.Command, out io.Reader, w io.WriteCloser) (o
output, readErr = ioutil.ReadAll(out)
close(done)
}()
fmt.Println("executing")
err = cmd.Execute()
if err != nil {
return output, err

View file

@ -55,6 +55,7 @@ bind = "localhost:0"
env: map[string]string{"PILOSA_CLUSTER.HOSTS": "example.com:1110,example.com:1111"},
cfgFileContent: `
bind = "localhost:0"
data-dir = "` + actualDataDir + `"
[cluster]
hosts = [
"localhost:19444",
@ -76,6 +77,7 @@ bind = "localhost:0"
env: map[string]string{"PILOSA_PROFILE.CPU_TIME": "1m"},
cfgFileContent: `
bind = "localhost:0"
data-dir = "` + actualDataDir + `"
[cluster]
poll-interval = "2m0s"
hosts = [

29
cmd/sort_test.go Normal file
View file

@ -0,0 +1,29 @@
package cmd_test
import (
"strings"
"testing"
)
func TestSortHelp(t *testing.T) {
output, err := ExecNewRootCommand(t, "sort", "--help")
if !strings.Contains(output, "Usage:") ||
!strings.Contains(output, "Flags:") ||
!strings.Contains(output, "pilosa sort") || err != nil {
t.Fatalf("Command 'sort --help' not working, err: '%v', output: '%s'", err, output)
}
}
func TestSortNoPath(t *testing.T) {
output, err := ExecNewRootCommand(t, "sort")
if !strings.Contains(err.Error(), "path required") {
t.Fatalf("Command 'sort' without args should error but: err: '%v', output: '%v'", err, output)
}
}
func TestSortMultiPath(t *testing.T) {
output, err := ExecNewRootCommand(t, "sort", "one", "two")
if !strings.Contains(err.Error(), "only one path") {
t.Fatalf("Command 'sort' without args should error but: err: '%v', output: '%v'", err, output)
}
}