| 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 generic |
| 18 |
|
| 19 |
import ( |
| 20 |
"errors" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"k8s.io/apimachinery/pkg/labels" |
| 24 |
"k8s.io/apimachinery/pkg/runtime" |
| 25 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 26 |
"k8s.io/apiserver/pkg/admission" |
| 27 |
"k8s.io/client-go/tools/cache" |
| 28 |
) |
| 29 |
|
| 30 |
func TestCachedHasSynced(t *testing.T) { |
| 31 |
|
| 32 |
called := 0 |
| 33 |
result := false |
| 34 |
cachedFunc := cachedHasSynced(func() bool { |
| 35 |
called++ |
| 36 |
return result |
| 37 |
}) |
| 38 |
|
| 39 |
if cachedFunc() { |
| 40 |
t.Fatal("expected false") |
| 41 |
} |
| 42 |
if called != 1 { |
| 43 |
t.Fatalf("expected called=1, got %d", called) |
| 44 |
} |
| 45 |
|
| 46 |
if cachedFunc() { |
| 47 |
t.Fatal("expected false") |
| 48 |
} |
| 49 |
if called != 2 { |
| 50 |
t.Fatalf("expected called=2, got %d", called) |
| 51 |
} |
| 52 |
|
| 53 |
result = true |
| 54 |
if !cachedFunc() { |
| 55 |
t.Fatal("expected true") |
| 56 |
} |
| 57 |
if called != 3 { |
| 58 |
t.Fatalf("expected called=3, got %d", called) |
| 59 |
} |
| 60 |
|
| 61 |
if !cachedFunc() { |
| 62 |
t.Fatal("expected true") |
| 63 |
} |
| 64 |
if called != 3 { |
| 65 |
// no more calls once we return true |
| 66 |
t.Fatalf("expected called=3, got %d", called) |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func TestProtectedLister(t *testing.T) { |
| 71 |
|
| 72 |
hasSynced := false |
| 73 |
notReadyErr := errors.New("not ready") |
| 74 |
fake := &fakeLister{} |
| 75 |
l := &protectedLister{ |
| 76 |
hasSynced: func() bool { return hasSynced }, |
| 77 |
notReadyErr: notReadyErr, |
| 78 |
delegate: fake, |
| 79 |
} |
| 80 |
if _, err := l.List(nil); err != notReadyErr { |
| 81 |
t.Fatalf("expected %v, got %v", notReadyErr, err) |
| 82 |
} |
| 83 |
if _, err := l.Get(""); err != notReadyErr { |
| 84 |
t.Fatalf("expected %v, got %v", notReadyErr, err) |
| 85 |
} |
| 86 |
if fake.called != 0 { |
| 87 |
t.Fatalf("expected called=0, got %d", fake.called) |
| 88 |
} |
| 89 |
fake.called = 0 |
| 90 |
|
| 91 |
hasSynced = true |
| 92 |
|
| 93 |
if _, err := l.List(nil); err != errFakeLister { |
| 94 |
t.Fatalf("expected %v, got %v", errFakeLister, err) |
| 95 |
} |
| 96 |
if _, err := l.Get(""); err != errFakeLister { |
| 97 |
t.Fatalf("expected %v, got %v", errFakeLister, err) |
| 98 |
} |
| 99 |
if fake.called != 2 { |
| 100 |
t.Fatalf("expected called=2, got %d", fake.called) |
| 101 |
} |
| 102 |
fake.called = 0 |
| 103 |
|
| 104 |
hasSynced = false |
| 105 |
|
| 106 |
if _, err := l.List(nil); err != notReadyErr { |
| 107 |
t.Fatalf("expected %v, got %v", notReadyErr, err) |
| 108 |
} |
| 109 |
if _, err := l.Get(""); err != notReadyErr { |
| 110 |
t.Fatalf("expected %v, got %v", notReadyErr, err) |
| 111 |
} |
| 112 |
if fake.called != 0 { |
| 113 |
t.Fatalf("expected called=2, got %d", fake.called) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
var errFakeLister = errors.New("errFakeLister") |
| 118 |
|
| 119 |
type fakeLister struct { |
| 120 |
called int |
| 121 |
} |
| 122 |
|
| 123 |
func (f *fakeLister) List(selector labels.Selector) (ret []runtime.Object, err error) { |
| 124 |
f.called++ |
| 125 |
return nil, errFakeLister |
| 126 |
} |
| 127 |
func (f *fakeLister) Get(name string) (runtime.Object, error) { |
| 128 |
f.called++ |
| 129 |
return nil, errFakeLister |
| 130 |
} |
| 131 |
func (f *fakeLister) ByNamespace(namespace string) cache.GenericNamespaceLister { |
| 132 |
panic("not implemented") |
| 133 |
} |
| 134 |
|
| 135 |
func TestObjectCountEvaluatorHandles(t *testing.T) { |
| 136 |
evaluator := objectCountEvaluator{} |
| 137 |
testCases := []struct { |
| 138 |
name string |
| 139 |
attrs admission.Attributes |
| 140 |
want bool |
| 141 |
}{ |
| 142 |
{ |
| 143 |
name: "create", |
| 144 |
attrs: admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{Group: "core", Version: "v1", Kind: "Pod"}, "", "", schema.GroupVersionResource{Group: "core", Version: "v1", Resource: "pods"}, "", admission.Create, nil, false, nil), |
| 145 |
want: true, |
| 146 |
}, |
| 147 |
{ |
| 148 |
name: "update", |
| 149 |
attrs: admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{Group: "core", Version: "v1", Kind: "Pod"}, "", "", schema.GroupVersionResource{Group: "core", Version: "v1", Resource: "pods"}, "", admission.Update, nil, false, nil), |
| 150 |
want: false, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "delete", |
| 154 |
attrs: admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{Group: "core", Version: "v1", Kind: "Pod"}, "", "", schema.GroupVersionResource{Group: "core", Version: "v1", Resource: "pods"}, "", admission.Delete, nil, false, nil), |
| 155 |
want: false, |
| 156 |
}, |
| 157 |
{ |
| 158 |
name: "connect", |
| 159 |
attrs: admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{Group: "core", Version: "v1", Kind: "Pod"}, "", "", schema.GroupVersionResource{Group: "core", Version: "v1", Resource: "pods"}, "", admission.Connect, nil, false, nil), |
| 160 |
want: false, |
| 161 |
}, |
| 162 |
{ |
| 163 |
name: "create-subresource", |
| 164 |
attrs: admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{Group: "core", Version: "v1", Kind: "Pod"}, "", "", schema.GroupVersionResource{Group: "core", Version: "v1", Resource: "pods"}, "subresource", admission.Create, nil, false, nil), |
| 165 |
want: false, |
| 166 |
}, |
| 167 |
{ |
| 168 |
name: "update-subresource", |
| 169 |
attrs: admission.NewAttributesRecord(nil, nil, schema.GroupVersionKind{Group: "core", Version: "v1", Kind: "Pod"}, "", "", schema.GroupVersionResource{Group: "core", Version: "v1", Resource: "pods"}, "subresource", admission.Update, nil, false, nil), |
| 170 |
want: false, |
| 171 |
}, |
| 172 |
} |
| 173 |
for _, tc := range testCases { |
| 174 |
t.Run(tc.name, func(t *testing.T) { |
| 175 |
actual := evaluator.Handles(tc.attrs) |
| 176 |
|
| 177 |
if tc.want != actual { |
| 178 |
t.Errorf("%s expected:\n%v\n, actual:\n%v", tc.name, tc.want, actual) |
| 179 |
} |
| 180 |
}) |
| 181 |
} |
| 182 |
} |