From 60498ef33afc1aec36200ec978595a37f8a4afc9 Mon Sep 17 00:00:00 2001 From: Ryan SVIHLA Date: Tue, 30 Mar 2021 19:41:26 +0200 Subject: [PATCH] increase code coverage substantially (#11) * added tests around client * use an interface for client for testability reasons * added coverage --- README.md | 2 +- cmd/db.go | 11 +- cmd/db/create.go | 16 +- cmd/db/create_test.go | 186 ++++++++++++++++++++++- cmd/db/delete.go | 8 +- cmd/db/delete_test.go | 72 ++++++++- cmd/db/get.go | 61 ++++---- cmd/db/get_test.go | 70 ++++++++- cmd/db/list.go | 65 +++++---- cmd/db/list_test.go | 79 ++++++++++ cmd/db/park.go | 3 +- cmd/db/park_test.go | 21 ++- cmd/db/resize.go | 3 +- cmd/db/resize_test.go | 45 +++++- cmd/db/secBundle.go | 80 ++++------ cmd/db/secBundle_test.go | 35 ++++- cmd/db/unpark.go | 3 +- cmd/db/unpark_test.go | 21 ++- cmd/db_test.go | 19 ++- cmd/root_test.go | 2 +- coverage.md | 2 +- coverage_badge.png | Bin 2297 -> 2215 bytes go.mod | 1 - pkg/conf.go | 29 ++-- pkg/conf_test.go | 28 ++-- pkg/errors_test.go | 28 ++-- pkg/httputils/client.go | 33 +++++ pkg/httputils/client_test.go | 52 +++++++ pkg/login.go | 20 ++- pkg/login_test.go | 28 ++-- pkg/strfmt_test.go | 28 ++-- pkg/tests/client.go | 117 +++++++++++++++ pkg/tests/client_test.go | 276 +++++++++++++++++++++++++++++++++++ 33 files changed, 1223 insertions(+), 221 deletions(-) create mode 100644 pkg/httputils/client_test.go create mode 100644 pkg/tests/client.go create mode 100644 pkg/tests/client_test.go diff --git a/README.md b/README.md index 0d36a32..9892e49 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Apache 2.0 licensed Astra Cloud Management CLI [![Go Report Card](https://goreportcard.com/badge/github.com/rsds143/astra-cli)](https://goreportcard.com/report/github.com/rsds143/astra-cli) [![GitHub go.mod Go version of a Go module](https://img.shields.io/github/go-mod/go-version/rsds143/astra-cli)](https://img.shields.io/github/go-mod/go-version/rsds143/astra-cli) [![Latest Version](https://img.shields.io/github/v/release/rsds143/astra-cli?include_prereleases)](https://github.com/rsds143/astra-cli/releases) -![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-63%25-brightgreen.svg?longCache=true&style=flat) +![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-81%25-brightgreen.svg?longCache=true&style=flat) ## status diff --git a/cmd/db.go b/cmd/db.go index 4e93f9a..1d26d2e 100644 --- a/cmd/db.go +++ b/cmd/db.go @@ -40,8 +40,15 @@ var dbCmd = &cobra.Command{ Short: "Shows all the db commands", Long: `Shows all other db commands. Create, Delete, Get information on your databases`, Run: func(cobraCmd *cobra.Command, args []string) { - if err := cobraCmd.Usage(); err != nil { - fmt.Fprintf(os.Stderr, "Unable to print usage with error %v\n", err) + if err := executeDB(cobraCmd.Usage); err != nil { + os.Exit(1) } }, } + +func executeDB(usage func() error) error { + if err := usage(); err != nil { + return fmt.Errorf("warn unable to show usage %v", err) + } + return nil +} diff --git a/cmd/db/create.go b/cmd/db/create.go index 5050604..2b738ec 100644 --- a/cmd/db/create.go +++ b/cmd/db/create.go @@ -49,16 +49,10 @@ func init() { var CreateCmd = &cobra.Command{ Use: "create", Short: "creates a database by id", - Long: ``, + Long: `creates a database by id`, Run: func(cobraCmd *cobra.Command, args []string) { creds := &pkg.Creds{} - client, err := creds.Login() - if err != nil { - fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err) - os.Exit(1) - } - - err = executeCreate(client) + err := executeCreate(creds.Login) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) @@ -66,7 +60,11 @@ var CreateCmd = &cobra.Command{ }, } -func executeCreate(client *astraops.AuthenticatedClient) error { +func executeCreate(makeClient func() (pkg.Client, error)) error { + client, err := makeClient() + if err != nil { + return fmt.Errorf("unable to login with error %v", err) + } capacity := int32(createDbCapacityUnit) createDb := astraops.CreateDb{ Name: createDbName, diff --git a/cmd/db/create_test.go b/cmd/db/create_test.go index f97f7ac..7b169d7 100644 --- a/cmd/db/create_test.go +++ b/cmd/db/create_test.go @@ -15,8 +15,188 @@ //Package db is where the Astra DB commands are package db -import "testing" +import ( + "fmt" + "testing" -func TestCreate(t *testing.T) { - t.Skip("ignore") + "github.com/rsds143/astra-cli/pkg" + tests "github.com/rsds143/astra-cli/pkg/tests" + "github.com/rsds143/astra-devops-sdk-go/astraops" +) + +func TestCreateGetsId(t *testing.T) { + expectedID := "abcd" + //setting package variables by hand, there be dragons + mockClient := &tests.MockClient{ + Databases: []astraops.Database{ + { + ID: expectedID, + }, + }, + } + err := executeCreate(func() (pkg.Client, error) { + return mockClient, nil + }) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + + if len(mockClient.Calls()) != 1 { + t.Fatalf("expected 1 call but was %v", len(mockClient.Calls())) + } +} +func TestCreateLoginFails(t *testing.T) { + //setting package variables by hand, there be dragons + mockClient := &tests.MockClient{} + err := executeCreate(func() (pkg.Client, error) { + return mockClient, fmt.Errorf("service down") + }) + if err == nil { + t.Fatal("expected error") + } + + expected := "unable to login with error service down" + if err.Error() != expected { + t.Errorf("expected '%v' but was '%v'", expected, err.Error()) + } + if len(mockClient.Calls()) != 0 { + t.Fatalf("expected 0 call but was %v", len(mockClient.Calls())) + } +} + +func TestCreateFails(t *testing.T) { + //setting package variables by hand, there be dragons + mockClient := &tests.MockClient{ + ErrorQueue: []error{fmt.Errorf("service down")}, + } + err := executeCreate(func() (pkg.Client, error) { + return mockClient, nil + }) + if err == nil { + t.Fatal("expected error") + } + + if len(mockClient.Calls()) != 1 { + t.Fatalf("expected 1 call but was %v", len(mockClient.Calls())) + } +} + +func TestCreateSetsName(t *testing.T) { + mockClient := &tests.MockClient{} + createDbName = "mydb" + err := executeCreate(func() (pkg.Client, error) { + return mockClient, nil + }) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + arg0 := mockClient.Call(0).(astraops.CreateDb) + if arg0.Name != createDbName { + t.Errorf("expected '%v' but was '%v'", arg0.Name, createDbName) + } +} + +func TestCreateSetsKeyspace(t *testing.T) { + mockClient := &tests.MockClient{} + createDbKeyspace = "myKeyspace" + err := executeCreate(func() (pkg.Client, error) { + return mockClient, nil + }) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + arg0 := mockClient.Call(0).(astraops.CreateDb) + if arg0.Keyspace != createDbKeyspace { + t.Errorf("expected '%v' but was '%v'", arg0.Keyspace, createDbKeyspace) + } +} + +func TestCreateSetsCapacityUnit(t *testing.T) { + mockClient := &tests.MockClient{} + createDbCapacityUnit = 10000 + err := executeCreate(func() (pkg.Client, error) { + return mockClient, nil + }) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + arg0 := mockClient.Call(0).(astraops.CreateDb) + if arg0.CapacityUnits != int32(createDbCapacityUnit) { + t.Errorf("expected '%v' but was '%v'", arg0.CapacityUnits, createDbCapacityUnit) + } +} + +func TestCreateSetsRegion(t *testing.T) { + mockClient := &tests.MockClient{} + createDbRegion = "EU-West1" + err := executeCreate(func() (pkg.Client, error) { + return mockClient, nil + }) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + arg0 := mockClient.Call(0).(astraops.CreateDb) + if arg0.Region != createDbRegion { + t.Errorf("expected '%v' but was '%v'", arg0.Region, createDbRegion) + } +} + +func TestCreateSetsUser(t *testing.T) { + mockClient := &tests.MockClient{} + createDbUser = "john@james.com" + err := executeCreate(func() (pkg.Client, error) { + return mockClient, nil + }) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + arg0 := mockClient.Call(0).(astraops.CreateDb) + if arg0.User != createDbUser { + t.Errorf("expected '%v' but was '%v'", arg0.User, createDbUser) + } +} + +func TestCreateSetsPass(t *testing.T) { + mockClient := &tests.MockClient{} + createDbUser = "afdfdf" + err := executeCreate(func() (pkg.Client, error) { + return mockClient, nil + }) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + arg0 := mockClient.Call(0).(astraops.CreateDb) + if arg0.Password != createDbPassword { + t.Errorf("expected '%v' but was '%v'", arg0.Password, createDbPassword) + } +} + +func TestCreateSetsTier(t *testing.T) { + mockClient := &tests.MockClient{} + createDbTier = "afdfdf" + err := executeCreate(func() (pkg.Client, error) { + return mockClient, nil + }) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + arg0 := mockClient.Call(0).(astraops.CreateDb) + if arg0.Tier != createDbTier { + t.Errorf("expected '%v' but was '%v'", arg0.Tier, createDbTier) + } +} + +func TestCreateSetsProvider(t *testing.T) { + mockClient := &tests.MockClient{} + createDbCloudProvider = "ryanscloud" + err := executeCreate(func() (pkg.Client, error) { + return mockClient, nil + }) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + arg0 := mockClient.Call(0).(astraops.CreateDb) + if arg0.CloudProvider != createDbCloudProvider { + t.Errorf("expected '%v' but was '%v'", arg0.CloudProvider, createDbCloudProvider) + } } diff --git a/cmd/db/delete.go b/cmd/db/delete.go index 83a88fd..c02b38f 100644 --- a/cmd/db/delete.go +++ b/cmd/db/delete.go @@ -30,7 +30,8 @@ var DeleteCmd = &cobra.Command{ Long: `deletes a database from your Astra account by ID`, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - msg, err := execteDelete(args) + creds := &pkg.Creds{} + msg, err := executeDelete(args, creds.Login) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) @@ -39,9 +40,8 @@ var DeleteCmd = &cobra.Command{ }, } -func execteDelete(args []string) (string, error) { - creds := &pkg.Creds{} - client, err := creds.Login() +func executeDelete(args []string, makeClient func() (pkg.Client, error)) (string, error) { + client, err := makeClient() if err != nil { return "", fmt.Errorf("unable to login with error '%v'", err) } diff --git a/cmd/db/delete_test.go b/cmd/db/delete_test.go index f2b6832..1a706cf 100644 --- a/cmd/db/delete_test.go +++ b/cmd/db/delete_test.go @@ -15,8 +15,76 @@ //Package db is where the Astra DB commands are package db -import "testing" +import ( + "fmt" + "testing" + + "github.com/rsds143/astra-cli/pkg" + tests "github.com/rsds143/astra-cli/pkg/tests" +) func TestDelete(t *testing.T) { - t.Skip("ignore") + mockClient := &tests.MockClient{} + id := "123" + msg, err := executeDelete([]string{id}, func() (pkg.Client, error) { + return mockClient, nil + }) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + if len(mockClient.Calls()) != 1 { + t.Fatalf("expected 1 call but was %v", len(mockClient.Calls())) + } + if id != mockClient.Call(0) { + t.Errorf("expected '%v' but was '%v'", id, mockClient.Call(0)) + } + expected := "database 123 deleted" + if expected != msg { + t.Errorf("expected '%v' but was '%v'", expected, msg) + } +} + +func TestDeleteLoginError(t *testing.T) { + mockClient := &tests.MockClient{} + id := "123" + msg, err := executeDelete([]string{id}, func() (pkg.Client, error) { + return mockClient, fmt.Errorf("unable to login") + }) + if err == nil { + t.Fatalf("should be returning an error and is not") + } + expectedError := "unable to login with error 'unable to login'" + if err.Error() != expectedError { + t.Errorf("expected '%v' but was '%v'", expectedError, err) + } + if len(mockClient.Calls()) != 0 { + t.Fatalf("expected no calls but was %v", len(mockClient.Calls())) + } + + if "" != msg { + t.Errorf("expected emtpy but was '%v'", msg) + } +} + +func TestDeleteError(t *testing.T) { + mockClient := &tests.MockClient{ + ErrorQueue: []error{fmt.Errorf("timeout error")}, + } + id := "123" + msg, err := executeDelete([]string{id}, func() (pkg.Client, error) { + return mockClient, nil + }) + if err == nil { + t.Fatal("expected error but none came") + } + if len(mockClient.Calls()) != 1 { + t.Fatalf("expected 1 call but was %v", len(mockClient.Calls())) + } + if id != mockClient.Call(0) { + t.Errorf("expected '%v' but was '%v'", id, mockClient.Call(0)) + } + expected := "" + if expected != msg { + t.Errorf("expected '%v' but was '%v'", expected, msg) + } } diff --git a/cmd/db/get.go b/cmd/db/get.go index c54d3e4..b64ecb5 100644 --- a/cmd/db/get.go +++ b/cmd/db/get.go @@ -16,6 +16,7 @@ package db import ( + "bytes" "encoding/json" "fmt" "os" @@ -39,37 +40,43 @@ var GetCmd = &cobra.Command{ Args: cobra.ExactArgs(1), Run: func(cobraCmd *cobra.Command, args []string) { creds := &pkg.Creds{} - client, err := creds.Login() + txt, err := executeGet(args, creds.Login) if err != nil { fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err) os.Exit(1) } - id := args[0] - var db astraops.Database - if db, err = client.FindDb(id); err != nil { - fmt.Fprintf(os.Stderr, "unable to get '%s' with error %v\n", id, err) - os.Exit(1) + fmt.Println(txt) + }, +} + +func executeGet(args []string, login func() (pkg.Client, error)) (string, error) { + client, err := login() + if err != nil { + return "", fmt.Errorf("unable to login with error %v", err) + } + id := args[0] + var db astraops.Database + if db, err = client.FindDb(id); err != nil { + return "", fmt.Errorf("unable to get '%s' with error %v", id, err) + } + switch getFmt { + case "text": + var rows [][]string + rows = append(rows, []string{"name", "id", "status"}) + rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)}) + var buf bytes.Buffer + err = pkg.WriteRows(&buf, rows) + if err != nil { + return "", fmt.Errorf("unexpected error writing out text %v", err) } - switch getFmt { - case "text": - var rows [][]string - rows = append(rows, []string{"name", "id", "status"}) - rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)}) - err = pkg.WriteRows(os.Stdout, rows) - if err != nil { - fmt.Fprintf(os.Stderr, "unexpected error writing out text %v\n", err) - os.Exit(1) - } - case "json": - b, err := json.MarshalIndent(db, "", " ") - if err != nil { - fmt.Fprintf(os.Stderr, "unexpected error marshaling to json: '%v', Try -output text instead\n", err) - os.Exit(1) - } - fmt.Println(string(b)) - default: - fmt.Fprintf(os.Stderr, "-output %q is not valid option.\n", getFmt) - os.Exit(1) + return buf.String(), nil + case "json": + b, err := json.MarshalIndent(db, "", " ") + if err != nil { + return "", fmt.Errorf("unexpected error marshaling to json: '%v', Try -output text instead", err) } - }, + return string(b), nil + default: + return "", fmt.Errorf("-output %q is not valid option", getFmt) + } } diff --git a/cmd/db/get_test.go b/cmd/db/get_test.go index 81c22d0..584b130 100644 --- a/cmd/db/get_test.go +++ b/cmd/db/get_test.go @@ -15,8 +15,74 @@ //Package db is where the Astra DB commands are package db -import "testing" +import ( + "encoding/json" + "strings" + "testing" + + "github.com/rsds143/astra-cli/pkg" + tests "github.com/rsds143/astra-cli/pkg/tests" + "github.com/rsds143/astra-devops-sdk-go/astraops" +) func TestGet(t *testing.T) { - t.Skip("ignore") + getFmt = "json" + dbs := []astraops.Database{ + {ID: "1"}, + {ID: "2"}, + } + jsonTxt, err := executeGet([]string{"1"}, func() (pkg.Client, error) { + return &tests.MockClient{ + Databases: dbs, + }, nil + + }) + if err != nil { + t.Fatalf("unexpected error %v", err) + } + var fromServer astraops.Database + err = json.Unmarshal([]byte(jsonTxt), &fromServer) + if err != nil { + t.Fatalf("unexpected error with json %v with text %v", err, jsonTxt) + } + if fromServer.ID != dbs[0].ID { + t.Errorf("expected '%v' but was '%v'", dbs[0].ID, fromServer.ID) + } +} + +func TestGetText(t *testing.T) { + getFmt = "text" + dbs := []astraops.Database{ + { + ID: "1", + Info: astraops.DatabaseInfo{ + Name: "A", + }, + Status: astraops.ACTIVE, + }, + { + ID: "2", + Info: astraops.DatabaseInfo{ + Name: "B", + }, + Status: astraops.TERMINATING, + }, + } + txt, err := executeGet([]string{"1"}, func() (pkg.Client, error) { + return &tests.MockClient{ + Databases: dbs, + }, nil + + }) + if err != nil { + t.Fatalf("unexpected error %v", err) + } + expected := strings.Join([]string{ + "name id status", + "A 1 ACTIVE", + }, + "\n") + if txt != expected { + t.Errorf("expected '%v' but was '%v'", expected, txt) + } } diff --git a/cmd/db/list.go b/cmd/db/list.go index 3366ad6..2340ab3 100644 --- a/cmd/db/list.go +++ b/cmd/db/list.go @@ -16,6 +16,7 @@ package db import ( + "bytes" "encoding/json" "fmt" "os" @@ -46,38 +47,44 @@ var ListCmd = &cobra.Command{ Long: `lists all databases in your Astra account`, Run: func(cmd *cobra.Command, args []string) { creds := &pkg.Creds{} - client, err := creds.Login() + msg, err := executeList(args, creds.Login) if err != nil { - fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err) + fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } - var dbs []astraops.Database - if dbs, err = client.ListDb(include, provider, startingAfter, int32(limit)); err != nil { - fmt.Fprintf(os.Stderr, "unable to get list of dbs with error %v\n", err) - os.Exit(1) + fmt.Println(msg) + }, +} + +func executeList(args []string, login func() (pkg.Client, error)) (string, error) { + client, err := login() + if err != nil { + return "", fmt.Errorf("unable to login with error '%v'", err) + } + var dbs []astraops.Database + if dbs, err = client.ListDb(include, provider, startingAfter, int32(limit)); err != nil { + return "", fmt.Errorf("unable to get list of dbs with error '%v'", err) + } + switch listFmt { + case "text": + var rows [][]string + rows = append(rows, []string{"name", "id", "status"}) + for _, db := range dbs { + rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)}) } - switch listFmt { - case "text": - var rows [][]string - rows = append(rows, []string{"name", "id", "status"}) - for _, db := range dbs { - rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)}) - } - err = pkg.WriteRows(os.Stdout, rows) - if err != nil { - fmt.Fprintf(os.Stderr, "unexpected error writing text output %v", err) - os.Exit(1) - } - case "json": - b, err := json.MarshalIndent(dbs, "", " ") - if err != nil { - fmt.Fprintf(os.Stderr, "unexpected error marshaling to json: '%v', Try -output text instead\n", err) - os.Exit(1) - } - fmt.Println(string(b)) - default: - fmt.Fprintf(os.Stderr, "-output %q is not valid option.\n", getFmt) - os.Exit(1) + var out bytes.Buffer + err = pkg.WriteRows(&out, rows) + if err != nil { + return "", fmt.Errorf("unexpected error writing text output '%v'", err) } - }, + return out.String(), nil + case "json": + b, err := json.MarshalIndent(dbs, "", " ") + if err != nil { + return "", fmt.Errorf("unexpected error marshaling to json: '%v', Try -output text instead", err) + } + return string(b), nil + default: + return "", fmt.Errorf("-output %q is not valid option", getFmt) + } } diff --git a/cmd/db/list_test.go b/cmd/db/list_test.go index 4746a04..42b6f88 100644 --- a/cmd/db/list_test.go +++ b/cmd/db/list_test.go @@ -14,3 +14,82 @@ //Package db is where the Astra DB commands are package db + +import ( + "encoding/json" + "strings" + "testing" + + "github.com/rsds143/astra-cli/pkg" + tests "github.com/rsds143/astra-cli/pkg/tests" + "github.com/rsds143/astra-devops-sdk-go/astraops" +) + +func TestList(t *testing.T) { + listFmt = "json" + dbs := []astraops.Database{ + {ID: "1"}, + {ID: "2"}, + } + jsonTxt, err := executeList([]string{"", "", "", "10"}, func() (pkg.Client, error) { + return &tests.MockClient{ + Databases: dbs, + }, nil + + }) + if err != nil { + t.Fatalf("unexpected error %v", err) + } + var fromServer []astraops.Database + err = json.Unmarshal([]byte(jsonTxt), &fromServer) + if err != nil { + t.Fatalf("unexpected error with json %v with text %v", err, jsonTxt) + } + if len(fromServer) != len(dbs) { + t.Errorf("expected '%v' but was '%v'", len(dbs), len(fromServer)) + } + if fromServer[0].ID != dbs[0].ID { + t.Errorf("expected '%v' but was '%v'", dbs[0].ID, fromServer[0].ID) + } + if fromServer[1].ID != dbs[1].ID { + t.Errorf("expected '%v' but was '%v'", dbs[1].ID, fromServer[1].ID) + } +} + +func TestListText(t *testing.T) { + listFmt = "text" + dbs := []astraops.Database{ + { + ID: "1", + Info: astraops.DatabaseInfo{ + Name: "A", + }, + Status: astraops.ACTIVE, + }, + { + ID: "2", + Info: astraops.DatabaseInfo{ + Name: "B", + }, + Status: astraops.TERMINATING, + }, + } + txt, err := executeList([]string{"", "", "", "10"}, func() (pkg.Client, error) { + return &tests.MockClient{ + Databases: dbs, + }, nil + + }) + if err != nil { + t.Fatalf("unexpected error %v", err) + } + expected := strings.Join([]string{ + "name id status", + "A 1 ACTIVE", + "B 2 TERMINATING", + }, + "\n") + if txt != expected { + t.Errorf("expected '%v' but was '%v'", expected, txt) + } +} diff --git a/cmd/db/park.go b/cmd/db/park.go index 18c4a6a..4fc0f45 100644 --- a/cmd/db/park.go +++ b/cmd/db/park.go @@ -20,7 +20,6 @@ import ( "os" "github.com/rsds143/astra-cli/pkg" - "github.com/rsds143/astra-devops-sdk-go/astraops" "github.com/spf13/cobra" ) @@ -47,7 +46,7 @@ var ParkCmd = &cobra.Command{ // executePark parks the database with the specified ID. If no ID is provided // the command will error out -func executePark(args []string, client *astraops.AuthenticatedClient) error { +func executePark(args []string, client pkg.Client) error { id := args[0] fmt.Printf("starting to park database %v\n", id) if err := client.Park(id); err != nil { diff --git a/cmd/db/park_test.go b/cmd/db/park_test.go index 8e5d898..a0d2b87 100644 --- a/cmd/db/park_test.go +++ b/cmd/db/park_test.go @@ -15,8 +15,25 @@ //Package db is where the Astra DB commands are package db -import "testing" +import ( + "testing" + + tests "github.com/rsds143/astra-cli/pkg/tests" +) func TestPark(t *testing.T) { - t.Skip("ignore") + //setting package variables by hand, there be dragons + mockClient := &tests.MockClient{} + id := "qdfkjoj" + err := executePark([]string{id}, mockClient) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + + if len(mockClient.Calls()) != 1 { + t.Fatalf("expected 1 call but was %v", len(mockClient.Calls())) + } + if id != mockClient.Call(0) { + t.Errorf("expected '%v' but was '%v'", id, mockClient.Call(0)) + } } diff --git a/cmd/db/resize.go b/cmd/db/resize.go index 2f73252..69e9e62 100644 --- a/cmd/db/resize.go +++ b/cmd/db/resize.go @@ -21,7 +21,6 @@ import ( "strconv" "github.com/rsds143/astra-cli/pkg" - "github.com/rsds143/astra-devops-sdk-go/astraops" "github.com/spf13/cobra" ) @@ -48,7 +47,7 @@ var ResizeCmd = &cobra.Command{ //executeResize resizes the database with the specified ID with the specified size. If no ID is provided // the command will error out -func executeResize(args []string, client *astraops.AuthenticatedClient) error { +func executeResize(args []string, client pkg.Client) error { id := args[0] capacityUnitRaw := args[1] capacityUnit, err := strconv.Atoi(capacityUnitRaw) diff --git a/cmd/db/resize_test.go b/cmd/db/resize_test.go index e949db3..b1718f2 100644 --- a/cmd/db/resize_test.go +++ b/cmd/db/resize_test.go @@ -15,8 +15,49 @@ //Package db is where the Astra DB commands are package db -import "testing" +import ( + "testing" + + tests "github.com/rsds143/astra-cli/pkg/tests" +) func TestResize(t *testing.T) { - t.Skip("ignore") + //setting package variables by hand, there be dragons + mockClient := &tests.MockClient{} + id := "qdfkjoj" + size := "100" + err := executeResize([]string{id, size}, mockClient) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + + if len(mockClient.Calls()) != 1 { + t.Fatalf("expected 1 call but was %v", len(mockClient.Calls())) + } + actualID := mockClient.Call(0).([]interface{})[0] + if id != actualID { + t.Errorf("expected '%v' but was '%v'", id, actualID) + } + actualSize := mockClient.Call(0).([]interface{})[1].(int32) + if int32(100) != actualSize { + t.Errorf("expected '%v' but was '%v'", size, actualSize) + } +} + +func TestResizeParseError(t *testing.T) { + //setting package variables by hand, there be dragons + mockClient := &tests.MockClient{} + id := "qdfkjoj" + size := "abcd" + err := executeResize([]string{id, size}, mockClient) + if err == nil { + t.Fatal("expected error") + } + expectedError := "Unable to parse command line with args: qdfkjoj, abcd. Nested error was 'unable to parse capacity unit 'abcd' with error strconv.Atoi: parsing \"abcd\": invalid syntax'" + if err.Error() != expectedError { + t.Errorf("expected '%v' but was '%v'", expectedError, err.Error()) + } + if len(mockClient.Calls()) != 0 { + t.Fatalf("expected 0 call but was %v", len(mockClient.Calls())) + } } diff --git a/cmd/db/secBundle.go b/cmd/db/secBundle.go index ec8ceb9..6d5fe2d 100644 --- a/cmd/db/secBundle.go +++ b/cmd/db/secBundle.go @@ -18,7 +18,6 @@ package db import ( "encoding/json" "fmt" - "io" "os" "github.com/rsds143/astra-cli/pkg" @@ -43,58 +42,39 @@ var SecBundleCmd = &cobra.Command{ Args: cobra.ExactArgs(1), Run: func(cobraCmd *cobra.Command, args []string) { creds := &pkg.Creds{} - client, err := creds.Login() + out, err := executeSecBundle(args, creds.Login) if err != nil { - fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err) + fmt.Fprintf(os.Stderr, "%v\n", err) os.Exit(1) } - id := args[0] - var secBundle astraops.SecureBundle - if secBundle, err = client.GetSecureBundle(id); err != nil { - fmt.Fprintf(os.Stderr, "unable to get '%s' with error %v\n", id, err) - os.Exit(1) + fmt.Println(out) + }, +} + +func executeSecBundle(args []string, login func() (pkg.Client, error)) (string, error) { + client, err := login() + if err != nil { + return "", fmt.Errorf("unable to login with error %v", err) + } + id := args[0] + var secBundle astraops.SecureBundle + if secBundle, err = client.GetSecureBundle(id); err != nil { + return "", fmt.Errorf("unable to get '%s' with error %v", id, err) + } + switch secBundleFmt { + case "zip": + bytesWritten, err := httputils.DownloadZip(secBundle.DownloadURL, secBundleLoc) + if err != nil { + return "", fmt.Errorf("error outputing zip format '%v'", err) } - switch secBundleFmt { - case "zip": - httpClient := httputils.NewHTTPClient() - res, err := httpClient.Get(secBundle.DownloadURL) - if err != nil { - fmt.Fprintf(os.Stderr, "unable to download zip with error %v\n", err) - os.Exit(1) - } - defer func() { - err = res.Body.Close() - if err != nil { - fmt.Fprintf(os.Stderr, "Warn: error closing http response body %v\n for request %v with status code %v", err, secBundle.DownloadURL, res.StatusCode) - } - }() - f, err := os.Create(secBundleLoc) - if err != nil { - fmt.Fprintf(os.Stderr, "unable to create file to save too %v\n", err) - os.Exit(1) - } - defer func() { - err = f.Close() - if err != nil { - fmt.Fprintf(os.Stderr, "Warn: error closing file %v for file %v\n", err, secBundleLoc) - } - }() - i, err := io.Copy(f, res.Body) - if err != nil { - fmt.Fprintf(os.Stderr, "unable to copy downloaded file to %v\n", err) - os.Exit(1) - } - fmt.Printf("file %v saved %v bytes written\n", secBundleLoc, i) - case "json": - b, err := json.MarshalIndent(secBundle, "", " ") - if err != nil { - fmt.Fprintf(os.Stderr, "unexpected error marshaling to json: '%v', Try -output text instead\n", err) - os.Exit(1) - } - fmt.Println(string(b)) - default: - fmt.Fprintf(os.Stderr, "-output %q is not valid option.", secBundleFmt) - os.Exit(1) + return fmt.Sprintf("file %v saved %v bytes written", secBundleLoc, bytesWritten), nil + case "json": + b, err := json.MarshalIndent(secBundle, "", " ") + if err != nil { + return "", fmt.Errorf("unexpected error marshaling to json: '%v', Try -output text instead", err) } - }, + return string(b), nil + default: + return "", fmt.Errorf("-output %q is not valid option", secBundleFmt) + } } diff --git a/cmd/db/secBundle_test.go b/cmd/db/secBundle_test.go index 7d8d992..631953b 100644 --- a/cmd/db/secBundle_test.go +++ b/cmd/db/secBundle_test.go @@ -15,8 +15,39 @@ //Package db is where the Astra DB commands are package db -import "testing" +import ( + "encoding/json" + "testing" + + "github.com/rsds143/astra-cli/pkg" + tests "github.com/rsds143/astra-cli/pkg/tests" + "github.com/rsds143/astra-devops-sdk-go/astraops" +) func TestSecBundle(t *testing.T) { - t.Skip("ignore") + id := "abc" + secBundleLoc = "my_loc" + secBundleFmt = "json" + bundle := astraops.SecureBundle{ + DownloadURL: "abcd", + DownloadURLInternal: "wyz", + DownloadURLMigrationProxy: "opu", + DownloadURLMigrationProxyInternal: "zert", + } + jsonTxt, err := executeSecBundle([]string{id}, func() (pkg.Client, error) { + return &tests.MockClient{ + Bundle: bundle, + }, nil + }) + if err != nil { + t.Fatalf("unexpected error %v", err) + } + var fromServer astraops.SecureBundle + err = json.Unmarshal([]byte(jsonTxt), &fromServer) + if err != nil { + t.Fatalf("unexpected error with json %v", err) + } + if fromServer != bundle { + t.Errorf("expected '%v' but was '%v'", bundle, fromServer) + } } diff --git a/cmd/db/unpark.go b/cmd/db/unpark.go index f214e0e..83c2a15 100644 --- a/cmd/db/unpark.go +++ b/cmd/db/unpark.go @@ -20,7 +20,6 @@ import ( "os" "github.com/rsds143/astra-cli/pkg" - "github.com/rsds143/astra-devops-sdk-go/astraops" "github.com/spf13/cobra" ) @@ -47,7 +46,7 @@ var UnparkCmd = &cobra.Command{ // executeUnpark unparks the database with the specified ID. If no ID is provided // the command will error out -func executeUnpark(args []string, client *astraops.AuthenticatedClient) error { +func executeUnpark(args []string, client pkg.Client) error { id := args[0] fmt.Printf("starting to unpark database %v\n", id) if err := client.Unpark(id); err != nil { diff --git a/cmd/db/unpark_test.go b/cmd/db/unpark_test.go index 1dce625..99bb45f 100644 --- a/cmd/db/unpark_test.go +++ b/cmd/db/unpark_test.go @@ -15,8 +15,25 @@ //Package db is where the Astra DB commands are package db -import "testing" +import ( + "testing" + + tests "github.com/rsds143/astra-cli/pkg/tests" +) func TestUnpark(t *testing.T) { - t.Skip("ignore") + //setting package variables by hand, there be dragons + mockClient := &tests.MockClient{} + id := "qdfkjoj" + err := executeUnpark([]string{id}, mockClient) + if err != nil { + t.Fatalf("unexpected error '%v'", err) + } + + if len(mockClient.Calls()) != 1 { + t.Fatalf("expected 1 call but was %v", len(mockClient.Calls())) + } + if id != mockClient.Call(0) { + t.Errorf("expected '%v' but was '%v'", id, mockClient.Call(0)) + } } diff --git a/cmd/db_test.go b/cmd/db_test.go index f70d798..491da6e 100644 --- a/cmd/db_test.go +++ b/cmd/db_test.go @@ -15,8 +15,21 @@ //Package cmd contains all fo the commands for the cli package cmd -import "testing" +import ( + "errors" + "testing" +) -func TestDbCmd(t *testing.T) { - t.Skip("ignore") +func TestDBUsageFails(t *testing.T) { + fails := func() error { + return errors.New("error showing usage") + } + err := executeDB(fails) + if err == nil { + t.Fatal("there is supposed to be an error") + } + expected := "warn unable to show usage error showing usage" + if err.Error() != expected { + t.Errorf("expected '%v' but was '%v'", expected, err.Error()) + } } diff --git a/cmd/root_test.go b/cmd/root_test.go index 8016512..1687271 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -20,7 +20,7 @@ import ( "testing" ) -func TestUsageFails(t *testing.T) { +func TestRootUsageFails(t *testing.T) { fails := func() error { return errors.New("error showing usage") } diff --git a/coverage.md b/coverage.md index fbd9881..dbb2473 100644 --- a/coverage.md +++ b/coverage.md @@ -1 +1 @@ -![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-63%25-brightgreen.svg?longCache=true&style=flat) \ No newline at end of file +![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-81%25-brightgreen.svg?longCache=true&style=flat) \ No newline at end of file diff --git a/coverage_badge.png b/coverage_badge.png index d506a65b0aa8aa277623a04057d30d98db78c751..f63eea280318b72a11635ced626c601910268264 100644 GI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-x=+UE; zmzOhZ)+|(2B{nve($Z4C`|i6jd5#}H&YU@OSg~RShYue{RaKgrntzCok0(DrADhia zOiTaf^vDPp7rJnW~?!_na|J92jKnp-)H5@m489)!NEbw%F0-@Xc67r z-PF|7;BYwDvSka&$;p(Jm7ypKhYlSgJ3E`l9(!zDjcYO5%X$6z7`80k%^d69eBbv^ zUOTdhQ-)8F0uotwtY!6`k7H0GXt?+_J3e`qo*8W;#;=%Ggw!3zpIvVu>m+8oQrW!h z_Xyzm!)y7*{D0R`>nvqwXNNRY2!Y4r2^LYK(V)}mFdB`>vdqB104|p+c-S$U%_xfU zleiQ`!C){D9v(g}Pj7E8eSLl0e*5jnvK%zY?RMky`GR-hlqpjH=;-JmCMITteos#i zUauEfmeJ{S`2BvOqN3>U@5k+Squ1*xDk@^;%$e-mxqow9jcYNQp@9K`r^S!YBan5V zpP`_O8r}x_I|B4~1P}pGj1mT$jH1_!81L;2Fcbj&)PLvOGlZvg_*`SOD`?Fl`(K5W zm6bJlUUN(Uo__ji7A#nR*XyOJsfissc3gL;8xIrx`RrQ!-@p3XxN#!`0|Q8sgsQ5T zOs0^lOj#&QrWz_j2p8~w`^Cj#v0V4tjT$sr3>Dx~gb<$sE64493n4Li3>BX$Uaxn( uPN(w&xBi0iA7Y{n6^JfL@VUq5D*qQra>D0fBahDj0000 0 { + err = c.ErrorQueue[0] + c.ErrorQueue[0] = nil + c.ErrorQueue = c.ErrorQueue[1:] + } + return err +} + +//getError pops the next db object stored off the stack +func (c *MockClient) getDb() astraops.Database { + var db astraops.Database + if len(c.Databases) > 0 { + db = c.Databases[0] + c.Databases = c.Databases[1:] + } + return db +} + +//Call returns a call at the specified index +func (c *MockClient) Call(index int) interface{} { + return c.calls[index] +} + +//Calls returns all calls made in order +func (c *MockClient) Calls() []interface{} { + return c.calls +} + +//CreateDb returns the next error and the next db created +func (c *MockClient) CreateDb(db astraops.CreateDb) (astraops.Database, error) { + c.calls = append(c.calls, db) + return c.getDb(), c.getError() +} + +//Terminate returns the next error and stores the id used, internal is ignored +func (c *MockClient) Terminate(id string, internal bool) error { + c.calls = append(c.calls, id) + return c.getError() +} + +//FindDb returns the next database and next error, the id call is stored +func (c *MockClient) FindDb(id string) (astraops.Database, error) { + c.calls = append(c.calls, id) + return c.getDb(), c.getError() +} + +//ListDb returns all databases and stores the arguments as an interface array +func (c *MockClient) ListDb(include string, provider string, startingAfter string, limit int32) ([]astraops.Database, error) { + + c.calls = append(c.calls, []interface{}{ + include, + provider, + startingAfter, + limit, + }) + return c.Databases, c.getError() +} + +//Unpark returns the next error, the id call is stored +func (c *MockClient) Unpark(id string) error { + c.calls = append(c.calls, id) + return c.getError() +} + +//Park returns the next error, the id call is stored +func (c *MockClient) Park(id string) error { + c.calls = append(c.calls, id) + return c.getError() +} + +//Resize returns the next error, the id call and size is stored +func (c *MockClient) Resize(id string, size int32) error { + c.calls = append(c.calls, []interface{}{id, size}) + return c.getError() +} + +//GetSecureBundle returns the next error, the secured bundle stored, and the id call is stored +func (c *MockClient) GetSecureBundle(id string) (astraops.SecureBundle, error) { + c.calls = append(c.calls, id) + return c.Bundle, c.getError() +} + +//GetTierInfo returns the next error, and the tierinfo objects stored +func (c *MockClient) GetTierInfo() ([]astraops.TierInfo, error) { + return c.Tiers, c.getError() +} diff --git a/pkg/tests/client_test.go b/pkg/tests/client_test.go new file mode 100644 index 0000000..0c80ff6 --- /dev/null +++ b/pkg/tests/client_test.go @@ -0,0 +1,276 @@ +// Copyright 2021 Ryan Svihla +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//Package test is for test utilies and mocks +package test + +import ( + "errors" + "testing" + + "github.com/rsds143/astra-devops-sdk-go/astraops" +) + +func TestGetError(t *testing.T) { + client := &MockClient{ + ErrorQueue: []error{ + errors.New("error 1"), + errors.New("error 2"), + errors.New("error 3"), + }, + } + err := client.getError() + if err.Error() != "error 1" { + t.Errorf("expected 'error 1' but was '%v'", err.Error()) + } + err = client.getError() + if err.Error() != "error 2" { + t.Errorf("expected 'error 2' but was '%v'", err.Error()) + } + err = client.getError() + if err.Error() != "error 3" { + t.Errorf("expected 'error 3' but was '%v'", err.Error()) + } + err = client.getError() + if err != nil { + t.Errorf("expected nil but was '%v'", err.Error()) + } +} + +func TestGetDB(t *testing.T) { + client := &MockClient{ + Databases: []astraops.Database{ + {ID: "1"}, + {ID: "2"}, + {ID: "3"}, + }, + } + id := client.getDb().ID + if id != "1" { + t.Errorf("expected '1' but was '%v'", id) + } + id = client.getDb().ID + if id != "2" { + t.Errorf("expected '2' but was '%v'", id) + } + id = client.getDb().ID + if id != "3" { + t.Errorf("expected '3' but was '%v'", id) + } + id = client.getDb().ID + if id != "" { + t.Errorf("expected '' but was '%v'", id) + } +} + +func TestPark(t *testing.T) { + client := &MockClient{} + id := "DSQ" + err := client.Park(id) + if err != nil { + t.Fatal("unexpected error") + } + if client.Call(0) != id { + t.Errorf("expected '%v' but was '%v'", id, client.Call(0)) + } + if len(client.Calls()) != 1 { + t.Errorf("expected '%v' but was '%v'", 1, len(client.Calls())) + } +} + +func TestUnpark(t *testing.T) { + client := &MockClient{} + id := "DSQ" + err := client.Unpark(id) + if err != nil { + t.Fatal("unexpected error") + } + if client.Call(0) != id { + t.Errorf("expected '%v' but was '%v'", id, client.Call(0)) + } + if len(client.Calls()) != 1 { + t.Errorf("expected '%v' but was '%v'", 1, len(client.Calls())) + } +} + +func TestTerminate(t *testing.T) { + client := &MockClient{} + id := "DSQ" + err := client.Terminate(id, false) + if err != nil { + t.Fatal("unexpected error") + } + if client.Call(0) != id { + t.Errorf("expected '%v' but was '%v'", id, client.Call(0)) + } + if len(client.Calls()) != 1 { + t.Errorf("expected '%v' but was '%v'", 1, len(client.Calls())) + } +} + +func TestGetSecurteBundleId(t *testing.T) { + url := "myurl" + client := &MockClient{ + Bundle: astraops.SecureBundle{ + DownloadURL: url, + }, + } + id := "DSQ" + bundle, err := client.GetSecureBundle(id) + if err != nil { + t.Fatal("unexpected error") + } + if bundle.DownloadURL != url { + t.Errorf("expected '%v' but was '%v'", url, bundle.DownloadURL) + } + if client.Call(0) != id { + t.Errorf("expected '%v' but was '%v'", id, client.Call(0)) + } + if len(client.Calls()) != 1 { + t.Errorf("expected '%v' but was '%v'", 1, len(client.Calls())) + } +} + +func TestFindDb(t *testing.T) { + id := "DSQ" + + client := &MockClient{ + Databases: []astraops.Database{ + {ID: id}, + {ID: "fakeid"}, + }, + } + db, err := client.FindDb(id) + if err != nil { + t.Fatal("unexpected error") + } + if db.ID != id { + t.Errorf("expected '%v' but was '%v'", id, db.ID) + } + if client.Call(0) != id { + t.Errorf("expected '%v' but was '%v'", id, client.Call(0)) + } + if len(client.Calls()) != 1 { + t.Errorf("expected '%v' but was '%v'", 1, len(client.Calls())) + } +} + +func TestCreateDb(t *testing.T) { + id := "DSQ" + + client := &MockClient{ + Databases: []astraops.Database{ + {ID: id}, + {ID: "fakeid"}, + }, + } + db, err := client.CreateDb(astraops.CreateDb{ + Name: "myname", + }) + if err != nil { + t.Fatal("unexpected error") + } + if db.ID != id { + t.Errorf("expected '%v' but was '%v'", id, db.ID) + } + if client.Call(0).(astraops.CreateDb).Name != "myname" { + t.Errorf("expected '%v' but was '%v'", "myname", client.Call(0).(astraops.CreateDb).Name) + } + if len(client.Calls()) != 1 { + t.Errorf("expected '%v' but was '%v'", 1, len(client.Calls())) + } +} + +func TestResize(t *testing.T) { + client := &MockClient{} + id := "DSQ" + var size int32 = 10 + err := client.Resize(id, int32(size)) + if err != nil { + t.Fatal("unexpected error") + } + actual := client.Call(0).([]interface{}) + if actual[0].(string) != id { + t.Errorf("expected '%v' but was '%v'", id, actual[0]) + } + if actual[1].(int32) != size { + t.Errorf("expected '%v' but was '%v'", size, actual[1]) + } + if len(client.Calls()) != 1 { + t.Errorf("expected '%v' but was '%v'", 1, len(client.Calls())) + } +} + +func TestTiers(t *testing.T) { + client := &MockClient{ + Tiers: []astraops.TierInfo{ + {Tier: "abc"}, + }, + } + tiers, err := client.GetTierInfo() + if err != nil { + t.Fatal("unexpected error") + } + + if tiers[0].Tier != "abc" { + t.Errorf("expected '%v' but was '%v'", "abc", tiers[0].Tier) + } + + if len(client.Calls()) != 0 { + t.Errorf("expected '%v' but was '%v'", 0, len(client.Calls())) + } +} + +func TestListdDb(t *testing.T) { + id1 := "1" + id2 := "2" + include := "filter" + provider := "gcp" + starting := "today" + var limit int32 = 1000 + client := &MockClient{ + Databases: []astraops.Database{ + {ID: id1}, + {ID: id2}, + }, + } + dbs, err := client.ListDb(include, provider, starting, limit) + if err != nil { + t.Fatal("unexpected error") + } + if len(dbs) != 2 { + t.Errorf("expected '%v' but was '%v'", 2, len(dbs)) + } + calls := client.Calls() + if len(calls) != 1 { + t.Errorf("expected '%v' but was '%v'", 1, len(calls)) + } + args := calls[0].([]interface{}) + actualInclude := args[0].(string) + if actualInclude != include { + t.Errorf("expected '%v' but was '%v'", include, actualInclude) + } + actualProvider := args[1].(string) + if actualProvider != provider { + t.Errorf("expected '%v' but was '%v'", provider, actualProvider) + } + actualStarting := args[2].(string) + if actualStarting != starting { + t.Errorf("expected '%v' but was '%v'", starting, actualStarting) + } + actualLimit := args[3].(int32) + if actualLimit != limit { + t.Errorf("expected '%v' but was '%v'", limit, actualLimit) + } +}