2021-02-26 20:00:25 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/ecr"
|
2021-06-04 16:25:06 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
2021-05-04 16:37:45 +00:00
|
|
|
awstest "github.com/cloudskiff/driftctl/test/aws"
|
2021-02-26 20:00:25 +00:00
|
|
|
|
|
|
|
"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) {
|
2021-02-26 20:00:25 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2021-05-04 16:37:45 +00:00
|
|
|
mocks func(client *awstest.MockFakeECR)
|
2021-02-26 20:00:25 +00:00
|
|
|
want []*ecr.Repository
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "List with 2 pages",
|
2021-05-04 16:37:45 +00:00
|
|
|
mocks: func(client *awstest.MockFakeECR) {
|
2021-02-26 20:00:25 +00:00
|
|
|
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
|
2021-06-04 16:25:06 +00:00
|
|
|
})).Return(nil).Once()
|
2021-02-26 20:00:25 +00:00
|
|
|
},
|
|
|
|
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-06-04 16:25:06 +00:00
|
|
|
store := cache.New(1)
|
2021-05-04 16:37:45 +00:00
|
|
|
client := awstest.MockFakeECR{}
|
|
|
|
tt.mocks(&client)
|
2021-02-26 20:00:25 +00:00
|
|
|
r := &ecrRepository{
|
2021-05-04 16:37:45 +00:00
|
|
|
client: &client,
|
2021-06-04 16:25:06 +00:00
|
|
|
cache: store,
|
2021-02-26 20:00:25 +00:00
|
|
|
}
|
2021-03-02 12:47:30 +00:00
|
|
|
got, err := r.ListAllRepositories()
|
2021-02-26 20:00:25 +00:00
|
|
|
assert.Equal(t, tt.wantErr, err)
|
2021-06-04 16:25:06 +00:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
// Check that results were cached
|
|
|
|
cachedData, err := r.ListAllRepositories()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, got, cachedData)
|
|
|
|
assert.IsType(t, []*ecr.Repository{}, store.Get("ecrListAllRepositories"))
|
|
|
|
}
|
|
|
|
|
2021-02-26 20:00:25 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|