Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(cli): Add unit tests and restructure how cmds are invoked #92

Merged
merged 39 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
3407a0b
Move commands to their own functions
Mar 16, 2021
7af3e82
feat(cli): Add test for root cmd
Mar 18, 2021
f2756fd
Remove test
Mar 22, 2021
8fb20ea
Add list test
Mar 22, 2021
1cac755
Add create test
Mar 22, 2021
9237e82
Add `add` test
Mar 22, 2021
96919c2
Add describe test
Mar 22, 2021
d37a84b
Add open test
Mar 22, 2021
77062a8
Add remove test
Mar 22, 2021
22a021d
Add update test
Mar 22, 2021
9ffd1cc
Add test for adding a resource
Mar 22, 2021
fd6bbfb
Rename display to utils pkg
Mar 22, 2021
b9c0b97
Add license
Mar 22, 2021
9f3a721
Separate auth cmds
Mar 22, 2021
3b3d1fd
Move functions used to its cmd file
Mar 22, 2021
be07e0e
Add connect test
Mar 22, 2021
f091b9b
Add create connector test
Mar 22, 2021
4dd718a
Refactor cmds
Mar 22, 2021
17dcf9c
Add create endpoint test
Mar 22, 2021
86eb100
Add create pipeline test
Mar 22, 2021
6bf1b30
Add describe connector test
Mar 23, 2021
dc7e5d9
Add describe endpoint test
Mar 23, 2021
5ca7db7
Add describe resource test
Mar 23, 2021
37f2036
Add logs test
Mar 23, 2021
1c951fc
Add remove connector test
Mar 23, 2021
cd8ef9b
Add remove endpoint test
Mar 23, 2021
6ffdb2e
Add remove pipeline test
Mar 23, 2021
155c5f1
Add remove resource test
Mar 23, 2021
550a24d
Add update connector test
Mar 23, 2021
206ebd6
Add update pipeline test
Mar 23, 2021
4514a25
Add instructions for running tests
Mar 23, 2021
714078e
Add update resource test
Mar 23, 2021
dbedcf5
Remove unnecessary code
Mar 23, 2021
07fadb8
Update comments
Mar 23, 2021
3d3356f
Fix cmd file name
Mar 23, 2021
6c874f4
Separate logs connector to its own file
Mar 23, 2021
f7ba760
Remove duplicate make build
Mar 23, 2021
925a77a
Update test
Mar 23, 2021
57a973c
Clearer var name
Mar 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ gomod:

.PHONY: test
test:
go test -v ${GO_TEST_FLAGS} -count=1 ./...
go test -v ${GO_TEST_FLAGS} -count=1 -timeout 5m ./...

.PHONY: docs
docs:
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ git tag is pushed to the repo.
* Tag - `git tag -a vX.X.X -m "<message goes here>"`
* Push - `git push origin vX.X.X`

## Tests

```
make test
```

## Contributing

