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