Skip to content

Commit d9c57ce

Browse files
author
Ryan Svihla
committed
added tests around multiple parts
1 parent 26d0002 commit d9c57ce

16 files changed

+153
-137
lines changed

cmd/db/create.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ package db
1717

1818
import (
1919
"fmt"
20+
"os"
21+
2022
"github.com/rsds143/astra-cli/pkg"
2123
"github.com/rsds143/astra-devops-sdk-go/astraops"
2224
"github.com/spf13/cobra"
23-
"os"
2425
)
2526

2627
var createDbName string
@@ -50,7 +51,8 @@ var CreateCmd = &cobra.Command{
5051
Short: "creates a database by id",
5152
Long: ``,
5253
Run: func(cobraCmd *cobra.Command, args []string) {
53-
client, err := pkg.LoginClient()
54+
creds := &pkg.Creds{}
55+
client, err := creds.Login()
5456
if err != nil {
5557
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
5658
os.Exit(1)

cmd/db/delete.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ var DeleteCmd = &cobra.Command{
3030
Long: `deletes a database from your Astra account by ID`,
3131
Args: cobra.ExactArgs(1),
3232
Run: func(cmd *cobra.Command, args []string) {
33-
client, err := pkg.LoginClient()
33+
creds := &pkg.Creds{}
34+
client, err := creds.Login()
3435
if err != nil {
3536
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
3637
os.Exit(1)

cmd/db/get.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"os"
22-
"strings"
2322

2423
"github.com/rsds143/astra-cli/pkg"
2524
"github.com/rsds143/astra-devops-sdk-go/astraops"
@@ -39,7 +38,8 @@ var GetCmd = &cobra.Command{
3938
Long: `gets a database from your Astra account by ID`,
4039
Args: cobra.ExactArgs(1),
4140
Run: func(cobraCmd *cobra.Command, args []string) {
42-
client, err := pkg.LoginClient()
41+
creds := &pkg.Creds{}
42+
client, err := creds.Login()
4343
if err != nil {
4444
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
4545
os.Exit(1)
@@ -55,8 +55,10 @@ var GetCmd = &cobra.Command{
5555
var rows [][]string
5656
rows = append(rows, []string{"name", "id", "status"})
5757
rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)})
58-
for _, row := range pkg.PadColumns(rows) {
59-
fmt.Println(strings.Join(row, " "))
58+
err = pkg.WriteRows(os.Stdout, rows)
59+
if err != nil {
60+
fmt.Fprintf(os.Stderr, "unexpected error writing out text %v\n", err)
61+
os.Exit(1)
6062
}
6163
case "json":
6264
b, err := json.MarshalIndent(db, "", " ")

cmd/db/list.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"os"
22-
"strings"
2322

2423
"github.com/rsds143/astra-cli/pkg"
2524
"github.com/rsds143/astra-devops-sdk-go/astraops"
@@ -46,7 +45,8 @@ var ListCmd = &cobra.Command{
4645
Short: "lists all databases",
4746
Long: `lists all databases in your Astra account`,
4847
Run: func(cmd *cobra.Command, args []string) {
49-
client, err := pkg.LoginClient()
48+
creds := &pkg.Creds{}
49+
client, err := creds.Login()
5050
if err != nil {
5151
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
5252
os.Exit(1)
@@ -63,8 +63,10 @@ var ListCmd = &cobra.Command{
6363
for _, db := range dbs {
6464
rows = append(rows, []string{db.Info.Name, db.ID, string(db.Status)})
6565
}
66-
for _, row := range pkg.PadColumns(rows) {
67-
fmt.Println(strings.Join(row, " "))
66+
err = pkg.WriteRows(os.Stdout, rows)
67+
if err != nil {
68+
fmt.Fprintf(os.Stderr, "unexpected error writing text output %v", err)
69+
os.Exit(1)
6870
}
6971
case "json":
7072
b, err := json.MarshalIndent(dbs, "", " ")

cmd/db/park.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ var ParkCmd = &cobra.Command{
3131
Long: `parks the database specified, only works on classic tier databases and can take a very long time to park (20-30 minutes)`,
3232
Args: cobra.ExactArgs(1),
3333
Run: func(cobraCmd *cobra.Command, args []string) {
34-
client, err := pkg.LoginClient()
34+
creds := &pkg.Creds{}
35+
client, err := creds.Login()
3536
if err != nil {
3637
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
3738
os.Exit(1)

cmd/db/resize.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ var ResizeCmd = &cobra.Command{
3232
Long: "Resizes a database by id with the specified capacity unit. Note does not work on serverless.",
3333
Args: cobra.ExactArgs(2),
3434
Run: func(cobraCmd *cobra.Command, args []string) {
35-
36-
client, err := pkg.LoginClient()
35+
creds := &pkg.Creds{}
36+
client, err := creds.Login()
3737
if err != nil {
3838
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
3939
os.Exit(1)

cmd/db/secBundle.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ var SecBundleCmd = &cobra.Command{
4242
Long: `gets the secure connetion bundle for the database from your Astra account by ID`,
4343
Args: cobra.ExactArgs(1),
4444
Run: func(cobraCmd *cobra.Command, args []string) {
45-
client, err := pkg.LoginClient()
45+
creds := &pkg.Creds{}
46+
client, err := creds.Login()
4647
if err != nil {
4748
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
4849
os.Exit(1)

cmd/db/tiers.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"encoding/json"
2020
"fmt"
2121
"os"
22-
"strings"
2322

2423
"github.com/rsds143/astra-cli/pkg"
2524
"github.com/rsds143/astra-devops-sdk-go/astraops"
@@ -39,7 +38,8 @@ var TiersCmd = &cobra.Command{
3938
Long: `List all available tiers on the Astra DevOps API. Each tier is a combination of costs, size, region, and name`,
4039
Run: func(cmd *cobra.Command, args []string) {
4140
var tiers []astraops.TierInfo
42-
client, err := pkg.LoginClient()
41+
creds := &pkg.Creds{}
42+
client, err := creds.Login()
4343
if err != nil {
4444
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
4545
os.Exit(1)
@@ -72,8 +72,10 @@ var TiersCmd = &cobra.Command{
7272
fmt.Sprintf("$%.2f", costMonth),
7373
fmt.Sprintf("$%.2f", costMin)})
7474
}
75-
for _, row := range pkg.PadColumns(rows) {
76-
fmt.Println(strings.Join(row, " "))
75+
err = pkg.WriteRows(os.Stdout, rows)
76+
if err != nil {
77+
fmt.Fprintf(os.Stderr, "unexpected error writing text output %v", err)
78+
os.Exit(1)
7779
}
7880
case "json":
7981
b, err := json.MarshalIndent(tiers, "", " ")

cmd/db/unpark.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ var UnparkCmd = &cobra.Command{
3131
Long: `parks the database specified, only works on classic tier databases and can take a very long time to park (20-30 minutes)`,
3232
Args: cobra.ExactArgs(1),
3333
Run: func(cobraCmd *cobra.Command, args []string) {
34-
client, err := pkg.LoginClient()
34+
creds := &pkg.Creds{}
35+
client, err := creds.Login()
3536
if err != nil {
3637
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
3738
os.Exit(1)

cmd/login.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var loginCmd = &cobra.Command{
4444
Short: "Stores credentials for the cli to use in other commands to operate on the Astra DevOps API",
4545
Long: `Token or service account is saved in .config/astra/ for use by the other commands`,
4646
Run: func(cobraCmd *cobra.Command, args []string) {
47-
confDir, confFiles, err := pkg.GetHome()
47+
confDir, confFiles, err := pkg.GetHome(os.UserHomeDir)
4848
if err != nil {
4949
fmt.Printf("%v\n", err)
5050
os.Exit(3)

pkg/conf.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ package pkg
1919
import (
2020
"encoding/json"
2121
"fmt"
22-
"github.com/rsds143/astra-devops-sdk-go/astraops"
2322
"io"
2423
"os"
2524
"path"
2625
"strings"
26+
27+
"github.com/rsds143/astra-devops-sdk-go/astraops"
2728
)
2829

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

5758
// GetHome returns the configuration directory and file
5859
// error will return if there is no user home folder
59-
func GetHome() (confDir string, confFiles ConfFiles, err error) {
60+
func GetHome(getHome func() (string, error)) (confDir string, confFiles ConfFiles, err error) {
6061
var home string
61-
home, err = os.UserHomeDir()
62+
home, err = getHome()
6263
if err != nil {
63-
return "", ConfFiles{}, fmt.Errorf("unable to get user home directory with error %s", err)
64+
return "", ConfFiles{}, fmt.Errorf("unable to get user home directory with error '%s'", err)
6465
}
6566
confDir = path.Join(home, ".config", "astra")
6667

@@ -78,21 +79,24 @@ func ReadToken(tokenFile string) (string, error) {
7879
if err != nil {
7980
return "", &FileNotFoundError{
8081
Path: tokenFile,
81-
Err: fmt.Errorf("unable to read login file with error %w", err),
82+
Err: fmt.Errorf("unable to read login file with error '%w'", err),
8283
}
8384
}
8485
defer func() {
8586
if err := f.Close(); err != nil {
86-
fmt.Printf("warning unable to close %v with error %v", tokenFile, err)
87+
fmt.Printf("warning unable to close %v with error '%v'", tokenFile, err)
8788
}
8889
}()
8990
b, err := io.ReadAll(f)
9091
if err != nil {
91-
return "", fmt.Errorf("unable to read login file %s with error %w", tokenFile, err)
92+
return "", fmt.Errorf("unable to read login file '%s' with error '%w'", tokenFile, err)
93+
}
94+
if len(b) == 0 {
95+
return "", fmt.Errorf("token file '%s' is emtpy", tokenFile)
9296
}
9397
token := strings.Trim(string(b), "\n")
9498
if !strings.HasPrefix(token, "AstraCS") {
95-
return "", fmt.Errorf("invalid token in login file %s with error %s", tokenFile, err)
99+
return "", fmt.Errorf("missing prefix 'AstraCS' in token file '%s'", tokenFile)
96100
}
97101
return token, nil
98102
}
@@ -123,5 +127,14 @@ func ReadLogin(saJSONFile string) (astraops.ClientInfo, error) {
123127
Err: fmt.Errorf("unable to parse json from login file %s with error %s", saJSONFile, err),
124128
}
125129
}
130+
if clientInfo.ClientID == "" {
131+
return astraops.ClientInfo{}, fmt.Errorf("Invalid service account: Client ID for service account is emtpy for file '%v'", saJSONFile)
132+
}
133+
if clientInfo.ClientName == "" {
134+
return astraops.ClientInfo{}, fmt.Errorf("Invalid service account: Client name for service account is emtpy for file '%v'", saJSONFile)
135+
}
136+
if clientInfo.ClientSecret == "" {
137+
return astraops.ClientInfo{}, fmt.Errorf("Invalid service account: Client secret for service account is emtpy for file '%v'", saJSONFile)
138+
}
126139
return clientInfo, err
127140
}

pkg/conf_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,54 @@ func TestReadLoginWithEmptyFile(t *testing.T) {
6060
t.Errorf("expected %T but was %T", e, err)
6161
}
6262
}
63+
64+
func TestUnableToGetHomeFolder(t *testing.T) {
65+
_, _, err := GetHome(func() (string, error) { return "", errors.New("unable to get home") })
66+
if err == nil {
67+
t.Fatal("expected error but none was present")
68+
}
69+
}
70+
71+
func TestReadTokenWithNoFile(t *testing.T) {
72+
_, err := ReadToken("testdata/notthere")
73+
if err == nil {
74+
t.Fatal("expected an error but there was none")
75+
}
76+
var e *FileNotFoundError
77+
if !errors.As(err, &e) {
78+
t.Errorf("expected %T but was %T", e, err)
79+
}
80+
}
81+
82+
func TestMissingId(t *testing.T) {
83+
_, err := ReadLogin("testdata/missing-id.json")
84+
if err == nil {
85+
t.Fatal("expected an error but there was none")
86+
}
87+
expected := "Invalid service account: Client ID for service account is emtpy for file 'testdata/missing-id.json'"
88+
if err.Error() != expected {
89+
t.Errorf("expected '%v' but was '%v'", expected, err)
90+
}
91+
}
92+
93+
func TestMissingName(t *testing.T) {
94+
_, err := ReadLogin("testdata/missing-name.json")
95+
if err == nil {
96+
t.Fatal("expected an error but there was none")
97+
}
98+
expected := "Invalid service account: Client name for service account is emtpy for file 'testdata/missing-name.json'"
99+
if err.Error() != expected {
100+
t.Errorf("expected '%v' but was '%v'", expected, err)
101+
}
102+
}
103+
104+
func TestMissingSecret(t *testing.T) {
105+
_, err := ReadLogin("testdata/missing-secret.json")
106+
if err == nil {
107+
t.Fatal("expected an error but there was none")
108+
}
109+
expected := "Invalid service account: Client secret for service account is emtpy for file 'testdata/missing-secret.json'"
110+
if err.Error() != expected {
111+
t.Errorf("expected '%v' but was '%v'", expected, err)
112+
}
113+
}

pkg/errors.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type JSONParseError struct {
4141
}
4242

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

4747
//FileNotFoundError when unable to read file
@@ -51,5 +51,5 @@ type FileNotFoundError struct {
5151
}
5252

5353
func (j *FileNotFoundError) Error() string {
54-
return fmt.Sprintf("Unable to find file error: %s. Path to file %s", j.Err, j.Path)
54+
return fmt.Sprintf("Unable to find file '%v' with error: '%s'", j.Path, j.Err)
5555
}

pkg/flag.go

-48
This file was deleted.

0 commit comments

Comments
 (0)