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