package validate import ( "context" "testing" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/tfsdk" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-go/tftypes" "github.com/hashicorp/terraform-provider-awscc/internal/tfresource" ) func TestARNValidator(t *testing.T) { t.Parallel() type testCase struct { val tftypes.Value f func(context.Context, tftypes.Value) (attr.Value, error) expectError bool } tests := map[string]testCase{ "not a string": { val: tftypes.NewValue(tftypes.Bool, true), f: types.BoolType.ValueFromTerraform, expectError: true, }, "unknown string": { val: tftypes.NewValue(tftypes.String, tftypes.UnknownValue), f: types.StringType.ValueFromTerraform, }, "null string": { val: tftypes.NewValue(tftypes.String, nil), f: types.StringType.ValueFromTerraform, }, "valid string": { val: tftypes.NewValue(tftypes.String, "arn:aws:kafka:us-west-2:123456789012:cluster/tf-acc-test-3972327032919409894/09494266-aaf8-48e7-b7ce-8548498bb813-11"), f: types.StringType.ValueFromTerraform, }, "invalid string": { val: tftypes.NewValue(tftypes.String, "not ok"), f: types.StringType.ValueFromTerraform, expectError: true, }, } for name, test := range tests { name, test := name, test t.Run(name, func(t *testing.T) { ctx := context.TODO() val, err := test.f(ctx, test.val) if err != nil { t.Fatalf("got unexpected error: %s", err) } request := tfsdk.ValidateAttributeRequest{ AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} ARN().Validate(ctx, request, &response) if !response.Diagnostics.HasError() && test.expectError { t.Fatal("expected error, got no error") } if response.Diagnostics.HasError() && !test.expectError { t.Fatalf("got unexpected error: %s", tfresource.DiagsError(response.Diagnostics)) } }) } } func TestIAMPolicyARNValidator(t *testing.T) { t.Parallel() type testCase struct { val tftypes.Value f func(context.Context, tftypes.Value) (attr.Value, error) expectError bool } tests := map[string]testCase{ "not a string": { val: tftypes.NewValue(tftypes.Bool, true), f: types.BoolType.ValueFromTerraform, expectError: true, }, "unknown string": { val: tftypes.NewValue(tftypes.String, tftypes.UnknownValue), f: types.StringType.ValueFromTerraform, }, "null string": { val: tftypes.NewValue(tftypes.String, nil), f: types.StringType.ValueFromTerraform, }, "valid IAM Policy ARN": { val: tftypes.NewValue(tftypes.String, "arn:aws:iam::123456789012:policy/policy_name"), f: types.StringType.ValueFromTerraform, }, "invalid ARN": { val: tftypes.NewValue(tftypes.String, "arn:aws:iam::123456789012:user/user_name"), f: types.StringType.ValueFromTerraform, expectError: true, }, "not an ARN": { val: tftypes.NewValue(tftypes.String, "not an ARN"), f: types.StringType.ValueFromTerraform, expectError: true, }, } for name, test := range tests { name, test := name, test t.Run(name, func(t *testing.T) { ctx := context.TODO() val, err := test.f(ctx, test.val) if err != nil { t.Fatalf("got unexpected error: %s", err) } request := tfsdk.ValidateAttributeRequest{ AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), AttributeConfig: val, } response := tfsdk.ValidateAttributeResponse{} IAMPolicyARN().Validate(ctx, request, &response) if !response.Diagnostics.HasError() && test.expectError { t.Fatal("expected error, got no error") } if response.Diagnostics.HasError() && !test.expectError { t.Fatalf("got unexpected error: %s", tfresource.DiagsError(response.Diagnostics)) } }) } }