2020-12-09 15:31:34 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2020-12-15 10:07:03 +00:00
|
|
|
"fmt"
|
2020-12-09 15:31:34 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/test/goldenfile"
|
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty/json"
|
|
|
|
|
2021-03-25 11:13:52 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awsutil"
|
|
|
|
"github.com/r3labs/diff/v2"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"github.com/zclconf/go-cty/cty/gocty"
|
|
|
|
)
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
func doTestDiff(got []*resource.Resource, dirName string, provider terraform.TerraformProvider, deserializer *resource.Deserializer, shouldUpdate bool) (diff.Changelog, error) {
|
2021-05-21 14:09:45 +00:00
|
|
|
resources := make(map[string][]resource.Attributes)
|
2020-12-15 10:07:03 +00:00
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
for _, r := range got {
|
|
|
|
res, exist := resources[r.TerraformType()]
|
2020-12-15 10:07:03 +00:00
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
if !exist {
|
|
|
|
resources[r.TerraformType()] = []resource.Attributes{*r.Attributes()}
|
|
|
|
continue
|
2020-12-15 10:07:03 +00:00
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
|
|
|
|
resources[r.TerraformType()] = append(res, *r.Attributes())
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
expectedResources := []*resource.Resource{}
|
2021-05-21 14:09:45 +00:00
|
|
|
for ty, resList := range resources {
|
2021-07-21 13:13:12 +00:00
|
|
|
resGoldenName := goldenfile.ResultsFilename
|
2021-05-21 14:09:45 +00:00
|
|
|
if len(resources) > 1 {
|
2020-12-15 10:07:03 +00:00
|
|
|
resGoldenName = fmt.Sprintf("results.%s.golden.json", ty)
|
|
|
|
}
|
|
|
|
ctyType := cty.List(provider.Schema()[ty].Block.ImpliedType())
|
|
|
|
if shouldUpdate {
|
|
|
|
ctVal, err := gocty.ToCtyValue(resList, ctyType)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
unm, err := json.Marshal(ctVal, ctyType)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
goldenfile.WriteFile(dirName, unm, resGoldenName)
|
|
|
|
}
|
|
|
|
file := goldenfile.ReadFile(dirName, resGoldenName)
|
|
|
|
decodedJson, err := json.Unmarshal(file, ctyType)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
decodedResources, err := deserializer.Deserialize(ty, decodedJson.AsValueSlice())
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-12-15 10:07:03 +00:00
|
|
|
expectedResources = append(expectedResources, decodedResources...)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 11:13:52 +00:00
|
|
|
differ, err := diff.NewDiffer(diff.SliceOrdering(true))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got = resource.Sort(got)
|
|
|
|
expectedResources = resource.Sort(expectedResources)
|
|
|
|
|
|
|
|
return differ.Diff(got, expectedResources)
|
2020-12-15 10:07:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 13:12:25 +00:00
|
|
|
// CtyTestDiff Deprecated
|
2021-08-09 14:03:04 +00:00
|
|
|
func CtyTestDiff(got []*resource.Resource, dirName string, provider terraform.TerraformProvider, deserializer *resource.Deserializer, shouldUpdate bool, t *testing.T) {
|
2021-05-21 14:09:45 +00:00
|
|
|
changelog, err := doTestDiff(got, dirName, provider, deserializer, shouldUpdate)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if len(changelog) > 0 {
|
|
|
|
for _, change := range changelog {
|
|
|
|
t.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|