Skip to content

Commit

Permalink
Adding ExpectNonEmptyPlan to TestTest_TestStep_ProviderFactories_Expe…
Browse files Browse the repository at this point in the history
…ctWarningRefresh (#16)
  • Loading branch information
bendbennett committed Jan 18, 2023
1 parent ad2edeb commit 28c6676
Showing 1 changed file with 60 additions and 40 deletions.
100 changes: 60 additions & 40 deletions helper/resource/teststep_providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2382,52 +2382,72 @@ func TestTest_TestStep_ProviderFactories_ExpectWarningRefresh(t *testing.T) {
t.Parallel()

Test(t, TestCase{
ProviderFactories: map[string]func() (*schema.Provider, error){
"random": func() (*schema.Provider, error) { //nolint:unparam // required signature
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"random_password": {
CreateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
d.SetId("id")
return nil
},
DeleteContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
},
ReadContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) (diags diag.Diagnostics) {
// Only generate diag when ReadContext is called for the second
// time during terraform refresh.
if d.Get("id") == "new_id" {
return append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "warning diagnostic - summary",
})
}

// Update id when ReadContext is called following resource creation
// during terraform apply.
d.SetId("new_id")

return
},
Schema: map[string]*schema.Schema{
"id": {
Computed: true,
Type: schema.TypeString,
Steps: []TestStep{
{
ProviderFactories: map[string]func() (*schema.Provider, error){
"random": func() (*schema.Provider, error) { //nolint:unparam // required signature
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"random_password": {
CreateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
d.SetId("id")
return nil
},
DeleteContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
},
ReadContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) (diags diag.Diagnostics) {
return nil
},
Schema: map[string]*schema.Schema{
"id": {
Computed: true,
Type: schema.TypeString,
},
},
},
},
},
}, nil
},
}, nil
},
},
Steps: []TestStep{
{
},
Config: `resource "random_password" "test" { }`,
},
{
RefreshState: true,
ExpectWarning: regexp.MustCompile(`.*warning diagnostic - summary`),
ProviderFactories: map[string]func() (*schema.Provider, error){
"random": func() (*schema.Provider, error) { //nolint:unparam // required signature
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"random_password": {
CreateContext: func(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
d.SetId("id")
return nil
},
DeleteContext: func(_ context.Context, _ *schema.ResourceData, _ interface{}) diag.Diagnostics {
return nil
},
ReadContext: func(_ context.Context, d *schema.ResourceData, _ interface{}) (diags diag.Diagnostics) {
return append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "warning diagnostic - summary",
})
},
Schema: map[string]*schema.Schema{
"id": {
Computed: true,
Type: schema.TypeString,
},
},
},
},
}, nil
},
},
RefreshState: true,
// ExpectNonEmptyPlan is set to true otherwise following error is generated:
// # random_password.test will be destroyed
// # (because random_password.test is not in configuration)
ExpectNonEmptyPlan: true,
ExpectWarning: regexp.MustCompile(`.*warning diagnostic - summary`),
},
},
})
Expand Down

0 comments on commit 28c6676

Please sign in to comment.