Skip to content

Commit

Permalink
Merge pull request #15 from thiagonache/feature/string_normalize
Browse files Browse the repository at this point in the history
feat: comparing nested structures
  • Loading branch information
thiagonache authored Mar 18, 2021
2 parents 20d7eef + 9ea27b7 commit 87aae44
Show file tree
Hide file tree
Showing 3 changed files with 4,853 additions and 14 deletions.
40 changes: 26 additions & 14 deletions terraformtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"os"
"strings"

"github.com/tidwall/gjson"
)
Expand Down Expand Up @@ -38,7 +39,7 @@ type Resource struct {

// ResourceSet stores the resources (items) and diff of the plan file.
type ResourceSet struct {
Resources map[string]map[string]map[string]gjson.Result
Resources map[string]map[string]map[string]string
CompDiff compDiff
}

Expand All @@ -48,7 +49,7 @@ func ReadPlan(planPath string) (*Plan, error) {
tf := &Plan{
loopControl: loopControl{maxDepth: 10},
Resources: ResourceSet{
Resources: map[string]map[string]map[string]gjson.Result{},
Resources: map[string]map[string]map[string]string{},
CompDiff: compDiff{},
},
}
Expand Down Expand Up @@ -109,9 +110,9 @@ func (tfPlan *Plan) transform(key, value gjson.Result) bool {
tfPlan.loopControl.curItemSubKey = "Values"
_, ok := tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex]
if !ok {
tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex] = map[string]map[string]gjson.Result{}
tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex] = map[string]map[string]string{}
}
tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex][tfPlan.loopControl.curItemSubKey] = map[string]gjson.Result{}
tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex][tfPlan.loopControl.curItemSubKey] = map[string]string{}
value.ForEach(tfPlan.transform)
case "address":
// We are only interested in addresses of resources
Expand All @@ -122,17 +123,24 @@ func (tfPlan *Plan) transform(key, value gjson.Result) bool {
tfPlan.loopControl.curItemIndex = value.String()
_, ok := tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex]
if !ok {
tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex] = map[string]map[string]gjson.Result{}
tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex] = map[string]map[string]string{}
}
tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex][tfPlan.loopControl.curItemSubKey] = map[string]gjson.Result{}
tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex][tfPlan.loopControl.curItemSubKey] = map[string]string{}

default:
value := normalizeItem(value.String())
tfPlan.Resources.Resources[tfPlan.loopControl.curItemIndex][tfPlan.loopControl.curItemSubKey][key.String()] = value
}

return true
}

func normalizeItem(item string) string {
item = strings.ReplaceAll(item, "\n", "")
item = strings.ReplaceAll(item, " ", "")
return item
}

func (rs *ResourceSet) newCompDiffItem(key, want, got string) {
item := compDiffItem{
got: got,
Expand All @@ -155,8 +163,9 @@ func (rs *ResourceSet) Contains(r Resource) bool {
rs.newCompDiffItem(k, "exist", "nil")
return false
}
if valueFound.String() != v {
rs.newCompDiffItem(k, v, valueFound.String())
v = normalizeItem(v)
if valueFound != v {
rs.newCompDiffItem(k, v, valueFound)
return false
}
}
Expand All @@ -172,8 +181,9 @@ func (rs *ResourceSet) Contains(r Resource) bool {
rs.newCompDiffItem(k, "exist", "nil")
return false
}
if valueFound.String() != v {
rs.newCompDiffItem(k, v, valueFound.String())
v = normalizeItem(v)
if valueFound != v {
rs.newCompDiffItem(k, v, valueFound)
return false
}
}
Expand All @@ -197,8 +207,9 @@ func Equal(resources []Resource, rs *ResourceSet) bool {
rs.newCompDiffItem(r.Address, "exist in plan", "nil")
return false
}
if valueFound.String() != v {
rs.newCompDiffItem(k, v, valueFound.String())
v = normalizeItem(v)
if valueFound != v {
rs.newCompDiffItem(k, v, valueFound)
return false
}
}
Expand All @@ -209,8 +220,9 @@ func Equal(resources []Resource, rs *ResourceSet) bool {
rs.newCompDiffItem(r.Address, "exist in plan", "nil")
return false
}
if valueFound.String() != v {
rs.newCompDiffItem(k, v, valueFound.String())
v = normalizeItem(v)
if valueFound != v {
rs.newCompDiffItem(k, v, valueFound)
return false
}
}
Expand Down
39 changes: 39 additions & 0 deletions terraformtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,42 @@ func TestDiffExpected(t *testing.T) {
})
}
}

func TestEKSCluster(t *testing.T) {
testCases := []struct {
desc string
planPath string
want Resource
}{
{
desc: "EKS Cluster",
planPath: "./testdata/ekspoc.plan.json",
want: Resource{
Address: "module.eks.aws_eks_cluster.this[0]",
Metadata: map[string]string{
"type": "aws_eks_cluster",
"name": "this",
"index": "0",
},
Values: map[string]string{
"name": "argocd-playground",
"encryption_config": `[
{ "provider": [{}], "resources": ["secrets"] }
]`,
},
},
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
tfPlan, err := ReadPlan(tC.planPath)
if err != nil {
t.Fatalf("%v", err)
}
gotRS := tfPlan.Resources
if !gotRS.Contains(tC.want) {
t.Error(gotRS.Diff())
}
})
}
}
Loading

0 comments on commit 87aae44

Please sign in to comment.