...

Source file src/github.com/awslabs/amazon-ecr-credential-helper/ecr-login/ecr_test.go

Documentation: github.com/awslabs/amazon-ecr-credential-helper/ecr-login

     1  // Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"). You may
     4  // not use this file except in compliance with the License. A copy of the
     5  // License is located at
     6  //
     7  //	http://aws.amazon.com/apache2.0/
     8  //
     9  // or in the "license" file accompanying this file. This file is distributed
    10  // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
    11  // express or implied. See the License for the specific language governing
    12  // permissions and limitations under the License.
    13  
    14  package ecr
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  	"testing"
    20  
    21  	ecr "github.com/awslabs/amazon-ecr-credential-helper/ecr-login/api"
    22  	mock_api "github.com/awslabs/amazon-ecr-credential-helper/ecr-login/mocks"
    23  	"github.com/docker/docker-credential-helpers/credentials"
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  const (
    28  	region           = "us-east-1"
    29  	proxyEndpoint    = "123456789012" + ".dkr.ecr." + region + ".amazonaws.com"
    30  	proxyEndpointUrl = "https://" + proxyEndpoint
    31  	expectedUsername = "username"
    32  	expectedPassword = "password"
    33  )
    34  
    35  func TestGetSuccess(t *testing.T) {
    36  	factory := &mock_api.MockClientFactory{}
    37  	client := &mock_api.MockClient{}
    38  
    39  	helper := NewECRHelper(WithClientFactory(factory))
    40  
    41  	factory.NewClientFromRegionFn = func(_ string) ecr.Client { return client }
    42  	client.GetCredentialsFn = func(serverURL string) (*ecr.Auth, error) {
    43  		if serverURL != proxyEndpoint {
    44  			return nil, fmt.Errorf("unexpected input: %s", serverURL)
    45  		}
    46  		return &ecr.Auth{
    47  			Username:      expectedUsername,
    48  			Password:      expectedPassword,
    49  			ProxyEndpoint: proxyEndpointUrl,
    50  		}, nil
    51  	}
    52  
    53  	username, password, err := helper.Get(proxyEndpoint)
    54  	assert.Nil(t, err)
    55  	assert.Equal(t, expectedUsername, username)
    56  	assert.Equal(t, expectedPassword, password)
    57  }
    58  
    59  func TestGetError(t *testing.T) {
    60  	factory := &mock_api.MockClientFactory{}
    61  	client := &mock_api.MockClient{}
    62  
    63  	helper := NewECRHelper(WithClientFactory(factory))
    64  
    65  	factory.NewClientFromRegionFn = func(_ string) ecr.Client { return client }
    66  	client.GetCredentialsFn = func(serverURL string) (*ecr.Auth, error) {
    67  		return nil, errors.New("test error")
    68  	}
    69  
    70  	username, password, err := helper.Get(proxyEndpoint)
    71  	assert.True(t, credentials.IsErrCredentialsNotFound(err))
    72  	assert.Empty(t, username)
    73  	assert.Empty(t, password)
    74  }
    75  
    76  func TestGetNoMatch(t *testing.T) {
    77  	helper := NewECRHelper(WithClientFactory(nil))
    78  
    79  	username, password, err := helper.Get("not-ecr-server-url")
    80  	assert.True(t, credentials.IsErrCredentialsNotFound(err))
    81  	assert.Empty(t, username)
    82  	assert.Empty(t, password)
    83  }
    84  
    85  func TestListSuccess(t *testing.T) {
    86  	factory := &mock_api.MockClientFactory{}
    87  	client := &mock_api.MockClient{}
    88  
    89  	helper := NewECRHelper(WithClientFactory(factory))
    90  
    91  	factory.NewClientWithDefaultsFn = func() ecr.Client { return client }
    92  	client.ListCredentialsFn = func() ([]*ecr.Auth, error) {
    93  		return []*ecr.Auth{{
    94  			Username:      expectedUsername,
    95  			Password:      expectedPassword,
    96  			ProxyEndpoint: proxyEndpointUrl,
    97  		}}, nil
    98  	}
    99  
   100  	serverList, err := helper.List()
   101  	assert.NoError(t, err)
   102  	assert.Len(t, serverList, 1)
   103  	assert.Equal(t, expectedUsername, serverList[proxyEndpointUrl])
   104  }
   105  
   106  func TestListFailure(t *testing.T) {
   107  	factory := &mock_api.MockClientFactory{}
   108  	client := &mock_api.MockClient{}
   109  
   110  	helper := NewECRHelper(WithClientFactory(factory))
   111  
   112  	factory.NewClientWithDefaultsFn = func() ecr.Client { return client }
   113  	client.ListCredentialsFn = func() ([]*ecr.Auth, error) {
   114  		return nil, errors.New("nope")
   115  	}
   116  
   117  	serverList, err := helper.List()
   118  	assert.Error(t, err)
   119  	assert.Len(t, serverList, 0)
   120  }
   121  

View as plain text