Issue 151: Add ecr_repository resource tests

main
Louis TOUSSAINT 2021-02-26 21:00:25 +01:00
parent cf634bfd54
commit 11437d03da
17 changed files with 518254 additions and 0 deletions

2524
mocks/ECRClient.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,23 @@
[
{
"Arn": "arn:aws:ecr:eu-west-3:726421854799:repository/bar",
"Id": "bar",
"ImageTagMutability": "MUTABLE",
"Name": "bar",
"RegistryId": "726421854799",
"RepositoryUrl": "726421854799.dkr.ecr.eu-west-3.amazonaws.com/bar",
"Tags": null,
"EncryptionConfiguration": [
{
"EncryptionType": "AES256",
"KmsKey": ""
}
],
"ImageScanningConfiguration": [
{
"ScanOnPush": true
}
],
"Timeouts": null
}
]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,43 @@
{
"version": 4,
"terraform_version": "0.14.5",
"serial": 3,
"lineage": "864e4cdc-6eda-4b30-6bc6-2b17766dcd69",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "aws_ecr_repository",
"name": "foo",
"provider": "provider[\"registry.terraform.io/hashicorp/aws\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"arn": "arn:aws:ecr:eu-west-3:726421854799:repository/bar",
"encryption_configuration": [
{
"encryption_type": "AES256",
"kms_key": ""
}
],
"id": "bar",
"image_scanning_configuration": [
{
"scan_on_push": true
}
],
"image_tag_mutability": "MUTABLE",
"name": "bar",
"registry_id": "726421854799",
"repository_url": "726421854799.dkr.ecr.eu-west-3.amazonaws.com/bar",
"tags": null,
"timeouts": null
},
"sensitive_attributes": [],
"private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiZGVsZXRlIjoxMjAwMDAwMDAwMDAwfX0="
}
]
}
]
}

View File

@ -0,0 +1,93 @@
package aws
import (
"context"
"testing"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/test"
"github.com/cloudskiff/driftctl/test/goldenfile"
testmocks "github.com/cloudskiff/driftctl/test/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/cloudskiff/driftctl/pkg/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
"github.com/cloudskiff/driftctl/pkg/terraform"
)
func TestEcrRepositorySupplier_Resources(t *testing.T) {
cases := []struct {
test string
dirName string
mocks func(client *repository.MockECRRepository)
err error
}{
{
test: "no repository",
dirName: "ecr_repository_empty",
mocks: func(client *repository.MockECRRepository) {
client.On("ListAllRepository").Return([]*ecr.Repository{}, nil)
},
err: nil,
},
{
test: "multiple repositories",
dirName: "ecr_repository_multiple",
mocks: func(client *repository.MockECRRepository) {
client.On("ListAllRepository").Return([]*ecr.Repository{
{RepositoryName: aws.String("test_ecr")},
{RepositoryName: aws.String("bar")},
}, nil)
},
err: nil,
},
{
test: "cannot list repository",
dirName: "ecr_repository_empty",
mocks: func(client *repository.MockECRRepository) {
client.On("ListAllRepository").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsEcrRepositoryResourceType),
},
}
for _, c := range cases {
shouldUpdate := c.dirName == *goldenfile.Update
providerLibrary := terraform.NewProviderLibrary()
supplierLibrary := resource.NewSupplierLibrary()
if shouldUpdate {
provider, err := InitTestAwsProvider(providerLibrary)
if err != nil {
t.Fatal(err)
}
supplierLibrary.AddSupplier(NewECRRepositorySupplier(provider))
}
t.Run(c.test, func(tt *testing.T) {
fakeClient := repository.MockECRRepository{}
c.mocks(&fakeClient)
provider := testmocks.NewMockedGoldenTFProvider(c.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
deserializer := awsdeserializer.NewECRRepositoryDeserializer()
s := &ECRRepositorySupplier{
provider,
deserializer,
&fakeClient,
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
}
got, err := s.Resources()
assert.Equal(tt, c.err, err)
mock.AssertExpectationsForObjects(tt)
test.CtyTestDiff(got, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}

View File

@ -0,0 +1,78 @@
package repository
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/aws/aws-sdk-go/aws"
"github.com/stretchr/testify/mock"
"github.com/cloudskiff/driftctl/mocks"
"github.com/r3labs/diff/v2"
"github.com/stretchr/testify/assert"
)
func Test_ecrRepository_ListAllRepository(t *testing.T) {
tests := []struct {
name string
mocks func(client *mocks.ECRClient)
want []*ecr.Repository
wantErr error
}{
{
name: "List with 2 pages",
mocks: func(client *mocks.ECRClient) {
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) {
client := &mocks.ECRClient{}
tt.mocks(client)
r := &ecrRepository{
client: client,
}
got, err := r.ListAllRepository()
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()
}
})
}
}

View File

@ -0,0 +1,36 @@
// Code generated by mockery v1.0.0. DO NOT EDIT.
package repository
import (
ecr "github.com/aws/aws-sdk-go/service/ecr"
mock "github.com/stretchr/testify/mock"
)
// MockECRRepository is an autogenerated mock type for the MockECRRepository type
type MockECRRepository struct {
mock.Mock
}
// ListAllRepository provides a mock function with given fields:
func (_m *MockECRRepository) ListAllRepository() ([]*ecr.Repository, error) {
ret := _m.Called()
var r0 []*ecr.Repository
if rf, ok := ret.Get(0).(func() []*ecr.Repository); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]*ecr.Repository)
}
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
} else {
r1 = ret.Error(1)
}
return r0, r1
}

