Merge pull request #709 from cloudskiff/test_new_remote_archi
Add tests for new remote architecturemain
commit
d7d6807965
|
@ -209,7 +209,7 @@ func scanRun(opts *pkg.ScanOptions) error {
|
|||
logrus.Trace("Exited")
|
||||
}()
|
||||
|
||||
scanner := pkg.NewScanner(supplierLibrary.Suppliers(), remoteLibrary, alerter, pkg.ScannerOptions{Deep: opts.Deep})
|
||||
scanner := remote.NewScanner(supplierLibrary.Suppliers(), remoteLibrary, alerter, remote.ScannerOptions{Deep: opts.Deep})
|
||||
|
||||
iacSupplier, err := supplier.GetIACSupplier(opts.From, providerLibrary, opts.BackendOptions, iacProgress, resFactory)
|
||||
if err != nil {
|
||||
|
|
1616
pkg/driftctl_test.go
1616
pkg/driftctl_test.go
File diff suppressed because it is too large
Load Diff
|
@ -40,13 +40,17 @@ type awsConfig struct {
|
|||
type AWSTerraformProvider struct {
|
||||
*terraform.TerraformProvider
|
||||
session *session.Session
|
||||
name string
|
||||
version string
|
||||
}
|
||||
|
||||
func NewAWSTerraformProvider(version string, progress output.Progress, configDir string) (*AWSTerraformProvider, error) {
|
||||
p := &AWSTerraformProvider{}
|
||||
providerKey := "aws"
|
||||
p := &AWSTerraformProvider{
|
||||
version: version,
|
||||
name: "aws",
|
||||
}
|
||||
installer, err := tf.NewProviderInstaller(tf.ProviderConfig{
|
||||
Key: providerKey,
|
||||
Key: p.name,
|
||||
Version: version,
|
||||
ConfigDir: configDir,
|
||||
})
|
||||
|
@ -57,7 +61,7 @@ func NewAWSTerraformProvider(version string, progress output.Progress, configDir
|
|||
SharedConfigState: session.SharedConfigEnable,
|
||||
}))
|
||||
tfProvider, err := terraform.NewTerraformProvider(installer, terraform.TerraformProviderConfig{
|
||||
Name: providerKey,
|
||||
Name: p.name,
|
||||
DefaultAlias: *p.session.Config.Region,
|
||||
GetProviderConfig: func(alias string) interface{} {
|
||||
return awsConfig{
|
||||
|
@ -72,3 +76,11 @@ func NewAWSTerraformProvider(version string, progress output.Progress, configDir
|
|||
p.TerraformProvider = tfProvider
|
||||
return p, err
|
||||
}
|
||||
|
||||
func (a *AWSTerraformProvider) Name() string {
|
||||
return a.name
|
||||
}
|
||||
|
||||
func (p *AWSTerraformProvider) Version() string {
|
||||
return p.version
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ type S3BucketDetailsFetcher struct {
|
|||
deserializer *resource.Deserializer
|
||||
}
|
||||
|
||||
func NewS3BucketDetailsFetcher(provider *AWSTerraformProvider, deserializer *resource.Deserializer) *S3BucketDetailsFetcher {
|
||||
func NewS3BucketDetailsFetcher(provider terraform.ResourceReader, deserializer *resource.Deserializer) *S3BucketDetailsFetcher {
|
||||
return &S3BucketDetailsFetcher{
|
||||
reader: provider,
|
||||
deserializer: deserializer,
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"Typ": "WyJvYmplY3QiLHsiYnVja2V0Ijoic3RyaW5nIiwiZGVzdGluYXRpb24iOlsibGlzdCIsWyJvYmplY3QiLHsiYnVja2V0IjpbImxpc3QiLFsib2JqZWN0Iix7ImFjY291bnRfaWQiOiJzdHJpbmciLCJidWNrZXRfYXJuIjoic3RyaW5nIiwiZW5jcnlwdGlvbiI6WyJsaXN0IixbIm9iamVjdCIseyJzc2Vfa21zIjpbImxpc3QiLFsib2JqZWN0Iix7ImtleV9pZCI6InN0cmluZyJ9XV0sInNzZV9zMyI6WyJsaXN0IixbIm9iamVjdCIse31dXX1dXSwiZm9ybWF0Ijoic3RyaW5nIiwicHJlZml4Ijoic3RyaW5nIn1dXX1dXSwiZW5hYmxlZCI6ImJvb2wiLCJmaWx0ZXIiOlsibGlzdCIsWyJvYmplY3QiLHsicHJlZml4Ijoic3RyaW5nIn1dXSwiaWQiOiJzdHJpbmciLCJpbmNsdWRlZF9vYmplY3RfdmVyc2lvbnMiOiJzdHJpbmciLCJuYW1lIjoic3RyaW5nIiwib3B0aW9uYWxfZmllbGRzIjpbInNldCIsInN0cmluZyJdLCJzY2hlZHVsZSI6WyJsaXN0IixbIm9iamVjdCIseyJmcmVxdWVuY3kiOiJzdHJpbmcifV1dfV0=",
|
||||
"Val": "eyJidWNrZXQiOiJidWNrZXQtbWFydGluLXRlc3QtZHJpZnQyIiwiZGVzdGluYXRpb24iOlt7ImJ1Y2tldCI6W3siYWNjb3VudF9pZCI6IiIsImJ1Y2tldF9hcm4iOiJhcm46YXdzOnMzOjo6YnVja2V0LW1hcnRpbi10ZXN0LWRyaWZ0MiIsImVuY3J5cHRpb24iOltdLCJmb3JtYXQiOiJPUkMiLCJwcmVmaXgiOiIifV19XSwiZW5hYmxlZCI6dHJ1ZSwiZmlsdGVyIjpbXSwiaWQiOiJidWNrZXQtbWFydGluLXRlc3QtZHJpZnQyOkludmVudG9yeTJfQnVja2V0MiIsImluY2x1ZGVkX29iamVjdF92ZXJzaW9ucyI6IkFsbCIsIm5hbWUiOiJJbnZlbnRvcnkyX0J1Y2tldDIiLCJvcHRpb25hbF9maWVsZHMiOltdLCJzY2hlZHVsZSI6W3siZnJlcXVlbmN5IjoiRGFpbHkifV19",
|
||||
"Err": null
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"Typ": "WyJvYmplY3QiLHsiYnVja2V0Ijoic3RyaW5nIiwiZGVzdGluYXRpb24iOlsibGlzdCIsWyJvYmplY3QiLHsiYnVja2V0IjpbImxpc3QiLFsib2JqZWN0Iix7ImFjY291bnRfaWQiOiJzdHJpbmciLCJidWNrZXRfYXJuIjoic3RyaW5nIiwiZW5jcnlwdGlvbiI6WyJsaXN0IixbIm9iamVjdCIseyJzc2Vfa21zIjpbImxpc3QiLFsib2JqZWN0Iix7ImtleV9pZCI6InN0cmluZyJ9XV0sInNzZV9zMyI6WyJsaXN0IixbIm9iamVjdCIse31dXX1dXSwiZm9ybWF0Ijoic3RyaW5nIiwicHJlZml4Ijoic3RyaW5nIn1dXX1dXSwiZW5hYmxlZCI6ImJvb2wiLCJmaWx0ZXIiOlsibGlzdCIsWyJvYmplY3QiLHsicHJlZml4Ijoic3RyaW5nIn1dXSwiaWQiOiJzdHJpbmciLCJpbmNsdWRlZF9vYmplY3RfdmVyc2lvbnMiOiJzdHJpbmciLCJuYW1lIjoic3RyaW5nIiwib3B0aW9uYWxfZmllbGRzIjpbInNldCIsInN0cmluZyJdLCJzY2hlZHVsZSI6WyJsaXN0IixbIm9iamVjdCIseyJmcmVxdWVuY3kiOiJzdHJpbmcifV1dfV0=",
|
||||
"Val": "eyJidWNrZXQiOiJidWNrZXQtbWFydGluLXRlc3QtZHJpZnQyIiwiZGVzdGluYXRpb24iOlt7ImJ1Y2tldCI6W3siYWNjb3VudF9pZCI6IiIsImJ1Y2tldF9hcm4iOiJhcm46YXdzOnMzOjo6YnVja2V0LW1hcnRpbi10ZXN0LWRyaWZ0MiIsImVuY3J5cHRpb24iOltdLCJmb3JtYXQiOiJPUkMiLCJwcmVmaXgiOiIifV19XSwiZW5hYmxlZCI6dHJ1ZSwiZmlsdGVyIjpbXSwiaWQiOiJidWNrZXQtbWFydGluLXRlc3QtZHJpZnQyOkludmVudG9yeV9CdWNrZXQyIiwiaW5jbHVkZWRfb2JqZWN0X3ZlcnNpb25zIjoiQWxsIiwibmFtZSI6IkludmVudG9yeV9CdWNrZXQyIiwib3B0aW9uYWxfZmllbGRzIjpbXSwic2NoZWR1bGUiOlt7ImZyZXF1ZW5jeSI6IkRhaWx5In1dfQ==",
|
||||
"Err": null
|
||||
}
|
|
@ -1 +1,56 @@
|
|||
[]
|
||||
[
|
||||
{
|
||||
"bucket": "bucket-martin-test-drift2",
|
||||
"destination": [
|
||||
{
|
||||
"bucket": [
|
||||
{
|
||||
"account_id": "",
|
||||
"bucket_arn": "arn:aws:s3:::bucket-martin-test-drift2",
|
||||
"encryption": [],
|
||||
"format": "ORC",
|
||||
"prefix": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"enabled": true,
|
||||
"filter": [],
|
||||
"id": "bucket-martin-test-drift2:Inventory_Bucket2",
|
||||
"included_object_versions": "All",
|
||||
"name": "Inventory_Bucket2",
|
||||
"optional_fields": [],
|
||||
"schedule": [
|
||||
{
|
||||
"frequency": "Daily"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"bucket": "bucket-martin-test-drift2",
|
||||
"destination": [
|
||||
{
|
||||
"bucket": [
|
||||
{
|
||||
"account_id": "",
|
||||
"bucket_arn": "arn:aws:s3:::bucket-martin-test-drift2",
|
||||
"encryption": [],
|
||||
"format": "ORC",
|
||||
"prefix": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"enabled": true,
|
||||
"filter": [],
|
||||
"id": "bucket-martin-test-drift2:Inventory2_Bucket2",
|
||||
"included_object_versions": "All",
|
||||
"name": "Inventory2_Bucket2",
|
||||
"optional_fields": [],
|
||||
"schedule": [
|
||||
{
|
||||
"frequency": "Daily"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
File diff suppressed because it is too large
Load Diff
|
@ -11,6 +11,8 @@ import (
|
|||
|
||||
type GithubTerraformProvider struct {
|
||||
*terraform.TerraformProvider
|
||||
name string
|
||||
version string
|
||||
}
|
||||
|
||||
type githubConfig struct {
|
||||
|
@ -20,10 +22,12 @@ type githubConfig struct {
|
|||
}
|
||||
|
||||
func NewGithubTerraformProvider(version string, progress output.Progress, configDir string) (*GithubTerraformProvider, error) {
|
||||
p := &GithubTerraformProvider{}
|
||||
providerKey := "github"
|
||||
p := &GithubTerraformProvider{
|
||||
version: version,
|
||||
name: "github",
|
||||
}
|
||||
installer, err := tf.NewProviderInstaller(tf.ProviderConfig{
|
||||
Key: providerKey,
|
||||
Key: p.name,
|
||||
Version: version,
|
||||
ConfigDir: configDir,
|
||||
})
|
||||
|
@ -31,7 +35,7 @@ func NewGithubTerraformProvider(version string, progress output.Progress, config
|
|||
return nil, err
|
||||
}
|
||||
tfProvider, err := terraform.NewTerraformProvider(installer, terraform.TerraformProviderConfig{
|
||||
Name: providerKey,
|
||||
Name: p.name,
|
||||
DefaultAlias: p.GetConfig().getDefaultOwner(),
|
||||
GetProviderConfig: func(owner string) interface{} {
|
||||
return githubConfig{
|
||||
|
@ -60,3 +64,11 @@ func (p GithubTerraformProvider) GetConfig() githubConfig {
|
|||
Organization: os.Getenv("GITHUB_ORGANIZATION"),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *GithubTerraformProvider) Name() string {
|
||||
return p.name
|
||||
}
|
||||
|
||||
func (p *GithubTerraformProvider) Version() string {
|
||||
return p.version
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ func (e *EnumerationAccessDeniedAlert) GetProviderMessage() string {
|
|||
return message
|
||||
}
|
||||
|
||||
func HandleResourceEnumerationError(err error, alerter *alerter.Alerter) error {
|
||||
func HandleResourceEnumerationError(err error, alerter alerter.AlerterInterface) error {
|
||||
listError, ok := err.(*remoteerror.ResourceEnumerationError)
|
||||
if !ok {
|
||||
return err
|
||||
|
|
|
@ -0,0 +1,138 @@
|
|||
package remote
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
awssdk "github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/s3"
|
||||
"github.com/cloudskiff/driftctl/mocks"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/common"
|
||||
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
|
||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
||||
terraform2 "github.com/cloudskiff/driftctl/test/terraform"
|
||||
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/client"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
||||
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
"github.com/cloudskiff/driftctl/test"
|
||||
"github.com/cloudskiff/driftctl/test/goldenfile"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestS3Bucket(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
test string
|
||||
dirName string
|
||||
mocks func(repository *repository.MockS3Repository)
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
test: "multiple bucket", dirName: "aws_s3_bucket_multiple",
|
||||
mocks: func(repository *repository.MockS3Repository) {
|
||||
repository.On(
|
||||
"ListAllBuckets",
|
||||
).Return([]*s3.Bucket{
|
||||
{Name: awssdk.String("bucket-martin-test-drift")},
|
||||
{Name: awssdk.String("bucket-martin-test-drift2")},
|
||||
{Name: awssdk.String("bucket-martin-test-drift3")},
|
||||
}, nil)
|
||||
|
||||
repository.On(
|
||||
"GetBucketLocation",
|
||||
"bucket-martin-test-drift",
|
||||
).Return(
|
||||
"eu-west-1",
|
||||
nil,
|
||||
)
|
||||
|
||||
repository.On(
|
||||
"GetBucketLocation",
|
||||
"bucket-martin-test-drift2",
|
||||
).Return(
|
||||
"eu-west-3",
|
||||
nil,
|
||||
)
|
||||
|
||||
repository.On(
|
||||
"GetBucketLocation",
|
||||
"bucket-martin-test-drift3",
|
||||
).Return(
|
||||
"ap-northeast-1",
|
||||
nil,
|
||||
)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "cannot list bucket", dirName: "s3_bucket_list",
|
||||
mocks: func(repository *repository.MockS3Repository) {
|
||||
repository.On("ListAllBuckets").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
||||
},
|
||||
wantErr: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsS3BucketResourceType),
|
||||
},
|
||||
}
|
||||
|
||||
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
||||
resourceaws.InitResourcesMetadata(schemaRepository)
|
||||
factory := terraform.NewTerraformResourceFactory(schemaRepository)
|
||||
deserializer := resource.NewDeserializer(factory)
|
||||
alerter := &mocks.AlerterInterface{}
|
||||
|
||||
for _, c := range tests {
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
shouldUpdate := c.dirName == *goldenfile.Update
|
||||
|
||||
session := session.Must(session.NewSessionWithOptions(session.Options{
|
||||
SharedConfigState: session.SharedConfigEnable,
|
||||
}))
|
||||
|
||||
scanOptions := ScannerOptions{Deep: true}
|
||||
providerLibrary := terraform.NewProviderLibrary()
|
||||
remoteLibrary := common.NewRemoteLibrary()
|
||||
|
||||
// Initialize mocks
|
||||
fakeRepo := &repository.MockS3Repository{}
|
||||
c.mocks(fakeRepo)
|
||||
var repo repository.S3Repository = fakeRepo
|
||||
providerVersion := "3.19.0"
|
||||
realProvider, err := terraform2.InitTestAwsProvider(providerLibrary, providerVersion)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider := terraform2.NewFakeTerraformProvider(realProvider)
|
||||
provider.WithResponse(c.dirName)
|
||||
|
||||
// Replace mock by real resources if we are in update mode
|
||||
if shouldUpdate {
|
||||
err := realProvider.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider.ShouldUpdate()
|
||||
repo = repository.NewS3Repository(client.NewAWSClientFactory(session), cache.New(0))
|
||||
}
|
||||
|
||||
remoteLibrary.AddEnumerator(aws.NewS3BucketEnumerator(repo, factory, tf.TerraformProviderConfig{
|
||||
Name: "test",
|
||||
DefaultAlias: "eu-west-3",
|
||||
}))
|
||||
remoteLibrary.AddDetailsFetcher(resourceaws.AwsS3BucketResourceType, aws.NewS3BucketDetailsFetcher(provider, deserializer))
|
||||
|
||||
s := NewScanner(nil, remoteLibrary, alerter, scanOptions)
|
||||
got, err := s.Resources()
|
||||
assert.Equal(tt, err, c.wantErr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
test.TestAgainstGoldenFile(got, resourceaws.AwsS3BucketResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -1,11 +1,10 @@
|
|||
package pkg
|
||||
package remote
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudskiff/driftctl/pkg/alerter"
|
||||
"github.com/cloudskiff/driftctl/pkg/parallel"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/common"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
"github.com/pkg/errors"
|
||||
|
@ -21,11 +20,11 @@ type Scanner struct {
|
|||
enumeratorRunner *parallel.ParallelRunner
|
||||
detailsFetcherRunner *parallel.ParallelRunner
|
||||
remoteLibrary *common.RemoteLibrary
|
||||
alerter *alerter.Alerter
|
||||
alerter alerter.AlerterInterface
|
||||
options ScannerOptions
|
||||
}
|
||||
|
||||
func NewScanner(resourceSuppliers []resource.Supplier, remoteLibrary *common.RemoteLibrary, alerter *alerter.Alerter, options ScannerOptions) *Scanner {
|
||||
func NewScanner(resourceSuppliers []resource.Supplier, remoteLibrary *common.RemoteLibrary, alerter alerter.AlerterInterface, options ScannerOptions) *Scanner {
|
||||
return &Scanner{
|
||||
resourceSuppliers: resourceSuppliers,
|
||||
enumeratorRunner: parallel.NewParallelRunner(context.TODO(), 10),
|
||||
|
@ -64,7 +63,7 @@ func (s *Scanner) legacyScan() ([]resource.Resource, error) {
|
|||
s.enumeratorRunner.Run(func() (interface{}, error) {
|
||||
res, err := supplier.Resources()
|
||||
if err != nil {
|
||||
err := remote.HandleResourceEnumerationError(err, s.alerter)
|
||||
err := HandleResourceEnumerationError(err, s.alerter)
|
||||
if err == nil {
|
||||
return []resource.Resource{}, nil
|
||||
}
|
|
@ -5,4 +5,6 @@ type TerraformProvider interface {
|
|||
SchemaSupplier
|
||||
ResourceReader
|
||||
Cleanup()
|
||||
Name() string
|
||||
Version() string
|
||||
}
|
||||
|
|
|
@ -74,6 +74,7 @@ func doTestDiff(got []resource.Resource, dirName string, provider terraform.Terr
|
|||
return differ.Diff(got, expectedResources)
|
||||
}
|
||||
|
||||
// CtyTestDiff Deprecated
|
||||
func CtyTestDiff(got []resource.Resource, dirName string, provider terraform.TerraformProvider, deserializer *resource.Deserializer, shouldUpdate bool, t *testing.T) {
|
||||
changelog, err := doTestDiff(got, dirName, provider, deserializer, shouldUpdate)
|
||||
if err != nil {
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func WriteTestFile(p string, content []byte) error {
|
||||
_, filename, _, _ := runtime.Caller(1)
|
||||
return ioutil.WriteFile(path.Join(path.Dir(filename), p), content, 0644)
|
||||
}
|
||||
|
||||
func ReadTestFile(p string) ([]byte, error) {
|
||||
_, filename, _, _ := runtime.Caller(1)
|
||||
return ioutil.ReadFile(path.Join(path.Dir(filename), p))
|
||||
}
|
|
@ -167,3 +167,11 @@ func getFileNameSuffix(args terraform.ReadResourceArgs) string {
|
|||
}
|
||||
|
||||
func (p MockedGoldenTFProvider) Cleanup() {}
|
||||
|
||||
func (p *MockedGoldenTFProvider) Name() string {
|
||||
return p.realProvider.Name()
|
||||
}
|
||||
|
||||
func (p *MockedGoldenTFProvider) Version() string {
|
||||
return p.realProvider.Version()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,128 @@
|
|||
package terraform
|
||||
|
||||
import (
|
||||
gojson "encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
"github.com/cloudskiff/driftctl/test"
|
||||
"github.com/cloudskiff/driftctl/test/goldenfile"
|
||||
"github.com/cloudskiff/driftctl/test/mocks"
|
||||
"github.com/hashicorp/terraform/providers"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
)
|
||||
|
||||
type FakeTerraformProvider struct {
|
||||
realProvider terraform.TerraformProvider
|
||||
shouldUpdate bool
|
||||
response string
|
||||
}
|
||||
|
||||
func NewFakeTerraformProvider(realProvider terraform.TerraformProvider) *FakeTerraformProvider {
|
||||
return &FakeTerraformProvider{realProvider: realProvider}
|
||||
}
|
||||
|
||||
func (p *FakeTerraformProvider) ShouldUpdate() {
|
||||
p.shouldUpdate = true
|
||||
}
|
||||
|
||||
func (m *FakeTerraformProvider) Schema() map[string]providers.Schema {
|
||||
if m.shouldUpdate {
|
||||
schema := m.realProvider.Schema()
|
||||
m.writeSchema(schema)
|
||||
return schema
|
||||
}
|
||||
return m.readSchema()
|
||||
}
|
||||
|
||||
func (p *FakeTerraformProvider) WithResponse(response string) *FakeTerraformProvider {
|
||||
p.response = response
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *FakeTerraformProvider) ReadResource(args terraform.ReadResourceArgs) (*cty.Value, error) {
|
||||
if p.response == "" {
|
||||
return nil, errors.New("WithResponse should be called before ReadResource to specify a directory to fetch fake response")
|
||||
}
|
||||
if p.shouldUpdate {
|
||||
readResource, err := p.realProvider.ReadResource(args)
|
||||
p.writeResource(args, readResource, err)
|
||||
return readResource, err
|
||||
}
|
||||
|
||||
return p.readResource(args)
|
||||
}
|
||||
|
||||
func (p *FakeTerraformProvider) writeSchema(schema map[string]providers.Schema) {
|
||||
marshal, err := gojson.Marshal(schema)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
err = test.WriteTestFile(fmt.Sprintf("schemas/%s/%s.json", p.realProvider.Name(), p.realProvider.Version()), marshal)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *FakeTerraformProvider) readSchema() map[string]providers.Schema {
|
||||
content, err := test.ReadTestFile(fmt.Sprintf("schemas/%s/%s.json", p.realProvider.Name(), p.realProvider.Version()))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
var schema map[string]providers.Schema
|
||||
if err := gojson.Unmarshal(content, &schema); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return schema
|
||||
}
|
||||
|
||||
func (p *FakeTerraformProvider) writeResource(args terraform.ReadResourceArgs, readResource *cty.Value, err error) {
|
||||
var readRes = mocks.ReadResource{
|
||||
Value: readResource,
|
||||
Err: err,
|
||||
}
|
||||
|
||||
marshalled, err := gojson.Marshal(&readRes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fileName := p.getFileName(args)
|
||||
goldenfile.WriteFile(p.response, marshalled, fileName)
|
||||
}
|
||||
|
||||
func (p *FakeTerraformProvider) readResource(args terraform.ReadResourceArgs) (*cty.Value, error) {
|
||||
fileName := p.getFileName(args)
|
||||
content := goldenfile.ReadFile(p.response, fileName)
|
||||
var readRes mocks.ReadResource
|
||||
if err := gojson.Unmarshal(content, &readRes); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return readRes.Value, readRes.Err
|
||||
}
|
||||
|
||||
func (p *FakeTerraformProvider) getFileName(args terraform.ReadResourceArgs) string {
|
||||
suffix := ""
|
||||
keys := make([]string, 0, len(args.Attributes))
|
||||
for k := range args.Attributes {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, k := range keys {
|
||||
suffix = fmt.Sprintf("%s-%s", suffix, args.Attributes[k])
|
||||
}
|
||||
fileName := fmt.Sprintf("%s-%s%s.res.golden.json", args.Ty, args.ID, suffix)
|
||||
return fileName
|
||||
}
|
||||
|
||||
func (p *FakeTerraformProvider) Cleanup() {}
|
||||
|
||||
func (p *FakeTerraformProvider) Name() string {
|
||||
return p.realProvider.Name()
|
||||
}
|
||||
|
||||
func (p *FakeTerraformProvider) Version() string {
|
||||
return p.realProvider.Version()
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package terraform
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/cloudskiff/driftctl/pkg/output"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws"
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
)
|
||||
|
||||
func InitTestAwsProvider(providerLibrary *terraform.ProviderLibrary, version string) (*aws.AWSTerraformProvider, error) {
|
||||
progress := &output.MockProgress{}
|
||||
progress.On("Inc").Maybe().Return()
|
||||
provider, err := aws.NewAWSTerraformProvider(version, progress, os.TempDir())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
providerLibrary.AddProvider(terraform.AWS, provider)
|
||||
return provider, nil
|
||||
}
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,74 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/awsutil"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
"github.com/cloudskiff/driftctl/test/goldenfile"
|
||||
"github.com/r3labs/diff/v2"
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
"github.com/zclconf/go-cty/cty/gocty"
|
||||
"github.com/zclconf/go-cty/cty/json"
|
||||
)
|
||||
|
||||
func TestAgainstGoldenFile(
|
||||
got []resource.Resource,
|
||||
ty string,
|
||||
dirName string,
|
||||
provider terraform.TerraformProvider,
|
||||
deserializer *resource.Deserializer,
|
||||
shouldUpdate bool,
|
||||
tt *testing.T,
|
||||
) {
|
||||
resGoldenName := "results.golden.json"
|
||||
var expectedResources []resource.Resource
|
||||
ctyType := cty.List(provider.Schema()[ty].Block.ImpliedType())
|
||||
|
||||
// update golden file
|
||||
if shouldUpdate {
|
||||
ctVal, err := gocty.ToCtyValue(got, ctyType)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
unm, err := json.Marshal(ctVal, ctyType)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
goldenfile.WriteFile(dirName, unm, resGoldenName)
|
||||
}
|
||||
|
||||
// read golden file
|
||||
file := goldenfile.ReadFile(dirName, resGoldenName)
|
||||
decodedJson, err := json.Unmarshal(file, ctyType)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
decodedResources, err := deserializer.Deserialize(ty, decodedJson.AsValueSlice())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
expectedResources = append(expectedResources, decodedResources...)
|
||||
|
||||
// diff
|
||||
differ, err := diff.NewDiffer(diff.SliceOrdering(true))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
got = resource.Sort(got)
|
||||
expectedResources = resource.Sort(expectedResources)
|
||||
|
||||
changelog, err := differ.Diff(got, expectedResources)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if len(changelog) > 0 {
|
||||
for _, change := range changelog {
|
||||
tt.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To))
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue