github.com/argoproj/argo-cd/v3@v3.2.1/cmd/argocd-k8s-auth/commands/aws_test.go (about) 1 package commands 2 3 import ( 4 "errors" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestGetSignedRequestWithRetry(t *testing.T) { 13 t.Parallel() 14 15 ctx := t.Context() 16 17 t.Run("will return signed request on first attempt", func(t *testing.T) { 18 // given 19 t.Parallel() 20 mock := &signedRequestMock{ 21 returnFunc: func(_ *signedRequestMock) (string, error) { 22 return "token", nil 23 }, 24 } 25 26 // when 27 signed, err := getSignedRequestWithRetry(ctx, time.Second, time.Millisecond, "cluster-name", "", "", mock.getSignedRequestMock) 28 29 // then 30 require.NoError(t, err) 31 assert.Equal(t, "token", signed) 32 }) 33 t.Run("will return signed request on third attempt", func(t *testing.T) { 34 // given 35 t.Parallel() 36 mock := &signedRequestMock{ 37 returnFunc: func(m *signedRequestMock) (string, error) { 38 if m.getSignedRequestCalls < 3 { 39 return "", errors.New("some error") 40 } 41 return "token", nil 42 }, 43 } 44 45 // when 46 signed, err := getSignedRequestWithRetry(ctx, time.Second, time.Millisecond, "cluster-name", "", "", mock.getSignedRequestMock) 47 48 // then 49 require.NoError(t, err) 50 assert.Equal(t, "token", signed) 51 }) 52 t.Run("will return error on timeout", func(t *testing.T) { 53 // given 54 t.Parallel() 55 mock := &signedRequestMock{ 56 returnFunc: func(_ *signedRequestMock) (string, error) { 57 return "", errors.New("some error") 58 }, 59 } 60 61 // when 62 signed, err := getSignedRequestWithRetry(ctx, time.Second, time.Millisecond, "cluster-name", "", "", mock.getSignedRequestMock) 63 64 // then 65 require.Error(t, err) 66 assert.Empty(t, signed) 67 }) 68 } 69 70 type signedRequestMock struct { 71 getSignedRequestCalls int 72 returnFunc func(m *signedRequestMock) (string, error) 73 } 74 75 func (m *signedRequestMock) getSignedRequestMock(_, _ string, _ string) (string, error) { 76 m.getSignedRequestCalls++ 77 return m.returnFunc(m) 78 }