@@ -24,24 +24,50 @@ import (
2424 "testing"
2525 "time"
2626
27- "github.com/aws/aws-sdk-go/aws"
28- "github.com/aws/aws-sdk-go/service/ecr"
29- "github.com/aws/aws-sdk-go/service/ecrpublic"
30- "github.com/golang/mock/gomock"
27+ "github.com/aws/aws-sdk-go-v2/aws"
28+ "github.com/aws/aws-sdk-go-v2/service/ecr"
29+ "github.com/aws/aws-sdk-go-v2/service/ecr/types"
30+ "github.com/aws/aws-sdk-go-v2/service/ecrpublic"
31+ publictypes "github.com/aws/aws-sdk-go-v2/service/ecrpublic/types"
32+ "github.com/stretchr/testify/mock"
3133 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
32- "k8s.io/cloud-provider-aws/pkg/mocks"
3334 v1 "k8s.io/kubelet/pkg/apis/credentialprovider/v1"
3435)
3536
37+ type MockedECR struct {
38+ mock.Mock
39+ }
40+
41+ func (m * MockedECR ) GetAuthorizationToken (ctx context.Context , params * ecr.GetAuthorizationTokenInput , optFns ... func (* ecr.Options )) (* ecr.GetAuthorizationTokenOutput , error ) {
42+ args := m .Called (ctx , params )
43+ if args .Get (1 ) != nil {
44+ return args .Get (0 ).(* ecr.GetAuthorizationTokenOutput ), args .Get (1 ).(error )
45+ }
46+ return args .Get (0 ).(* ecr.GetAuthorizationTokenOutput ), nil
47+ }
48+
49+ // ECRPublic abstracts the calls we make to aws-sdk for testing purposes
50+ type MockedECRPublic struct {
51+ mock.Mock
52+ }
53+
54+ func (m * MockedECRPublic ) GetAuthorizationToken (ctx context.Context , params * ecrpublic.GetAuthorizationTokenInput , optFns ... func (* ecrpublic.Options )) (* ecrpublic.GetAuthorizationTokenOutput , error ) {
55+ args := m .Called (ctx , params )
56+ if args .Get (1 ) != nil {
57+ return args .Get (0 ).(* ecrpublic.GetAuthorizationTokenOutput ), args .Get (1 ).(error )
58+ }
59+ return args .Get (0 ).(* ecrpublic.GetAuthorizationTokenOutput ), nil
60+ }
61+
3662func generatePrivateGetAuthorizationTokenOutput (user string , password string , proxy string , expiration * time.Time ) * ecr.GetAuthorizationTokenOutput {
3763 creds := []byte (fmt .Sprintf ("%s:%s" , user , password ))
38- data := & ecr .AuthorizationData {
64+ data := types .AuthorizationData {
3965 AuthorizationToken : aws .String (base64 .StdEncoding .EncodeToString (creds )),
4066 ExpiresAt : expiration ,
4167 ProxyEndpoint : aws .String (proxy ),
4268 }
4369 output := & ecr.GetAuthorizationTokenOutput {
44- AuthorizationData : []* ecr .AuthorizationData {data },
70+ AuthorizationData : []types .AuthorizationData {data },
4571 }
4672 return output
4773}
@@ -60,11 +86,6 @@ func generateResponse(registry string, username string, password string) *v1.Cre
6086}
6187
6288func Test_GetCredentials_Private (t * testing.T ) {
63- ctrl := gomock .NewController (t )
64- defer ctrl .Finish ()
65-
66- mockECR := mocks .NewMockECR (ctrl )
67-
6889 testcases := []struct {
6990 name string
7091 image string
@@ -109,7 +130,7 @@ func Test_GetCredentials_Private(t *testing.T) {
109130 {
110131 name : "empty authorization token" ,
111132 image : "123456789123.dkr.ecr.us-west-2.amazonaws.com" ,
112- getAuthorizationTokenOutput : & ecr.GetAuthorizationTokenOutput {AuthorizationData : []* ecr .AuthorizationData {{}}},
133+ getAuthorizationTokenOutput : & ecr.GetAuthorizationTokenOutput {AuthorizationData : []types .AuthorizationData {{}}},
113134 getAuthorizationTokenError : nil ,
114135 expectedError : errors .New ("authorization token in response was nil" ),
115136 },
@@ -124,19 +145,19 @@ func Test_GetCredentials_Private(t *testing.T) {
124145 name : "invalid authorization token" ,
125146 image : "123456789123.dkr.ecr.us-west-2.amazonaws.com" ,
126147 getAuthorizationTokenOutput : & ecr.GetAuthorizationTokenOutput {
127- AuthorizationData : []* ecr .AuthorizationData {
128- {AuthorizationToken : aws .String (base64 .StdEncoding .EncodeToString ([]byte (fmt . Sprint ( "foo" ) )))},
148+ AuthorizationData : []types .AuthorizationData {
149+ {AuthorizationToken : aws .String (base64 .StdEncoding .EncodeToString ([]byte ("foo" )))},
129150 },
130151 },
131152 getAuthorizationTokenError : nil ,
132153 expectedError : errors .New ("error parsing username and password from authorization token" ),
133154 },
134155 }
135-
136156 for _ , testcase := range testcases {
137157 t .Run (testcase .name , func (t * testing.T ) {
138- p := & ecrPlugin {ecr : mockECR }
139- mockECR .EXPECT ().GetAuthorizationToken (gomock .Any ()).Return (testcase .getAuthorizationTokenOutput , testcase .getAuthorizationTokenError )
158+ mockECR := MockedECR {}
159+ p := & ecrPlugin {ecr : & mockECR }
160+ mockECR .On ("GetAuthorizationToken" , mock .Anything , mock .Anything ).Return (testcase .getAuthorizationTokenOutput , testcase .getAuthorizationTokenError )
140161
141162 creds , err := p .GetCredentials (context .TODO (), testcase .image , testcase .args )
142163
@@ -163,7 +184,7 @@ func Test_GetCredentials_Private(t *testing.T) {
163184
164185func generatePublicGetAuthorizationTokenOutput (user string , password string , proxy string , expiration * time.Time ) * ecrpublic.GetAuthorizationTokenOutput {
165186 creds := []byte (fmt .Sprintf ("%s:%s" , user , password ))
166- data := & ecrpublic .AuthorizationData {
187+ data := & publictypes .AuthorizationData {
167188 AuthorizationToken : aws .String (base64 .StdEncoding .EncodeToString (creds )),
168189 ExpiresAt : expiration ,
169190 }
@@ -174,11 +195,6 @@ func generatePublicGetAuthorizationTokenOutput(user string, password string, pro
174195}
175196
176197func Test_GetCredentials_Public (t * testing.T ) {
177- ctrl := gomock .NewController (t )
178- defer ctrl .Finish ()
179-
180- mockECRPublic := mocks .NewMockECRPublic (ctrl )
181-
182198 testcases := []struct {
183199 name string
184200 image string
@@ -211,7 +227,7 @@ func Test_GetCredentials_Public(t *testing.T) {
211227 {
212228 name : "empty authorization token" ,
213229 image : "public.ecr.aws" ,
214- getAuthorizationTokenOutput : & ecrpublic.GetAuthorizationTokenOutput {AuthorizationData : & ecrpublic .AuthorizationData {}},
230+ getAuthorizationTokenOutput : & ecrpublic.GetAuthorizationTokenOutput {AuthorizationData : & publictypes .AuthorizationData {}},
215231 getAuthorizationTokenError : nil ,
216232 expectedError : errors .New ("authorization token in response was nil" ),
217233 },
@@ -226,8 +242,8 @@ func Test_GetCredentials_Public(t *testing.T) {
226242 name : "invalid authorization token" ,
227243 image : "public.ecr.aws" ,
228244 getAuthorizationTokenOutput : & ecrpublic.GetAuthorizationTokenOutput {
229- AuthorizationData : & ecrpublic .AuthorizationData {
230- AuthorizationToken : aws .String (base64 .StdEncoding .EncodeToString ([]byte (fmt . Sprint ( "foo" ) ))),
245+ AuthorizationData : & publictypes .AuthorizationData {
246+ AuthorizationToken : aws .String (base64 .StdEncoding .EncodeToString ([]byte ("foo" ))),
231247 },
232248 },
233249 getAuthorizationTokenError : nil ,
@@ -237,8 +253,9 @@ func Test_GetCredentials_Public(t *testing.T) {
237253
238254 for _ , testcase := range testcases {
239255 t .Run (testcase .name , func (t * testing.T ) {
240- p := & ecrPlugin {ecrPublic : mockECRPublic }
241- mockECRPublic .EXPECT ().GetAuthorizationToken (gomock .Any ()).Return (testcase .getAuthorizationTokenOutput , testcase .getAuthorizationTokenError )
256+ mockECRPublic := MockedECRPublic {}
257+ p := & ecrPlugin {ecrPublic : & mockECRPublic }
258+ mockECRPublic .On ("GetAuthorizationToken" , mock .Anything , mock .Anything ).Return (testcase .getAuthorizationTokenOutput , testcase .getAuthorizationTokenError )
242259
243260 creds , err := p .GetCredentials (context .TODO (), testcase .image , testcase .args )
244261
0 commit comments