From f0d945e9855d424e549e4bd92d0140dbaed49dbf Mon Sep 17 00:00:00 2001 From: d-lau Date: Tue, 11 Aug 2020 21:55:59 +0900 Subject: [PATCH] add a parse test --- go.mod | 1 + go.sum | 4 +++ parse_test.go | 82 +++++++++++++++++++++++++++++++++++++++++++ test/nestedmap.stdout | 30 ++++++++++++++++ 4 files changed, 117 insertions(+) create mode 100644 parse_test.go create mode 100644 test/nestedmap.stdout diff --git a/go.mod b/go.mod index 383aa88..a2d2dd8 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 92b965f..d615b15 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/parse_test.go b/parse_test.go new file mode 100644 index 0000000..80a0743 --- /dev/null +++ b/parse_test.go @@ -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) + } + }) + } +} \ No newline at end of file diff --git a/test/nestedmap.stdout b/test/nestedmap.stdout new file mode 100644 index 0000000..d19e5ed --- /dev/null +++ b/test/nestedmap.stdout @@ -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. \ No newline at end of file