| 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 mutating |
| 18 |
|
| 19 |
import ( |
| 20 |
"github.com/stretchr/testify/assert" |
| 21 |
"testing" |
| 22 |
|
| 23 |
v1 "k8s.io/api/core/v1" |
| 24 |
"k8s.io/apimachinery/pkg/types" |
| 25 |
) |
| 26 |
|
| 27 |
func TestFullReinvocation(t *testing.T) { |
| 28 |
key1 := key{PolicyUID: types.NamespacedName{Name: "p1"}, BindingUID: types.NamespacedName{Name: "b1"}} |
| 29 |
key2 := key{PolicyUID: types.NamespacedName{Name: "p2"}, BindingUID: types.NamespacedName{Name: "b2"}} |
| 30 |
key3 := key{PolicyUID: types.NamespacedName{Name: "p3"}, BindingUID: types.NamespacedName{Name: "b3"}} |
| 31 |
|
| 32 |
cm1v1 := &v1.ConfigMap{Data: map[string]string{"v": "1"}} |
| 33 |
cm1v2 := &v1.ConfigMap{Data: map[string]string{"v": "2"}} |
| 34 |
|
| 35 |
rc := policyReinvokeContext{} |
| 36 |
|
| 37 |
// key1 is invoked and it updates the configmap |
| 38 |
rc.SetLastPolicyInvocationOutput(cm1v1) |
| 39 |
rc.RequireReinvokingPreviouslyInvokedPlugins() |
| 40 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key1) |
| 41 |
|
| 42 |
assert.True(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v2)) |
| 43 |
|
| 44 |
// key2 is invoked and it updates the configmap |
| 45 |
rc.SetLastPolicyInvocationOutput(cm1v2) |
| 46 |
rc.RequireReinvokingPreviouslyInvokedPlugins() |
| 47 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key2) |
| 48 |
|
| 49 |
assert.True(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v1)) |
| 50 |
|
| 51 |
// key3 is invoked but it does not change anything |
| 52 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key3) |
| 53 |
|
| 54 |
assert.False(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v2)) |
| 55 |
|
| 56 |
// key1 is reinvoked |
| 57 |
assert.True(t, rc.ShouldReinvoke(key1)) |
| 58 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key1) |
| 59 |
rc.SetLastPolicyInvocationOutput(cm1v1) |
| 60 |
|
| 61 |
assert.True(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v2)) |
| 62 |
rc.RequireReinvokingPreviouslyInvokedPlugins() |
| 63 |
|
| 64 |
// key2 is reinvoked |
| 65 |
assert.True(t, rc.ShouldReinvoke(key2)) |
| 66 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key2) |
| 67 |
rc.SetLastPolicyInvocationOutput(cm1v2) |
| 68 |
|
| 69 |
assert.True(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v1)) |
| 70 |
rc.RequireReinvokingPreviouslyInvokedPlugins() |
| 71 |
|
| 72 |
// key3 is reinvoked, because the reinvocations have changed the resource |
| 73 |
assert.True(t, rc.ShouldReinvoke(key3)) |
| 74 |
} |
| 75 |
|
| 76 |
func TestPartialReinvocation(t *testing.T) { |
| 77 |
key1 := key{PolicyUID: types.NamespacedName{Name: "p1"}, BindingUID: types.NamespacedName{Name: "b1"}} |
| 78 |
key2 := key{PolicyUID: types.NamespacedName{Name: "p2"}, BindingUID: types.NamespacedName{Name: "b2"}} |
| 79 |
key3 := key{PolicyUID: types.NamespacedName{Name: "p3"}, BindingUID: types.NamespacedName{Name: "b3"}} |
| 80 |
|
| 81 |
cm1v1 := &v1.ConfigMap{Data: map[string]string{"v": "1"}} |
| 82 |
cm1v2 := &v1.ConfigMap{Data: map[string]string{"v": "2"}} |
| 83 |
|
| 84 |
rc := policyReinvokeContext{} |
| 85 |
|
| 86 |
// key1 is invoked and it updates the configmap |
| 87 |
rc.SetLastPolicyInvocationOutput(cm1v1) |
| 88 |
rc.RequireReinvokingPreviouslyInvokedPlugins() |
| 89 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key1) |
| 90 |
|
| 91 |
assert.True(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v2)) |
| 92 |
|
| 93 |
// key2 is invoked and it updates the configmap |
| 94 |
rc.SetLastPolicyInvocationOutput(cm1v2) |
| 95 |
rc.RequireReinvokingPreviouslyInvokedPlugins() |
| 96 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key2) |
| 97 |
|
| 98 |
assert.True(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v1)) |
| 99 |
|
| 100 |
// key3 is invoked but it does not change anything |
| 101 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key3) |
| 102 |
|
| 103 |
assert.False(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v2)) |
| 104 |
|
| 105 |
// key1 is reinvoked but does not change anything |
| 106 |
assert.True(t, rc.ShouldReinvoke(key1)) |
| 107 |
|
| 108 |
// key2 is not reinvoked because nothing changed since last invocation |
| 109 |
assert.False(t, rc.ShouldReinvoke(key2)) |
| 110 |
|
| 111 |
// key3 is not reinvoked because nothing changed since last invocation |
| 112 |
assert.False(t, rc.ShouldReinvoke(key3)) |
| 113 |
} |
| 114 |
|
| 115 |
func TestNoReinvocation(t *testing.T) { |
| 116 |
key1 := key{PolicyUID: types.NamespacedName{Name: "p1"}, BindingUID: types.NamespacedName{Name: "b1"}} |
| 117 |
key2 := key{PolicyUID: types.NamespacedName{Name: "p2"}, BindingUID: types.NamespacedName{Name: "b2"}} |
| 118 |
key3 := key{PolicyUID: types.NamespacedName{Name: "p3"}, BindingUID: types.NamespacedName{Name: "b3"}} |
| 119 |
|
| 120 |
cm1v1 := &v1.ConfigMap{Data: map[string]string{"v": "1"}} |
| 121 |
|
| 122 |
rc := policyReinvokeContext{} |
| 123 |
|
| 124 |
// key1 is invoked and it updates the configmap |
| 125 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key1) |
| 126 |
rc.SetLastPolicyInvocationOutput(cm1v1) |
| 127 |
|
| 128 |
assert.False(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v1)) |
| 129 |
|
| 130 |
// key2 is invoked but does not change anything |
| 131 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key2) |
| 132 |
rc.SetLastPolicyInvocationOutput(cm1v1) |
| 133 |
|
| 134 |
assert.False(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v1)) |
| 135 |
|
| 136 |
// key3 is invoked but it does not change anything |
| 137 |
rc.AddReinvocablePolicyToPreviouslyInvoked(key3) |
| 138 |
rc.SetLastPolicyInvocationOutput(cm1v1) |
| 139 |
|
| 140 |
assert.False(t, rc.IsOutputChangedSinceLastPolicyInvocation(cm1v1)) |
| 141 |
|
| 142 |
// no keys are reinvoked |
| 143 |
assert.False(t, rc.ShouldReinvoke(key1)) |
| 144 |
assert.False(t, rc.ShouldReinvoke(key2)) |
| 145 |
assert.False(t, rc.ShouldReinvoke(key3)) |
| 146 |
|
| 147 |
} |