Skip to content

Commit

Permalink
test: test compatibility for email and webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
tosuke committed Jan 14, 2025
1 parent aba4eac commit 6085c39
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 17 deletions.
14 changes: 14 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
fwprovider "github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/mackerelio-labs/terraform-provider-mackerel/internal/provider"
sdkmackerel "github.com/mackerelio-labs/terraform-provider-mackerel/mackerel"
)
Expand All @@ -26,6 +28,18 @@ func preCheck(t *testing.T) {
t.Helper()
}

func stepNoPlanInFramework(config string) resource.TestStep {
return resource.TestStep{
Config: config,
ProtoV5ProviderFactories: protoV5FrameworkProviderFactories,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
}
}

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

Expand Down
106 changes: 89 additions & 17 deletions internal/provider/resource_mackerel_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"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 @@ -31,6 +30,56 @@ func Test_MackerelChannelResource_schema(t *testing.T) {
}
}

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

resourceName := "mackerel_channel.email"
name := acctest.RandomWithPrefix("tf-channel-email-compat")
config := `
resource "mackerel_channel" "email" {
name = "` + name + `"
email {}
}`

resource.Test(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
Steps: []resource.TestStep{
{
Config: config,
ProtoV5ProviderFactories: protoV5SDKProviderFactories,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("email").AtSliceIndex(0).AtMapKey("events"),
knownvalue.Null()),
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("email").AtSliceIndex(0).AtMapKey("emails"),
knownvalue.Null()),
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("email").AtSliceIndex(0).AtMapKey("user_ids"),
knownvalue.Null()),
},
},
stepNoPlanInFramework(config),
{
Config: config,
ProtoV5ProviderFactories: protoV5SDKProviderFactories,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("email").AtSliceIndex(0).AtMapKey("events"),
knownvalue.ListSizeExact(0)),
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("email").AtSliceIndex(0).AtMapKey("emails"),
knownvalue.ListSizeExact(0)),
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("email").AtSliceIndex(0).AtMapKey("user_ids"),
knownvalue.ListSizeExact(0)),
},
},
stepNoPlanInFramework(config),
},
})
}

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

Expand Down Expand Up @@ -61,16 +110,7 @@ resource "mackerel_channel" "slack" {
knownvalue.Null()),
},
},
// Test: Framework
{
Config: config,
ProtoV5ProviderFactories: protoV5FrameworkProviderFactories,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
},
},
stepNoPlanInFramework(config),
// Test: SDK
// Apply config twice to normalize the state.
{
Expand All @@ -86,16 +126,48 @@ resource "mackerel_channel" "slack" {
knownvalue.MapSizeExact(0)),
},
},
// Test: Framework
stepNoPlanInFramework(config),
},
})
}

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

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

config := `
resource "mackerel_channel" "webhook" {
name = "` + name + `"
webhook {
url = "https://example.test"
}
}`

resource.Test(t, resource.TestCase{
PreCheck: func() { preCheck(t) },
Steps: []resource.TestStep{
{
Config: config,
ProtoV5ProviderFactories: protoV5SDKProviderFactories,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("webhook").AtSliceIndex(0).AtMapKey("events"),
knownvalue.Null()),
},
},
stepNoPlanInFramework(config),
{
Config: config,
ProtoV5ProviderFactories: protoV5FrameworkProviderFactories,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectEmptyPlan(),
},
ProtoV5ProviderFactories: protoV5SDKProviderFactories,
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName,
tfjsonpath.New("webhook").AtSliceIndex(0).AtMapKey("events"),
knownvalue.ListSizeExact(0)),
},
},
stepNoPlanInFramework(config),
},
})
}

0 comments on commit 6085c39

Please sign in to comment.