1
2
3
4 package taskrunner
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
11 "k8s.io/apimachinery/pkg/types"
12 "sigs.k8s.io/cli-utils/pkg/apply/cache"
13 ktestutil "sigs.k8s.io/cli-utils/pkg/kstatus/polling/testutil"
14 "sigs.k8s.io/cli-utils/pkg/kstatus/status"
15 "sigs.k8s.io/cli-utils/pkg/object"
16 )
17
18 var deployment1y = `
19 apiVersion: apps/v1
20 kind: Deployment
21 metadata:
22 name: Foo
23 namespace: default
24 spec:
25 replicas: 1
26 status:
27 replicas: 1
28 readyReplicas: 1
29 updatedReplicas: 1
30 availableReplicas: 1
31 conditions:
32 - status: "True"
33 type: Available
34 - status: "True"
35 type: Ready
36 `
37
38 var custom1y = `
39 apiVersion: custom.io/v1alpha1
40 kind: Custom
41 metadata:
42 name: Foo
43 namespace: default
44 spec: {}
45 status:
46 conditions:
47 - status: "False"
48 type: Ready
49 `
50
51
52 func withGeneration(obj *unstructured.Unstructured, gen int64) *unstructured.Unstructured {
53 obj = obj.DeepCopy()
54 obj.SetGeneration(gen)
55 return obj
56 }
57
58 func TestCollector_ConditionMet(t *testing.T) {
59 deployment1 := ktestutil.YamlToUnstructured(t, deployment1y)
60 deployment1Meta := object.UnstructuredToObjMetadata(deployment1)
61 custom1 := ktestutil.YamlToUnstructured(t, custom1y)
62 custom1Meta := object.UnstructuredToObjMetadata(custom1)
63
64 testCases := map[string]struct {
65 cacheContents []cache.ResourceStatus
66 appliedGen map[object.ObjMetadata]int64
67 ids object.ObjMetadataSet
68 condition Condition
69 expectedResult bool
70 }{
71 "single resource with current status": {
72 cacheContents: []cache.ResourceStatus{
73 {
74 Resource: withGeneration(deployment1, 42),
75 Status: status.CurrentStatus,
76 },
77 },
78 appliedGen: map[object.ObjMetadata]int64{
79 deployment1Meta: 42,
80 },
81 ids: object.ObjMetadataSet{
82 deployment1Meta,
83 },
84 condition: AllCurrent,
85 expectedResult: true,
86 },
87 "single resource with current status and old generation": {
88 cacheContents: []cache.ResourceStatus{
89 {
90 Resource: withGeneration(deployment1, 41),
91 Status: status.CurrentStatus,
92 },
93 },
94 appliedGen: map[object.ObjMetadata]int64{
95 deployment1Meta: 42,
96 },
97 ids: object.ObjMetadataSet{
98 deployment1Meta,
99 },
100 condition: AllCurrent,
101 expectedResult: false,
102 },
103 "multiple resources not all current": {
104 cacheContents: []cache.ResourceStatus{
105 {
106 Resource: withGeneration(deployment1, 42),
107 Status: status.InProgressStatus,
108 },
109 {
110 Resource: withGeneration(custom1, 0),
111 Status: status.CurrentStatus,
112 },
113 },
114 appliedGen: map[object.ObjMetadata]int64{
115 deployment1Meta: 42,
116 custom1Meta: 0,
117 },
118 ids: object.ObjMetadataSet{
119 deployment1Meta,
120 custom1Meta,
121 },
122 condition: AllCurrent,
123 expectedResult: false,
124 },
125 "multiple resources single with old generation": {
126 cacheContents: []cache.ResourceStatus{
127 {
128 Resource: withGeneration(deployment1, 42),
129 Status: status.CurrentStatus,
130 },
131 {
132 Resource: withGeneration(custom1, 4),
133 Status: status.CurrentStatus,
134 },
135 },
136 appliedGen: map[object.ObjMetadata]int64{
137 deployment1Meta: 42,
138 custom1Meta: 5,
139 },
140 ids: object.ObjMetadataSet{
141 deployment1Meta,
142 custom1Meta,
143 },
144 condition: AllCurrent,
145 expectedResult: false,
146 },
147 }
148
149 for tn, tc := range testCases {
150 t.Run(tn, func(t *testing.T) {
151 resourceCache := cache.NewResourceCacheMap()
152 if tc.cacheContents != nil {
153 resourceCache.Load(tc.cacheContents...)
154 }
155
156 taskContext := NewTaskContext(nil, resourceCache)
157
158 if tc.appliedGen != nil {
159 for id, gen := range tc.appliedGen {
160 taskContext.InventoryManager().AddSuccessfulApply(id, types.UID("unused"), gen)
161 }
162 }
163
164 res := conditionMet(taskContext, tc.ids, tc.condition)
165
166 assert.Equal(t, tc.expectedResult, res)
167 })
168 }
169 }
170
View as plain text