Skip to content

Commit 5e0ce12

Browse files
committed
add e2e test with provider schema capabilities
enable destroy planning for the simple providers used in the e2e tests
1 parent cc46933 commit 5e0ce12

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

internal/command/e2etest/provider_plugin_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ func TestProviderProtocols(t *testing.T) {
7272
}
7373

7474
if !strings.Contains(stdout, "Apply complete! Resources: 2 added, 0 changed, 0 destroyed.") {
75-
t.Fatalf("wrong output:\n%s", stdout)
75+
t.Fatalf("wrong output:\nstdout:%s\nstderr%s", stdout, stderr)
76+
}
77+
78+
/// DESTROY
79+
stdout, stderr, err = tf.Run("destroy", "-auto-approve")
80+
if err != nil {
81+
t.Fatalf("unexpected apply error: %s\nstderr:\n%s", err, stderr)
82+
}
83+
84+
if !strings.Contains(stdout, "Resources: 2 destroyed") {
85+
t.Fatalf("wrong destroy output\nstdout:%s\nstderr:%s", stdout, stderr)
7686
}
7787
}

internal/grpcwrap/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (p *provider) GetSchema(_ context.Context, req *tfplugin5.GetProviderSchema
5959
}
6060
}
6161

62+
resp.Capabilities = &tfplugin5.GetProviderSchema_Capabilities{
63+
PlanDestroy: p.schema.Capabilities.PlanDestroy,
64+
}
65+
6266
// include any diagnostics from the original GetSchema call
6367
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, p.schema.Diagnostics)
6468

internal/grpcwrap/provider6.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ func (p *provider6) GetProviderSchema(_ context.Context, req *tfplugin6.GetProvi
5959
}
6060
}
6161

62+
resp.Capabilities = &tfplugin6.GetProviderSchema_Capabilities{
63+
PlanDestroy: p.schema.Capabilities.PlanDestroy,
64+
}
65+
6266
// include any diagnostics from the original GetSchema call
6367
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, p.schema.Diagnostics)
6468

internal/provider-simple-v6/provider.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package simple
33

44
import (
55
"errors"
6+
"fmt"
67
"time"
78

89
"github.com/hashicorp/terraform/internal/configs/configschema"
@@ -42,6 +43,9 @@ func Provider() providers.Interface {
4243
DataSources: map[string]providers.Schema{
4344
"simple_resource": simpleResource,
4445
},
46+
Capabilities: providers.Capabilities{
47+
PlanDestroy: true,
48+
},
4549
},
4650
}
4751
}
@@ -88,7 +92,10 @@ func (s simple) PlanResourceChange(req providers.PlanResourceChangeRequest) (res
8892
if req.ProposedNewState.IsNull() {
8993
// destroy op
9094
resp.PlannedState = req.ProposedNewState
91-
resp.PlannedPrivate = req.PriorPrivate
95+
96+
// signal that this resource was properly planned for destruction,
97+
// verifying that the schema capabilities with PlanDestroy took effect.
98+
resp.PlannedPrivate = []byte("destroy planned")
9299
return resp
93100
}
94101

@@ -104,6 +111,11 @@ func (s simple) PlanResourceChange(req providers.PlanResourceChangeRequest) (res
104111

105112
func (s simple) ApplyResourceChange(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
106113
if req.PlannedState.IsNull() {
114+
// make sure this was transferred from the plan action
115+
if string(req.PlannedPrivate) != "destroy planned" {
116+
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("resource not planned for destroy, private data %q", req.PlannedPrivate))
117+
}
118+
107119
resp.NewState = req.PlannedState
108120
return resp
109121
}

internal/provider-simple/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ func Provider() providers.Interface {
4242
DataSources: map[string]providers.Schema{
4343
"simple_resource": simpleResource,
4444
},
45+
Capabilities: providers.Capabilities{
46+
PlanDestroy: true,
47+
},
4548
},
4649
}
4750
}

0 commit comments

Comments
 (0)