Skip to content

Commit

Permalink
normalize all integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmenglund committed Nov 5, 2023
1 parent 12145fd commit b9dda55
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 77 deletions.
6 changes: 3 additions & 3 deletions cmd/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down Expand Up @@ -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
},
}
Expand Down Expand Up @@ -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
},
}
Expand Down
39 changes: 16 additions & 23 deletions cmd/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
17 changes: 9 additions & 8 deletions cmd/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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]))
Expand All @@ -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"},
Expand Down Expand Up @@ -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"},
Expand All @@ -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
},
Expand All @@ -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"},
Expand All @@ -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
},
Expand Down Expand Up @@ -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
},
Expand All @@ -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)

Expand Down
70 changes: 70 additions & 0 deletions cmd/api_key_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
2 changes: 1 addition & 1 deletion cmd/query_lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
}
}
Expand Down
21 changes: 5 additions & 16 deletions cmd/query_lambda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
8 changes: 4 additions & 4 deletions cmd/verbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 5 additions & 18 deletions cmd/workspace_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
26 changes: 22 additions & 4 deletions internal/test/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit b9dda55

Please sign in to comment.