|
| 1 | +package functions |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/function" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/types/basetypes" |
| 13 | +) |
| 14 | + |
| 15 | +func TestFunctionRun_location_from_id(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + |
| 18 | + location := "us-central1" |
| 19 | + |
| 20 | + // Happy path inputs |
| 21 | + validId := fmt.Sprintf("projects/my-project/locations/%s/services/my-service", location) |
| 22 | + validSelfLink := fmt.Sprintf("https://run.googleapis.com/v2/%s", validId) |
| 23 | + validOpStyleResourceName := fmt.Sprintf("//run.googleapis.com/v2/%s", validId) |
| 24 | + |
| 25 | + // Unhappy path inputs |
| 26 | + repetitiveInput := fmt.Sprintf("https://run.googleapis.com/v2/projects/my-project/locations/%s/locations/not-this-one/services/my-service", location) // Multiple /locations/{{location}}/ |
| 27 | + invalidInput := "zones/us-central1-c/instances/my-instance" |
| 28 | + |
| 29 | + testCases := map[string]struct { |
| 30 | + request function.RunRequest |
| 31 | + expected function.RunResponse |
| 32 | + }{ |
| 33 | + "it returns the expected output value when given a valid resource id input": { |
| 34 | + request: function.RunRequest{ |
| 35 | + Arguments: function.NewArgumentsData([]attr.Value{types.StringValue(validId)}), |
| 36 | + }, |
| 37 | + expected: function.RunResponse{ |
| 38 | + Result: function.NewResultData(types.StringValue(location)), |
| 39 | + }, |
| 40 | + }, |
| 41 | + "it returns the expected output value when given a valid resource self_link input": { |
| 42 | + request: function.RunRequest{ |
| 43 | + Arguments: function.NewArgumentsData([]attr.Value{types.StringValue(validSelfLink)}), |
| 44 | + }, |
| 45 | + expected: function.RunResponse{ |
| 46 | + Result: function.NewResultData(types.StringValue(location)), |
| 47 | + }, |
| 48 | + }, |
| 49 | + "it returns the expected output value when given a valid OP style resource name input": { |
| 50 | + request: function.RunRequest{ |
| 51 | + Arguments: function.NewArgumentsData([]attr.Value{types.StringValue(validOpStyleResourceName)}), |
| 52 | + }, |
| 53 | + expected: function.RunResponse{ |
| 54 | + Result: function.NewResultData(types.StringValue(location)), |
| 55 | + }, |
| 56 | + }, |
| 57 | + "it returns the first submatch (with no error) when given repetitive input": { |
| 58 | + request: function.RunRequest{ |
| 59 | + Arguments: function.NewArgumentsData([]attr.Value{types.StringValue(repetitiveInput)}), |
| 60 | + }, |
| 61 | + expected: function.RunResponse{ |
| 62 | + Result: function.NewResultData(types.StringValue(location)), |
| 63 | + }, |
| 64 | + }, |
| 65 | + "it returns an error when given input with no submatches": { |
| 66 | + request: function.RunRequest{ |
| 67 | + Arguments: function.NewArgumentsData([]attr.Value{types.StringValue(invalidInput)}), |
| 68 | + }, |
| 69 | + expected: function.RunResponse{ |
| 70 | + Result: function.NewResultData(types.StringNull()), |
| 71 | + Error: function.NewArgumentFuncError(0, fmt.Sprintf("The input string \"%s\" doesn't contain the expected pattern \"locations/{location}/\".", invalidInput)), |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + for name, testCase := range testCases { |
| 77 | + tn, tc := name, testCase |
| 78 | + |
| 79 | + t.Run(tn, func(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + |
| 82 | + // Arrange |
| 83 | + got := function.RunResponse{ |
| 84 | + Result: function.NewResultData(basetypes.StringValue{}), |
| 85 | + } |
| 86 | + |
| 87 | + // Act |
| 88 | + NewLocationFromIdFunction().Run(context.Background(), tc.request, &got) |
| 89 | + |
| 90 | + // Assert |
| 91 | + if diff := cmp.Diff(got.Result, tc.expected.Result); diff != "" { |
| 92 | + t.Errorf("unexpected diff between expected and received result: %s", diff) |
| 93 | + } |
| 94 | + if diff := cmp.Diff(got.Error, tc.expected.Error); diff != "" { |
| 95 | + t.Errorf("unexpected diff between expected and received errors: %s", diff) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
0 commit comments