Skip to content

Commit add5dd4

Browse files
authored
Add framework function object parameter attribute requiredness tests (#226)
Reference: hashicorp/terraform-plugin-framework#955 This adds acceptance tests that capture the linked issue around object parameter attribute requiredness with the existing behavior of Terraform and available functionality of the framework. These are being added as regression checks should framework functionality ever be introduced that either automatically or enables developers to make object attributes optional. It should be considered a breaking change should the existing function definition begin not causing an error, as provider developers may be reliant and desire this existing behavior for their use cases. Without `ErrorCheck` (and temporarily `TerraformVersionChecks` to workaround prerelease issues): ``` === CONT TestObjectFunction_Known_AttributeRequired_Error object_function_test.go:53: Step 1/1 error: Error running pre-apply plan: exit status 1 Error: Invalid function argument on terraform_plugin_test.tf line 13, in output "test": 13: value = provider::framework::object({ 14: "attr1" = "value1", 15: }) ├──────────────── │ while calling provider::framework::object(param1) Invalid value for "param1" parameter: attribute "attr2" is required. --- FAIL: TestObjectFunction_Known_AttributeRequired_Error (0.30s) ``` With `ErrorCheck`: ``` --- PASS: TestObjectFunction_Known_AttributeRequired_Error (0.48s) --- PASS: TestObjectFunction_Known_AttributeRequired_Null (0.53s) ```
1 parent aca8e32 commit add5dd4

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

internal/framework5provider/object_function_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,70 @@ func TestObjectFunction_known(t *testing.T) {
4646
})
4747
}
4848

49+
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/955
50+
func TestObjectFunction_Known_AttributeRequired_Error(t *testing.T) {
51+
t.Parallel()
52+
53+
resource.UnitTest(t, resource.TestCase{
54+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
55+
tfversion.SkipBelow(tfversion.Version1_8_0),
56+
},
57+
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
58+
"framework": providerserver.NewProtocol5WithError(New()),
59+
},
60+
Steps: []resource.TestStep{
61+
{
62+
Config: `
63+
output "test" {
64+
value = provider::framework::object({
65+
"attr1" = "value1"
66+
})
67+
}`,
68+
// This error should always remain with the existing definition
69+
// as provider developers may be reliant and desire this
70+
// Terraform behavior. If new framework functionality is added
71+
// to support optional object attributes, it should be tested
72+
// separately.
73+
ExpectError: regexp.MustCompile(`attribute "attr2" is required`),
74+
},
75+
},
76+
})
77+
}
78+
79+
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/955
80+
func TestObjectFunction_Known_AttributeRequired_Null(t *testing.T) {
81+
resource.UnitTest(t, resource.TestCase{
82+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
83+
tfversion.SkipBelow(tfversion.Version1_8_0),
84+
},
85+
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){
86+
"framework": providerserver.NewProtocol5WithError(New()),
87+
},
88+
Steps: []resource.TestStep{
89+
{
90+
// AllowNullValue being disabled should not affect this
91+
// configuration being valid. That setting only refers to the
92+
// object itself.
93+
Config: `
94+
output "test" {
95+
value = provider::framework::object({
96+
"attr1" = "value1"
97+
"attr2" = null
98+
})
99+
}`,
100+
ConfigStateChecks: []statecheck.StateCheck{
101+
statecheck.ExpectKnownOutputValue("test", knownvalue.ObjectExact(
102+
map[string]knownvalue.Check{
103+
"attr1": knownvalue.StringExact("value1"),
104+
"attr2": knownvalue.Null(),
105+
},
106+
)),
107+
},
108+
},
109+
},
110+
})
111+
}
112+
49113
func TestObjectFunction_null(t *testing.T) {
50114
resource.UnitTest(t, resource.TestCase{
51115
TerraformVersionChecks: []tfversion.TerraformVersionCheck{

internal/framework6provider/object_function_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,70 @@ func TestObjectFunction_known(t *testing.T) {
4646
})
4747
}
4848

49+
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/955
50+
func TestObjectFunction_Known_AttributeRequired_Error(t *testing.T) {
51+
t.Parallel()
52+
53+
resource.UnitTest(t, resource.TestCase{
54+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
55+
tfversion.SkipBelow(tfversion.Version1_8_0),
56+
},
57+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
58+
"framework": providerserver.NewProtocol6WithError(New()),
59+
},
60+
Steps: []resource.TestStep{
61+
{
62+
Config: `
63+
output "test" {
64+
value = provider::framework::object({
65+
"attr1" = "value1"
66+
})
67+
}`,
68+
// This error should always remain with the existing definition
69+
// as provider developers may be reliant and desire this
70+
// Terraform behavior. If new framework functionality is added
71+
// to support optional object attributes, it should be tested
72+
// separately.
73+
ExpectError: regexp.MustCompile(`attribute "attr2" is required`),
74+
},
75+
},
76+
})
77+
}
78+
79+
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/955
80+
func TestObjectFunction_Known_AttributeRequired_Null(t *testing.T) {
81+
resource.UnitTest(t, resource.TestCase{
82+
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
83+
tfversion.SkipBelow(tfversion.Version1_8_0),
84+
},
85+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
86+
"framework": providerserver.NewProtocol6WithError(New()),
87+
},
88+
Steps: []resource.TestStep{
89+
{
90+
// AllowNullValue being disabled should not affect this
91+
// configuration being valid. That setting only refers to the
92+
// object itself.
93+
Config: `
94+
output "test" {
95+
value = provider::framework::object({
96+
"attr1" = "value1"
97+
"attr2" = null
98+
})
99+
}`,
100+
ConfigStateChecks: []statecheck.StateCheck{
101+
statecheck.ExpectKnownOutputValue("test", knownvalue.ObjectExact(
102+
map[string]knownvalue.Check{
103+
"attr1": knownvalue.StringExact("value1"),
104+
"attr2": knownvalue.Null(),
105+
},
106+
)),
107+
},
108+
},
109+
},
110+
})
111+
}
112+
49113
func TestObjectFunction_null(t *testing.T) {
50114
resource.UnitTest(t, resource.TestCase{
51115
TerraformVersionChecks: []tfversion.TerraformVersionCheck{

0 commit comments

Comments
 (0)