|
| 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 | +type configMapsGetter struct { |
| 93 | + configMap *v1.ConfigMap |
| 94 | + getError error |
| 95 | + createError error |
| 96 | +} |
| 97 | + |
| 98 | +func (c *configMapsGetter) ConfigMaps(namespace string) corev1client.ConfigMapInterface { |
| 99 | + return &configMapClient{ |
| 100 | + configMap: c.configMap, |
| 101 | + getError: c.getError, |
| 102 | + createError: c.createError, |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +type configMapClient struct { |
| 107 | + configMap *v1.ConfigMap |
| 108 | + getError error |
| 109 | + createError error |
| 110 | +} |
| 111 | + |
| 112 | +func (c *configMapClient) Create(configMap *v1.ConfigMap) (*v1.ConfigMap, error) { |
| 113 | + return c.configMap, c.createError |
| 114 | +} |
| 115 | + |
| 116 | +func (c *configMapClient) Get(name string, getOptions metav1.GetOptions) (*v1.ConfigMap, error) { |
| 117 | + if c.getError != nil { |
| 118 | + return nil, c.getError |
| 119 | + } |
| 120 | + return c.configMap, nil |
| 121 | +} |
| 122 | + |
| 123 | +func (c *configMapClient) Update(*v1.ConfigMap) (*v1.ConfigMap, error) { |
| 124 | + panic("not implemented") |
| 125 | +} |
| 126 | + |
| 127 | +func (c *configMapClient) Delete(name string, options *metav1.DeleteOptions) error { |
| 128 | + panic("not implemented") |
| 129 | +} |
| 130 | + |
| 131 | +func (c *configMapClient) DeleteCollection(options *metav1.DeleteOptions, listOptions metav1.ListOptions) error { |
| 132 | + panic("not implemented") |
| 133 | +} |
| 134 | + |
| 135 | +func (c *configMapClient) List(opts metav1.ListOptions) (*v1.ConfigMapList, error) { |
| 136 | + panic("not implemented") |
| 137 | +} |
| 138 | + |
| 139 | +func (c *configMapClient) Watch(opts metav1.ListOptions) (watch.Interface, error) { |
| 140 | + panic("not implemented") |
| 141 | +} |
| 142 | + |
| 143 | +func (c *configMapClient) Patch(name string, pt types.PatchType, data []byte, subresources ...string) (result *v1.ConfigMap, err error) { |
| 144 | + panic("not implemented") |
| 145 | +} |
0 commit comments