|
1 | | -// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io> |
2 | | -// |
3 | | -// SPDX-License-Identifier: CC0-1.0 |
4 | | - |
5 | | -package compute |
6 | | - |
7 | | -import ( |
8 | | - "errors" |
9 | | - "strings" |
10 | | - "testing" |
11 | | - |
12 | | - "github.com/google/go-cmp/cmp" |
13 | | - "github.com/google/go-cmp/cmp/cmpopts" |
14 | | - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
15 | | -) |
16 | | - |
17 | | -func TestNetworkFirewallPolicyRuleReadWrapper(t *testing.T) { |
18 | | - type args struct { |
19 | | - origErr error |
20 | | - id string |
21 | | - } |
22 | | - type want struct { |
23 | | - err error |
24 | | - id string |
25 | | - } |
26 | | - cases := map[string]struct { |
27 | | - reason string |
28 | | - args args |
29 | | - want want |
30 | | - }{ |
31 | | - "NotFoundError": { |
32 | | - reason: "A 400 error indicating the rule does not exist should clear the ID and return nil", |
33 | | - args: args{ |
34 | | - origErr: errors.New(`Error when reading or editing ComputeNetworkFirewallPolicyRule "projects/my-project/global/firewallPolicies/my-policy/rules/328": googleapi: Error 400: Invalid value for field 'priority': '328'. The firewall policy does not contain a rule at priority 328., invalid`), |
35 | | - id: "projects/my-project/global/firewallPolicies/my-policy/rules/328", |
36 | | - }, |
37 | | - want: want{ |
38 | | - err: nil, |
39 | | - id: "", |
40 | | - }, |
41 | | - }, |
42 | | - "OtherError": { |
43 | | - reason: "Other errors should be returned as-is without clearing the ID", |
44 | | - args: args{ |
45 | | - origErr: errors.New("some other API error"), |
46 | | - id: "projects/my-project/global/firewallPolicies/my-policy/rules/328", |
47 | | - }, |
48 | | - want: want{ |
49 | | - err: cmpopts.AnyError, |
50 | | - id: "projects/my-project/global/firewallPolicies/my-policy/rules/328", |
51 | | - }, |
52 | | - }, |
53 | | - "NoError": { |
54 | | - reason: "A successful read should pass through without modification", |
55 | | - args: args{ |
56 | | - origErr: nil, |
57 | | - id: "projects/my-project/global/firewallPolicies/my-policy/rules/328", |
58 | | - }, |
59 | | - want: want{ |
60 | | - err: nil, |
61 | | - id: "projects/my-project/global/firewallPolicies/my-policy/rules/328", |
62 | | - }, |
63 | | - }, |
64 | | - } |
65 | | - |
66 | | - for name, tc := range cases { |
67 | | - t.Run(name, func(t *testing.T) { |
68 | | - // Create a minimal schema.Resource to get a valid ResourceData. |
69 | | - r := &schema.Resource{ |
70 | | - Schema: map[string]*schema.Schema{ |
71 | | - "priority": { |
72 | | - Type: schema.TypeInt, |
73 | | - Optional: true, |
74 | | - }, |
75 | | - }, |
76 | | - } |
77 | | - d := r.TestResourceData() |
78 | | - d.SetId(tc.args.id) |
79 | | - |
80 | | - // Build the wrapper the same way the configurator does. |
81 | | - origRead := func(d *schema.ResourceData, meta interface{}) error { |
82 | | - return tc.args.origErr |
83 | | - } |
84 | | - wrappedRead := func(d *schema.ResourceData, meta interface{}) error { |
85 | | - err := origRead(d, meta) |
86 | | - if err != nil && strings.Contains(err.Error(), "does not contain a rule at priority") { |
87 | | - d.SetId("") |
88 | | - return nil |
89 | | - } |
90 | | - return err |
91 | | - } |
92 | | - |
93 | | - err := wrappedRead(d, nil) |
94 | | - |
95 | | - if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" { |
96 | | - t.Errorf("%s\nwrappedRead(...): -want error, +got error:\n%s", tc.reason, diff) |
97 | | - } |
98 | | - if diff := cmp.Diff(tc.want.id, d.Id()); diff != "" { |
99 | | - t.Errorf("%s\nwrappedRead(...): -want ID, +got ID:\n%s", tc.reason, diff) |
100 | | - } |
101 | | - }) |
102 | | - } |
103 | | -} |
| 1 | +// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: CC0-1.0 |
| 4 | + |
| 5 | +package compute |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/google/go-cmp/cmp" |
| 13 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 15 | +) |
| 16 | + |
| 17 | +func TestNetworkFirewallPolicyRuleReadWrapper(t *testing.T) { |
| 18 | + type args struct { |
| 19 | + origErr error |
| 20 | + id string |
| 21 | + } |
| 22 | + type want struct { |
| 23 | + err error |
| 24 | + id string |
| 25 | + } |
| 26 | + cases := map[string]struct { |
| 27 | + reason string |
| 28 | + args args |
| 29 | + want want |
| 30 | + }{ |
| 31 | + "NotFoundError": { |
| 32 | + reason: "A 400 error indicating the rule does not exist should clear the ID and return nil", |
| 33 | + args: args{ |
| 34 | + origErr: errors.New(`Error when reading or editing ComputeNetworkFirewallPolicyRule "projects/my-project/global/firewallPolicies/my-policy/rules/328": googleapi: Error 400: Invalid value for field 'priority': '328'. The firewall policy does not contain a rule at priority 328., invalid`), |
| 35 | + id: "projects/my-project/global/firewallPolicies/my-policy/rules/328", |
| 36 | + }, |
| 37 | + want: want{ |
| 38 | + err: nil, |
| 39 | + id: "", |
| 40 | + }, |
| 41 | + }, |
| 42 | + "OtherError": { |
| 43 | + reason: "Other errors should be returned as-is without clearing the ID", |
| 44 | + args: args{ |
| 45 | + origErr: errors.New("some other API error"), |
| 46 | + id: "projects/my-project/global/firewallPolicies/my-policy/rules/328", |
| 47 | + }, |
| 48 | + want: want{ |
| 49 | + err: cmpopts.AnyError, |
| 50 | + id: "projects/my-project/global/firewallPolicies/my-policy/rules/328", |
| 51 | + }, |
| 52 | + }, |
| 53 | + "NoError": { |
| 54 | + reason: "A successful read should pass through without modification", |
| 55 | + args: args{ |
| 56 | + origErr: nil, |
| 57 | + id: "projects/my-project/global/firewallPolicies/my-policy/rules/328", |
| 58 | + }, |
| 59 | + want: want{ |
| 60 | + err: nil, |
| 61 | + id: "projects/my-project/global/firewallPolicies/my-policy/rules/328", |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + for name, tc := range cases { |
| 67 | + t.Run(name, func(t *testing.T) { |
| 68 | + // Create a minimal schema.Resource to get a valid ResourceData. |
| 69 | + r := &schema.Resource{ |
| 70 | + Schema: map[string]*schema.Schema{ |
| 71 | + "priority": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Optional: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + d := r.TestResourceData() |
| 78 | + d.SetId(tc.args.id) |
| 79 | + |
| 80 | + // Build the wrapper the same way the configurator does. |
| 81 | + origRead := func(d *schema.ResourceData, meta interface{}) error { |
| 82 | + return tc.args.origErr |
| 83 | + } |
| 84 | + wrappedRead := func(d *schema.ResourceData, meta interface{}) error { |
| 85 | + err := origRead(d, meta) |
| 86 | + if err != nil && strings.Contains(err.Error(), "does not contain a rule at priority") { |
| 87 | + d.SetId("") |
| 88 | + return nil |
| 89 | + } |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + err := wrappedRead(d, nil) |
| 94 | + |
| 95 | + if diff := cmp.Diff(tc.want.err, err, cmpopts.EquateErrors()); diff != "" { |
| 96 | + t.Errorf("%s\nwrappedRead(...): -want error, +got error:\n%s", tc.reason, diff) |
| 97 | + } |
| 98 | + if diff := cmp.Diff(tc.want.id, d.Id()); diff != "" { |
| 99 | + t.Errorf("%s\nwrappedRead(...): -want ID, +got ID:\n%s", tc.reason, diff) |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments