|
| 1 | +/* |
| 2 | +Copyright 2025 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 conditions |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/google/go-cmp/cmp" |
| 25 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 26 | + |
| 27 | + xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" |
| 28 | + "github.com/crossplane/crossplane-runtime/pkg/resource/fake" |
| 29 | + "github.com/crossplane/crossplane-runtime/pkg/test" |
| 30 | +) |
| 31 | + |
| 32 | +// Check that conditionsImpl implements ConditionManager. |
| 33 | +var _ Manager = (*ObservedGenerationPropagationManager)(nil) |
| 34 | + |
| 35 | +// Check that conditionSet implements ConditionSet. |
| 36 | +var _ ConditionSet = (*observedGenerationPropagationConditionSet)(nil) |
| 37 | + |
| 38 | +func TestOGConditionSetMark(t *testing.T) { |
| 39 | + manager := new(ObservedGenerationPropagationManager) |
| 40 | + |
| 41 | + tests := map[string]struct { |
| 42 | + reason string |
| 43 | + start []xpv1.Condition |
| 44 | + mark []xpv1.Condition |
| 45 | + want []xpv1.Condition |
| 46 | + }{ |
| 47 | + "ProvideNoConditions": { |
| 48 | + reason: "If updating a resource without conditions with no new conditions, conditions should remain empty.", |
| 49 | + start: nil, |
| 50 | + mark: nil, |
| 51 | + want: nil, |
| 52 | + }, |
| 53 | + "EmptyAppendCondition": { |
| 54 | + reason: "If starting with a resource without conditions, and we mark a condition, it should propagate to conditions with the correct generation.", |
| 55 | + start: nil, |
| 56 | + mark: []xpv1.Condition{xpv1.ReconcileSuccess()}, |
| 57 | + want: []xpv1.Condition{xpv1.ReconcileSuccess().WithObservedGeneration(42)}, |
| 58 | + }, |
| 59 | + "ExistingMarkNothing": { |
| 60 | + reason: "If the resource has a condition and we update nothing, nothing should change.", |
| 61 | + start: []xpv1.Condition{xpv1.Available().WithObservedGeneration(1)}, |
| 62 | + mark: nil, |
| 63 | + want: []xpv1.Condition{xpv1.Available().WithObservedGeneration(1)}, |
| 64 | + }, |
| 65 | + "ExistingUpdated": { |
| 66 | + reason: "If a resource starts with a condition, and we update it, we should see the observedGeneration be updated", |
| 67 | + start: []xpv1.Condition{xpv1.ReconcileSuccess().WithObservedGeneration(1)}, |
| 68 | + mark: []xpv1.Condition{xpv1.ReconcileSuccess()}, |
| 69 | + want: []xpv1.Condition{xpv1.ReconcileSuccess().WithObservedGeneration(42)}, |
| 70 | + }, |
| 71 | + "ExistingAppended": { |
| 72 | + reason: "If a resource has an existing condition and we make another condition, the new condition should merge into the conditions list.", |
| 73 | + start: []xpv1.Condition{xpv1.Available().WithObservedGeneration(1)}, |
| 74 | + mark: []xpv1.Condition{xpv1.ReconcileSuccess()}, |
| 75 | + want: []xpv1.Condition{xpv1.Available().WithObservedGeneration(1), xpv1.ReconcileSuccess().WithObservedGeneration(42)}, |
| 76 | + }, |
| 77 | + } |
| 78 | + for name, tt := range tests { |
| 79 | + t.Run(name, func(t *testing.T) { |
| 80 | + ut := newManaged(42, tt.start...) |
| 81 | + c := manager.For(ut) |
| 82 | + c.MarkConditions(tt.mark...) |
| 83 | + if diff := cmp.Diff(tt.want, ut.Conditions, test.EquateConditions(), cmpopts.EquateApproxTime(1*time.Second)); diff != "" { |
| 84 | + t.Errorf("\nReason: %s\n-want, +got:\n%s", tt.reason, diff) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + t.Run("ManageNilObject", func(t *testing.T) { |
| 90 | + c := manager.For(nil) |
| 91 | + if c == nil { |
| 92 | + t.Errorf("manager.For(nil) = %v, want non-nil", c) |
| 93 | + } |
| 94 | + // Test that Marking on a Manager that has a nil object does not end up panicking. |
| 95 | + c.MarkConditions(xpv1.ReconcileSuccess()) |
| 96 | + // Success! |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +func TestOGManagerFor(t *testing.T) { |
| 101 | + tests := map[string]struct { |
| 102 | + reason string |
| 103 | + o ObjectWithConditions |
| 104 | + want ConditionSet |
| 105 | + }{ |
| 106 | + "NilObject": { |
| 107 | + reason: "Even if an object is nil, the manager should return a non-nil ConditionSet", |
| 108 | + want: &observedGenerationPropagationConditionSet{}, |
| 109 | + }, |
| 110 | + "Object": { |
| 111 | + reason: "Object propagates into manager.", |
| 112 | + o: &fake.Managed{}, |
| 113 | + want: &observedGenerationPropagationConditionSet{ |
| 114 | + o: &fake.Managed{}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + } |
| 118 | + for name, tt := range tests { |
| 119 | + t.Run(name, func(t *testing.T) { |
| 120 | + m := &ObservedGenerationPropagationManager{} |
| 121 | + if got := m.For(tt.o); !reflect.DeepEqual(got, tt.want) { |
| 122 | + t.Errorf("\nReason: %s\nFor() = %v, want %v", tt.reason, got, tt.want) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func newManaged(generation int64, conditions ...xpv1.Condition) *fake.Managed { |
| 129 | + mg := &fake.Managed{} |
| 130 | + mg.Generation = generation |
| 131 | + mg.SetConditions(conditions...) |
| 132 | + return mg |
| 133 | +} |
0 commit comments