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