2021-04-26 22:26:47 +02:00
|
|
|
package configschema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-06-24 23:53:43 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/lang/marks"
|
2021-04-26 22:26:47 +02:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBlockValueMarks(t *testing.T) {
|
|
|
|
schema := &Block{
|
|
|
|
Attributes: map[string]*Attribute{
|
|
|
|
"unsensitive": {
|
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"sensitive": {
|
|
|
|
Type: cty.String,
|
|
|
|
Sensitive: true,
|
|
|
|
},
|
2021-11-24 23:30:42 +01:00
|
|
|
"nested": {
|
|
|
|
NestedType: &Object{
|
|
|
|
Attributes: map[string]*Attribute{
|
|
|
|
"boop": {
|
|
|
|
Type: cty.String,
|
|
|
|
},
|
|
|
|
"honk": {
|
|
|
|
Type: cty.String,
|
|
|
|
Sensitive: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Nesting: NestingList,
|
|
|
|
},
|
|
|
|
},
|
2021-04-26 22:26:47 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
BlockTypes: map[string]*NestedBlock{
|
|
|
|
"list": {
|
|
|
|
Nesting: NestingList,
|
|
|
|
Block: Block{
|
|
|
|
Attributes: map[string]*Attribute{
|
|
|
|
"unsensitive": {
|
|
|
|
Type: cty.String,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"sensitive": {
|
|
|
|
Type: cty.String,
|
|
|
|
Sensitive: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-24 23:30:42 +01:00
|
|
|
testCases := map[string]struct {
|
2021-04-26 22:26:47 +02:00
|
|
|
given cty.Value
|
|
|
|
expect cty.Value
|
|
|
|
}{
|
2021-11-24 23:30:42 +01:00
|
|
|
"unknown object": {
|
2021-04-26 22:26:47 +02:00
|
|
|
cty.UnknownVal(schema.ImpliedType()),
|
|
|
|
cty.UnknownVal(schema.ImpliedType()),
|
|
|
|
},
|
2021-11-24 23:30:42 +01:00
|
|
|
"null object": {
|
2021-05-06 17:08:20 +02:00
|
|
|
cty.NullVal(schema.ImpliedType()),
|
|
|
|
cty.NullVal(schema.ImpliedType()),
|
|
|
|
},
|
2021-11-24 23:30:42 +01:00
|
|
|
"object with unknown attributes and blocks": {
|
2021-04-26 22:26:47 +02:00
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"sensitive": cty.UnknownVal(cty.String),
|
|
|
|
"unsensitive": cty.UnknownVal(cty.String),
|
2021-11-24 23:30:42 +01:00
|
|
|
"nested": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
|
|
|
|
"boop": cty.String,
|
|
|
|
"honk": cty.String,
|
|
|
|
}))),
|
|
|
|
"list": cty.UnknownVal(schema.BlockTypes["list"].ImpliedType()),
|
2021-04-26 22:26:47 +02:00
|
|
|
}),
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
2021-06-24 23:53:43 +02:00
|
|
|
"sensitive": cty.UnknownVal(cty.String).Mark(marks.Sensitive),
|
2021-04-26 22:26:47 +02:00
|
|
|
"unsensitive": cty.UnknownVal(cty.String),
|
2021-11-24 23:30:42 +01:00
|
|
|
"nested": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
|
|
|
|
"boop": cty.String,
|
|
|
|
"honk": cty.String,
|
|
|
|
}))),
|
|
|
|
"list": cty.UnknownVal(schema.BlockTypes["list"].ImpliedType()),
|
2021-04-26 22:26:47 +02:00
|
|
|
}),
|
|
|
|
},
|
2021-11-24 23:30:42 +01:00
|
|
|
"object with block value": {
|
2021-04-26 22:26:47 +02:00
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"sensitive": cty.NullVal(cty.String),
|
|
|
|
"unsensitive": cty.UnknownVal(cty.String),
|
2021-11-24 23:30:42 +01:00
|
|
|
"nested": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
|
|
|
|
"boop": cty.String,
|
|
|
|
"honk": cty.String,
|
|
|
|
}))),
|
2021-04-26 22:26:47 +02:00
|
|
|
"list": cty.ListVal([]cty.Value{
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"sensitive": cty.UnknownVal(cty.String),
|
|
|
|
"unsensitive": cty.UnknownVal(cty.String),
|
|
|
|
}),
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"sensitive": cty.NullVal(cty.String),
|
|
|
|
"unsensitive": cty.NullVal(cty.String),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
2021-06-24 23:53:43 +02:00
|
|
|
"sensitive": cty.NullVal(cty.String).Mark(marks.Sensitive),
|
2021-04-26 22:26:47 +02:00
|
|
|
"unsensitive": cty.UnknownVal(cty.String),
|
2021-11-24 23:30:42 +01:00
|
|
|
"nested": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
|
|
|
|
"boop": cty.String,
|
|
|
|
"honk": cty.String,
|
|
|
|
}))),
|
2021-04-26 22:26:47 +02:00
|
|
|
"list": cty.ListVal([]cty.Value{
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
2021-06-24 23:53:43 +02:00
|
|
|
"sensitive": cty.UnknownVal(cty.String).Mark(marks.Sensitive),
|
2021-04-26 22:26:47 +02:00
|
|
|
"unsensitive": cty.UnknownVal(cty.String),
|
|
|
|
}),
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
2021-06-24 23:53:43 +02:00
|
|
|
"sensitive": cty.NullVal(cty.String).Mark(marks.Sensitive),
|
2021-04-26 22:26:47 +02:00
|
|
|
"unsensitive": cty.NullVal(cty.String),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
},
|
2021-11-24 23:30:42 +01:00
|
|
|
"object with known values and nested attribute": {
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"sensitive": cty.StringVal("foo"),
|
|
|
|
"unsensitive": cty.StringVal("bar"),
|
|
|
|
"nested": cty.ListVal([]cty.Value{
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"boop": cty.StringVal("foo"),
|
|
|
|
"honk": cty.StringVal("bar"),
|
|
|
|
}),
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"boop": cty.NullVal(cty.String),
|
|
|
|
"honk": cty.NullVal(cty.String),
|
|
|
|
}),
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"boop": cty.UnknownVal(cty.String),
|
|
|
|
"honk": cty.UnknownVal(cty.String),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
"list": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
|
|
|
|
"sensitive": cty.String,
|
|
|
|
"unsensitive": cty.String,
|
|
|
|
}))),
|
|
|
|
}),
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"sensitive": cty.StringVal("foo").Mark(marks.Sensitive),
|
|
|
|
"unsensitive": cty.StringVal("bar"),
|
|
|
|
"nested": cty.ListVal([]cty.Value{
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"boop": cty.StringVal("foo"),
|
|
|
|
"honk": cty.StringVal("bar").Mark(marks.Sensitive),
|
|
|
|
}),
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"boop": cty.NullVal(cty.String),
|
|
|
|
"honk": cty.NullVal(cty.String).Mark(marks.Sensitive),
|
|
|
|
}),
|
|
|
|
cty.ObjectVal(map[string]cty.Value{
|
|
|
|
"boop": cty.UnknownVal(cty.String),
|
|
|
|
"honk": cty.UnknownVal(cty.String).Mark(marks.Sensitive),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
"list": cty.NullVal(cty.List(cty.Object(map[string]cty.Type{
|
|
|
|
"sensitive": cty.String,
|
|
|
|
"unsensitive": cty.String,
|
|
|
|
}))),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2021-04-26 22:26:47 +02:00
|
|
|
got := tc.given.MarkWithPaths(schema.ValueMarks(tc.given, nil))
|
|
|
|
if !got.RawEquals(tc.expect) {
|
|
|
|
t.Fatalf("\nexpected: %#v\ngot: %#v\n", tc.expect, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|