Skip to content

Commit

Permalink
Add mfa_serial etc... to testdata/credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
kenzo0107 committed Dec 12, 2019
1 parent 4dcab70 commit cae8c2e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 35 deletions.
67 changes: 33 additions & 34 deletions pkg/utility/profile_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package utility

import (
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -15,24 +10,28 @@ import (
)

var (
testProfiles = []string{"default", "hoge", "moge"}
testProfiles = []string{
"default",
"hoge",
"moge|role_arn = arn:aws:iam::1234567890:role/stsRole|source_profile = hoge|mfa_serial = arn:aws:iam::604257609175:mfa/kenzo.tanaka",
}
)

func TestGetProfilesByTestCredentialsPath(t *testing.T) {
cp := filepath.Join("..", "..", "testdata", "credentials")
profiles, err := GetProfiles(cp)
if err != nil {
t.Error("cannot get profiles from credentilas path")
t.Error("wrong result: \nerr is not nil")
}
if !reflect.DeepEqual(profiles, testProfiles) {
t.Errorf("GetProfiles(\"%s\") should be []string{\"default\", \"hoge\", \"moge\"}, but does not match.", cp)
if diff := cmp.Diff(profiles, testProfiles); diff != "" {
t.Errorf("wrong result: \n%s", diff)
}
}

func TestGetProfilesByEmptyCredentialsPath(t *testing.T) {
profiles, err := GetProfiles("")
if e, a := "open : no such file or directory", err.Error(); e != a {
t.Errorf("expect %v, got %v", e, a)
profiles, err := GetProfiles("notfound_credentials")
if err == nil {
t.Error("wrong result: \nerr is nil")
}
if len(profiles) != 0 {
t.Error("wrong result: profiles = []string{}")
Expand All @@ -48,34 +47,14 @@ func finderProfileTesting(t *testing.T, expectedProfile string) {
termbox.Event{Type: termbox.EventKey, Key: termbox.KeyEnter})...)
actualProfile, err := FinderProfile(testProfiles)
if err != nil {
t.Error("cannot get profile")
t.Error("wrong result: \nerr is not nil")
}
if diff := cmp.Diff(expectedProfile, actualProfile); diff != "" {
t.Errorf("wrong result: \n%s", diff)
}
actual := term.GetResult()

g := fmt.Sprintf("finder_profiles_%s_ui.golden", expectedProfile)
fname := filepath.Join("..", "..", "testdata", g)
// ioutil.WriteFile(fname, []byte(actual), 0644)
b, err := ioutil.ReadFile(fname)
if err != nil {
t.Fatalf("failed to load a golden file: %s", err)
}
expected := string(b)
if runtime.GOOS == "windows" {
expected = strings.Replace(expected, "\r\n", "\n", -1)
}

if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf("wrong result: \n%s", diff)
}
}

func TestFinderProfile(t *testing.T) {
term := fuzzyfinder.UseMockedTerminal()
term.SetSize(60, 10)

for _, testcase := range []struct {
name string
call func(t *testing.T)
Expand All @@ -95,7 +74,27 @@ func TestFinderProfile(t *testing.T) {
{
"profile moge",
func(t *testing.T) {
finderProfileTesting(t, "moge")
finderProfileTesting(t, "moge|role_arn = arn:aws:iam::1234567890:role/stsRole|source_profile = hoge|mfa_serial = arn:aws:iam::604257609175:mfa/kenzo.tanaka")
},
},
{
"profile bar not found",
func(t *testing.T) {
types := "bar"
term := fuzzyfinder.UseMockedTerminal()
term.SetSize(60, 10)

term.SetEvents(append(
TermboxKeys(types),
termbox.Event{Type: termbox.EventKey, Key: termbox.KeyEnter})...)

profile, err := FinderProfile(testProfiles)
if err == nil {
t.Error("wrong result: \nerr is nil")
}
if diff := cmp.Diff(profile, ""); diff != "" {
t.Error("wrong result: \nprofile is not empty")
}
},
},
} {
Expand Down
7 changes: 6 additions & 1 deletion testdata/credentials
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ aws_secret_access_key = abcdefghijklmnopqrstuvwxyz

[moge]
aws_access_key_id = abcdefg1234567890
aws_secret_access_key = abcdefghijklmnopqrstuvwxyz
aws_secret_access_key = abcdefghijklmnopqrstuvwxyz
region = ap-northeast-1
output = json
role_arn = arn:aws:iam::1234567890:role/stsRole
source_profile = hoge
mfa_serial = arn:aws:iam::604257609175:mfa/kenzo.tanaka

0 comments on commit cae8c2e

Please sign in to comment.