From b8bed97ef45410ed9a6e65309706323119dbc4b3 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 28 Oct 2020 14:51:04 -0400 Subject: [PATCH] test for RPC warnings with no errors --- terraform/context_apply_test.go | 49 ++++++++++++++++++++++++++++++ terraform/context_plan_test.go | 43 ++++++++++++++++++++++++++ terraform/context_validate_test.go | 43 ++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) diff --git a/terraform/context_apply_test.go b/terraform/context_apply_test.go index 8b7254f0b..f65ce9bd5 100644 --- a/terraform/context_apply_test.go +++ b/terraform/context_apply_test.go @@ -12230,3 +12230,52 @@ resource "test_resource" "foo" { t.Fatal("missing 'test_resource.foo' in state:", state) } } + +func TestContext2Apply_rpcDiagnostics(t *testing.T) { + m := testModuleInline(t, map[string]string{ + "main.tf": ` +resource "test_instance" "a" { +} +`, + }) + + p := testProvider("test") + p.PlanResourceChangeFn = testDiffFn + p.ApplyResourceChangeFn = testApplyFn + p.GetSchemaReturn = &ProviderSchema{ + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Computed: true}, + }, + }, + }, + } + + p.ValidateResourceTypeConfigResponse = providers.ValidateResourceTypeConfigResponse{ + Diagnostics: tfdiags.Diagnostics(nil).Append(tfdiags.SimpleWarning("don't frobble")), + } + + ctx := testContext2(t, &ContextOpts{ + Config: m, + Providers: map[addrs.Provider]providers.Factory{ + addrs.NewDefaultProvider("test"): testProviderFuncFixed(p), + }, + }) + _, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatal(diags.Err()) + } + + _, diags = ctx.Apply() + if diags.HasErrors() { + t.Fatal(diags.Err()) + } + + for _, d := range diags { + des := d.Description().Summary + if !strings.Contains(des, "frobble") { + t.Fatalf(`expected frobble, got %q`, des) + } + } +} diff --git a/terraform/context_plan_test.go b/terraform/context_plan_test.go index 819870f96..33d0c091a 100644 --- a/terraform/context_plan_test.go +++ b/terraform/context_plan_test.go @@ -6421,3 +6421,46 @@ data "test_data_source" "b" { t.Fatal("data source b was not read during plan") } } + +func TestContext2Plan_rpcDiagnostics(t *testing.T) { + m := testModuleInline(t, map[string]string{ + "main.tf": ` +resource "test_instance" "a" { +} +`, + }) + + p := testProvider("test") + p.PlanResourceChangeFn = testDiffFn + p.GetSchemaReturn = &ProviderSchema{ + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Computed: true}, + }, + }, + }, + } + + p.ValidateResourceTypeConfigResponse = providers.ValidateResourceTypeConfigResponse{ + Diagnostics: tfdiags.Diagnostics(nil).Append(tfdiags.SimpleWarning("don't herd cats")), + } + + ctx := testContext2(t, &ContextOpts{ + Config: m, + Providers: map[addrs.Provider]providers.Factory{ + addrs.NewDefaultProvider("test"): testProviderFuncFixed(p), + }, + }) + _, diags := ctx.Plan() + if diags.HasErrors() { + t.Fatal(diags.Err()) + } + + for _, d := range diags { + des := d.Description().Summary + if !strings.Contains(des, "cats") { + t.Fatalf(`expected cats, got %q`, des) + } + } +} diff --git a/terraform/context_validate_test.go b/terraform/context_validate_test.go index ec1f9b091..4f5f45a9f 100644 --- a/terraform/context_validate_test.go +++ b/terraform/context_validate_test.go @@ -1795,3 +1795,46 @@ resource "test_instance" "a" { } } } + +func TestContext2Validate_rpcDiagnostics(t *testing.T) { + // validate module and output depends_on + m := testModuleInline(t, map[string]string{ + "main.tf": ` +resource "test_instance" "a" { +} +`, + }) + + p := testProvider("test") + p.GetSchemaReturn = &ProviderSchema{ + ResourceTypes: map[string]*configschema.Block{ + "test_instance": { + Attributes: map[string]*configschema.Attribute{ + "id": {Type: cty.String, Computed: true}, + }, + }, + }, + } + + p.ValidateResourceTypeConfigResponse = providers.ValidateResourceTypeConfigResponse{ + Diagnostics: tfdiags.Diagnostics(nil).Append(tfdiags.SimpleWarning("don't frobble")), + } + + ctx := testContext2(t, &ContextOpts{ + Config: m, + Providers: map[addrs.Provider]providers.Factory{ + addrs.NewDefaultProvider("test"): testProviderFuncFixed(p), + }, + }) + diags := ctx.Validate() + if diags.HasErrors() { + t.Fatal(diags.Err()) + } + + for _, d := range diags { + des := d.Description().Summary + if !strings.Contains(des, "frobble") { + t.Fatalf(`expected frobble, got %q`, des) + } + } +}