Skip to content

Commit

Permalink
Sort results (#75)
Browse files Browse the repository at this point in the history
* Update terraform_state_test.go

* Update build.yml

* Sort results

* Add tests  (#2)

* Update terraform_state_test.go

* Update terraform_state_test.go

* Add tests to GetAllOutputChanges and GetAllResourceChanges

* Update terraform_state_test.go
  • Loading branch information
lcssanches authored Aug 28, 2024
1 parent 9e403ca commit be5c60e
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ jobs:
uses: actions/setup-go@v4
with:
go-version: "1.21"

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest

- name: Linter
uses: docker://morphy/revive-action:v2

- name: Test
run: go test -v ./...
Expand Down
17 changes: 17 additions & 0 deletions terraformstate/terraform_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terraformstate
import (
"encoding/json"
"fmt"
"sort"

tfjson "github.com/hashicorp/terraform-json"
)
Expand Down Expand Up @@ -107,6 +108,18 @@ func GetAllResourceChanges(plan tfjson.Plan) map[string]ResourceChanges {
recreatedResources := recreatedResources(plan.ResourceChanges)
importedResources := importedResources(plan.ResourceChanges)

sortResources := func(resources ResourceChanges) {
sort.Slice(resources, func(i, j int) bool {
return resources[i].Address < resources[j].Address
})
}

sortResources(addedResources)
sortResources(deletedResources)
sortResources(updatedResources)
sortResources(recreatedResources)
sortResources(importedResources)

return map[string]ResourceChanges{
"import": importedResources,
"add": addedResources,
Expand All @@ -123,6 +136,10 @@ func GetAllOutputChanges(plan tfjson.Plan) map[string][]string {
deletedResources := filterOutputs(plan.OutputChanges, "delete")
updatedResources := filterOutputs(plan.OutputChanges, "update")

sort.Strings(addedResources)
sort.Strings(deletedResources)
sort.Strings(updatedResources)

return map[string][]string{
"add": addedResources,
"delete": deletedResources,
Expand Down
68 changes: 68 additions & 0 deletions terraformstate/terraform_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestResourceChangeColor(t *testing.T) {

assert.Equal(t, color, expectedColor)
}

CreateDelete := &ResourceChange{Change: &Change{Actions: []Action{ActionCreate, ActionDelete}}}
color, _ := GetColorPrefixAndSuffixText(CreateDelete)
assert.Equal(t, color, ColorMagenta)
Expand All @@ -31,6 +32,73 @@ func TestResourceChangeColor(t *testing.T) {
assert.Equal(t, color, ColorMagenta)
}

func TestGetAllResourceChanges(t *testing.T) {
resourceChanges := ResourceChanges{
&ResourceChange{Address: "create2", Change: &Change{Actions: Actions{ActionCreate}}},
&ResourceChange{Address: "create1", Change: &Change{Actions: Actions{ActionCreate}}},
&ResourceChange{Address: "delete2", Change: &Change{Actions: Actions{ActionDelete}}},
&ResourceChange{Address: "delete1", Change: &Change{Actions: Actions{ActionDelete}}},
&ResourceChange{Address: "update2", Change: &Change{Actions: Actions{ActionUpdate}}},
&ResourceChange{Address: "update1", Change: &Change{Actions: Actions{ActionUpdate}}},
&ResourceChange{Address: "import2", Change: &Change{Importing: &Importing{ID: "id1"}}},
&ResourceChange{Address: "import1", Change: &Change{Importing: &Importing{ID: "id2"}}},
&ResourceChange{Address: "recreate2", Change: &Change{Actions: Actions{ActionDelete, ActionCreate}}},
&ResourceChange{Address: "recreate1", Change: &Change{Actions: Actions{ActionDelete, ActionCreate}}},
}
plan := tfjson.Plan{ResourceChanges: resourceChanges}

result := GetAllResourceChanges(plan)

expectedResourceChanges := map[string]ResourceChanges{
"add": {
&ResourceChange{Address: "create1", Change: &Change{Actions: Actions{ActionCreate}}},
&ResourceChange{Address: "create2", Change: &Change{Actions: Actions{ActionCreate}}},
},
"delete": {
&ResourceChange{Address: "delete1", Change: &Change{Actions: Actions{ActionDelete}}},
&ResourceChange{Address: "delete2", Change: &Change{Actions: Actions{ActionDelete}}},
},
"update": {
&ResourceChange{Address: "update1", Change: &Change{Actions: Actions{ActionUpdate}}},
&ResourceChange{Address: "update2", Change: &Change{Actions: Actions{ActionUpdate}}},
},
"recreate": {
&ResourceChange{Address: "recreate1", Change: &Change{Actions: Actions{ActionDelete, ActionCreate}}},
&ResourceChange{Address: "recreate2", Change: &Change{Actions: Actions{ActionDelete, ActionCreate}}},
},
"import": {
&ResourceChange{Address: "import1", Change: &Change{Importing: &Importing{ID: "id2"}}},
&ResourceChange{Address: "import2", Change: &Change{Importing: &Importing{ID: "id1"}}},
},
}

assert.Equal(t, expectedResourceChanges, result)
}

func TestGetAllOutputChanges(t *testing.T) {

outputChanges := map[string]*Change{
"create2": {Actions: Actions{ActionCreate}},
"create1": {Actions: Actions{ActionCreate}},
"delete2": {Actions: Actions{ActionDelete}},
"delete1": {Actions: Actions{ActionDelete}},
"update2": {Actions: Actions{ActionUpdate}},
"update1": {Actions: Actions{ActionUpdate}},
}

plan := tfjson.Plan{OutputChanges: outputChanges}

result := GetAllOutputChanges(plan)

expectedResourceChanges := map[string][]string{
"add": {"create1", "create2"},
"delete": {"delete1", "delete2"},
"update": {"update1", "update2"},
}

assert.Equal(t, expectedResourceChanges, result)
}

func TestResourceChangeSuffix(t *testing.T) {
ExpectedSuffix := map[Action]string{
ActionCreate: "(+)",
Expand Down

0 comments on commit be5c60e

Please sign in to comment.