View File

@ -0,0 +1 @@
[]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,5 @@
{
"Typ": "WyJvYmplY3QiLHsiYXJuIjoic3RyaW5nIiwiZW5jcnlwdGlvbl9jb25maWd1cmF0aW9uIjpbImxpc3QiLFsib2JqZWN0Iix7ImVuY3J5cHRpb25fdHlwZSI6InN0cmluZyIsImttc19rZXkiOiJzdHJpbmcifV1dLCJpZCI6InN0cmluZyIsImltYWdlX3NjYW5uaW5nX2NvbmZpZ3VyYXRpb24iOlsibGlzdCIsWyJvYmplY3QiLHsic2Nhbl9vbl9wdXNoIjoiYm9vbCJ9XV0sImltYWdlX3RhZ19tdXRhYmlsaXR5Ijoic3RyaW5nIiwibmFtZSI6InN0cmluZyIsInJlZ2lzdHJ5X2lkIjoic3RyaW5nIiwicmVwb3NpdG9yeV91cmwiOiJzdHJpbmciLCJ0YWdzIjpbIm1hcCIsInN0cmluZyJdLCJ0aW1lb3V0cyI6WyJvYmplY3QiLHsiZGVsZXRlIjoic3RyaW5nIn1dfV0=",
"Val": "eyJhcm4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jpldS13ZXN0LTM6NzI2NDIxODU0Nzk5OnJlcG9zaXRvcnkvdGVzdF9lY3IiLCJlbmNyeXB0aW9uX2NvbmZpZ3VyYXRpb24iOlt7ImVuY3J5cHRpb25fdHlwZSI6IkFFUzI1NiIsImttc19rZXkiOiIifV0sImlkIjoidGVzdF9lY3IiLCJpbWFnZV9zY2FubmluZ19jb25maWd1cmF0aW9uIjpbeyJzY2FuX29uX3B1c2giOmZhbHNlfV0sImltYWdlX3RhZ19tdXRhYmlsaXR5IjoiTVVUQUJMRSIsIm5hbWUiOiJ0ZXN0X2VjciIsInJlZ2lzdHJ5X2lkIjoiNzI2NDIxODU0Nzk5IiwicmVwb3NpdG9yeV91cmwiOiI3MjY0MjE4NTQ3OTkuZGtyLmVjci5ldS13ZXN0LTMuYW1hem9uYXdzLmNvbS90ZXN0X2VjciIsInRhZ3MiOnt9LCJ0aW1lb3V0cyI6eyJkZWxldGUiOm51bGx9fQ==",
"Err": null
}

View File

