|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package framework |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 16 | + "github.com/hashicorp/terraform-plugin-go/tftypes" |
| 17 | + "github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag" |
| 18 | + "github.com/hashicorp/terraform-provider-aws/internal/provider/framework/resourceattribute" |
| 19 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 20 | +) |
| 21 | + |
| 22 | +func TestResourceSetRegionInStateInterceptor_Read(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + const name = "example" |
| 26 | + |
| 27 | + region := os.Getenv("AWS_DEFAULT_REGION") |
| 28 | + if region == "" { |
| 29 | + t.Skip("AWS_DEFAULT_REGION env var must be set for ResourceSetRegionInStateInterceptor test.") |
| 30 | + } |
| 31 | + |
| 32 | + ctx := context.Background() |
| 33 | + client := mockClient{region: region} |
| 34 | + icpt := resourceSetRegionInStateInterceptor{} |
| 35 | + |
| 36 | + s := schema.Schema{ |
| 37 | + Attributes: map[string]schema.Attribute{ |
| 38 | + names.AttrName: schema.StringAttribute{Required: true}, |
| 39 | + names.AttrRegion: resourceattribute.Region(), |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + tests := map[string]struct { |
| 44 | + startState tfsdk.State |
| 45 | + expectSet bool |
| 46 | + }{ |
| 47 | + "when state is present then region is set": { |
| 48 | + startState: stateFromSchema(ctx, s, map[string]string{"name": name}), |
| 49 | + expectSet: true, |
| 50 | + }, |
| 51 | + "when state is null then it remains null": { |
| 52 | + startState: tfsdk.State{ |
| 53 | + Raw: tftypes.NewValue(s.Type().TerraformType(ctx), nil), |
| 54 | + Schema: s, |
| 55 | + }, |
| 56 | + expectSet: false, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for tn, tc := range tests { |
| 61 | + t.Run(tn, func(t *testing.T) { |
| 62 | + t.Parallel() |
| 63 | + |
| 64 | + req := resource.ReadRequest{State: tc.startState} |
| 65 | + resp := resource.ReadResponse{State: tc.startState} |
| 66 | + |
| 67 | + if diags := icpt.read(ctx, interceptorOptions[resource.ReadRequest, resource.ReadResponse]{ |
| 68 | + c: client, |
| 69 | + request: &req, |
| 70 | + response: &resp, |
| 71 | + when: After, |
| 72 | + }); diags.HasError() { |
| 73 | + t.Fatalf("unexpected diags: %s", diags) |
| 74 | + } |
| 75 | + |
| 76 | + if tc.expectSet { |
| 77 | + got := getStateAttributeValue(ctx, t, resp.State, path.Root("region")) |
| 78 | + if got != region { |
| 79 | + t.Errorf("expected region %q, got %q", region, got) |
| 80 | + } |
| 81 | + } else { |
| 82 | + if !resp.State.Raw.IsNull() { |
| 83 | + t.Errorf("expected State.Raw to stay null, got %#v", resp.State.Raw) |
| 84 | + } |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func getStateAttributeValue(ctx context.Context, t *testing.T, st tfsdk.State, p path.Path) string { |
| 91 | + t.Helper() |
| 92 | + |
| 93 | + var v types.String |
| 94 | + if diags := st.GetAttribute(ctx, p, &v); diags.HasError() { |
| 95 | + t.Fatalf("unexpected error getting State attribute %q: %s", p, fwdiag.DiagnosticsError(diags)) |
| 96 | + } |
| 97 | + return v.ValueString() |
| 98 | +} |
0 commit comments