@@ -21,11 +21,111 @@ import (
21
21
"time"
22
22
23
23
. "github.com/onsi/gomega"
24
+ admissionregistration "k8s.io/api/admissionregistration/v1"
25
+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24
26
"k8s.io/apimachinery/pkg/util/wait"
25
27
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/config"
28
+ "sigs.k8s.io/cluster-api/cmd/clusterctl/internal/scheme"
26
29
"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
27
30
)
28
31
32
+ func Test_certManagerClient_getManifestObjects (t * testing.T ) {
33
+
34
+ tests := []struct {
35
+ name string
36
+ expectErr bool
37
+ assert func (* testing.T , []unstructured.Unstructured )
38
+ }{
39
+ {
40
+ name : "it should not contain the cert-manager-leaderelection ClusterRoleBinding" ,
41
+ expectErr : false ,
42
+ assert : func (t * testing.T , objs []unstructured.Unstructured ) {
43
+ for _ , o := range objs {
44
+ if o .GetKind () == "ClusterRoleBinding" && o .GetName () == "cert-manager-leaderelection" {
45
+ t .Error ("should not find cert-manager-leaderelection ClusterRoleBinding" )
46
+ }
47
+ }
48
+ },
49
+ },
50
+ {
51
+ name : "the MutatingWebhookConfiguration should have sideEffects set to None " ,
52
+ expectErr : false ,
53
+ assert : func (t * testing.T , objs []unstructured.Unstructured ) {
54
+ found := false
55
+ for i := range objs {
56
+ o := objs [i ]
57
+ if o .GetKind () == "MutatingWebhookConfiguration" && o .GetName () == "cert-manager-webhook" {
58
+ w := & admissionregistration.MutatingWebhookConfiguration {}
59
+ err := scheme .Scheme .Convert (& o , w , nil )
60
+ if err != nil {
61
+ t .Errorf ("did not expect err, got %s" , err )
62
+ }
63
+ if len (w .Webhooks ) != 1 {
64
+ t .Error ("expected 1 webhook to be configured" )
65
+ }
66
+ wh := w .Webhooks [0 ]
67
+ if wh .SideEffects != nil && * wh .SideEffects == admissionregistration .SideEffectClassNone {
68
+ found = true
69
+ }
70
+ }
71
+ }
72
+ if ! found {
73
+ t .Error ("Expected to find cert-manager-webhook MutatingWebhookConfiguration with sideEffects=None" )
74
+ }
75
+ },
76
+ },
77
+ {
78
+ name : "the ValidatingWebhookConfiguration should have sideEffects set to None " ,
79
+ expectErr : false ,
80
+ assert : func (t * testing.T , objs []unstructured.Unstructured ) {
81
+ found := false
82
+ for i := range objs {
83
+ o := objs [i ]
84
+ if o .GetKind () == "ValidatingWebhookConfiguration" && o .GetName () == "cert-manager-webhook" {
85
+ w := & admissionregistration.ValidatingWebhookConfiguration {}
86
+ err := scheme .Scheme .Convert (& o , w , nil )
87
+ if err != nil {
88
+ t .Errorf ("did not expect err, got %s" , err )
89
+ }
90
+ if len (w .Webhooks ) != 1 {
91
+ t .Error ("expected 1 webhook to be configured" )
92
+ }
93
+ wh := w .Webhooks [0 ]
94
+ if wh .SideEffects != nil && * wh .SideEffects == admissionregistration .SideEffectClassNone {
95
+ found = true
96
+ }
97
+ }
98
+ }
99
+ if ! found {
100
+ t .Error ("Expected to find cert-manager-webhook ValidatingWebhookConfiguration with sideEffects=None" )
101
+ }
102
+ },
103
+ },
104
+ }
105
+
106
+ for _ , tt := range tests {
107
+ t .Run (tt .name , func (t * testing.T ) {
108
+ g := NewWithT (t )
109
+
110
+ pollImmediateWaiter := func (interval , timeout time.Duration , condition wait.ConditionFunc ) error {
111
+ return nil
112
+ }
113
+ fakeConfigClient := newFakeConfig ("" )
114
+
115
+ cm := newCertMangerClient (fakeConfigClient , nil , pollImmediateWaiter )
116
+ objs , err := cm .getManifestObjs ()
117
+
118
+ if tt .expectErr {
119
+ g .Expect (err ).To (HaveOccurred ())
120
+ return
121
+ }
122
+ g .Expect (err ).ToNot (HaveOccurred ())
123
+ tt .assert (t , objs )
124
+ })
125
+ }
126
+
127
+ }
128
+
29
129
func Test_GetTimeout (t * testing.T ) {
30
130
31
131
pollImmediateWaiter := func (interval , timeout time.Duration , condition wait.ConditionFunc ) error {
0 commit comments