@@ -17,10 +17,7 @@ limitations under the License.
17
17
package controllers
18
18
19
19
import (
20
- "context"
21
- "errors"
22
20
"fmt"
23
- "reflect"
24
21
"testing"
25
22
"time"
26
23
@@ -30,50 +27,22 @@ import (
30
27
kubeadmv1alpha2 "sigs.k8s.io/cluster-api-bootstrap-provider-kubeadm/api/v1alpha2"
31
28
"sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2"
32
29
ctrl "sigs.k8s.io/controller-runtime"
33
- "sigs.k8s.io/controller-runtime/pkg/client"
30
+ "sigs.k8s.io/controller-runtime/pkg/client/fake "
34
31
"sigs.k8s.io/controller-runtime/pkg/runtime/log"
35
32
)
36
33
37
- type myClient struct {
38
- db map [string ]runtime.Object
39
- }
40
-
41
- // Very basic implementation of copying pointers of interfaces around, stolen from controller runtime
42
- func (c * myClient ) Get (ctx context.Context , key client.ObjectKey , out runtime.Object ) error {
43
- obj , ok := c .db [key .String ()]
44
- if ! ok {
45
- return errors .New ("object not found" )
46
- }
47
- obj = obj .(runtime.Object ).DeepCopyObject ()
48
- outVal := reflect .ValueOf (out )
49
- objVal := reflect .ValueOf (obj )
50
- reflect .Indirect (outVal ).Set (reflect .Indirect (objVal ))
51
- return nil
52
- }
53
- func (c * myClient ) List (ctx context.Context , list runtime.Object , opts ... client.ListOptionFunc ) error {
54
- return nil
55
- }
56
- func (c * myClient ) Create (ctx context.Context , obj runtime.Object , opts ... client.CreateOptionFunc ) error {
57
- return nil
58
- }
59
- func (c * myClient ) Delete (ctx context.Context , obj runtime.Object , opts ... client.DeleteOptionFunc ) error {
60
- return nil
61
- }
62
- func (c * myClient ) Update (ctx context.Context , obj runtime.Object , opts ... client.UpdateOptionFunc ) error {
63
- return nil
64
- }
65
- func (c * myClient ) Patch (ctx context.Context , obj runtime.Object , patch client.Patch , opts ... client.PatchOptionFunc ) error {
66
- return nil
67
- }
68
- func (c * myClient ) Status () client.StatusWriter {
69
- return c
34
+ func setupScheme () * runtime.Scheme {
35
+ scheme := runtime .NewScheme ()
36
+ v1alpha2 .AddToScheme (scheme )
37
+ kubeadmv1alpha2 .AddToScheme (scheme )
38
+ return scheme
70
39
}
71
40
72
41
func TestSuccessfulReconcileShouldNotRequeue (t * testing.T ) {
73
- objects := map [ string ]runtime.Object {
74
- "ns/cfg" : & kubeadmv1alpha2.KubeadmConfig {
42
+ objects := [ ]runtime.Object {
43
+ & kubeadmv1alpha2.KubeadmConfig {
75
44
ObjectMeta : metav1.ObjectMeta {
76
- Namespace : "ns " ,
45
+ Namespace : "default " ,
77
46
Name : "cfg" ,
78
47
OwnerReferences : []metav1.OwnerReference {
79
48
{
@@ -84,7 +53,7 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
84
53
},
85
54
},
86
55
},
87
- "ns/my-machine" : & v1alpha2.Machine {
56
+ & v1alpha2.Machine {
88
57
ObjectMeta : metav1.ObjectMeta {
89
58
Namespace : "default" ,
90
59
Name : "my-machine" ,
@@ -93,8 +62,13 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
93
62
},
94
63
},
95
64
},
96
- "ns/my-cluster" : & v1alpha2.Cluster {
65
+ & v1alpha2.Cluster {
66
+ ObjectMeta : metav1.ObjectMeta {
67
+ Namespace : "default" ,
68
+ Name : "my-cluster" ,
69
+ },
97
70
Status : v1alpha2.ClusterStatus {
71
+ InfrastructureReady : true ,
98
72
APIEndpoints : []v1alpha2.APIEndpoint {
99
73
{
100
74
Host : "example.com" ,
@@ -104,9 +78,7 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
104
78
},
105
79
},
106
80
}
107
- myclient := & myClient {
108
- db : objects ,
109
- }
81
+ myclient := fake .NewFakeClientWithScheme (setupScheme (), objects ... )
110
82
111
83
k := & KubeadmConfigReconciler {
112
84
Log : log .ZapLogger (true ),
@@ -115,7 +87,7 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
115
87
116
88
request := ctrl.Request {
117
89
NamespacedName : types.NamespacedName {
118
- Namespace : "ns " ,
90
+ Namespace : "default " ,
119
91
Name : "cfg" ,
120
92
},
121
93
}
@@ -132,19 +104,18 @@ func TestSuccessfulReconcileShouldNotRequeue(t *testing.T) {
132
104
}
133
105
134
106
func TestNoErrorIfNoMachineRefIsFound (t * testing.T ) {
135
- myclient := & myClient {
136
- db : map [string ]runtime.Object {
137
- "ns/cfg" : & kubeadmv1alpha2.KubeadmConfig {
138
- ObjectMeta : metav1.ObjectMeta {
139
- OwnerReferences : []metav1.OwnerReference {
140
- {
141
- Kind : "some non machine kind" ,
142
- },
107
+ objects := []runtime.Object {
108
+ & kubeadmv1alpha2.KubeadmConfig {
109
+ ObjectMeta : metav1.ObjectMeta {
110
+ OwnerReferences : []metav1.OwnerReference {
111
+ {
112
+ Kind : "some non machine kind" ,
143
113
},
144
114
},
145
115
},
146
116
},
147
117
}
118
+ myclient := fake .NewFakeClientWithScheme (setupScheme (), objects ... )
148
119
149
120
k := & KubeadmConfigReconciler {
150
121
Log : log .ZapLogger (true ),
0 commit comments