diff --git a/cmd/alias.go b/cmd/alias.go index 19e7846..cf9b54b 100644 --- a/cmd/alias.go +++ b/cmd/alias.go @@ -121,7 +121,7 @@ func NewCreateAliasCmd() *cobra.Command { return err } - _, _ = fmt.Fprintf(cmd.OutOrStdout(), "alias %s created", alias.GetName()) + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "alias %s created\n", alias.GetName()) return nil }, } @@ -160,7 +160,7 @@ func NewUpdateAliasCmd() *cobra.Command { return err } - _, _ = fmt.Fprintf(cmd.OutOrStdout(), "alias %s updated", name) + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "alias %s updated\n", name) return nil }, } @@ -197,7 +197,7 @@ func NewDeleteAliasCmd() *cobra.Command { return err } - _, _ = fmt.Fprintf(cmd.OutOrStdout(), "alias %s deleted", name) + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "alias %s deleted\n", name) return nil }, } diff --git a/cmd/alias_test.go b/cmd/alias_test.go index b71dfdd..e36d552 100644 --- a/cmd/alias_test.go +++ b/cmd/alias_test.go @@ -25,43 +25,36 @@ func TestAliasSuite(t *testing.T) { } func (s *AliasTestSuite) Test_0_Create() { - c := cmd.NewCreateAliasCmd() - out := test.Wrapper(c, []string{s.name, "commons._events"}) - err := c.Execute() - s.Require().NoError(err) - s.Equal(fmt.Sprintf("alias %s created", s.name), out.String()) + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(s.T(), c, "create", "alias", s.name, "commons._events") + + s.Equal(fmt.Sprintf("alias %s created\n", s.name), out.String()) } func (s *AliasTestSuite) Test_1_Get() { - c := cmd.NewGetAliasCmd() - out := test.Wrapper(c, []string{s.name}) + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(s.T(), c, "get", "alias", s.name) - err := c.Execute() - s.Require().NoError(err) s.NotEmpty(out.String()) } func (s *AliasTestSuite) Test_2_List() { - c := cmd.NewListAliasesCmd() - out := test.Wrapper(c, []string{}) + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(s.T(), c, "list", "aliases") - err := c.Execute() - s.Require().NoError(err) s.NotEmpty(out.String()) } func (s *AliasTestSuite) Test_3_Update() { - c := cmd.NewUpdateAliasCmd() - out := test.Wrapper(c, []string{s.name, "commons._events"}) - err := c.Execute() - s.Require().NoError(err) - s.NotEmpty(fmt.Sprintf("alias %s deleted", s.name), out.String()) + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(s.T(), c, "update", "alias", s.name, "commons._events") + + s.Equal(fmt.Sprintf("alias %s updated\n", s.name), out.String()) } func (s *AliasTestSuite) Test_4_Delete() { - c := cmd.NewDeleteAliasCmd() - out := test.Wrapper(c, []string{s.name}) - err := c.Execute() - s.Require().NoError(err) - s.NotEmpty(fmt.Sprintf("alias %s deleted", s.name), out.String()) + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(s.T(), c, "delete", "alias", s.name) + + s.Equal(fmt.Sprintf("alias %s deleted\n", s.name), out.String()) } diff --git a/cmd/api_key.go b/cmd/api_key.go index ee0cca7..7d1bcff 100644 --- a/cmd/api_key.go +++ b/cmd/api_key.go @@ -13,7 +13,7 @@ import ( // TODO when implementing create apikey: // the role should have auto-completion for the list of roles the user has access to -func newListAPIKeysCmd() *cobra.Command { +func NewListAPIKeysCmd() *cobra.Command { cmd := cobra.Command{ Use: "apikeys [USER]", Aliases: []string{"ak", "api", "apikey"}, @@ -28,6 +28,7 @@ func newListAPIKeysCmd() *cobra.Command { return err } + logger.Info("args", "args", args) var opts []option.APIKeyOption if len(args) > 0 { opts = append(opts, option.ForUser(args[0])) @@ -52,7 +53,7 @@ func newListAPIKeysCmd() *cobra.Command { return &cmd } -func newGetAPIKeyCmd() *cobra.Command { +func NewGetAPIKeyCmd() *cobra.Command { cmd := cobra.Command{ Use: "apikey NAME", Aliases: []string{"ak"}, @@ -87,7 +88,7 @@ func newGetAPIKeyCmd() *cobra.Command { return &cmd } -func newDeleteAPIKeyCmd() *cobra.Command { +func NewDeleteAPIKeyCmd() *cobra.Command { cmd := cobra.Command{ Use: "apikey NAME", Aliases: []string{"ak"}, @@ -112,7 +113,7 @@ func newDeleteAPIKeyCmd() *cobra.Command { return err } - _, _ = fmt.Fprintf(cmd.OutOrStdout(), "deleted %s\n", args[0]) + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "apikey %s deleted\n", args[0]) return nil }, @@ -124,7 +125,7 @@ func newDeleteAPIKeyCmd() *cobra.Command { return &cmd } -func newCreateAPIKeyCmd() *cobra.Command { +func NewCreateAPIKeyCmd() *cobra.Command { cmd := cobra.Command{ Use: "apikey NAME", Aliases: []string{"ak"}, @@ -148,7 +149,7 @@ func newCreateAPIKeyCmd() *cobra.Command { return err } - _, _ = fmt.Fprintf(cmd.OutOrStdout(), "created %s\n", apikey.GetName()) + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "apikey %s created\n", apikey.GetName()) return nil }, @@ -187,7 +188,7 @@ func newUpdateAPIKeyCmd() *cobra.Command { return err } - _, _ = fmt.Fprintf(cmd.OutOrStdout(), "updated %s to %s\n", apikey.GetName(), apikey.GetState()) + _, _ = fmt.Fprintf(cmd.OutOrStdout(), "apikey %s updated to %s\n", apikey.GetName(), apikey.GetState()) return nil }, @@ -200,7 +201,7 @@ func newUpdateAPIKeyCmd() *cobra.Command { fmt.Sprintf("the state of the apikey, either %s or %s", option.KeyActive, option.KeySuspended)) _ = cmd.RegisterFlagCompletionFunc(StateFlag, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { - return []string{option.KeyActive.String(), option.KeySuspended.String()}, cobra.ShellCompDirectiveNoFileComp + return []string{string(option.KeyActive), string(option.KeySuspended)}, cobra.ShellCompDirectiveNoFileComp }) _ = cobra.MarkFlagRequired(cmd.Flags(), StateFlag) diff --git a/cmd/api_key_test.go b/cmd/api_key_test.go new file mode 100644 index 0000000..0c5a7c6 --- /dev/null +++ b/cmd/api_key_test.go @@ -0,0 +1,70 @@ +//go:build integration + +package cmd_test + +import ( + "fmt" + "github.com/rockset/cli/cmd" + "github.com/rockset/cli/internal/test" + "github.com/rockset/rockset-go-client" + "github.com/rockset/rockset-go-client/option" + "github.com/stretchr/testify/suite" + "testing" +) + +type APIKeySuite struct { + suite.Suite + rc *rockset.RockClient + name string +} + +func TestAPIKeySuite(t *testing.T) { + test.SkipUnlessIntegrationTest(t) + + s := APIKeySuite{ + name: "random", + } + suite.Run(t, &s) +} + +func (s *APIKeySuite) TearDownSuite() { + // try to remove the apikey in case it is lingering + c := cmd.NewRootCmd("test") + _ = test.Wrapper(s.T(), c, "delete", "apikey", s.name) + _ = c.Execute() +} + +func (s *APIKeySuite) Test_0_Create() { + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(s.T(), c, "create", "apikey", s.name) + + s.Equal(fmt.Sprintf("apikey %s created\n", s.name), out.String()) +} + +func (s *APIKeySuite) Test_1_Get() { + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(s.T(), c, "get", "apikey", s.name) + + s.NotEmpty(out.String()) +} + +func (s *APIKeySuite) Test_2_List() { + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(s.T(), c, "list", "apikey") + + s.NotEmpty(out.String()) +} + +func (s *APIKeySuite) Test_3_Update() { + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(s.T(), c, "update", "apikey", s.name, "--state", string(option.KeySuspended)) + + s.Equal(fmt.Sprintf("apikey %s updated to %s\n", s.name, option.KeySuspended), out.String()) +} + +func (s *APIKeySuite) Test_8_Delete() { + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(s.T(), c, "delete", "apikey", s.name) + + s.Equal(fmt.Sprintf("apikey %s deleted\n", s.name), out.String()) +} diff --git a/cmd/query_lambda.go b/cmd/query_lambda.go index 06693db..eae1ba6 100644 --- a/cmd/query_lambda.go +++ b/cmd/query_lambda.go @@ -181,7 +181,7 @@ func NewExecuteQueryLambdaCmd() *cobra.Command { panic("not implemented - need to define file format") } else { for _, p := range params { - fields := strings.SplitN(p, ":", 1) + fields := strings.SplitN(p, ":", 2) opts = append(opts, option.WithQueryLambdaParameter(fields[0], "", fields[1])) } } diff --git a/cmd/query_lambda_test.go b/cmd/query_lambda_test.go index a90a525..4ac648c 100644 --- a/cmd/query_lambda_test.go +++ b/cmd/query_lambda_test.go @@ -3,30 +3,19 @@ package cmd_test import ( - "bytes" "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - "github.com/rockset/cli/cmd" - "github.com/rockset/cli/config" "github.com/rockset/cli/internal/test" + "github.com/stretchr/testify/assert" ) func TestExecuteLambdaCmd(t *testing.T) { test.SkipUnlessIntegrationTest(t) - params := "testdata/params.json" - buf := bytes.NewBufferString("") - c := cmd.NewExecuteQueryLambdaCmd() - err := c.Flags().Set("region", config.Usw2a1) - require.NoError(t, err) - c.SetArgs([]string{"--params", params, "commons.events2"}) - c.SetOut(buf) - - err = c.Execute() + c := cmd.NewRootCmd("test") + // TODO test ql with --param + out := test.WrapAndExecute(t, c, "execute", "ql", "events2") - require.Nil(t, err) - assert.Equal(t, ``, buf.String()) + assert.NotEmpty(t, out.String()) } diff --git a/cmd/verbs.go b/cmd/verbs.go index e50d35c..4f7c053 100644 --- a/cmd/verbs.go +++ b/cmd/verbs.go @@ -146,10 +146,10 @@ func addVerbs(root *cobra.Command) { listCmd.AddCommand(newListRolesCommand()) // API keys - createCmd.AddCommand(newCreateAPIKeyCmd()) - deleteCmd.AddCommand(newDeleteAPIKeyCmd()) - getCmd.AddCommand(newGetAPIKeyCmd()) - listCmd.AddCommand(newListAPIKeysCmd()) + createCmd.AddCommand(NewCreateAPIKeyCmd()) + deleteCmd.AddCommand(NewDeleteAPIKeyCmd()) + getCmd.AddCommand(NewGetAPIKeyCmd()) + listCmd.AddCommand(NewListAPIKeysCmd()) updateCmd.AddCommand(newUpdateAPIKeyCmd()) // query lambda diff --git a/cmd/workspace_test.go b/cmd/workspace_test.go index 84bd58c..420dcb0 100644 --- a/cmd/workspace_test.go +++ b/cmd/workspace_test.go @@ -1,33 +1,20 @@ -//go:build integration +//xgo:build integration package cmd_test import ( - "bytes" - "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" + "testing" "github.com/rockset/cli/cmd" - "github.com/rockset/cli/config" "github.com/rockset/cli/internal/test" ) func TestGetWorkspaceCmd(t *testing.T) { test.SkipUnlessIntegrationTest(t) - buf := &bytes.Buffer{} - c := cmd.NewGetWorkspaceCmd() - c.SetArgs([]string{cmd.DefaultWorkspace}) - err := c.Flags().Set("region", config.Use1a1) - require.NoError(t, err) - c.SetOut(buf) - - err = c.Execute() + c := cmd.NewRootCmd("test") + out := test.WrapAndExecute(t, c, "get", "workspace", cmd.DefaultWorkspace) - require.Nil(t, err) - assert.Equal(t, - `workspace info: {CreatedAt:2020-02-04T18:53:28Z CreatedBy: Name:commons Description:default workspace CollectionCount:4}, -`, buf.String()) + assert.NotEmpty(t, out.String()) } diff --git a/internal/test/cmd.go b/internal/test/cmd.go index 8d0aecf..d6091d3 100644 --- a/internal/test/cmd.go +++ b/internal/test/cmd.go @@ -2,19 +2,37 @@ package test import ( "bytes" + "github.com/rockset/cli/config" + "github.com/stretchr/testify/require" + "log" + "testing" "github.com/spf13/cobra" "github.com/rockset/cli/format" ) -func Wrapper(c *cobra.Command, args []string) *bytes.Buffer { +// Wrapper wraps a cobra.Command and adds the --region and --format flags +func Wrapper(t *testing.T, c *cobra.Command, args ...string) *bytes.Buffer { + t.Helper() + out := &bytes.Buffer{} - // using "format" here to avoid an import cycle - // extract flags into a separate package to fix? - c.Flags().String("format", string(format.JSONFormat), "") + // using string values for flags here to avoid an import cycle, extract flags into a separate package to fix? + require.NoError(t, c.PersistentFlags().Set("format", string(format.JSONFormat))) + require.NoError(t, c.PersistentFlags().Set("cluster", config.Use1a1)) + c.SetOut(out) c.SetArgs(args) return out } + +// WrapAndExecute wraps the command with Wrapper and then executes it +func WrapAndExecute(t *testing.T, c *cobra.Command, args ...string) *bytes.Buffer { + t.Helper() + log.Printf("args: %+v", args) + out := Wrapper(t, c, args...) + require.NoError(t, c.Execute()) + + return out +}