See [CONTRIBUTING.md](/CONTRIBUTING.md).
15 changes: 9 additions & 6 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import (

var resName, resType string

var addCmd = &cobra.Command{
Use: "add",
Short: "Add a resource to your Meroxa resource catalog",
}
// AddCmd represents the `meroxa add` command
func AddCmd() *cobra.Command {
addCmd := &cobra.Command{
Use: "add",
Short: "Add a resource to your Meroxa resource catalog",
}

addCmd.AddCommand(AddResourceCmd())

func init() {
RootCmd.AddCommand(addCmd)
return addCmd
}
131 changes: 66 additions & 65 deletions cmd/add_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,87 +22,86 @@ import (
"errors"
"fmt"

"github.com/meroxa/cli/display"
"github.com/meroxa/cli/utils"
"github.com/meroxa/meroxa-go"
"github.com/spf13/cobra"
)

var addResourceCmd = &cobra.Command{
Use: "resource <resource-name> --type <resource-type>",
Short: "Add a resource to your Meroxa resource catalog",
Long: `Use the add command to add resources to your Meroxa resource catalog.`,
Example: "\n" +
"meroxa add resource store --type postgres -u $DATABASE_URL\n" +
"meroxa add resource datalake --type s3 -u \"s3://$AWS_ACCESS_KEY_ID:$AWS_ACCESS_KEY_SECRET@us-east-1/meroxa-demos\"\n" +
"meroxa add resource warehouse --type redshift -u $REDSHIFT_URL\n" +
"meroxa add resource slack --type url -u $WEBHOOK_URL\n",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires resource name\n\nUsage:\n meroxa add resource <resource-name> [flags]")
}

resName = args[0]

c, err := client()

if err != nil {
return err
}

r := meroxa.CreateResourceInput{
Type: resType,
Name: resName,
URL: resURL,
Metadata: nil,
}

// TODO: Figure out best way to handle creds and metadata
// Get credentials (expect a JSON string)
if resCredentials != "" {
var creds meroxa.Credentials
err = json.Unmarshal([]byte(resCredentials), &creds)
if err != nil {
return err
// AddResourceCmd represents the `meroxa add resource` command
func AddResourceCmd() *cobra.Command {
addResourceCmd := &cobra.Command{
Use: "resource <resource-name> --type <resource-type>",
Short: "Add a resource to your Meroxa resource catalog",
Long: `Use the add command to add resources to your Meroxa resource catalog.`,
Example: "\n" +
"meroxa add resource store --type postgres -u $DATABASE_URL\n" +
"meroxa add resource datalake --type s3 -u \"s3://$AWS_ACCESS_KEY_ID:$AWS_ACCESS_KEY_SECRET@us-east-1/meroxa-demos\"\n" +
"meroxa add resource warehouse --type redshift -u $REDSHIFT_URL\n" +
"meroxa add resource slack --type url -u $WEBHOOK_URL\n",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires resource name\n\nUsage:\n meroxa add resource <resource-name> [flags]")
}

r.Credentials = &creds
}
resName = args[0]

c, err := client()

if resMetadata != "" {
var metadata map[string]string
err = json.Unmarshal([]byte(resMetadata), &metadata)
if err != nil {
return err
}

r.Metadata = metadata
}
r := meroxa.CreateResourceInput{
Type: resType,
Name: resName,
URL: resURL,
Metadata: nil,
}

ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, clientTimeOut)
defer cancel()
// TODO: Figure out best way to handle creds and metadata
// Get credentials (expect a JSON string)
if resCredentials != "" {
var creds meroxa.Credentials
err = json.Unmarshal([]byte(resCredentials), &creds)
if err != nil {
return err
}

if !flagRootOutputJSON {
fmt.Printf("Adding %s resource (%s)...\n", resName, resType)
}
r.Credentials = &creds
}

res, err := c.CreateResource(ctx, &r)
if err != nil {
return err
}
if resMetadata != "" {
var metadata map[string]string
err = json.Unmarshal([]byte(resMetadata), &metadata)
if err != nil {
return err
}

if flagRootOutputJSON {
display.JSONPrint(res)
} else {
fmt.Printf("Resource %s successfully added!\n", res.Name)
}
r.Metadata = metadata
}

return nil
},
}
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, clientTimeOut)
defer cancel()

func init() {
addCmd.AddCommand(addResourceCmd)
if !flagRootOutputJSON {
fmt.Printf("Adding %s resource (%s)...\n", resName, resType)
}

res, err := c.CreateResource(ctx, &r)
if err != nil {
return err
}

if flagRootOutputJSON {
utils.JSONPrint(res)
} else {
fmt.Printf("Resource %s successfully added!\n", res.Name)
}

return nil
},
}

addResourceCmd.Flags().StringVarP(&resType, "type", "", "", "resource type")
addResourceCmd.MarkFlagRequired("type")
Expand All @@ -112,4 +111,6 @@ func init() {

addResourceCmd.Flags().StringVarP(&resCredentials, "credentials", "", "", "resource credentials")
addResourceCmd.Flags().StringVarP(&resMetadata, "metadata", "m", "", "resource metadata")
}

return addResourceCmd
}
47 changes: 47 additions & 0 deletions cmd/add_resource_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmd

import (
"bytes"
"io/ioutil"
"strings"
"testing"
)

