Skip to content

Commit

Permalink
add e2e test with provider schema capabilities
Browse files Browse the repository at this point in the history
enable destroy planning for the simple providers used in the e2e tests
  • Loading branch information
jbardin committed Jun 3, 2022
1 parent a2943c7 commit 6b18b3f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
12 changes: 11 additions & 1 deletion internal/command/e2etest/provider_plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ func TestProviderProtocols(t *testing.T) {
}

if !strings.Contains(stdout, "Apply complete! Resources: 2 added, 0 changed, 0 destroyed.") {
t.Fatalf("wrong output:\n%s", stdout)
t.Fatalf("wrong output:\nstdout:%s\nstderr%s", stdout, stderr)
}

/// DESTROY
stdout, stderr, err = tf.Run("destroy", "-auto-approve")
if err != nil {
t.Fatalf("unexpected apply error: %s\nstderr:\n%s", err, stderr)
}

if !strings.Contains(stdout, "Resources: 2 destroyed") {
t.Fatalf("wrong destroy output\nstdout:%s\nstderr:%s", stdout, stderr)
}
}
4 changes: 4 additions & 0 deletions internal/grpcwrap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (p *provider) GetSchema(_ context.Context, req *tfplugin5.GetProviderSchema
}
}

resp.Capabilities = &tfplugin5.GetProviderSchema_Capabilities{
PlanDestroy: p.schema.Capabilities.PlanDestroy,
}

// include any diagnostics from the original GetSchema call
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, p.schema.Diagnostics)

Expand Down
4 changes: 4 additions & 0 deletions internal/grpcwrap/provider6.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (p *provider6) GetProviderSchema(_ context.Context, req *tfplugin6.GetProvi
}
}

resp.Capabilities = &tfplugin6.GetProviderSchema_Capabilities{
PlanDestroy: p.schema.Capabilities.PlanDestroy,
}

// include any diagnostics from the original GetSchema call
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, p.schema.Diagnostics)

Expand Down
14 changes: 13 additions & 1 deletion internal/provider-simple-v6/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package simple

import (
"errors"
"fmt"
"time"

"github.com/hashicorp/terraform/internal/configs/configschema"
Expand Down Expand Up @@ -42,6 +43,9 @@ func Provider() providers.Interface {
DataSources: map[string]providers.Schema{
"simple_resource": simpleResource,
},
Capabilities: providers.Capabilities{
//PlanDestroy: true,
},
},
}
}
Expand Down Expand Up @@ -88,7 +92,10 @@ func (s simple) PlanResourceChange(req providers.PlanResourceChangeRequest) (res
if req.ProposedNewState.IsNull() {
// destroy op
resp.PlannedState = req.ProposedNewState
resp.PlannedPrivate = req.PriorPrivate

// signal that this resource was properly planned for destruction,
// verifying that the schema capabilities with PlanDestroy took effect.
resp.PlannedPrivate = []byte("destroy planned")
return resp
}

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

func (s simple) ApplyResourceChange(req providers.ApplyResourceChangeRequest) (resp providers.ApplyResourceChangeResponse) {
if req.PlannedState.IsNull() {
// make sure this was transferred from the plan action
if string(req.PlannedPrivate) != "destroy planned" {
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("resource not planned for destroy, private data %q", req.PlannedPrivate))
}

resp.NewState = req.PlannedState
return resp
}
Expand Down
3 changes: 3 additions & 0 deletions internal/provider-simple/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func Provider() providers.Interface {
DataSources: map[string]providers.Schema{
"simple_resource": simpleResource,
},
Capabilities: providers.Capabilities{
PlanDestroy: true,
},
},
}
}
Expand Down

0 comments on commit 6b18b3f

Please sign in to comment.