|
| 1 | +/* |
| 2 | +Copyright 2019 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/pkg/errors" |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + "k8s.io/apimachinery/pkg/watch" |
| 29 | + corev1client "k8s.io/client-go/kubernetes/typed/core/v1" |
| 30 | + clusterv2 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha2" |
| 31 | + "sigs.k8s.io/controller-runtime/pkg/runtime/log" |
| 32 | +) |
| 33 | + |
| 34 | +func TestControlPlaneInitLockerAcquire(t *testing.T) { |
| 35 | + tests := []struct { |
| 36 | + name string |
| 37 | + configMap *v1.ConfigMap |
| 38 | + getError error |
| 39 | + createError error |
| 40 | + expectAcquire bool |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "configmap already exists", |
| 44 | + configMap: &v1.ConfigMap{}, |
| 45 | + expectAcquire: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "error getting configmap", |
| 49 | + getError: errors.New("get error"), |
| 50 | + expectAcquire: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "create succeeds", |
| 54 | + getError: apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "configmaps"}, "uid1-configmap"), |
| 55 | + expectAcquire: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "create fails", |
| 59 | + getError: apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "configmaps"}, "uid1-configmap"), |
| 60 | + createError: errors.New("create error"), |
| 61 | + expectAcquire: false, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + for _, tc := range tests { |
| 66 | + t.Run(tc.name, func(t *testing.T) { |
| 67 | + l := &controlPlaneInitLocker{ |
| 68 | + log: log.ZapLogger(true), |
| 69 | + configMapClient: &configMapsGetter{ |
| 70 | + configMap: tc.configMap, |
| 71 | + getError: tc.getError, |
| 72 | + createError: tc.createError, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + cluster := &clusterv2.Cluster{ |
| 77 | + ObjectMeta: metav1.ObjectMeta{ |
| 78 | + Namespace: "ns1", |
| 79 | + Name: "name1", |
| 80 | + UID: types.UID("uid1"), |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + acquired := l.Acquire(cluster) |
| 85 | + if tc.expectAcquire != acquired { |
| 86 | + t.Errorf("expected %t, got %t", tc.expectAcquire, acquired) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestControlPlaneInitLockerRelease(t *testing.T) { |
| 93 | + tests := []struct { |
| 94 | + name string |
| 95 | + configMap *v1.ConfigMap |
| 96 | + getError error |
| 97 | + deleteError error |
| 98 | + expectRelease bool |
| 99 | + }{ |
| 100 | + { |
| 101 | + name: "error getting configmap", |
| 102 | + getError: errors.New("get error"), |
| 103 | + expectRelease: false, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "configmap not found", |
| 107 | + getError: apierrors.NewNotFound(schema.GroupResource{Group: "", Resource: "configmaps"}, "uid1-configmap"), |
| 108 | + expectRelease: true, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "delete succeeds", |
| 112 | + expectRelease: true, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "delete fails", |
| 116 | + deleteError: errors.New("delete error"), |
| 117 | + expectRelease: false, |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + for _, tc := range tests { |
| 122 | + t.Run(tc.name, func(t *testing.T) { |
| 123 | + l := &controlPlaneInitLocker{ |
| 124 | + log: log.ZapLogger(true), |
| 125 | + configMapClient: &configMapsGetter{ |
| 126 | + configMap: tc.configMap, |
| 127 | + getError: tc.getError, |
| 128 | + deleteError: tc.deleteError, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + cluster := &clusterv2.Cluster{ |
| 133 | + ObjectMeta: metav1.ObjectMeta{ |
| 134 | + Namespace: "ns1", |
| 135 | + Name: "name1", |
| 136 | + UID: types.UID("uid1"), |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + released := l.Release(cluster) |
| 141 | + if tc.expectRelease != released { |
| 142 | + t.Errorf("expected %t, got %t", tc.expectRelease, released) |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +type configMapsGetter struct { |
| 149 | + configMap *v1.ConfigMap |
| 150 | + getError error |
| 151 | + createError error |
| 152 | + deleteError error |
| 153 | +} |
| 154 | + |
| 155 | +func (c *configMapsGetter) ConfigMaps(namespace string) corev1client.ConfigMapInterface { |
| 156 | + return &configMapClient{ |
| 157 | + configMap: c.configMap, |
| 158 | + getError: c.getError, |
| 159 | + createError: c.createError, |
| 160 | + deleteError: c.deleteError, |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +type configMapClient struct { |
| 165 | + configMap *v1.ConfigMap |
| 166 | + getError error |
| 167 | + createError error |
| 168 | + deleteError error |
| 169 | +} |
| 170 | + |
| 171 | +func (c *configMapClient) Create(configMap *v1.ConfigMap) (*v1.ConfigMap, error) { |
| 172 | + return c.configMap, c.createError |
| 173 | +} |
| 174 | + |
| 175 | +func (c *configMapClient) Get(name string, getOptions metav1.GetOptions) (*v1.ConfigMap, error) { |
| 176 | + if c.getError != nil { |
| 177 | + return nil, c.getError |
| 178 | + } |
| 179 | + return c.configMap, nil |
| 180 | +} |
| 181 | + |
| 182 | +func (c *configMapClient) Update(*v1.ConfigMap) (*v1.ConfigMap, error) { |
| 183 | + panic("not implemented") |
| 184 | +} |
| 185 | + |
| 186 | +func (c *configMapClient) Delete(name string, options *metav1.DeleteOptions) error { |
| 187 | + return c.deleteError |
| 188 | +} |
| 189 | + |
| 190 | +func (c *configMapClient) DeleteCollection(options *metav1.DeleteOptions, listOptions metav1.ListOptions) error { |
| 191 | + panic("not implemented") |
| 192 | +} |
| 193 | + |
| 194 | +func (c *configMapClient) List(opts metav1.ListOptions) (*v1.ConfigMapList, error) { |
| 195 | + panic("not implemented") |
| 196 | +} |
| 197 | + |
| 198 | +func (c *configMapClient) Watch(opts metav1.ListOptions) (watch.Interface, error) { |
| 199 | + panic("not implemented") |
| 200 | +} |
| 201 | + |
| 202 | +func (c *configMapClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1.ConfigMap, err error) { |
| 203 | + panic("not implemented") |
| 204 | +} |
0 commit comments