|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package funcerr_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/google/go-cmp/cmp" |
| 12 | + "github.com/hashicorp/terraform-plugin-log/tfsdklog" |
| 13 | + "github.com/hashicorp/terraform-plugin-log/tfsdklogtest" |
| 14 | + |
| 15 | + "github.com/hashicorp/terraform-plugin-go/internal/logging" |
| 16 | + "github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/funcerr" |
| 17 | +) |
| 18 | + |
| 19 | +func TestFunctionErrorHasError(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + testCases := map[string]struct { |
| 23 | + functionError *funcerr.FunctionError |
| 24 | + expected bool |
| 25 | + }{ |
| 26 | + "nil": { |
| 27 | + functionError: nil, |
| 28 | + expected: false, |
| 29 | + }, |
| 30 | + "empty": { |
| 31 | + functionError: &funcerr.FunctionError{}, |
| 32 | + expected: false, |
| 33 | + }, |
| 34 | + "argument": { |
| 35 | + functionError: &funcerr.FunctionError{ |
| 36 | + FunctionArgument: pointer(int64(0)), |
| 37 | + }, |
| 38 | + expected: true, |
| 39 | + }, |
| 40 | + "message": { |
| 41 | + functionError: &funcerr.FunctionError{ |
| 42 | + Text: "test function error", |
| 43 | + }, |
| 44 | + expected: true, |
| 45 | + }, |
| 46 | + "argument-and-message": { |
| 47 | + functionError: &funcerr.FunctionError{ |
| 48 | + FunctionArgument: pointer(int64(0)), |
| 49 | + Text: "test function error", |
| 50 | + }, |
| 51 | + expected: true, |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + for name, testCase := range testCases { |
| 56 | + name, testCase := name, testCase |
| 57 | + |
| 58 | + t.Run(name, func(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + got := testCase.functionError.HasError() |
| 62 | + |
| 63 | + if diff := cmp.Diff(got, testCase.expected); diff != "" { |
| 64 | + t.Errorf("unexpected difference: %s", diff) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestFunctionErrorLog(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + |
| 73 | + testCases := map[string]struct { |
| 74 | + functionError *funcerr.FunctionError |
| 75 | + expected []map[string]interface{} |
| 76 | + }{ |
| 77 | + "nil": { |
| 78 | + functionError: nil, |
| 79 | + expected: nil, |
| 80 | + }, |
| 81 | + "empty": { |
| 82 | + functionError: &funcerr.FunctionError{}, |
| 83 | + expected: nil, |
| 84 | + }, |
| 85 | + "argument": { |
| 86 | + functionError: &funcerr.FunctionError{ |
| 87 | + FunctionArgument: pointer(int64(0)), |
| 88 | + }, |
| 89 | + expected: []map[string]interface{}{ |
| 90 | + { |
| 91 | + "@level": "error", |
| 92 | + "@message": "Response contains function error", |
| 93 | + "@module": "sdk.proto", |
| 94 | + "function_error_argument": float64(0), |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + "message": { |
| 99 | + functionError: &funcerr.FunctionError{ |
| 100 | + Text: "test function error", |
| 101 | + }, |
| 102 | + expected: []map[string]interface{}{ |
| 103 | + { |
| 104 | + "@level": "error", |
| 105 | + "@message": "Response contains function error", |
| 106 | + "@module": "sdk.proto", |
| 107 | + "function_error_text": "test function error", |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + "argument-and-message": { |
| 112 | + functionError: &funcerr.FunctionError{ |
| 113 | + FunctionArgument: pointer(int64(0)), |
| 114 | + Text: "test function error", |
| 115 | + }, |
| 116 | + expected: []map[string]interface{}{ |
| 117 | + { |
| 118 | + "@level": "error", |
| 119 | + "@message": "Response contains function error", |
| 120 | + "@module": "sdk.proto", |
| 121 | + "function_error_text": "test function error", |
| 122 | + "function_error_argument": float64(0), |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + for name, testCase := range testCases { |
| 129 | + name, testCase := name, testCase |
| 130 | + |
| 131 | + t.Run(name, func(t *testing.T) { |
| 132 | + t.Parallel() |
| 133 | + |
| 134 | + var output bytes.Buffer |
| 135 | + |
| 136 | + ctx := tfsdklogtest.RootLogger(context.Background(), &output) |
| 137 | + ctx = logging.ProtoSubsystemContext(ctx, tfsdklog.Options{}) |
| 138 | + |
| 139 | + testCase.functionError.Log(ctx) |
| 140 | + |
| 141 | + entries, err := tfsdklogtest.MultilineJSONDecode(&output) |
| 142 | + |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("unable to read multiple line JSON: %s", err) |
| 145 | + } |
| 146 | + |
| 147 | + if diff := cmp.Diff(entries, testCase.expected); diff != "" { |
| 148 | + t.Errorf("unexpected difference: %s", diff) |
| 149 | + } |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments