diff --git a/internal/terraform/context_apply_ephemeral_test.go b/internal/terraform/context_apply_ephemeral_test.go index 96f0e41ee1..888a17c47d 100644 --- a/internal/terraform/context_apply_ephemeral_test.go +++ b/internal/terraform/context_apply_ephemeral_test.go @@ -275,3 +275,83 @@ output "data" { t.Error("CloseEphemeralResourceCalled not called") } } + +func TestContext2Apply_ephemeralChecks(t *testing.T) { + // test the full validate-plan-apply lifecycle for ephemeral conditions + m := testModuleInline(t, map[string]string{ + "main.tf": ` +variable "input" { + type = string +} + +ephemeral "ephem_resource" "data" { + lifecycle { + precondition { + condition = var.input == "ok" + error_message = "input not ok" + } + postcondition { + condition = self.value != null + error_message = "value is null" + } + } +} + +provider "test" { + test_string = ephemeral.ephem_resource.data.value +} + +resource "test_object" "test" { +} +`, + }) + + ephem := &testing_provider.MockProvider{ + GetProviderSchemaResponse: &providers.GetProviderSchemaResponse{ + EphemeralResourceTypes: map[string]providers.Schema{ + "ephem_resource": { + Block: &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "value": { + Type: cty.String, + Computed: true, + }, + }, + }, + }, + }, + }, + } + + ephem.OpenEphemeralResourceFn = func(providers.OpenEphemeralResourceRequest) (resp providers.OpenEphemeralResourceResponse) { + resp.Result = cty.ObjectVal(map[string]cty.Value{ + "value": cty.StringVal("test string"), + }) + return resp + } + + p := simpleMockProvider() + + ctx := testContext2(t, &ContextOpts{ + Providers: map[addrs.Provider]providers.Factory{ + addrs.NewDefaultProvider("ephem"): testProviderFuncFixed(ephem), + addrs.NewDefaultProvider("test"): testProviderFuncFixed(p), + }, + }) + + diags := ctx.Validate(m, &ValidateOpts{}) + assertNoDiagnostics(t, diags) + + plan, diags := ctx.Plan(m, nil, &PlanOpts{ + SetVariables: InputValues{ + "input": &InputValue{ + Value: cty.StringVal("ok"), + SourceType: ValueFromConfig, + }, + }, + }) + assertNoDiagnostics(t, diags) + + _, diags = ctx.Apply(plan, m, nil) + assertNoDiagnostics(t, diags) +}