-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathapiext_test.go
More file actions
152 lines (144 loc) · 4.23 KB
/
Copy pathapiext_test.go
File metadata and controls
152 lines (144 loc) · 4.23 KB
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
package resourcebuilder
import (
"testing"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestCheckCRDEstablished(t *testing.T) {
tests := []struct {
name string
crd *apiextv1.CustomResourceDefinition
wantErr bool
errSubstr string
}{
{
name: "Established=True returns nil",
crd: &apiextv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"},
Status: apiextv1.CustomResourceDefinitionStatus{
Conditions: []apiextv1.CustomResourceDefinitionCondition{
{
Type: apiextv1.NamesAccepted,
Status: apiextv1.ConditionTrue,
},
{
Type: apiextv1.Established,
Status: apiextv1.ConditionTrue,
},
},
},
},
wantErr: false,
},
{
name: "Established=False returns error with reason",
crd: &apiextv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"},
Status: apiextv1.CustomResourceDefinitionStatus{
Conditions: []apiextv1.CustomResourceDefinitionCondition{
{
Type: apiextv1.Established,
Status: apiextv1.ConditionFalse,
Reason: "Installing",
Message: "not yet established",
},
},
},
},
wantErr: true,
errSubstr: "Established=False",
},
{
name: "empty conditions returns error",
crd: &apiextv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"},
Status: apiextv1.CustomResourceDefinitionStatus{
Conditions: []apiextv1.CustomResourceDefinitionCondition{},
},
},
wantErr: true,
errSubstr: "does not declare an Established status condition",
},
{
name: "nil conditions returns error",
crd: &apiextv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"},
Status: apiextv1.CustomResourceDefinitionStatus{},
},
wantErr: true,
errSubstr: "does not declare an Established status condition",
},
{
name: "NamesAccepted but no Established returns error",
crd: &apiextv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"},
Status: apiextv1.CustomResourceDefinitionStatus{
Conditions: []apiextv1.CustomResourceDefinitionCondition{
{
Type: apiextv1.NamesAccepted,
Status: apiextv1.ConditionTrue,
},
},
},
},
wantErr: true,
errSubstr: "does not declare an Established status condition",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := checkCRDEstablished(tt.crd)
if tt.wantErr {
if err == nil {
t.Error("expected error, got nil")
} else if tt.errSubstr != "" {
if !containsSubstring(err.Error(), tt.errSubstr) {
t.Errorf("error %q does not contain %q", err.Error(), tt.errSubstr)
}
}
} else {
if err != nil {
t.Errorf("expected nil error, got: %v", err)
}
}
})
}
}
func TestCheckCustomResourceDefinitionHealthInitializingMode(t *testing.T) {
b := &builder{mode: InitializingMode}
crd := &apiextv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"},
Status: apiextv1.CustomResourceDefinitionStatus{},
}
if err := b.checkCustomResourceDefinitionHealth(t.Context(), crd); err != nil {
t.Errorf("InitializingMode should skip health check, got: %v", err)
}
}
func TestCheckCustomResourceDefinitionHealthEstablished(t *testing.T) {
b := &builder{mode: ReconcilingMode}
crd := &apiextv1.CustomResourceDefinition{
ObjectMeta: metav1.ObjectMeta{Name: "test.example.io"},
Status: apiextv1.CustomResourceDefinitionStatus{
Conditions: []apiextv1.CustomResourceDefinitionCondition{
{
Type: apiextv1.Established,
Status: apiextv1.ConditionTrue,
},
},
},
}
if err := b.checkCustomResourceDefinitionHealth(t.Context(), crd); err != nil {
t.Errorf("Established=True CRD should pass health check, got: %v", err)
}
}
func containsSubstring(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsSubstringImpl(s, substr))
}
func containsSubstringImpl(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}