@ -0,0 +1,48 @@
[
{
"arn": "arn:aws:ecr:eu-west-3:726421854799:repository/bar",
"encryption_configuration": [
{
"encryption_type": "AES256",
"kms_key": ""
}
],
"id": "bar",
"image_scanning_configuration": [
{
"scan_on_push": true
}
],
"image_tag_mutability": "MUTABLE",
"name": "bar",
"registry_id": "726421854799",
"repository_url": "726421854799.dkr.ecr.eu-west-3.amazonaws.com/bar",
"tags": {},
"timeouts": {
"delete": null
}
},
{
"arn": "arn:aws:ecr:eu-west-3:726421854799:repository/test_ecr",
"encryption_configuration": [
{
"encryption_type": "AES256",
"kms_key": ""
}
],
"id": "test_ecr",
"image_scanning_configuration": [
{
"scan_on_push": false
}
],
"image_tag_mutability": "MUTABLE",
"name": "test_ecr",
"registry_id": "726421854799",
"repository_url": "726421854799.dkr.ecr.eu-west-3.amazonaws.com/test_ecr",
"tags": {},
"timeouts": {
"delete": null
}
}
]

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,17 @@
resource "aws_ecr_repository" "foo" {
name = "bar"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
}
resource "aws_ecr_repository" "test_ecr_repository" {
name = "test_ecr"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = false
}
}

View File

@ -0,0 +1,78 @@
package aws_test
import (
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/cloudskiff/driftctl/pkg/analyser"
awsresources "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/test/acceptance"
"github.com/cloudskiff/driftctl/test/acceptance/awsutils"
"github.com/r3labs/diff/v2"
)
func TestAcc_AwsECRRepository(t *testing.T) {
var mutatedRepositoryID string
acceptance.Run(t, acceptance.AccTestCase{
Paths: []string{"./testdata/acc/aws_ecr_repository"},
Args: []string{"scan", "--filter", "Type=='aws_ecr_repository'"},
Checks: []acceptance.AccCheck{
{
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
Check: func(result *acceptance.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
}
result.AssertInfrastructureIsInSync()
result.AssertManagedCount(1)
mutatedRepositoryID = result.Managed()[0].TerraformId()
},
},
{
Env: map[string]string{
"AWS_REGION": "us-east-1",
},
PreExec: func() {
client := ecr.New(awsutils.Session())
_, err := client.PutImageTagMutability(&ecr.PutImageTagMutabilityInput{
RepositoryName: &mutatedRepositoryID,
ImageTagMutability: aws.String("IMMUTABLE"),
})
if err != nil {
t.Fatal(err)
}
},
Check: func(result *acceptance.ScanResult, stdout string, err error) {
if err != nil {
t.Fatal(err)
}
result.AssertDriftCountTotal(1)
result.AssertDeletedCount(0)
result.AssertManagedCount(1)
result.AssertUnmanagedCount(0)
result.AssertResourceHasDrift(
mutatedRepositoryID,
awsresources.AwsEcrRepositoryResourceType,
analyser.Change{
Change: diff.Change{
Type: diff.UPDATE,
Path: []string{"ImageTagMutability"},
From: "MUTABLE",
To: "IMMUTABLE",
},
Computed: false,
},
)
},
},
},
})
}

View File

@ -0,0 +1,20 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/aws" {
version = "3.30.0"
hashes = [
"h1:PmKa3uxO2mDA5FJfGmpX+4e0x70vFLV5Ka9NxkuMpUo=",
"zh:01f562a6a31fe46a8ca74804f360e3452b26f71abc549ce1f0ab5a8af2484cdf",
"zh:25bacc5ed725051f0ab1f7d575e45c901e5b8e1d50da4156a31dda92b2b7e481",
"zh:349b79979d9169db614d8ebd1bc2e0caeb7a38dc816e261b8b2b4b5204615519",
"zh:5e41446acc54c6fc15e82c3fa14b72174b30eba81e0711ede297e5620c55a628",
"zh:68ad98f6d612bdc35a65d48950abc8e75c69decb49db28258ce8eeb5458586b7",
"zh:704603d65e8bac17d203b57c2db142c3134a91076e1b4a31c40f75eb3257dde8",
"zh:a362c700032b2db047d16007d52f28b3f216d32671b6b355d23bdaa082c66a4b",
"zh:bd197797b41268de3c93cad02b7c655dc0c4d8661abb37544ca049e6b1eccae6",
"zh:deb12ef0e3396a71d485977ddc14b695775f7937097ebf2b2f53ed348a4365e7",
"zh:ec8a7d0f02738f290107d39bf401d68ddce82a95cd9d998003f7e04b3a196411",
"zh:ffcc43b6c5e7f26c55e2a8c539d7370fca8042722400a3e06bdce4240bd7088a",
]
}

View File

@ -0,0 +1,12 @@
provider "aws" {
region = "us-east-1"
}
resource "aws_ecr_repository" "foo" {
name = "bar"
image_tag_mutability = "MUTABLE"
image_scanning_configuration {
scan_on_push = true
}
}