Skip to content

Commit

Permalink
test: check compatiblity with SDK/framework
Browse files Browse the repository at this point in the history
  • Loading branch information
tosuke committed Jan 14, 2025
1 parent 079515b commit aba4eac
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test:

.PHONY: testacc
testacc:
TF_ACC=1 go test -v ./mackerel/... -run $(TESTS) -timeout 120m
TF_ACC=1 go test -v ./... -run $(TESTS) -timeout 120m

.PHONY: local-build
local-build:
Expand Down
18 changes: 18 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,27 @@ import (
"testing"

fwprovider "github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/mackerelio-labs/terraform-provider-mackerel/internal/provider"
sdkmackerel "github.com/mackerelio-labs/terraform-provider-mackerel/mackerel"
)

var (
protoV5FrameworkProviderFactories = map[string]func() (tfprotov5.ProviderServer, error){
"mackerel": providerserver.NewProtocol5WithError(provider.New()),
}
protoV5SDKProviderFactories = map[string]func() (tfprotov5.ProviderServer, error){
"mackerel": func() (tfprotov5.ProviderServer, error) {
return sdkmackerel.Provider().GRPCProvider(), nil
},
}
)

func preCheck(t *testing.T) {
t.Helper()
}

func TestMackerelProvider_schema(t *testing.T) {
t.Parallel()

Expand Down
75 changes: 75 additions & 0 deletions internal/provider/resource_mackerel_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import (
"testing"

fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
"github.com/mackerelio-labs/terraform-provider-mackerel/internal/provider"
)

Expand All @@ -24,3 +30,72 @@ func Test_MackerelChannelResource_schema(t *testing.T) {
t.Fatalf("schema validation diagnostics: %+v", diags)
}
}

func TestAccCompat_MackerelChannelResource_Slack(t *testing.T) {
t.Parallel()

resourceName := "mackerel_channel.slack"
name := acctest.RandomWithPrefix("tf-channel-slack-compat")

config := `
resource "mackerel_channel" "slack" {
name = "` + name + `"
slack {
url = "https://hooks.slack.com/services/xxx/yyy/zzz"
}
}`

resource.Test(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
Steps: []resource.TestStep{
// Test: SDK
{
Config: config,
ProtoV5ProviderFactories: protoV5SDKProviderFactories,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("slack").AtSliceIndex(0).AtMapKey("events"),
knownvalue.Null()),
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("slack").AtSliceIndex(0).AtMapKey("mentions"),
knownvalue.Null()),
},
},
// Test: Framework
{
Config: config,
ProtoV5ProviderFactories: protoV5FrameworkProviderFactories,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
},
// Test: SDK
// Apply config twice to normalize the state.
{
Config: config,
ProtoV5ProviderFactories: protoV5SDKProviderFactories,
// { "slack": { "events": [], "menstions": {} }}
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("slack").AtSliceIndex(0).AtMapKey("events"),
knownvalue.ListSizeExact(0)),
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("slack").AtSliceIndex(0).AtMapKey("mentions"),
knownvalue.MapSizeExact(0)),
},
},
// Test: Framework
{
Config: config,
ProtoV5ProviderFactories: protoV5FrameworkProviderFactories,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
},
},
})
}

0 comments on commit aba4eac

Please sign in to comment.