| 1 |
/* |
| 2 |
Copyright 2024 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_test |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/stretchr/testify/require" |
| 24 |
|
| 25 |
"k8s.io/api/admissionregistration/v1" |
| 26 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 |
"k8s.io/apimachinery/pkg/runtime" |
| 28 |
"k8s.io/apimachinery/pkg/types" |
| 29 |
"k8s.io/apiserver/pkg/admission" |
| 30 |
"k8s.io/apiserver/pkg/admission/plugin/policy/generic" |
| 31 |
"k8s.io/apiserver/pkg/admission/plugin/policy/matching" |
| 32 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 33 |
"k8s.io/client-go/kubernetes" |
| 34 |
"k8s.io/client-go/tools/cache" |
| 35 |
) |
| 36 |
|
| 37 |
type fakeDispatcher struct{} |
| 38 |
|
| 39 |
func (fd *fakeDispatcher) Dispatch(context.Context, admission.Attributes, admission.ObjectInterfaces, []generic.PolicyHook[*FakePolicy, *FakeBinding, generic.Evaluator]) error { |
| 40 |
return nil |
| 41 |
} |
| 42 |
func (fd *fakeDispatcher) Start(context.Context) error { |
| 43 |
return nil |
| 44 |
} |
| 45 |
|
| 46 |
func makeTestDispatcher(authorizer.Authorizer, *matching.Matcher, kubernetes.Interface) generic.Dispatcher[generic.PolicyHook[*FakePolicy, *FakeBinding, generic.Evaluator]] { |
| 47 |
return &fakeDispatcher{} |
| 48 |
} |
| 49 |
|
| 50 |
func TestPolicySourceHasSyncedEmpty(t *testing.T) { |
| 51 |
testContext, testCancel, err := generic.NewPolicyTestContext( |
| 52 |
func(fp *FakePolicy) generic.PolicyAccessor { return fp }, |
| 53 |
func(fb *FakeBinding) generic.BindingAccessor { return fb }, |
| 54 |
func(fp *FakePolicy) generic.Evaluator { return nil }, |
| 55 |
makeTestDispatcher, |
| 56 |
nil, |
| 57 |
nil, |
| 58 |
) |
| 59 |
require.NoError(t, err) |
| 60 |
defer testCancel() |
| 61 |
require.NoError(t, testContext.Start()) |
| 62 |
|
| 63 |
// Should be able to wait for cache sync |
| 64 |
require.True(t, cache.WaitForCacheSync(testContext.Done(), testContext.Source.HasSynced), "cache should sync after informer running") |
| 65 |
} |
| 66 |
|
| 67 |
func TestPolicySourceHasSyncedInitialList(t *testing.T) { |
| 68 |
// Create a list of fake policies |
| 69 |
initialObjects := []runtime.Object{ |
| 70 |
&FakePolicy{ |
| 71 |
ObjectMeta: metav1.ObjectMeta{ |
| 72 |
Name: "policy1", |
| 73 |
}, |
| 74 |
}, |
| 75 |
&FakeBinding{ |
| 76 |
ObjectMeta: metav1.ObjectMeta{ |
| 77 |
Name: "binding1", |
| 78 |
}, |
| 79 |
PolicyName: "policy1", |
| 80 |
}, |
| 81 |
} |
| 82 |
|
| 83 |
testContext, testCancel, err := generic.NewPolicyTestContext( |
| 84 |
func(fp *FakePolicy) generic.PolicyAccessor { return fp }, |
| 85 |
func(fb *FakeBinding) generic.BindingAccessor { return fb }, |
| 86 |
func(fp *FakePolicy) generic.Evaluator { return nil }, |
| 87 |
makeTestDispatcher, |
| 88 |
initialObjects, |
| 89 |
nil, |
| 90 |
) |
| 91 |
require.NoError(t, err) |
| 92 |
defer testCancel() |
| 93 |
require.NoError(t, testContext.Start()) |
| 94 |
// Should be able to wait for cache sync |
| 95 |
require.Len(t, testContext.Source.Hooks(), 1, "should have one policy") |
| 96 |
require.NoError(t, testContext.UpdateAndWait( |
| 97 |
&FakePolicy{ |
| 98 |
ObjectMeta: metav1.ObjectMeta{ |
| 99 |
Name: "policy2", |
| 100 |
}, |
| 101 |
}, |
| 102 |
&FakeBinding{ |
| 103 |
ObjectMeta: metav1.ObjectMeta{ |
| 104 |
Name: "binding2", |
| 105 |
}, |
| 106 |
PolicyName: "policy2", |
| 107 |
}, |
| 108 |
)) |
| 109 |
require.Len(t, testContext.Source.Hooks(), 2, "should have two policies") |
| 110 |
require.NoError(t, testContext.UpdateAndWait( |
| 111 |
&FakePolicy{ |
| 112 |
ObjectMeta: metav1.ObjectMeta{ |
| 113 |
Name: "policy3", |
| 114 |
}, |
| 115 |
}, |
| 116 |
&FakeBinding{ |
| 117 |
ObjectMeta: metav1.ObjectMeta{ |
| 118 |
Name: "binding3", |
| 119 |
}, |
| 120 |
PolicyName: "policy3", |
| 121 |
}, |
| 122 |
&FakePolicy{ |
| 123 |
ObjectMeta: metav1.ObjectMeta{ |
| 124 |
Name: "policy2", |
| 125 |
}, |
| 126 |
ParamKind: &v1.ParamKind{ |
| 127 |
APIVersion: "policy.example.com/v1", |
| 128 |
Kind: "FakeParam", |
| 129 |
}, |
| 130 |
}, |
| 131 |
)) |
| 132 |
require.Len(t, testContext.Source.Hooks(), 3, "should have 3 policies") |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
func TestPolicySourceBindsToPolicies(t *testing.T) { |
| 137 |
// Create a list of fake policies |
| 138 |
initialObjects := []runtime.Object{ |
| 139 |
&FakePolicy{ |
| 140 |
ObjectMeta: metav1.ObjectMeta{ |
| 141 |
Name: "policy1", |
| 142 |
}, |
| 143 |
}, |
| 144 |
&FakeBinding{ |
| 145 |
ObjectMeta: metav1.ObjectMeta{ |
| 146 |
Name: "binding1", |
| 147 |
}, |
| 148 |
PolicyName: "policy1", |
| 149 |
}, |
| 150 |
} |
| 151 |
|
| 152 |
testContext, testCancel, err := generic.NewPolicyTestContext( |
| 153 |
func(fp *FakePolicy) generic.PolicyAccessor { return fp }, |
| 154 |
func(fb *FakeBinding) generic.BindingAccessor { return fb }, |
| 155 |
func(fp *FakePolicy) generic.Evaluator { return nil }, |
| 156 |
makeTestDispatcher, |
| 157 |
initialObjects, |
| 158 |
nil, |
| 159 |
) |
| 160 |
require.NoError(t, err) |
| 161 |
defer testCancel() |
| 162 |
require.NoError(t, testContext.Start()) |
| 163 |
|
| 164 |
require.Len(t, testContext.Source.Hooks(), 1, "should have one policy") |
| 165 |
require.Len(t, testContext.Source.Hooks()[0].Bindings, 1, "should have one binding") |
| 166 |
require.Equal(t, "binding1", testContext.Source.Hooks()[0].Bindings[0].GetName(), "should have one binding") |
| 167 |
|
| 168 |
// Change the binding to another policy (policies without bindings should |
| 169 |
// be ignored, so it should remove the first |
| 170 |
require.NoError(t, testContext.UpdateAndWait( |
| 171 |
&FakePolicy{ |
| 172 |
ObjectMeta: metav1.ObjectMeta{ |
| 173 |
Name: "policy2", |
| 174 |
}, |
| 175 |
}, |
| 176 |
&FakeBinding{ |
| 177 |
ObjectMeta: metav1.ObjectMeta{ |
| 178 |
Name: "binding1", |
| 179 |
}, |
| 180 |
PolicyName: "policy2", |
| 181 |
})) |
| 182 |
require.Len(t, testContext.Source.Hooks(), 1, "should have one policy") |
| 183 |
require.Equal(t, "policy2", testContext.Source.Hooks()[0].Policy.GetName(), "policy name should be policy2") |
| 184 |
require.Len(t, testContext.Source.Hooks()[0].Bindings, 1, "should have one binding") |
| 185 |
require.Equal(t, "binding1", testContext.Source.Hooks()[0].Bindings[0].GetName(), "binding name should be binding1") |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
type FakePolicy struct { |
| 190 |
metav1.TypeMeta |
| 191 |
metav1.ObjectMeta |
| 192 |
|
| 193 |
ParamKind *v1.ParamKind |
| 194 |
} |
| 195 |
|
| 196 |
var _ generic.PolicyAccessor = &FakePolicy{} |
| 197 |
|
| 198 |
type FakeBinding struct { |
| 199 |
metav1.TypeMeta |
| 200 |
metav1.ObjectMeta |
| 201 |
|
| 202 |
PolicyName string |
| 203 |
} |
| 204 |
|
| 205 |
var _ generic.BindingAccessor = &FakeBinding{} |
| 206 |
|
| 207 |
func (fp *FakePolicy) GetName() string { |
| 208 |
return fp.Name |
| 209 |
} |
| 210 |
|
| 211 |
func (fp *FakePolicy) GetNamespace() string { |
| 212 |
return fp.Namespace |
| 213 |
} |
| 214 |
|
| 215 |
func (fp *FakePolicy) GetParamKind() *v1.ParamKind { |
| 216 |
return fp.ParamKind |
| 217 |
} |
| 218 |
|
| 219 |
func (fb *FakePolicy) GetMatchConstraints() *v1.MatchResources { |
| 220 |
return nil |
| 221 |
} |
| 222 |
|
| 223 |
func (fb *FakePolicy) GetFailurePolicy() *v1.FailurePolicyType { |
| 224 |
return nil |
| 225 |
} |
| 226 |
|
| 227 |
func (fb *FakeBinding) GetName() string { |
| 228 |
return fb.Name |
| 229 |
} |
| 230 |
|
| 231 |
func (fb *FakeBinding) GetNamespace() string { |
| 232 |
return fb.Namespace |
| 233 |
} |
| 234 |
|
| 235 |
func (fb *FakeBinding) GetPolicyName() types.NamespacedName { |
| 236 |
return types.NamespacedName{ |
| 237 |
Name: fb.PolicyName, |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
func (fb *FakeBinding) GetMatchResources() *v1.MatchResources { |
| 242 |
return nil |
| 243 |
} |
| 244 |
|
| 245 |
func (fb *FakeBinding) GetParamRef() *v1.ParamRef { |
| 246 |
return nil |
| 247 |
} |
| 248 |
|
| 249 |
func (fp *FakePolicy) DeepCopyObject() runtime.Object { |
| 250 |
// totally fudged deepcopy |
| 251 |
newFP := &FakePolicy{} |
| 252 |
*newFP = *fp |
| 253 |
return newFP |
| 254 |
} |
| 255 |
|
| 256 |
func (fb *FakeBinding) DeepCopyObject() runtime.Object { |
| 257 |
// totally fudged deepcopy |
| 258 |
newFB := &FakeBinding{} |
| 259 |
*newFB = *fb |
| 260 |
return newFB |
| 261 |
} |