1
2
3
4 package status
5
6 import (
7 "testing"
8 "time"
9
10 "github.com/stretchr/testify/assert"
11 corev1 "k8s.io/api/core/v1"
12 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13 )
14
15 var pod = `
16 apiVersion: v1
17 kind: Pod
18 metadata:
19 generation: 1
20 name: test
21 namespace: qual
22 status:
23 phase: Running
24 `
25
26 var custom = `
27 apiVersion: v1beta1
28 kind: SomeCustomKind
29 metadata:
30 generation: 1
31 name: test
32 namespace: default
33 `
34
35 var timestamp = time.Now().Add(-1 * time.Minute).UTC().Format(time.RFC3339)
36
37 func addConditions(t *testing.T, u *unstructured.Unstructured, conditions []map[string]interface{}) {
38 conds := make([]interface{}, 0)
39 for _, c := range conditions {
40 conds = append(conds, c)
41 }
42 err := unstructured.SetNestedSlice(u.Object, conds, "status", "conditions")
43 if err != nil {
44 t.Fatal(err)
45 }
46 }
47
48 func TestAugmentConditions(t *testing.T) {
49 testCases := map[string]struct {
50 manifest string
51 withConditions []map[string]interface{}
52 expectedConditions []Condition
53 }{
54 "no existing conditions": {
55 manifest: pod,
56 withConditions: []map[string]interface{}{},
57 expectedConditions: []Condition{
58 {
59 Type: ConditionReconciling,
60 Status: corev1.ConditionTrue,
61 Reason: "PodRunningNotReady",
62 },
63 },
64 },
65 "has other existing conditions": {
66 manifest: pod,
67 withConditions: []map[string]interface{}{
68 {
69 "lastTransitionTime": timestamp,
70 "lastUpdateTime": timestamp,
71 "type": "Ready",
72 "status": "False",
73 "reason": "Pod has not started",
74 },
75 },
76 expectedConditions: []Condition{
77 {
78 Type: ConditionReconciling,
79 Status: corev1.ConditionTrue,
80 Reason: "PodRunningNotReady",
81 },
82 {
83 Type: "Ready",
84 Status: corev1.ConditionFalse,
85 Reason: "Pod has not started",
86 },
87 },
88 },
89 "already has condition of standard type InProgress": {
90 manifest: pod,
91 withConditions: []map[string]interface{}{
92 {
93 "lastTransitionTime": timestamp,
94 "lastUpdateTime": timestamp,
95 "type": ConditionReconciling.String(),
96 "status": "True",
97 "reason": "PodIsAbsolutelyNotReady",
98 },
99 },
100 expectedConditions: []Condition{
101 {
102 Type: ConditionReconciling,
103 Status: corev1.ConditionTrue,
104 Reason: "PodIsAbsolutelyNotReady",
105 },
106 },
107 },
108 "already has condition of standard type Failed": {
109 manifest: pod,
110 withConditions: []map[string]interface{}{
111 {
112 "lastTransitionTime": timestamp,
113 "lastUpdateTime": timestamp,
114 "type": ConditionStalled.String(),
115 "status": "True",
116 "reason": "PodHasFailed",
117 },
118 },
119 expectedConditions: []Condition{
120 {
121 Type: ConditionStalled,
122 Status: corev1.ConditionTrue,
123 Reason: "PodHasFailed",
124 },
125 },
126 },
127 "custom resource with no conditions": {
128 manifest: custom,
129 withConditions: []map[string]interface{}{},
130 expectedConditions: []Condition{},
131 },
132 }
133
134 for tn, tc := range testCases {
135 tc := tc
136 t.Run(tn, func(t *testing.T) {
137 u := y2u(t, tc.manifest)
138 addConditions(t, u, tc.withConditions)
139
140 err := Augment(u)
141 if err != nil {
142 t.Error(err)
143 }
144
145 obj, err := GetObjectWithConditions(u.Object)
146 if err != nil {
147 t.Error(err)
148 }
149
150 assert.Equal(t, len(tc.expectedConditions), len(obj.Status.Conditions))
151
152 for _, expectedCondition := range tc.expectedConditions {
153 found := false
154 for _, condition := range obj.Status.Conditions {
155 if expectedCondition.Type.String() != condition.Type {
156 continue
157 }
158 found = true
159 assert.Equal(t, expectedCondition.Type.String(), condition.Type)
160 assert.Equal(t, expectedCondition.Reason, condition.Reason)
161 }
162 assert.True(t, found)
163 }
164 })
165 }
166 }
167
View as plain text