Skip to content

Commit d0232da

Browse files
committed
validate on new create too and add tests
1 parent 6812355 commit d0232da

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

internal/terraform/context_apply_identity_test.go

+27-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ func TestContext2Apply_identity(t *testing.T) {
2222
prevRunState *states.State
2323
requiresReplace []cty.Path
2424
plannedIdentity cty.Value
25+
appliedIdentity cty.Value
2526

26-
expectedIdentity cty.Value
27+
expectedIdentity cty.Value
28+
expectDiagnostics tfdiags.Diagnostics
2729
}{
2830
"create": {
2931
plannedIdentity: cty.ObjectVal(map[string]cty.Value{
@@ -33,6 +35,17 @@ func TestContext2Apply_identity(t *testing.T) {
3335
"id": cty.StringVal("foo"),
3436
}),
3537
},
38+
"create - invalid applied identity schema": {
39+
plannedIdentity: cty.ObjectVal(map[string]cty.Value{
40+
"id": cty.StringVal("foo"),
41+
}),
42+
appliedIdentity: cty.ObjectVal(map[string]cty.Value{
43+
"id": cty.BoolVal(false),
44+
}),
45+
expectDiagnostics: tfdiags.Diagnostics{
46+
tfdiags.Sourceless(tfdiags.Error, "Provider produced an identity that doesn't match the schema", "Provider \"registry.terraform.io/hashicorp/test\" returned an identity for test_resource.test that doesn't match the identity schema: .id: string required, but received bool. \n\nThis is a bug in the provider, which should be reported in the provider's own issue tracker."),
47+
},
48+
},
3649

3750
"update": {
3851
prevRunState: states.BuildState(func(s *states.SyncState) {
@@ -165,10 +178,23 @@ func TestContext2Apply_identity(t *testing.T) {
165178
}
166179
}
167180

181+
if !tc.appliedIdentity.IsNull() {
182+
p.ApplyResourceChangeFn = func(req providers.ApplyResourceChangeRequest) providers.ApplyResourceChangeResponse {
183+
resp := providers.ApplyResourceChangeResponse{}
184+
resp.NewState = req.PlannedState
185+
resp.NewIdentity = tc.appliedIdentity
186+
return resp
187+
}
188+
}
189+
168190
plan, diags := ctx.Plan(m, tc.prevRunState, &PlanOpts{Mode: tc.mode})
169191
tfdiags.AssertNoDiagnostics(t, diags)
170192

171193
state, diags := ctx.Apply(plan, m, nil)
194+
if tc.expectDiagnostics.HasErrors() {
195+
tfdiags.AssertDiagnosticsMatch(t, diags, tc.expectDiagnostics)
196+
return
197+
}
172198
tfdiags.AssertNoDiagnostics(t, diags)
173199

174200
if !tc.expectedIdentity.IsNull() {

internal/terraform/context_plan_identity_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,14 @@ func TestContext2Plan_resource_identity_plan(t *testing.T) {
437437
"id": cty.StringVal("foo"),
438438
}),
439439
},
440+
"create - invalid planned identity schema": {
441+
plannedIdentity: cty.ObjectVal(map[string]cty.Value{
442+
"id": cty.BoolVal(false),
443+
}),
444+
expectDiagnostics: tfdiags.Diagnostics{
445+
tfdiags.Sourceless(tfdiags.Error, "Provider produced an identity that doesn't match the schema", "Provider \"registry.terraform.io/hashicorp/test\" returned an identity for test_resource.test that doesn't match the identity schema: .id: string required, but received bool. \n\nThis is a bug in the provider, which should be reported in the provider's own issue tracker."),
446+
},
447+
},
440448
"update": {
441449
prevRunState: states.BuildState(func(s *states.SyncState) {
442450
s.SetResourceInstanceCurrent(

internal/terraform/node_resource_abstract_instance.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,11 @@ func (n *NodeAbstractResourceInstance) plan(
11151115
// If the identity is not known we can not validate it did not change
11161116
if !diags.HasErrors() {
11171117
diags = diags.Append(n.validateIdentityDidNotChange(currentState, plannedIdentity))
1118-
diags = diags.Append(n.validateIdentityMatchesSchema(plannedIdentity, schema.Identity))
11191118
}
11201119
}
11211120

11221121
diags = diags.Append(n.validateIdentity(plannedIdentity))
1122+
diags = diags.Append(n.validateIdentityMatchesSchema(plannedIdentity, schema.Identity))
11231123
}
11241124
if diags.HasErrors() {
11251125
return nil, nil, deferred, keyData, diags
@@ -2946,7 +2946,7 @@ func (n *NodeAbstractResourceInstance) validateIdentityMatchesSchema(newIdentity
29462946
currentType := identitySchema.ImpliedType()
29472947
if errs := newType.TestConformance(currentType); len(errs) > 0 {
29482948
for _, err := range errs {
2949-
diags = diags.Append(tfdiags.Sourceless(
2949+
diags = diags.AppendWithoutDuplicates(tfdiags.Sourceless(
29502950
tfdiags.Error,
29512951
"Provider produced an identity that doesn't match the schema",
29522952
fmt.Sprintf(

0 commit comments

Comments
 (0)