Skip to content

Commit

Permalink
added tests around multiple parts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Svihla authored and foundev committed Mar 29, 2021
1 parent 460a5a4 commit 7bbd8bc
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 137 deletions.
6 changes: 4 additions & 2 deletions cmd/db/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ package db

import (
"fmt"
"os"

"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
"github.com/spf13/cobra"
"os"
)

var createDbName string
Expand Down Expand Up @@ -50,7 +51,8 @@ var CreateCmd = &cobra.Command{
Short: "creates a database by id",
Long: ``,
Run: func(cobraCmd *cobra.Command, args []string) {
client, err := pkg.LoginClient()
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)
Expand Down
3 changes: 2 additions & 1 deletion cmd/db/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
client, err := pkg.LoginClient()
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)
Expand Down
10 changes: 6 additions & 4 deletions cmd/db/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
Expand All @@ -39,7 +38,8 @@ var GetCmd = &cobra.Command{
Long: `gets a database from your Astra account by ID`,
Args: cobra.ExactArgs(1),
Run: func(cobraCmd *cobra.Command, args []string) {
client, err := pkg.LoginClient()
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)
Expand All @@ -55,8 +55,10 @@ var GetCmd = &cobra.Command{
var rows [][]string
rows = append(rows, []string{"name", "id", "status"})
rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)})
for _, row := range pkg.PadColumns(rows) {
fmt.Println(strings.Join(row, " "))
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, "", " ")
Expand Down
10 changes: 6 additions & 4 deletions cmd/db/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
Expand All @@ -46,7 +45,8 @@ var ListCmd = &cobra.Command{
Short: "lists all databases",
Long: `lists all databases in your Astra account`,
Run: func(cmd *cobra.Command, args []string) {
client, err := pkg.LoginClient()
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)
Expand All @@ -63,8 +63,10 @@ var ListCmd = &cobra.Command{
for _, db := range dbs {
rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)})
}
for _, row := range pkg.PadColumns(rows) {
fmt.Println(strings.Join(row, " "))
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, "", " ")
Expand Down
3 changes: 2 additions & 1 deletion cmd/db/park.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var ParkCmd = &cobra.Command{
Long: `parks the database specified, only works on classic tier databases and can take a very long time to park (20-30 minutes)`,
Args: cobra.ExactArgs(1),
Run: func(cobraCmd *cobra.Command, args []string) {
client, err := pkg.LoginClient()
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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/db/resize.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ var ResizeCmd = &cobra.Command{
Long: "Resizes a database by id with the specified capacity unit. Note does not work on serverless.",
Args: cobra.ExactArgs(2),
Run: func(cobraCmd *cobra.Command, args []string) {

client, err := pkg.LoginClient()
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)
Expand Down
3 changes: 2 additions & 1 deletion cmd/db/secBundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ var SecBundleCmd = &cobra.Command{
Long: `gets the secure connetion bundle for the database from your Astra account by ID`,
Args: cobra.ExactArgs(1),
Run: func(cobraCmd *cobra.Command, args []string) {
client, err := pkg.LoginClient()
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)
Expand Down
10 changes: 6 additions & 4 deletions cmd/db/tiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"fmt"
"os"
"strings"

"github.com/rsds143/astra-cli/pkg"
"github.com/rsds143/astra-devops-sdk-go/astraops"
Expand All @@ -39,7 +38,8 @@ var TiersCmd = &cobra.Command{
Long: `List all available tiers on the Astra DevOps API. Each tier is a combination of costs, size, region, and name`,
Run: func(cmd *cobra.Command, args []string) {
var tiers []astraops.TierInfo
client, err := pkg.LoginClient()
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)
Expand Down Expand Up @@ -72,8 +72,10 @@ var TiersCmd = &cobra.Command{
fmt.Sprintf("$%.2f", costMonth),
fmt.Sprintf("$%.2f", costMin)})
}
for _, row := range pkg.PadColumns(rows) {
fmt.Println(strings.Join(row, " "))
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(tiers, "", " ")
Expand Down
3 changes: 2 additions & 1 deletion cmd/db/unpark.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var UnparkCmd = &cobra.Command{
Long: `parks the database specified, only works on classic tier databases and can take a very long time to park (20-30 minutes)`,
Args: cobra.ExactArgs(1),
Run: func(cobraCmd *cobra.Command, args []string) {
client, err := pkg.LoginClient()
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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var loginCmd = &cobra.Command{
Short: "Stores credentials for the cli to use in other commands to operate on the Astra DevOps API",
Long: `Token or service account is saved in .config/astra/ for use by the other commands`,
Run: func(cobraCmd *cobra.Command, args []string) {
confDir, confFiles, err := pkg.GetHome()
confDir, confFiles, err := pkg.GetHome(os.UserHomeDir)
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(3)
Expand Down
29 changes: 21 additions & 8 deletions pkg/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package pkg
import (
"encoding/json"
"fmt"
"github.com/rsds143/astra-devops-sdk-go/astraops"
"io"
"os"
"path"
"strings"

"github.com/rsds143/astra-devops-sdk-go/astraops"
)

//ConfFiles supports both formats of credentials and will say if the token one is present
Expand Down Expand Up @@ -56,11 +57,11 @@ func (c ConfFiles) HasToken() (bool, error) {

// GetHome returns the configuration directory and file
// error will return if there is no user home folder
func GetHome() (confDir string, confFiles ConfFiles, err error) {
func GetHome(getHome func() (string, error)) (confDir string, confFiles ConfFiles, err error) {
var home string
home, err = os.UserHomeDir()
home, err = getHome()
if err != nil {
return "", ConfFiles{}, fmt.Errorf("unable to get user home directory with error %s", err)
return "", ConfFiles{}, fmt.Errorf("unable to get user home directory with error '%s'", err)
}
confDir = path.Join(home, ".config", "astra")

Expand All @@ -78,21 +79,24 @@ func ReadToken(tokenFile string) (string, error) {
if err != nil {
return "", &FileNotFoundError{
Path: tokenFile,
Err: fmt.Errorf("unable to read login file with error %w", err),
Err: fmt.Errorf("unable to read login file with error '%w'", err),
}
}
defer func() {
if err := f.Close(); err != nil {
fmt.Printf("warning unable to close %v with error %v", tokenFile, err)
fmt.Printf("warning unable to close %v with error '%v'", tokenFile, err)
}
}()
b, err := io.ReadAll(f)
if err != nil {
return "", fmt.Errorf("unable to read login file %s with error %w", tokenFile, err)
return "", fmt.Errorf("unable to read login file '%s' with error '%w'", tokenFile, err)
}
if len(b) == 0 {
return "", fmt.Errorf("token file '%s' is emtpy", tokenFile)
}
token := strings.Trim(string(b), "\n")
if !strings.HasPrefix(token, "AstraCS") {
return "", fmt.Errorf("invalid token in login file %s with error %s", tokenFile, err)
return "", fmt.Errorf("missing prefix 'AstraCS' in token file '%s'", tokenFile)
}
return token, nil
}
Expand Down Expand Up @@ -123,5 +127,14 @@ func ReadLogin(saJSONFile string) (astraops.ClientInfo, error) {
Err: fmt.Errorf("unable to parse json from login file %s with error %s", saJSONFile, err),
}
}
if clientInfo.ClientID == "" {
return astraops.ClientInfo{}, fmt.Errorf("Invalid service account: Client ID for service account is emtpy for file '%v'", saJSONFile)
}
if clientInfo.ClientName == "" {
return astraops.ClientInfo{}, fmt.Errorf("Invalid service account: Client name for service account is emtpy for file '%v'", saJSONFile)
}
if clientInfo.ClientSecret == "" {
return astraops.ClientInfo{}, fmt.Errorf("Invalid service account: Client secret for service account is emtpy for file '%v'", saJSONFile)
}
return clientInfo, err
}
51 changes: 51 additions & 0 deletions pkg/conf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,54 @@ func TestReadLoginWithEmptyFile(t *testing.T) {
t.Errorf("expected %T but was %T", e, err)
}
}

func TestUnableToGetHomeFolder(t *testing.T) {
_, _, err := GetHome(func() (string, error) { return "", errors.New("unable to get home") })
if err == nil {
t.Fatal("expected error but none was present")
}
}

func TestReadTokenWithNoFile(t *testing.T) {
_, err := ReadToken("testdata/notthere")
if err == nil {
t.Fatal("expected an error but there was none")
}
var e *FileNotFoundError
if !errors.As(err, &e) {
t.Errorf("expected %T but was %T", e, err)
}
}

func TestMissingId(t *testing.T) {
_, err := ReadLogin("testdata/missing-id.json")
if err == nil {
t.Fatal("expected an error but there was none")
}
expected := "Invalid service account: Client ID for service account is emtpy for file 'testdata/missing-id.json'"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err)
}
}

func TestMissingName(t *testing.T) {
_, err := ReadLogin("testdata/missing-name.json")
if err == nil {
t.Fatal("expected an error but there was none")
}
expected := "Invalid service account: Client name for service account is emtpy for file 'testdata/missing-name.json'"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err)
}
}

func TestMissingSecret(t *testing.T) {
_, err := ReadLogin("testdata/missing-secret.json")
if err == nil {
t.Fatal("expected an error but there was none")
}
expected := "Invalid service account: Client secret for service account is emtpy for file 'testdata/missing-secret.json'"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err)
}
}
4 changes: 2 additions & 2 deletions pkg/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type JSONParseError struct {
}

func (j *JSONParseError) Error() string {
return fmt.Sprintf("JSON parsing error: %s. Original file %s", j.Err, j.Original)
return fmt.Sprintf("JSON parsing error for json '%v' with error '%v'", j.Original, j.Err)
}

//FileNotFoundError when unable to read file
Expand All @@ -51,5 +51,5 @@ type FileNotFoundError struct {
}

func (j *FileNotFoundError) Error() string {
return fmt.Sprintf("Unable to find file error: %s. Path to file %s", j.Err, j.Path)
return fmt.Sprintf("Unable to find file '%v' with error: '%s'", j.Path, j.Err)
}
48 changes: 0 additions & 48 deletions pkg/flag.go

This file was deleted.

Loading

0 comments on commit 7bbd8bc

Please sign in to comment.