From cae8c2ec0442ed077ee905ffc000bdbe83d20afb Mon Sep 17 00:00:00 2001 From: kenzo0107 Date: Thu, 12 Dec 2019 22:20:44 +0900 Subject: [PATCH] Add mfa_serial etc... to testdata/credentials --- pkg/utility/profile_test.go | 67 ++++++++++++++++++------------------- testdata/credentials | 7 +++- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/pkg/utility/profile_test.go b/pkg/utility/profile_test.go index 9522b77..20b4d1f 100644 --- a/pkg/utility/profile_test.go +++ b/pkg/utility/profile_test.go @@ -1,12 +1,7 @@ package utility import ( - "fmt" - "io/ioutil" "path/filepath" - "reflect" - "runtime" - "strings" "testing" "github.com/google/go-cmp/cmp" @@ -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{}") @@ -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) @@ -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") + } }, }, } { diff --git a/testdata/credentials b/testdata/credentials index 65aaec5..c060b1f 100644 --- a/testdata/credentials +++ b/testdata/credentials @@ -8,4 +8,9 @@ aws_secret_access_key = abcdefghijklmnopqrstuvwxyz [moge] aws_access_key_id = abcdefg1234567890 -aws_secret_access_key = abcdefghijklmnopqrstuvwxyz \ No newline at end of file +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 \ No newline at end of file