|
| 1 | +// SPDX-FileCopyrightText: 2025 The Crossplane Authors <https://crossplane.io> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +package errors |
| 6 | + |
| 7 | +import ( |
| 8 | + "testing" |
| 9 | + |
| 10 | + xperrors "github.com/crossplane/crossplane-runtime/v2/pkg/errors" |
| 11 | + "github.com/crossplane/crossplane-runtime/v2/pkg/test" |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + fwdiag "github.com/hashicorp/terraform-plugin-framework/diag" |
| 14 | + fwpath "github.com/hashicorp/terraform-plugin-framework/path" |
| 15 | +) |
| 16 | + |
| 17 | +// fakeDiag is a minimal implementation of the framework diag.Diagnostic |
| 18 | +// interface sufficient for testing frameworkDiagnosticString. |
| 19 | +type fakeDiag struct { |
| 20 | + summary string |
| 21 | + detail string |
| 22 | +} |
| 23 | + |
| 24 | +func (f fakeDiag) Severity() fwdiag.Severity { return fwdiag.SeverityError } |
| 25 | +func (f fakeDiag) Summary() string { return f.summary } |
| 26 | +func (f fakeDiag) Detail() string { return f.detail } |
| 27 | +func (f fakeDiag) Equal(other fwdiag.Diagnostic) bool { |
| 28 | + o, ok := other.(fakeDiag) |
| 29 | + if !ok { |
| 30 | + return false |
| 31 | + } |
| 32 | + return f.summary == o.summary && f.detail == o.detail |
| 33 | +} |
| 34 | + |
| 35 | +// fakeWarnDiag implements a warning diagnostic. |
| 36 | +type fakeWarnDiag struct { |
| 37 | + summary string |
| 38 | + detail string |
| 39 | +} |
| 40 | + |
| 41 | +func (f fakeWarnDiag) Severity() fwdiag.Severity { return fwdiag.SeverityWarning } |
| 42 | +func (f fakeWarnDiag) Summary() string { return f.summary } |
| 43 | +func (f fakeWarnDiag) Detail() string { return f.detail } |
| 44 | +func (f fakeWarnDiag) Equal(other fwdiag.Diagnostic) bool { |
| 45 | + o, ok := other.(fakeWarnDiag) |
| 46 | + if !ok { |
| 47 | + return false |
| 48 | + } |
| 49 | + return f.summary == o.summary && f.detail == o.detail |
| 50 | +} |
| 51 | + |
| 52 | +// fakeDiagWithPath implements DiagnosticWithPath. |
| 53 | +type fakeDiagWithPath struct { |
| 54 | + summary string |
| 55 | + detail string |
| 56 | + p fwpath.Path |
| 57 | +} |
| 58 | + |
| 59 | +func (f fakeDiagWithPath) Severity() fwdiag.Severity { return fwdiag.SeverityError } |
| 60 | +func (f fakeDiagWithPath) Summary() string { return f.summary } |
| 61 | +func (f fakeDiagWithPath) Detail() string { return f.detail } |
| 62 | +func (f fakeDiagWithPath) Path() fwpath.Path { return f.p } |
| 63 | +func (f fakeDiagWithPath) Equal(other fwdiag.Diagnostic) bool { |
| 64 | + o, ok := other.(fakeDiagWithPath) |
| 65 | + if !ok { |
| 66 | + return false |
| 67 | + } |
| 68 | + return f.summary == o.summary && f.detail == o.detail && f.p.String() == o.p.String() |
| 69 | +} |
| 70 | + |
| 71 | +func TestFrameworkDiagnosticString(t *testing.T) { |
| 72 | + type args struct { |
| 73 | + d fwdiag.Diagnostic |
| 74 | + } |
| 75 | + type want struct { |
| 76 | + out string |
| 77 | + } |
| 78 | + cases := map[string]struct { |
| 79 | + args |
| 80 | + want |
| 81 | + }{ |
| 82 | + "SummaryOnly": { |
| 83 | + args: args{d: fakeDiag{summary: "read resource failed"}}, |
| 84 | + want: want{out: "read resource failed"}, |
| 85 | + }, |
| 86 | + "SummaryAndDetail": { |
| 87 | + args: args{d: fakeDiag{summary: "apply failed", detail: "invalid input provided"}}, |
| 88 | + want: want{out: "apply failed: invalid input provided"}, |
| 89 | + }, |
| 90 | + "TrimsSpaces": { |
| 91 | + args: args{d: fakeDiag{summary: " parse ", detail: " bad format "}}, |
| 92 | + want: want{out: "parse: bad format"}, |
| 93 | + }, |
| 94 | + "MultilineDetailPreserved": { |
| 95 | + args: args{d: fakeDiag{summary: "plan failed", detail: "line1\nline2"}}, |
| 96 | + want: want{out: "plan failed: line1\nline2"}, |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for name, tc := range cases { |
| 101 | + t.Run(name, func(t *testing.T) { |
| 102 | + got := frameworkDiagnosticString(tc.args.d) |
| 103 | + if diff := cmp.Diff(tc.want.out, got); diff != "" { |
| 104 | + t.Errorf("\n%s\nframeworkDiagnosticString(...): -want out, +got out:\n%s", name, diff) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestFrameworkDiagnosticsError(t *testing.T) { |
| 111 | + type args struct { |
| 112 | + ds fwdiag.Diagnostics |
| 113 | + } |
| 114 | + type want struct { |
| 115 | + err error |
| 116 | + } |
| 117 | + // Build a path for the path-bearing diag |
| 118 | + p := fwpath.Root("root").AtName("field") |
| 119 | + |
| 120 | + dErr1 := fakeDiag{summary: "op failed", detail: "reason one"} |
| 121 | + dErr2 := fakeDiagWithPath{summary: "apply failed", detail: "invalid value", p: p} |
| 122 | + dWarn := fakeWarnDiag{summary: "just a warning", detail: "ignore me"} |
| 123 | + |
| 124 | + cases := map[string]struct { |
| 125 | + args |
| 126 | + want |
| 127 | + }{ |
| 128 | + "NoDiagnostics": {}, |
| 129 | + "OnlyWarnings": { |
| 130 | + args: args{ds: fwdiag.Diagnostics{dWarn}}, |
| 131 | + want: want{err: nil}, |
| 132 | + }, |
| 133 | + "SingleError": { |
| 134 | + args: args{ds: fwdiag.Diagnostics{dErr1}}, |
| 135 | + want: want{err: xperrors.Join( |
| 136 | + xperrors.New("terraform diagnostic errors"), |
| 137 | + xperrors.New(frameworkDiagnosticString(dErr1)), |
| 138 | + )}, |
| 139 | + }, |
| 140 | + "MultipleErrorsWithPath": { |
| 141 | + args: args{ds: fwdiag.Diagnostics{dErr1, dErr2}}, |
| 142 | + want: want{err: xperrors.Join( |
| 143 | + xperrors.New("terraform diagnostic errors"), |
| 144 | + xperrors.New(frameworkDiagnosticString(dErr1)), |
| 145 | + xperrors.New(frameworkDiagnosticString(dErr2)), |
| 146 | + )}, |
| 147 | + }, |
| 148 | + } |
| 149 | + for name, tc := range cases { |
| 150 | + t.Run(name, func(t *testing.T) { |
| 151 | + got := FrameworkDiagnosticsError("terraform diagnostic errors", tc.args.ds) |
| 152 | + if diff := cmp.Diff(tc.want.err, got, test.EquateErrors()); diff != "" { |
| 153 | + t.Errorf("\n%s\nFrameworkDiagnosticsError(...): -want err, +got err:\n%s", name, diff) |
| 154 | + } |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
0 commit comments