@@ -17,6 +17,7 @@ limitations under the License.
17
17
package util
18
18
19
19
import (
20
+ "context"
20
21
"testing"
21
22
22
23
. "github.com/onsi/gomega"
@@ -25,6 +26,7 @@ import (
25
26
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
26
27
"k8s.io/apimachinery/pkg/runtime"
27
28
"k8s.io/utils/pointer"
29
+ "sigs.k8s.io/controller-runtime/pkg/client"
28
30
"sigs.k8s.io/controller-runtime/pkg/client/fake"
29
31
30
32
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -34,136 +36,146 @@ import (
34
36
)
35
37
36
38
func TestGetConfigOwner (t * testing.T ) {
37
- t .Run ("should get the owner when present (Machine)" , func (t * testing.T ) {
38
- g := NewWithT (t )
39
- myMachine := & clusterv1.Machine {
40
- ObjectMeta : metav1.ObjectMeta {
41
- Name : "my-machine" ,
42
- Namespace : metav1 .NamespaceDefault ,
43
- Labels : map [string ]string {
44
- clusterv1 .MachineControlPlaneLabel : "" ,
39
+ doTests := func (t * testing.T , getFn func (context.Context , client.Client , metav1.Object ) (* ConfigOwner , error )) {
40
+ t .Helper ()
41
+
42
+ t .Run ("should get the owner when present (Machine)" , func (t * testing.T ) {
43
+ g := NewWithT (t )
44
+ myMachine := & clusterv1.Machine {
45
+ ObjectMeta : metav1.ObjectMeta {
46
+ Name : "my-machine" ,
47
+ Namespace : metav1 .NamespaceDefault ,
48
+ Labels : map [string ]string {
49
+ clusterv1 .MachineControlPlaneLabel : "" ,
50
+ },
45
51
},
46
- },
47
- Spec : clusterv1.MachineSpec {
48
- ClusterName : "my-cluster" ,
49
- Bootstrap : clusterv1.Bootstrap {
50
- DataSecretName : pointer .String ("my-data-secret" ),
52
+ Spec : clusterv1.MachineSpec {
53
+ ClusterName : "my-cluster" ,
54
+ Bootstrap : clusterv1.Bootstrap {
55
+ DataSecretName : pointer .String ("my-data-secret" ),
56
+ },
57
+ Version : pointer .String ("v1.19.6" ),
51
58
},
52
- Version : pointer .String ("v1.19.6" ),
53
- },
54
- Status : clusterv1.MachineStatus {
55
- InfrastructureReady : true ,
56
- },
57
- }
59
+ Status : clusterv1.MachineStatus {
60
+ InfrastructureReady : true ,
61
+ },
62
+ }
58
63
59
- c := fake .NewClientBuilder ().WithObjects (myMachine ).Build ()
60
- obj := & bootstrapv1.KubeadmConfig {
61
- ObjectMeta : metav1.ObjectMeta {
62
- OwnerReferences : []metav1.OwnerReference {
63
- {
64
- Kind : "Machine" ,
65
- APIVersion : clusterv1 .GroupVersion .String (),
66
- Name : "my-machine" ,
64
+ c := fake .NewClientBuilder ().WithObjects (myMachine ).Build ()
65
+ obj := & bootstrapv1.KubeadmConfig {
66
+ ObjectMeta : metav1.ObjectMeta {
67
+ OwnerReferences : []metav1.OwnerReference {
68
+ {
69
+ Kind : "Machine" ,
70
+ APIVersion : clusterv1 .GroupVersion .String (),
71
+ Name : "my-machine" ,
72
+ },
67
73
},
74
+ Namespace : metav1 .NamespaceDefault ,
75
+ Name : "my-resource-owned-by-machine" ,
68
76
},
69
- Namespace : metav1 .NamespaceDefault ,
70
- Name : "my-resource-owned-by-machine" ,
71
- },
72
- }
73
- configOwner , err := GetConfigOwner (ctx , c , obj )
74
- g .Expect (err ).NotTo (HaveOccurred ())
75
- g .Expect (configOwner ).ToNot (BeNil ())
76
- g .Expect (configOwner .ClusterName ()).To (BeEquivalentTo ("my-cluster" ))
77
- g .Expect (configOwner .IsInfrastructureReady ()).To (BeTrue ())
78
- g .Expect (configOwner .IsControlPlaneMachine ()).To (BeTrue ())
79
- g .Expect (configOwner .IsMachinePool ()).To (BeFalse ())
80
- g .Expect (configOwner .KubernetesVersion ()).To (Equal ("v1.19.6" ))
81
- g .Expect (* configOwner .DataSecretName ()).To (BeEquivalentTo ("my-data-secret" ))
82
- })
77
+ }
78
+ configOwner , err := getFn (ctx , c , obj )
79
+ g .Expect (err ).NotTo (HaveOccurred ())
80
+ g .Expect (configOwner ).ToNot (BeNil ())
81
+ g .Expect (configOwner .ClusterName ()).To (BeEquivalentTo ("my-cluster" ))
82
+ g .Expect (configOwner .IsInfrastructureReady ()).To (BeTrue ())
83
+ g .Expect (configOwner .IsControlPlaneMachine ()).To (BeTrue ())
84
+ g .Expect (configOwner .IsMachinePool ()).To (BeFalse ())
85
+ g .Expect (configOwner .KubernetesVersion ()).To (Equal ("v1.19.6" ))
86
+ g .Expect (* configOwner .DataSecretName ()).To (BeEquivalentTo ("my-data-secret" ))
87
+ })
83
88
84
- t .Run ("should get the owner when present (MachinePool)" , func (t * testing.T ) {
85
- _ = feature .MutableGates .Set ("MachinePool=true" )
89
+ t .Run ("should get the owner when present (MachinePool)" , func (t * testing.T ) {
90
+ _ = feature .MutableGates .Set ("MachinePool=true" )
86
91
87
- g := NewWithT (t )
88
- myPool := & expv1.MachinePool {
89
- ObjectMeta : metav1.ObjectMeta {
90
- Name : "my-machine-pool" ,
91
- Namespace : metav1 .NamespaceDefault ,
92
- Labels : map [string ]string {
93
- clusterv1 .MachineControlPlaneLabel : "" ,
92
+ g := NewWithT (t )
93
+ myPool := & expv1.MachinePool {
94
+ ObjectMeta : metav1.ObjectMeta {
95
+ Name : "my-machine-pool" ,
96
+ Namespace : metav1 .NamespaceDefault ,
97
+ Labels : map [string ]string {
98
+ clusterv1 .MachineControlPlaneLabel : "" ,
99
+ },
94
100
},
95
- },
96
- Spec : expv1. MachinePoolSpec {
97
- ClusterName : "my-cluster" ,
98
- Template : clusterv1.MachineTemplateSpec {
99
- Spec : clusterv1. MachineSpec {
100
- Version : pointer . String ( "v1.19.6" ) ,
101
+ Spec : expv1. MachinePoolSpec {
102
+ ClusterName : "my-cluster" ,
103
+ Template : clusterv1. MachineTemplateSpec {
104
+ Spec : clusterv1.MachineSpec {
105
+ Version : pointer . String ( "v1.19.6" ),
106
+ } ,
101
107
},
102
108
},
103
- },
104
- Status : expv1.MachinePoolStatus {
105
- InfrastructureReady : true ,
106
- },
107
- }
109
+ Status : expv1.MachinePoolStatus {
110
+ InfrastructureReady : true ,
111
+ },
112
+ }
108
113
109
- c := fake .NewClientBuilder ().WithObjects (myPool ).Build ()
110
- obj := & bootstrapv1.KubeadmConfig {
111
- ObjectMeta : metav1.ObjectMeta {
112
- OwnerReferences : []metav1.OwnerReference {
113
- {
114
- Kind : "MachinePool" ,
115
- APIVersion : expv1 .GroupVersion .String (),
116
- Name : "my-machine-pool" ,
114
+ c := fake .NewClientBuilder ().WithObjects (myPool ).Build ()
115
+ obj := & bootstrapv1.KubeadmConfig {
116
+ ObjectMeta : metav1.ObjectMeta {
117
+ OwnerReferences : []metav1.OwnerReference {
118
+ {
119
+ Kind : "MachinePool" ,
120
+ APIVersion : expv1 .GroupVersion .String (),
121
+ Name : "my-machine-pool" ,
122
+ },
117
123
},
124
+ Namespace : metav1 .NamespaceDefault ,
125
+ Name : "my-resource-owned-by-machine-pool" ,
118
126
},
119
- Namespace : metav1 .NamespaceDefault ,
120
- Name : "my-resource-owned-by-machine-pool" ,
121
- },
122
- }
123
- configOwner , err := GetConfigOwner (ctx , c , obj )
124
- g .Expect (err ).NotTo (HaveOccurred ())
125
- g .Expect (configOwner ).ToNot (BeNil ())
126
- g .Expect (configOwner .ClusterName ()).To (BeEquivalentTo ("my-cluster" ))
127
- g .Expect (configOwner .IsInfrastructureReady ()).To (BeTrue ())
128
- g .Expect (configOwner .IsControlPlaneMachine ()).To (BeFalse ())
129
- g .Expect (configOwner .IsMachinePool ()).To (BeTrue ())
130
- g .Expect (configOwner .KubernetesVersion ()).To (Equal ("v1.19.6" ))
131
- g .Expect (configOwner .DataSecretName ()).To (BeNil ())
132
- })
127
+ }
128
+ configOwner , err := getFn (ctx , c , obj )
129
+ g .Expect (err ).NotTo (HaveOccurred ())
130
+ g .Expect (configOwner ).ToNot (BeNil ())
131
+ g .Expect (configOwner .ClusterName ()).To (BeEquivalentTo ("my-cluster" ))
132
+ g .Expect (configOwner .IsInfrastructureReady ()).To (BeTrue ())
133
+ g .Expect (configOwner .IsControlPlaneMachine ()).To (BeFalse ())
134
+ g .Expect (configOwner .IsMachinePool ()).To (BeTrue ())
135
+ g .Expect (configOwner .KubernetesVersion ()).To (Equal ("v1.19.6" ))
136
+ g .Expect (configOwner .DataSecretName ()).To (BeNil ())
137
+ })
133
138
134
- t .Run ("return an error when not found" , func (t * testing.T ) {
135
- g := NewWithT (t )
136
- c := fake .NewClientBuilder ().Build ()
137
- obj := & bootstrapv1.KubeadmConfig {
138
- ObjectMeta : metav1.ObjectMeta {
139
- OwnerReferences : []metav1.OwnerReference {
140
- {
141
- Kind : "Machine" ,
142
- APIVersion : clusterv1 .GroupVersion .String (),
143
- Name : "my-machine" ,
139
+ t .Run ("return an error when not found" , func (t * testing.T ) {
140
+ g := NewWithT (t )
141
+ c := fake .NewClientBuilder ().Build ()
142
+ obj := & bootstrapv1.KubeadmConfig {
143
+ ObjectMeta : metav1.ObjectMeta {
144
+ OwnerReferences : []metav1.OwnerReference {
145
+ {
146
+ Kind : "Machine" ,
147
+ APIVersion : clusterv1 .GroupVersion .String (),
148
+ Name : "my-machine" ,
149
+ },
144
150
},
151
+ Namespace : metav1 .NamespaceDefault ,
152
+ Name : "my-resource-owned-by-machine" ,
145
153
},
146
- Namespace : metav1 .NamespaceDefault ,
147
- Name : "my-resource-owned-by-machine" ,
148
- },
149
- }
150
- _ , err := GetConfigOwner (ctx , c , obj )
151
- g .Expect (err ).To (HaveOccurred ())
152
- })
154
+ }
155
+ _ , err := getFn (ctx , c , obj )
156
+ g .Expect (err ).To (HaveOccurred ())
157
+ })
153
158
154
- t .Run ("return nothing when there is no owner" , func (t * testing.T ) {
155
- g := NewWithT (t )
156
- c := fake .NewClientBuilder ().Build ()
157
- obj := & bootstrapv1.KubeadmConfig {
158
- ObjectMeta : metav1.ObjectMeta {
159
- OwnerReferences : []metav1.OwnerReference {},
160
- Namespace : metav1 .NamespaceDefault ,
161
- Name : "my-resource-owned-by-machine" ,
162
- },
163
- }
164
- configOwner , err := GetConfigOwner (ctx , c , obj )
165
- g .Expect (err ).NotTo (HaveOccurred ())
166
- g .Expect (configOwner ).To (BeNil ())
159
+ t .Run ("return nothing when there is no owner" , func (t * testing.T ) {
160
+ g := NewWithT (t )
161
+ c := fake .NewClientBuilder ().Build ()
162
+ obj := & bootstrapv1.KubeadmConfig {
163
+ ObjectMeta : metav1.ObjectMeta {
164
+ OwnerReferences : []metav1.OwnerReference {},
165
+ Namespace : metav1 .NamespaceDefault ,
166
+ Name : "my-resource-owned-by-machine" ,
167
+ },
168
+ }
169
+ configOwner , err := getFn (ctx , c , obj )
170
+ g .Expect (err ).NotTo (HaveOccurred ())
171
+ g .Expect (configOwner ).To (BeNil ())
172
+ })
173
+ }
174
+ t .Run ("uncached" , func (t * testing.T ) {
175
+ doTests (t , GetConfigOwner )
176
+ })
177
+ t .Run ("cached" , func (t * testing.T ) {
178
+ doTests (t , GetConfigOwnerFromCache )
167
179
})
168
180
}
169
181
0 commit comments