From 74b79dd92879b3e2eb4fccb09675c241f608e46a Mon Sep 17 00:00:00 2001 From: Matt Jaffee Date: Thu, 16 Mar 2017 13:34:34 -0500 Subject: [PATCH] add tests for subcommands --- cmd/bench_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ cmd/check_test.go | 22 ++++++++++++++++++++++ cmd/export_test.go | 39 +++++++++++++++++++++++++++++++++++++++ cmd/import.go | 2 +- cmd/import_test.go | 38 ++++++++++++++++++++++++++++++++++++++ cmd/restore_test.go | 39 +++++++++++++++++++++++++++++++++++++++ cmd/root.go | 2 +- cmd/root_test.go | 1 - cmd/server_test.go | 2 ++ cmd/sort_test.go | 29 +++++++++++++++++++++++++++++ 10 files changed, 211 insertions(+), 3 deletions(-) create mode 100644 cmd/bench_test.go create mode 100644 cmd/check_test.go create mode 100644 cmd/export_test.go create mode 100644 cmd/import_test.go create mode 100644 cmd/restore_test.go create mode 100644 cmd/sort_test.go diff --git a/cmd/bench_test.go b/cmd/bench_test.go new file mode 100644 index 000000000..2b5e2c872 --- /dev/null +++ b/cmd/bench_test.go @@ -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) +} diff --git a/cmd/check_test.go b/cmd/check_test.go new file mode 100644 index 000000000..c65e38aa4 --- /dev/null +++ b/cmd/check_test.go @@ -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) + } +} diff --git a/cmd/export_test.go b/cmd/export_test.go new file mode 100644 index 000000000..d546a839a --- /dev/null +++ b/cmd/export_test.go @@ -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) +} diff --git a/cmd/import.go b/cmd/import.go index 4cad3ff28..c29d62800 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -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.", diff --git a/cmd/import_test.go b/cmd/import_test.go new file mode 100644 index 000000000..ac98539a4 --- /dev/null +++ b/cmd/import_test.go @@ -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) +} diff --git a/cmd/restore_test.go b/cmd/restore_test.go new file mode 100644 index 000000000..ef8a90d32 --- /dev/null +++ b/cmd/restore_test.go @@ -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) +} diff --git a/cmd/root.go b/cmd/root.go index a2d25e780..7448cb256 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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 diff --git a/cmd/root_test.go b/cmd/root_test.go index 7e132137f..74dac2919 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -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 diff --git a/cmd/server_test.go b/cmd/server_test.go index 4759065b1..d677d67a8 100644 --- a/cmd/server_test.go +++ b/cmd/server_test.go @@ -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 = [ diff --git a/cmd/sort_test.go b/cmd/sort_test.go new file mode 100644 index 000000000..dbc5e8972 --- /dev/null +++ b/cmd/sort_test.go @@ -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) + } +}