Skip to content

Commit

Permalink
Merge pull request #3 from drlau/parse-test
Browse files Browse the repository at this point in the history
add a parse test
  • Loading branch information
drlau authored Aug 11, 2020
2 parents b7d3d31 + f0d945e commit f60bfb0
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/drlau/tfplanparse
go 1.14

require (
github.com/google/go-cmp v0.5.1
github.com/mattn/go-colorable v0.1.7
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
Expand All @@ -7,3 +9,5 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepx
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9 h1:yi1hN8dcqI9l8klZfy4B8mJvFmmAxJEePIQQFNSd7Cs=
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
82 changes: 82 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package tfplanparse

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestParse(t *testing.T) {
cases := map[string]struct {
file string
expected []*ResourceChange
}{
// "basic plan": {
// file: "test/basic.stdout",
// expected: []*ResourceChange{
// // TODO
// },
// },
// "plan error": {
// file: "test/error.stdout",
// expected: nil,
// },
// "no changes": {
// file: "test/nochanges.stdout",
// expected: nil,
// },
// "array": {
// file: "test/array.stdout",
// expected: []*ResourceChange{
// // TODO
// },
// },
"nested map": {
file: "test/nestedmap.stdout",
expected: []*ResourceChange{
&ResourceChange{
Address: "module.mymodule.kubernetes_namespace.mynamespace",
ModuleAddress: "module.mymodule",
Type: "kubernetes_namespace",
Name: "mynamespace",
UpdateType: UpdateInPlaceResource,
AttributeChanges: nil,
MapAttributeChanges: []*MapAttributeChange{
&MapAttributeChange{
Name: "metadata",
AttributeChanges: nil,
MapAttributeChanges: []*MapAttributeChange{
&MapAttributeChange{
Name: "labels",
AttributeChanges: []*AttributeChange{
&AttributeChange{
Name: "newLabel",
OldValue: nil,
NewValue: "newLabel",
UpdateType: NewResource,
},
},
MapAttributeChanges: nil,
UpdateType: UpdateInPlaceResource,
},
},
UpdateType: UpdateInPlaceResource,
},
},
},
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
// TODO handle expected error
got, err := ParseFromFile(tc.file)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(got, tc.expected); diff != "" {
t.Errorf("(-got, +expected)\n%s", diff)
}
})
}
}
30 changes: 30 additions & 0 deletions test/nestedmap.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
------------------------------------------------------------------------

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# module.mymodule.kubernetes_namespace.mynamespace will be updated in-place
~ resource "kubernetes_namespace" "mynamespace" {
id = "namespace-id"

~ metadata {
annotations = {}
generation = 0
~ labels = {
"label" = "value"
"other" = "label"
+ "newLabel" = "newLabel"
}
name = "my-namespace"
resource_version = "123"
self_link = "/api/v1/namespaces/my-namespace"
uid = "some-uid-123"
}

timeouts {}
}

Plan: 0 to add, 1 to change, 0 to destroy.

0 comments on commit f60bfb0

Please sign in to comment.