|
| 1 | +/* |
| 2 | +Copyright 2024 The Crossplane Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package response_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/google/go-cmp/cmp" |
| 23 | + "google.golang.org/protobuf/testing/protocmp" |
| 24 | + "k8s.io/utils/ptr" |
| 25 | + |
| 26 | + "github.com/crossplane/function-sdk-go/proto/v1beta1" |
| 27 | + "github.com/crossplane/function-sdk-go/response" |
| 28 | +) |
| 29 | + |
| 30 | +// Condition types. |
| 31 | +const ( |
| 32 | + typeDatabaseReady = "DatabaseReady" |
| 33 | +) |
| 34 | + |
| 35 | +// Condition reasons. |
| 36 | +const ( |
| 37 | + reasonAvailable = "ReasonAvailable" |
| 38 | + reasonCreating = "ReasonCreating" |
| 39 | + reasonPriorFailure = "ReasonPriorFailure" |
| 40 | + reasonUnauthorized = "ReasonUnauthorized" |
| 41 | +) |
| 42 | + |
| 43 | +func TestCondition(t *testing.T) { |
| 44 | + type testFn func(*v1beta1.RunFunctionResponse) |
| 45 | + type args struct { |
| 46 | + fns []testFn |
| 47 | + } |
| 48 | + type want struct { |
| 49 | + conditions []*v1beta1.Condition |
| 50 | + } |
| 51 | + cases := map[string]struct { |
| 52 | + reason string |
| 53 | + args args |
| 54 | + want want |
| 55 | + }{ |
| 56 | + "CreateBasicRecords": { |
| 57 | + reason: "Correctly adds conditions to the response.", |
| 58 | + args: args{ |
| 59 | + fns: []testFn{ |
| 60 | + func(rsp *v1beta1.RunFunctionResponse) { |
| 61 | + response.ConditionTrue(rsp, typeDatabaseReady, reasonAvailable) |
| 62 | + }, |
| 63 | + func(rsp *v1beta1.RunFunctionResponse) { |
| 64 | + response.ConditionFalse(rsp, typeDatabaseReady, reasonCreating) |
| 65 | + }, |
| 66 | + func(rsp *v1beta1.RunFunctionResponse) { |
| 67 | + response.ConditionUnknown(rsp, typeDatabaseReady, reasonPriorFailure) |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + want: want{ |
| 72 | + conditions: []*v1beta1.Condition{ |
| 73 | + { |
| 74 | + Type: typeDatabaseReady, |
| 75 | + Status: v1beta1.Status_STATUS_CONDITION_TRUE, |
| 76 | + Reason: reasonAvailable, |
| 77 | + Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), |
| 78 | + }, |
| 79 | + { |
| 80 | + Type: typeDatabaseReady, |
| 81 | + Status: v1beta1.Status_STATUS_CONDITION_FALSE, |
| 82 | + Reason: reasonCreating, |
| 83 | + Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), |
| 84 | + }, |
| 85 | + { |
| 86 | + Type: typeDatabaseReady, |
| 87 | + Status: v1beta1.Status_STATUS_CONDITION_UNKNOWN, |
| 88 | + Reason: reasonPriorFailure, |
| 89 | + Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + "SetTargets": { |
| 95 | + reason: "Correctly sets targets on condition and adds it to the response.", |
| 96 | + args: args{ |
| 97 | + fns: []testFn{ |
| 98 | + func(rsp *v1beta1.RunFunctionResponse) { |
| 99 | + response.ConditionTrue(rsp, typeDatabaseReady, reasonAvailable).TargetComposite() |
| 100 | + }, |
| 101 | + func(rsp *v1beta1.RunFunctionResponse) { |
| 102 | + response.ConditionTrue(rsp, typeDatabaseReady, reasonAvailable).TargetCompositeAndClaim() |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + want: want{ |
| 107 | + conditions: []*v1beta1.Condition{ |
| 108 | + { |
| 109 | + Type: typeDatabaseReady, |
| 110 | + Status: v1beta1.Status_STATUS_CONDITION_TRUE, |
| 111 | + Reason: reasonAvailable, |
| 112 | + Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), |
| 113 | + }, |
| 114 | + { |
| 115 | + Type: typeDatabaseReady, |
| 116 | + Status: v1beta1.Status_STATUS_CONDITION_TRUE, |
| 117 | + Reason: reasonAvailable, |
| 118 | + Target: v1beta1.Target_TARGET_COMPOSITE_AND_CLAIM.Enum(), |
| 119 | + }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + "SetMessage": { |
| 124 | + reason: "Correctly sets message on condition and adds it to the response.", |
| 125 | + args: args{ |
| 126 | + fns: []testFn{ |
| 127 | + func(rsp *v1beta1.RunFunctionResponse) { |
| 128 | + response.ConditionTrue(rsp, typeDatabaseReady, reasonAvailable).WithMessage("a test message") |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + want: want{ |
| 133 | + conditions: []*v1beta1.Condition{ |
| 134 | + { |
| 135 | + Type: typeDatabaseReady, |
| 136 | + Status: v1beta1.Status_STATUS_CONDITION_TRUE, |
| 137 | + Reason: reasonAvailable, |
| 138 | + Target: v1beta1.Target_TARGET_COMPOSITE.Enum(), |
| 139 | + Message: ptr.To("a test message"), |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + "ChainOptions": { |
| 145 | + reason: "Can chain condition options together.", |
| 146 | + args: args{ |
| 147 | + fns: []testFn{ |
| 148 | + func(rsp *v1beta1.RunFunctionResponse) { |
| 149 | + response.ConditionTrue(rsp, typeDatabaseReady, reasonAvailable). |
| 150 | + WithMessage("a test message"). |
| 151 | + TargetCompositeAndClaim() |
| 152 | + }, |
| 153 | + func(rsp *v1beta1.RunFunctionResponse) { |
| 154 | + response.ConditionTrue(rsp, typeDatabaseReady, reasonAvailable). |
| 155 | + TargetCompositeAndClaim(). |
| 156 | + WithMessage("a test message") |
| 157 | + }, |
| 158 | + }, |
| 159 | + }, |
| 160 | + want: want{ |
| 161 | + conditions: []*v1beta1.Condition{ |
| 162 | + { |
| 163 | + Type: typeDatabaseReady, |
| 164 | + Status: v1beta1.Status_STATUS_CONDITION_TRUE, |
| 165 | + Reason: reasonAvailable, |
| 166 | + Target: v1beta1.Target_TARGET_COMPOSITE_AND_CLAIM.Enum(), |
| 167 | + Message: ptr.To("a test message"), |
| 168 | + }, |
| 169 | + { |
| 170 | + Type: typeDatabaseReady, |
| 171 | + Status: v1beta1.Status_STATUS_CONDITION_TRUE, |
| 172 | + Reason: reasonAvailable, |
| 173 | + Target: v1beta1.Target_TARGET_COMPOSITE_AND_CLAIM.Enum(), |
| 174 | + Message: ptr.To("a test message"), |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + }, |
| 179 | + } |
| 180 | + for name, tc := range cases { |
| 181 | + t.Run(name, func(t *testing.T) { |
| 182 | + rsp := &v1beta1.RunFunctionResponse{} |
| 183 | + for _, f := range tc.args.fns { |
| 184 | + f(rsp) |
| 185 | + } |
| 186 | + |
| 187 | + if diff := cmp.Diff(tc.want.conditions, rsp.GetConditions(), protocmp.Transform()); diff != "" { |
| 188 | + t.Errorf("\n%s\nFrom(...): -want, +got:\n%s", tc.reason, diff) |
| 189 | + } |
| 190 | + |
| 191 | + }) |
| 192 | + } |
| 193 | +} |
0 commit comments