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

Add test for printing of STAKs #9

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions .github/workflows/golang-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: golang-test
on:
push:
branches:
- master
- main
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- uses: n8maninger/action-golang-test@v1
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
- uses: actions/setup-go@v5
with:
go-version: '1.21.6'
cache: false
Expand Down
7 changes: 3 additions & 4 deletions lib/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package helper

import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -77,10 +78,8 @@ func SaveSession(filename string, config structs.Configuration) error {
////////////////////////////////////////////////////////////////////////////////

// PrintSTAK prints out the short term access keys for AWS auth.
func PrintSTAK(stak kion.STAK, account string) error {
fmt.Printf("export AWS_ACCESS_KEY_ID=%v\n", stak.AccessKey)
fmt.Printf("export AWS_SECRET_ACCESS_KEY=%v\n", stak.SecretAccessKey)
fmt.Printf("export AWS_SESSION_TOKEN=%v\n", stak.SessionToken)
func PrintSTAK(w io.Writer, stak kion.STAK) error {
fmt.Fprintf(w, "export AWS_ACCESS_KEY_ID=%v\nexport AWS_SECRET_ACCESS_KEY=%v\nexport AWS_SESSION_TOKEN=%v\n", stak.AccessKey, stak.SecretAccessKey, stak.SessionToken)
return nil
}

Expand Down
67 changes: 67 additions & 0 deletions lib/helper/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package helper

import (
"bytes"
"testing"

"github.com/kionsoftware/kion-cli/lib/kion"
)

func TestPrintSTAK(t *testing.T) {
tt := []struct {
description string
stak kion.STAK
want string
}{
{
"Empty",
kion.STAK{},
"export AWS_ACCESS_KEY_ID=\nexport AWS_SECRET_ACCESS_KEY=\nexport AWS_SESSION_TOKEN=\n",
},
// {
// "Panic Condition",
// kion.STAK{},
// "panic",
// },
{
"Partial STAK",
kion.STAK{
AccessKey: "",
SecretAccessKey: "aBCDeFg1hijkl2m3NOPqr4StUvWxY56z7abc8DEf",
SessionToken: "",
},
"export AWS_ACCESS_KEY_ID=\nexport AWS_SECRET_ACCESS_KEY=aBCDeFg1hijkl2m3NOPqr4StUvWxY56z7abc8DEf\nexport AWS_SESSION_TOKEN=\n",
},
{
"Full STAK",
kion.STAK{
AccessKey: "ASIAABCDEFGHIJ1K23LM",
SecretAccessKey: "aBCDeFg1hijkl2m3NOPqr4StUvWxY56z7abc8DEf",
SessionToken: "AbcDEFghIJKlMNoPQrStuVwXYZabcDEfGhI1JklmNoPQRStu2VWXYZaBcd34ef+GH+IJKLmNOPQRSTU5VwxyzABcdeFGHIj6KlMNoPQ7rSTUvW8X9yZAbCD0ef+gHIJkLMnoPqrstUVwxyzAb1CD2e34fgHiJKlMnOPqr56STuvwXyzABcdEfgh7IJK+8LM91No2pqrSTuvWxyz3ABCdEFGH4ijklMNOP5qrs6TUvWxyz789abcDefgH12iJKlM3no4pQRs+5t6UVw7/xy+ZaBcdE+FGhIj8kLmnOpqrstuvw9xyzab1cD/ef23GhIjkLMNoPQrstuv=",
},
"export AWS_ACCESS_KEY_ID=ASIAABCDEFGHIJ1K23LM\nexport AWS_SECRET_ACCESS_KEY=aBCDeFg1hijkl2m3NOPqr4StUvWxY56z7abc8DEf\nexport AWS_SESSION_TOKEN=AbcDEFghIJKlMNoPQrStuVwXYZabcDEfGhI1JklmNoPQRStu2VWXYZaBcd34ef+GH+IJKLmNOPQRSTU5VwxyzABcdeFGHIj6KlMNoPQ7rSTUvW8X9yZAbCD0ef+gHIJkLMnoPqrstUVwxyzAb1CD2e34fgHiJKlMnOPqr56STuvwXyzABcdEfgh7IJK+8LM91No2pqrSTuvWxyz3ABCdEFGH4ijklMNOP5qrs6TUvWxyz789abcDefgH12iJKlM3no4pQRs+5t6UVw7/xy+ZaBcdE+FGhIj8kLmnOpqrstuvw9xyzab1cD/ef23GhIjkLMNoPQrstuv=\n",
},
}

for _, tc := range tt {
t.Run(tc.description, func(t *testing.T) {
// defer func to handle panic in test
defer func() {
if tc.want == "panic" {
if r := recover(); r == nil {
t.Errorf("function should panic")
}
}
}()

var output bytes.Buffer
err := PrintSTAK(&output, tc.stak)
if err != nil {
t.Error(err)
}
if tc.want != "panic" && tc.want != output.String() {
t.Errorf("\ngot:\n %v\nwanted:\n %v", output.String(), tc.want)
}
})
}
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func genStaksPrivateAPI(cCtx *cli.Context, pMap map[string]kion.Project, project

// print or create subshell
if cCtx.Bool("print") {
return helper.PrintSTAK(stak, aMap[account].Number)
return helper.PrintSTAK(os.Stdout, stak)
} else {
return helper.CreateSubShell(aMap[account].Number, account, car, stak)
}
Expand Down Expand Up @@ -408,7 +408,7 @@ func genStaks(cCtx *cli.Context) error {

// print or create subshell
if cCtx.Bool("print") {
return helper.PrintSTAK(stak, aMap[account].Number)
return helper.PrintSTAK(os.Stdout, stak)
} else {
return helper.CreateSubShell(aMap[account].Number, account, car, stak)
}
Expand Down Expand Up @@ -447,7 +447,7 @@ func genStaksFav(cCtx *cli.Context) error {

// print or create subshell
if cCtx.Bool("print") {
return helper.PrintSTAK(stak, favorite.Account)
return helper.PrintSTAK(os.Stdout, stak)
} else {
return helper.CreateSubShell(favorite.Account, favorite.Name, favorite.CAR, stak)
}
Expand Down
Loading