func TestAddResourceCmd(t *testing.T) {
tests := []struct {
expected string
args []string
}{
{
"Error: required flag(s) \"type\", \"url\" not set",
[]string{"add", "resource"},
},
{
"Error: required flag(s) \"type\" not set",
[]string{"add", "resource", "--url", "myUrl"},
},
{
"Error: requires resource name",
[]string{"add", "resource", "--url", "myUrl", "--type", "postgres"},
},
// TODO: Add a test with resource name as argument and mocking the call
}

for _, tt := range tests {
rootCmd := RootCmd()
b := bytes.NewBufferString("")
rootCmd.SetOut(b)
rootCmd.SetErr(b)
rootCmd.SetArgs(tt.args)
rootCmd.Execute()
output, err := ioutil.ReadAll(b)

if err != nil {
t.Fatal(err)
}

if !strings.Contains(string(output), tt.expected) {
t.Fatalf("expected \"%s\" got \"%s\"", tt.expected, string(output))
}
}
}
40 changes: 40 additions & 0 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cmd

import (
"bytes"
"io/ioutil"
"strings"
"testing"
)

func TestAddCmd(t *testing.T) {
tests := []struct {
expected string
}{
{"Add a resource to your Meroxa resource catalog"},
{"Usage:\n" +
" meroxa add [command]"},
{"Available Commands:"},
{"resource Add a resource to your Meroxa resource catalog"},
{"Flags:\n" +
" -h, --help help for add"},
}

rootCmd := RootCmd()
b := bytes.NewBufferString("")
rootCmd.SetOut(b)
rootCmd.SetArgs([]string{"add"})
rootCmd.Execute()

out, err := ioutil.ReadAll(b)

if err != nil {
t.Fatal(err)
}

for _, tt := range tests {
if !strings.Contains(string(out), tt.expected) {
t.Fatalf("expected \"%s\" got \"%s\"", tt.expected, string(out))
}
}
}
84 changes: 42 additions & 42 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,54 @@ import (
"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(apiCmd)
}

var apiCmd = &cobra.Command{
Use: "api <method> <path> [body]",
Short: "Invoke Meroxa API",
Args: cobra.MinimumNArgs(2),
Example: `
// ApiCmd represents the `meroxa api` command
func ApiCmd() *cobra.Command {
return &cobra.Command{
Use: "api <method> <path> [body]",
Short: "Invoke Meroxa API",
Args: cobra.MinimumNArgs(2),
Example: `
meroxa api GET /v1/endpoints
meroxa api POST /v1/endpoints '{"protocol": "HTTP", "stream": "resource-2-499379-public.accounts", "name": "1234"}'`,
RunE: func(cmd *cobra.Command, args []string) error {
c, err := client()
if err != nil {
return err
}
RunE: func(cmd *cobra.Command, args []string) error {
c, err := client()
if err != nil {
return err
}

var (
method = args[0]
path = args[1]
body string
)
var (
method = args[0]
path = args[1]
body string
)

if len(args) > 2 {
body = args[2]
}
if len(args) > 2 {
body = args[2]
}

resp, err := c.MakeRequestString(context.Background(), method, path, body)
if err != nil {
return err
}
resp, err := c.MakeRequestString(context.Background(), method, path, body)
if err != nil {
return err
}

b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, b, "", "\t"); err != nil {
prettyJSON.Write(b)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var prettyJSON bytes.Buffer
if err := json.Indent(&prettyJSON, b, "", "\t"); err != nil {
prettyJSON.Write(b)
}

fmt.Printf("> %s %s\n", method, path)
fmt.Printf("< %s %s\n", resp.Status, resp.Proto)
for k, v := range resp.Header {
fmt.Printf("< %s %s\n", k, strings.Join(v, " "))
}
fmt.Printf(prettyJSON.String())
fmt.Printf("> %s %s\n", method, path)
fmt.Printf("< %s %s\n", resp.Status, resp.Proto)
for k, v := range resp.Header {
fmt.Printf("< %s %s\n", k, strings.Join(v, " "))
}
fmt.Printf(prettyJSON.String())

return nil
},
return nil
},
}
}

Loading