driftctl/pkg/remote/aws/repository/ecr_repository_test.go

79 lines
2.0 KiB
Go
Raw Normal View History

package repository
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/service/ecr"
2021-05-04 16:37:45 +00:00
awstest "github.com/cloudskiff/driftctl/test/aws"
"github.com/aws/aws-sdk-go/aws"
"github.com/stretchr/testify/mock"
"github.com/r3labs/diff/v2"
"github.com/stretchr/testify/assert"
)
2021-03-02 12:47:30 +00:00
func Test_ecrRepository_ListAllRepositories(t *testing.T) {
tests := []struct {
name string
2021-05-04 16:37:45 +00:00
mocks func(client *awstest.MockFakeECR)
want []*ecr.Repository
wantErr error
}{
{
name: "List with 2 pages",
2021-05-04 16:37:45 +00:00
mocks: func(client *awstest.MockFakeECR) {
client.On("DescribeRepositoriesPages",
&ecr.DescribeRepositoriesInput{},
mock.MatchedBy(func(callback func(res *ecr.DescribeRepositoriesOutput, lastPage bool) bool) bool {
callback(&ecr.DescribeRepositoriesOutput{
Repositories: []*ecr.Repository{
{RepositoryName: aws.String("1")},
{RepositoryName: aws.String("2")},
{RepositoryName: aws.String("3")},
},
}, false)
callback(&ecr.DescribeRepositoriesOutput{
Repositories: []*ecr.Repository{
{RepositoryName: aws.String("4")},
{RepositoryName: aws.String("5")},
{RepositoryName: aws.String("6")},
},
}, true)
return true
})).Return(nil)
},
want: []*ecr.Repository{
{RepositoryName: aws.String("1")},
{RepositoryName: aws.String("2")},
{RepositoryName: aws.String("3")},
{RepositoryName: aws.String("4")},
{RepositoryName: aws.String("5")},
{RepositoryName: aws.String("6")},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2021-05-04 16:37:45 +00:00
client := awstest.MockFakeECR{}
tt.mocks(&client)
r := &ecrRepository{
2021-05-04 16:37:45 +00:00
client: &client,
}
2021-03-02 12:47:30 +00:00
got, err := r.ListAllRepositories()
assert.Equal(t, tt.wantErr, err)
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s: %s -> %s", strings.Join(change.Path, "."), change.From, change.To)
}
t.Fail()
}
})
}
}