Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 218 additions & 34 deletions pf/tests/schemashim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (

// Test how various PF-based schemata translate to the shim.Schema layer.
func TestSchemaShimRepresentations(t *testing.T) {

type testCase struct {
name string
provider provider.Provider
Expand All @@ -41,16 +40,38 @@ func TestSchemaShimRepresentations(t *testing.T) {

testCases := []testCase{
{
"single-nested-block",
"simple-attribute",
&pb.Provider{
AllResources: []pb.Resource{{
ResourceSchema: schema.Schema{
Blocks: map[string]schema.Block{
"single_nested_block": schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"a1": schema.Float64Attribute{
Optional: true,
},
Attributes: map[string]schema.Attribute{
"simple_attribute": schema.StringAttribute{
Optional: true,
},
},
},
}},
},
autogold.Expect(`{
"resources": {
"_": {
"simple_attribute": {
"optional": true,
"type": 4
}
}
}
}`),
},
{
"object-attribute",
&pb.Provider{
AllResources: []pb.Resource{{
ResourceSchema: schema.Schema{
Attributes: map[string]schema.Attribute{
"object_attribute": schema.ObjectAttribute{
AttributeTypes: map[string]attr.Type{
"a1": types.StringType,
},
},
},
Expand All @@ -60,51 +81,132 @@ func TestSchemaShimRepresentations(t *testing.T) {
autogold.Expect(`{
"resources": {
"_": {
"single_nested_block": {
"object_attribute": {
"element": {
"resource": {
"a1": {
"optional": true,
"type": 3
"type": 4
}
}
},
"optional": true,
"type": 6
}
}
}
}`),
},
{
"list-nested-block",
"list-attribute",
&pb.Provider{
AllResources: []pb.Resource{{
ResourceSchema: schema.Schema{
Blocks: map[string]schema.Block{
"list_nested_block": schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"a1": schema.Float64Attribute{
Optional: true,
AllResources: []pb.Resource{
{
ResourceSchema: schema.Schema{
Attributes: map[string]schema.Attribute{
"list_attribute": schema.ListAttribute{
Optional: true,
ElementType: types.StringType,
},
},
},
},
},
},
autogold.Expect(`{
"resources": {
"_": {
"list_attribute": {
"element": {
"schema": {
"type": 4
}
},
"optional": true,
"type": 5
}
}
}
}`),
},
{
"list-attribute-object-element",
&pb.Provider{
AllResources: []pb.Resource{
{
ResourceSchema: schema.Schema{
Attributes: map[string]schema.Attribute{
"list_attribute": schema.ListAttribute{
Optional: true,
ElementType: types.ObjectType{
AttrTypes: map[string]attr.Type{
"a1": types.StringType,
},
},
},
},
},
},
}},
},
},
autogold.Expect(`{
"resources": {
"_": {
"list_nested_block": {
"list_attribute": {
"element": {
"resource": {
"a1": {
"optional": true,
"type": 3
}
"schema": {
"element": {
"resource": {
"a1": {
"type": 4
}
}
},
"type": 6
}
},
"optional": true,
"type": 5
}
}
}
}`),
},
{
"list-nested-attribute",
&pb.Provider{
AllResources: []pb.Resource{
{
ResourceSchema: schema.Schema{
Attributes: map[string]schema.Attribute{
"list_nested_attribute": schema.ListNestedAttribute{
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"a1": schema.StringAttribute{
Optional: true,
},
},
},
},
},
},
},
},
},
autogold.Expect(`{
"resources": {
"_": {
"list_nested_attribute": {
"element": {
"schema": {
"element": {
"resource": {
"a1": {
"optional": true,
"type": 4
}
}
},
"type": 6
}
},
"optional": true,
Expand Down Expand Up @@ -157,14 +259,55 @@ func TestSchemaShimRepresentations(t *testing.T) {
}`),
},
{
"object-attribute",
"single-nested-attribute",
&pb.Provider{
AllResources: []pb.Resource{
{
ResourceSchema: schema.Schema{
Attributes: map[string]schema.Attribute{
"single_nested_attribute": schema.SingleNestedAttribute{
Optional: true,
Attributes: map[string]schema.Attribute{
"a1": schema.StringAttribute{
Optional: true,
},
},
},
},
},
},
},
},
autogold.Expect(`{
"resources": {
"_": {
"single_nested_attribute": {
"element": {
"resource": {
"a1": {
"optional": true,
"type": 4
}
}
},
"optional": true,
"type": 6
}
}
}
}`),
},
{
"single-nested-block",
&pb.Provider{
AllResources: []pb.Resource{{
ResourceSchema: schema.Schema{
Attributes: map[string]schema.Attribute{
"object_attribute": schema.ObjectAttribute{
AttributeTypes: map[string]attr.Type{
"a1": types.StringType,
Blocks: map[string]schema.Block{
"single_nested_block": schema.SingleNestedBlock{
Attributes: map[string]schema.Attribute{
"a1": schema.StringAttribute{
Optional: true,
},
},
},
},
Expand All @@ -174,18 +317,59 @@ func TestSchemaShimRepresentations(t *testing.T) {
autogold.Expect(`{
"resources": {
"_": {
"object_attribute": {
"single_nested_block": {
"element": {
"resource": {
"a1": {
"optional": true,
"type": 4
}
}
},
"optional": true,
"type": 6
}
}
}
}`),
},
{
"list-nested-block",
&pb.Provider{
AllResources: []pb.Resource{{
ResourceSchema: schema.Schema{
Blocks: map[string]schema.Block{
"list_nested_block": schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"a1": schema.StringAttribute{
Optional: true,
},
},
},
},
},
},
}},
},
// TODO: Why is this different to list-nested-attribute? Should it be?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this expected? I wonder why the list-nested-attribute has 2 more layers of nesting in the shim layer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, this is probably elemSchemas

func elemSchemas(sch shim.Schema, ps *SchemaInfo) (shim.Schema, *SchemaInfo) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or not, elemSchemas is called after the shim schema is constructed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found it in

func (s *attrSchema) Elem() interface{} {

vs blocks:

func (s *blockSchema) Elem() interface{} {

Trying to see what happens if attr shims match the block ones here: #2232

Copy link
Contributor Author

@VenelinMartinov VenelinMartinov Jul 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explored further in #2232, seems plausible that these should actually match and the attribute one seems wrong here. Wondering how this affects schema.go

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you reach a conclusion here?

autogold.Expect(`{
"resources": {
"_": {
"list_nested_block": {
"element": {
"resource": {
"a1": {
"optional": true,
"type": 4
}
}
},
"optional": true,
"type": 5
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this is a list type - this means that the CastToTypeObject

func CastToTypeObject(tfs shim.Schema) (shim.SchemaMap, bool) {
is wrong for both PF and SDKv2 - list types CAN have a resource elem in PF, like in SDKv2

This is the root cause of #2180 fixed in #2181

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this out of date?

}
}
}
}`),
},
}
Expand Down