Skip to content

Commit

Permalink
internal/fwserver: Unit testing and changelog for ConfigValidators di…
Browse files Browse the repository at this point in the history
…agnostics overwrites (#622)

Reference: #619

Against current `main`:

```
--- FAIL: TestServerValidateDataSourceConfig (0.00s)
    --- FAIL: TestServerValidateDataSourceConfig/request-config-DataSourceWithConfigValidators-diagnostics (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_validatedatasourceconfig_test.go:301: unexpected difference:   &fwserver.ValidateDataSourceConfigResponse{
            - 	Diagnostics: diag.Diagnostics{diag.ErrorDiagnostic{detail: "error detail 2", summary: "error summary 2"}},
            + 	Diagnostics: diag.Diagnostics{
            + 		diag.ErrorDiagnostic{detail: "error detail 1", summary: "error summary 1"},
            + 		diag.ErrorDiagnostic{detail: "error detail 2", summary: "error summary 2"},
            + 	},
              }
--- FAIL: TestServerValidateProviderConfig (0.00s)
    --- FAIL: TestServerValidateProviderConfig/request-config-ProviderWithConfigValidators-diagnostics (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_validateproviderconfig_test.go:307: unexpected difference:   &fwserver.ValidateProviderConfigResponse{
              	PreparedConfig: &{Raw: s`tftypes.Object["test":tftypes.String]<"test":tftypes.String<"tes`..., Schema: schema.Schema{Attributes: {"test": schema.StringAttribute{Required: true}}}},
            - 	Diagnostics:    diag.Diagnostics{diag.ErrorDiagnostic{detail: "error detail 2", summary: "error summary 2"}},
            + 	Diagnostics: diag.Diagnostics{
            + 		diag.ErrorDiagnostic{detail: "error detail 1", summary: "error summary 1"},
            + 		diag.ErrorDiagnostic{detail: "error detail 2", summary: "error summary 2"},
            + 	},
              }
--- FAIL: TestServerValidateResourceConfig (0.00s)
    --- FAIL: TestServerValidateResourceConfig/request-config-ResourceWithConfigValidators-diagnostics (0.00s)
        /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_validateresourceconfig_test.go:301: unexpected difference:   &fwserver.ValidateResourceConfigResponse{
            - 	Diagnostics: diag.Diagnostics{diag.ErrorDiagnostic{detail: "error detail 2", summary: "error summary 2"}},
            + 	Diagnostics: diag.Diagnostics{
            + 		diag.ErrorDiagnostic{detail: "error detail 1", summary: "error summary 1"},
            + 		diag.ErrorDiagnostic{detail: "error detail 2", summary: "error summary 2"},
            + 	},
              }
```
  • Loading branch information
bflad authored Jan 13, 2023
1 parent 620823f commit 032f768
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
11 changes: 11 additions & 0 deletions .changelog/619.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:bug
datasource: Prevented `ConfigValidators` from unexpectedly modifying or removing prior validator diagnostics
```

```release-note:bug
provider: Prevented `ConfigValidators` from unexpectedly modifying or removing prior validator diagnostics
```

