Skip to content

Commit

Permalink
Deal with our nested blocks as cty.Value for now
Browse files Browse the repository at this point in the history
zclconf/go-cty#17 means we can't decode a nested block type that has
dynamically-typed attributes into a Go map directly, so for now we'll do
the decoding more manually until that upstream issue is fixed.
  • Loading branch information
apparentlymart committed Mar 17, 2019
1 parent a77b60d commit bfa0f99
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module github.com/apparentlymart/terraform-provider-testing

require (
github.com/apparentlymart/terraform-sdk v0.0.0-20190316235715-41c923c09cb1
github.com/zclconf/go-cty v0.0.0-20190201220620-4ca19710f056
github.com/zclconf/go-cty v0.0.0-20190317012026-9463876af40c
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ github.com/vmihailenco/msgpack v3.3.3+incompatible h1:wapg9xDUZDzGCNFlwc5SqI1rvc
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/zclconf/go-cty v0.0.0-20190201220620-4ca19710f056 h1:C6LhH3JHz2k6tnw5sYXBc8rD8SD/qFp6EhiZAcVyalk=
github.com/zclconf/go-cty v0.0.0-20190201220620-4ca19710f056/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
github.com/zclconf/go-cty v0.0.0-20190317012026-9463876af40c h1:bb0BX9dFsZuPWLoIEg3p5raPyWw7QlVyC5dqniUmfqM=
github.com/zclconf/go-cty v0.0.0-20190317012026-9463876af40c/go.mod h1:xnAOWiHeOqg2nWS62VtQ7pbOu17FtxJNW8RLEih+O3s=
golang.org/x/arch v0.0.0-20190226203302-36aee92af9e8/go.mod h1:cYlCBUl1MsqxdiKgmc4uh7TxZfWSFLOGSRR090WDxt8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
47 changes: 38 additions & 9 deletions testing/drt_assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

tfsdk "github.com/apparentlymart/terraform-sdk"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
)

type assertionsDRT struct {
Subject *string `cty:"subject"`

Check map[string]*assertionsDRTCheck `cty:"check"`
Equal map[string]*assertionsDRTEqual `cty:"equal"`
Checks cty.Value `cty:"check"`
Equals cty.Value `cty:"equal"`
}

type assertionsDRTEqual struct {
Expand Down Expand Up @@ -67,7 +68,20 @@ func assertionsDataResourceType() tfsdk.DataResourceType {
subject = *obj.Subject
}

for k, chk := range obj.Check {
for it := obj.Checks.ElementIterator(); it.Next(); {
k, v := it.Element()
var chk assertionsDRTCheck
err := gocty.FromCtyValue(v, &chk)
if err != nil {
// Should never happen; indicates that our struct is wrong.
diags = diags.Append(tfsdk.Diagnostic{
Severity: tfsdk.Error,
Summary: "Bug in 'testing' provider",
Detail: fmt.Sprintf("The provider encountered a problem while decoding the check %q block: %s.\n\nThis is a bug in the provider; please report it in the provider's issue tracker.", k.AsString(), err),
})
continue
}

if chk.Pass {
continue
}
Expand All @@ -83,18 +97,33 @@ func assertionsDataResourceType() tfsdk.DataResourceType {

msg := "Assertion failed"
if statement != "" {
msg = fmt.Sprintf("%s: %s", msg, statement)
msg = fmt.Sprintf("%s: %s.", msg, statement)
} else {
msg = msg + "."
}

diags = diags.Append(tfsdk.Diagnostic{
Severity: tfsdk.Error,
Summary: "Test failure",
Detail: msg,
Path: cty.Path(nil).GetAttr("check").Index(cty.StringVal(k)).GetAttr("expect"),
Path: cty.Path(nil).GetAttr("check").Index(k).GetAttr("expect"),
})
}

for k, eq := range obj.Equal {
for it := obj.Equals.ElementIterator(); it.Next(); {
k, v := it.Element()
var eq assertionsDRTEqual
err := gocty.FromCtyValue(v, &eq)
if err != nil {
// Should never happen; indicates that our struct is wrong.
diags = diags.Append(tfsdk.Diagnostic{
Severity: tfsdk.Error,
Summary: "Bug in 'testing' provider",
Detail: fmt.Sprintf("The provider encountered a problem while decoding the equal %q block: %s.\n\nThis is a bug in the provider; please report it in the provider's issue tracker.", k.AsString(), err),
})
continue
}

if eq.Got.RawEquals(eq.Want) {
// Assertion passes!
continue
Expand All @@ -111,16 +140,16 @@ func assertionsDataResourceType() tfsdk.DataResourceType {

var msg string
if statement != "" {
msg = fmt.Sprintf("Assertion failed: %s\n Want: %s\n Got: %s", statement, eq.Want, eq.Got)
msg = fmt.Sprintf("Assertion failed: %s.\n Want: %s\n Got: %s", statement, eq.Want, eq.Got)
} else {
msg = fmt.Sprintf("Assertion failed\n Want: %s\n Got: %s", eq.Want, eq.Got)
msg = fmt.Sprintf("Assertion failed.\n Want: %s\n Got: %s", eq.Want, eq.Got)
}

diags = diags.Append(tfsdk.Diagnostic{
Severity: tfsdk.Error,
Summary: "Test failure",
Detail: msg,
Path: cty.Path(nil).GetAttr("equal").Index(cty.StringVal(k)).GetAttr("got"),
Path: cty.Path(nil).GetAttr("equal").Index(k).GetAttr("got"),
})
}

Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/zclconf/go-cty/cty/gocty/in.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ github.com/oklog/run
# github.com/vmihailenco/msgpack v3.3.3+incompatible
github.com/vmihailenco/msgpack
github.com/vmihailenco/msgpack/codes
# github.com/zclconf/go-cty v0.0.0-20190201220620-4ca19710f056
# github.com/zclconf/go-cty v0.0.0-20190317012026-9463876af40c
github.com/zclconf/go-cty/cty
github.com/zclconf/go-cty/cty/convert
github.com/zclconf/go-cty/cty/gocty
github.com/zclconf/go-cty/cty/convert
github.com/zclconf/go-cty/cty/json
github.com/zclconf/go-cty/cty/msgpack
github.com/zclconf/go-cty/cty/set
Expand Down

0 comments on commit bfa0f99

Please sign in to comment.