|  | 
|  | 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | 
|  | 2 | +// | 
|  | 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may | 
|  | 4 | +// not use this file except in compliance with the License. A copy of the | 
|  | 5 | +// License is located at | 
|  | 6 | +// | 
|  | 7 | +//     http://aws.amazon.com/apache2.0/ | 
|  | 8 | +// | 
|  | 9 | +// or in the "license" file accompanying this file. This file is distributed | 
|  | 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | 
|  | 11 | +// express or implied. See the License for the specific language governing | 
|  | 12 | +// permissions and limitations under the License. | 
|  | 13 | + | 
|  | 14 | +package condition_test | 
|  | 15 | + | 
|  | 16 | +import ( | 
|  | 17 | +	"testing" | 
|  | 18 | + | 
|  | 19 | +	"github.com/stretchr/testify/assert" | 
|  | 20 | +	"github.com/stretchr/testify/mock" | 
|  | 21 | + | 
|  | 22 | +	ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1" | 
|  | 23 | +	ackcond "github.com/aws-controllers-k8s/runtime/pkg/condition" | 
|  | 24 | +	corev1 "k8s.io/api/core/v1" | 
|  | 25 | + | 
|  | 26 | +	ackmocks "github.com/aws-controllers-k8s/runtime/mocks/pkg/types" | 
|  | 27 | +) | 
|  | 28 | + | 
|  | 29 | +func TestConditionGetters(t *testing.T) { | 
|  | 30 | +	assert := assert.New(t) | 
|  | 31 | + | 
|  | 32 | +	conds := []*ackv1alpha1.Condition{} | 
|  | 33 | + | 
|  | 34 | +	r := &ackmocks.AWSResource{} | 
|  | 35 | +	r.On("Conditions").Return(conds) | 
|  | 36 | + | 
|  | 37 | +	got := ackcond.Synced(r) | 
|  | 38 | +	assert.Nil(got) | 
|  | 39 | + | 
|  | 40 | +	got = ackcond.Terminal(r) | 
|  | 41 | +	assert.Nil(got) | 
|  | 42 | + | 
|  | 43 | +	conds = append(conds, &ackv1alpha1.Condition{ | 
|  | 44 | +		Type:   ackv1alpha1.ConditionTypeResourceSynced, | 
|  | 45 | +		Status: corev1.ConditionFalse, | 
|  | 46 | +	}) | 
|  | 47 | + | 
|  | 48 | +	r = &ackmocks.AWSResource{} | 
|  | 49 | +	r.On("Conditions").Return(conds) | 
|  | 50 | + | 
|  | 51 | +	got = ackcond.Synced(r) | 
|  | 52 | +	assert.NotNil(got) | 
|  | 53 | + | 
|  | 54 | +	got = ackcond.Terminal(r) | 
|  | 55 | +	assert.Nil(got) | 
|  | 56 | + | 
|  | 57 | +	conds = append(conds, &ackv1alpha1.Condition{ | 
|  | 58 | +		Type:   ackv1alpha1.ConditionTypeTerminal, | 
|  | 59 | +		Status: corev1.ConditionFalse, | 
|  | 60 | +	}) | 
|  | 61 | + | 
|  | 62 | +	r = &ackmocks.AWSResource{} | 
|  | 63 | +	r.On("Conditions").Return(conds) | 
|  | 64 | + | 
|  | 65 | +	got = ackcond.Synced(r) | 
|  | 66 | +	assert.NotNil(got) | 
|  | 67 | + | 
|  | 68 | +	got = ackcond.Terminal(r) | 
|  | 69 | +	assert.NotNil(got) | 
|  | 70 | + | 
|  | 71 | +	gotAll := ackcond.AllOfType(r, ackv1alpha1.ConditionTypeAdvisory) | 
|  | 72 | +	assert.Empty(gotAll) | 
|  | 73 | + | 
|  | 74 | +	msg1 := "advice 1" | 
|  | 75 | +	conds = append(conds, &ackv1alpha1.Condition{ | 
|  | 76 | +		Type:    ackv1alpha1.ConditionTypeAdvisory, | 
|  | 77 | +		Status:  corev1.ConditionTrue, | 
|  | 78 | +		Message: &msg1, | 
|  | 79 | +	}) | 
|  | 80 | + | 
|  | 81 | +	msg2 := "advice 2" | 
|  | 82 | +	conds = append(conds, &ackv1alpha1.Condition{ | 
|  | 83 | +		Type:    ackv1alpha1.ConditionTypeAdvisory, | 
|  | 84 | +		Status:  corev1.ConditionTrue, | 
|  | 85 | +		Message: &msg2, | 
|  | 86 | +	}) | 
|  | 87 | + | 
|  | 88 | +	r = &ackmocks.AWSResource{} | 
|  | 89 | +	r.On("Conditions").Return(conds) | 
|  | 90 | + | 
|  | 91 | +	gotAll = ackcond.AllOfType(r, ackv1alpha1.ConditionTypeAdvisory) | 
|  | 92 | +	assert.NotEmpty(gotAll) | 
|  | 93 | +	assert.Equal(len(gotAll), 2) | 
|  | 94 | +} | 
|  | 95 | + | 
|  | 96 | +func TestConditionSetters(t *testing.T) { | 
|  | 97 | +	r := &ackmocks.AWSResource{} | 
|  | 98 | +	r.On("Conditions").Return([]*ackv1alpha1.Condition{}) | 
|  | 99 | + | 
|  | 100 | +	// Ensure that if there is no synced condition, it gets added... | 
|  | 101 | +	r.On( | 
|  | 102 | +		"ReplaceConditions", | 
|  | 103 | +		mock.MatchedBy(func(subject []*ackv1alpha1.Condition) bool { | 
|  | 104 | +			if len(subject) != 1 { | 
|  | 105 | +				return false | 
|  | 106 | +			} | 
|  | 107 | +			// We need to ignore timestamps for LastTransitionTime in our argument | 
|  | 108 | +			// assertions... | 
|  | 109 | +			return (subject[0].Type == ackv1alpha1.ConditionTypeResourceSynced && | 
|  | 110 | +				subject[0].Status == corev1.ConditionTrue && | 
|  | 111 | +				subject[0].Message == nil && | 
|  | 112 | +				subject[0].Reason == nil) | 
|  | 113 | +		}), | 
|  | 114 | +	) | 
|  | 115 | + | 
|  | 116 | +	ackcond.SetSynced(r, corev1.ConditionTrue, nil, nil) | 
|  | 117 | + | 
|  | 118 | +	// Ensure that SetSynced doesn't overwrite any other conditions... | 
|  | 119 | +	r = &ackmocks.AWSResource{} | 
|  | 120 | +	r.On("Conditions").Return( | 
|  | 121 | +		[]*ackv1alpha1.Condition{ | 
|  | 122 | +			&ackv1alpha1.Condition{ | 
|  | 123 | +				Type:   ackv1alpha1.ConditionTypeTerminal, | 
|  | 124 | +				Status: corev1.ConditionTrue, | 
|  | 125 | +			}, | 
|  | 126 | +		}, | 
|  | 127 | +	) | 
|  | 128 | +	r.On( | 
|  | 129 | +		"ReplaceConditions", | 
|  | 130 | +		mock.MatchedBy(func(subject []*ackv1alpha1.Condition) bool { | 
|  | 131 | +			if len(subject) != 2 { | 
|  | 132 | +				return false | 
|  | 133 | +			} | 
|  | 134 | +			return (subject[0].Type == ackv1alpha1.ConditionTypeTerminal && | 
|  | 135 | +				subject[0].Status == corev1.ConditionTrue && | 
|  | 136 | +				subject[1].Type == ackv1alpha1.ConditionTypeResourceSynced && | 
|  | 137 | +				subject[1].Status == corev1.ConditionFalse) | 
|  | 138 | +		}), | 
|  | 139 | +	) | 
|  | 140 | + | 
|  | 141 | +	ackcond.SetSynced(r, corev1.ConditionFalse, nil, nil) | 
|  | 142 | + | 
|  | 143 | +	// Ensure that SetSynced overwrites an existing synced condition... | 
|  | 144 | +	r = &ackmocks.AWSResource{} | 
|  | 145 | +	r.On("Conditions").Return( | 
|  | 146 | +		[]*ackv1alpha1.Condition{ | 
|  | 147 | +			&ackv1alpha1.Condition{ | 
|  | 148 | +				Type:   ackv1alpha1.ConditionTypeResourceSynced, | 
|  | 149 | +				Status: corev1.ConditionFalse, | 
|  | 150 | +			}, | 
|  | 151 | +		}, | 
|  | 152 | +	) | 
|  | 153 | +	r.On( | 
|  | 154 | +		"ReplaceConditions", | 
|  | 155 | +		mock.MatchedBy(func(subject []*ackv1alpha1.Condition) bool { | 
|  | 156 | +			if len(subject) != 1 { | 
|  | 157 | +				return false | 
|  | 158 | +			} | 
|  | 159 | +			return (subject[0].Type == ackv1alpha1.ConditionTypeResourceSynced && | 
|  | 160 | +				subject[0].Status == corev1.ConditionTrue) | 
|  | 161 | +		}), | 
|  | 162 | +	) | 
|  | 163 | + | 
|  | 164 | +	ackcond.SetSynced(r, corev1.ConditionTrue, nil, nil) | 
|  | 165 | + | 
|  | 166 | +	msg1 := "message 1" | 
|  | 167 | +	reason1 := "reason 1" | 
|  | 168 | + | 
|  | 169 | +	// Ensure that if there is no terminal condition, it gets added... | 
|  | 170 | +	r = &ackmocks.AWSResource{} | 
|  | 171 | +	r.On("Conditions").Return([]*ackv1alpha1.Condition{}) | 
|  | 172 | +	r.On( | 
|  | 173 | +		"ReplaceConditions", | 
|  | 174 | +		mock.MatchedBy(func(subject []*ackv1alpha1.Condition) bool { | 
|  | 175 | +			if len(subject) != 1 { | 
|  | 176 | +				return false | 
|  | 177 | +			} | 
|  | 178 | +			// We need to ignore timestamps for LastTransitionTime in our argument | 
|  | 179 | +			// assertions... | 
|  | 180 | +			return (subject[0].Type == ackv1alpha1.ConditionTypeTerminal && | 
|  | 181 | +				subject[0].Status == corev1.ConditionTrue && | 
|  | 182 | +				subject[0].Message == &msg1 && | 
|  | 183 | +				subject[0].Reason == &reason1) | 
|  | 184 | +		}), | 
|  | 185 | +	) | 
|  | 186 | + | 
|  | 187 | +	ackcond.SetTerminal(r, corev1.ConditionTrue, &msg1, &reason1) | 
|  | 188 | +} | 
0 commit comments