```release-note:bug
resource: Prevented `ConfigValidators` from unexpectedly modifying or removing prior validator diagnostics
```
22 changes: 18 additions & 4 deletions internal/fwserver/server_validatedatasourceconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestServerValidateDataSourceConfig(t *testing.T) {
},
expectedResponse: &fwserver.ValidateDataSourceConfigResponse{},
},
"request-config-DataSourceWithConfigValidators-diagnostic": {
"request-config-DataSourceWithConfigValidators-diagnostics": {
server: &fwserver.Server{
Provider: &testprovider.Provider{},
},
Expand All @@ -198,7 +198,17 @@ func TestServerValidateDataSourceConfig(t *testing.T) {
return []datasource.ConfigValidator{
&testprovider.DataSourceConfigValidator{
ValidateDataSourceMethod: func(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
resp.Diagnostics.AddError("error summary", "error detail")
resp.Diagnostics.AddError("error summary 1", "error detail 1")
},
},
&testprovider.DataSourceConfigValidator{
ValidateDataSourceMethod: func(ctx context.Context, req datasource.ValidateConfigRequest, resp *datasource.ValidateConfigResponse) {
// Intentionally set diagnostics instead of add/append.
// The framework should not overwrite existing diagnostics.
// Reference: https://github.com/hashicorp/terraform-plugin-framework-validators/pull/94
resp.Diagnostics = diag.Diagnostics{
diag.NewErrorDiagnostic("error summary 2", "error detail 2"),
}
},
},
}
Expand All @@ -208,8 +218,12 @@ func TestServerValidateDataSourceConfig(t *testing.T) {
expectedResponse: &fwserver.ValidateDataSourceConfigResponse{
Diagnostics: diag.Diagnostics{
diag.NewErrorDiagnostic(
"error summary",
"error detail",
"error summary 1",
"error detail 1",
),
diag.NewErrorDiagnostic(
"error summary 2",
"error detail 2",
),
}},
},
Expand Down
22 changes: 18 additions & 4 deletions internal/fwserver/server_validateproviderconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func TestServerValidateProviderConfig(t *testing.T) {
PreparedConfig: &testConfig,
},
},
"request-config-ProviderWithConfigValidators-diagnostic": {
"request-config-ProviderWithConfigValidators-diagnostics": {
server: &fwserver.Server{
Provider: &testprovider.ProviderWithConfigValidators{
Provider: &testprovider.Provider{
Expand All @@ -197,7 +197,17 @@ func TestServerValidateProviderConfig(t *testing.T) {
return []provider.ConfigValidator{
&testprovider.ProviderConfigValidator{
ValidateProviderMethod: func(ctx context.Context, req provider.ValidateConfigRequest, resp *provider.ValidateConfigResponse) {
resp.Diagnostics.AddError("error summary", "error detail")
resp.Diagnostics.AddError("error summary 1", "error detail 1")
},
},
&testprovider.ProviderConfigValidator{
ValidateProviderMethod: func(ctx context.Context, req provider.ValidateConfigRequest, resp *provider.ValidateConfigResponse) {
// Intentionally set diagnostics instead of add/append.
// The framework should not overwrite existing diagnostics.
// Reference: https://github.com/hashicorp/terraform-plugin-framework-validators/pull/94
resp.Diagnostics = diag.Diagnostics{
diag.NewErrorDiagnostic("error summary 2", "error detail 2"),
}
},
},
}
Expand All @@ -210,8 +220,12 @@ func TestServerValidateProviderConfig(t *testing.T) {
expectedResponse: &fwserver.ValidateProviderConfigResponse{
Diagnostics: diag.Diagnostics{
diag.NewErrorDiagnostic(
"error summary",
"error detail",
"error summary 1",
"error detail 1",
),
diag.NewErrorDiagnostic(
"error summary 2",
"error detail 2",
),
},
PreparedConfig: &testConfig,
Expand Down
22 changes: 18 additions & 4 deletions internal/fwserver/server_validateresourceconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func TestServerValidateResourceConfig(t *testing.T) {
},
expectedResponse: &fwserver.ValidateResourceConfigResponse{},
},
"request-config-ResourceWithConfigValidators-diagnostic": {
"request-config-ResourceWithConfigValidators-diagnostics": {
server: &fwserver.Server{
Provider: &testprovider.Provider{},
},
Expand All @@ -198,7 +198,17 @@ func TestServerValidateResourceConfig(t *testing.T) {
return []resource.ConfigValidator{
&testprovider.ResourceConfigValidator{
ValidateResourceMethod: func(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
resp.Diagnostics.AddError("error summary", "error detail")
resp.Diagnostics.AddError("error summary 1", "error detail 1")
},
},
&testprovider.ResourceConfigValidator{
ValidateResourceMethod: func(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
// Intentionally set diagnostics instead of add/append.
// The framework should not overwrite existing diagnostics.
// Reference: https://github.com/hashicorp/terraform-plugin-framework-validators/pull/94
resp.Diagnostics = diag.Diagnostics{
diag.NewErrorDiagnostic("error summary 2", "error detail 2"),
}
},
},
}
Expand All @@ -208,8 +218,12 @@ func TestServerValidateResourceConfig(t *testing.T) {
expectedResponse: &fwserver.ValidateResourceConfigResponse{
Diagnostics: diag.Diagnostics{
diag.NewErrorDiagnostic(
"error summary",
"error detail",
"error summary 1",
"error detail 1",
),
diag.NewErrorDiagnostic(
"error summary 2",
"error detail 2",
),
}},
},
Expand Down

0 comments on commit 032f768

Please sign in to comment.