Skip to content

Commit 190c8c7

Browse files
committed
Use per-policy marker names for VAP integration tests.
Writes to policy resources don't instantaneously take effect in admission. ValidatingAdmissionPolicy integration tests determine that the policies under test have taken effect by adding a sentinel policy rule and polling until that rule is applied to a request. If the marker resource names are the same for each test case in a series of test cases, then observing a policy's effect on a marker request only indicates that _any_ test policy is in effect, but it's not necessarily the policy the current test case is waiting for. For example: 1. Test 1 creates a policy and binding. 2. The policy and binding are observed by the admission plugin and take effect. 3. Test 1 observes that a policy is in effect via marker requests. 4. Test 1 exercises the behavior under test and successfully deletes the policy and binding it created. 5. Test 2 creates a policy and binding. 6. Test 2 observes that a policy is in effect via marker requests, but the policy in effect is still the one created by Test 1. 7. Test 2 exercises the behavior under test, which fails because it was evaluated against Test 1's policy. Generating a per-policy name for the marker resource in each test resolves the timing issue. In the example, step (6) will not proceed until the admission plugin has observed the policy and binding created in (5).
1 parent 1d932bd commit 190c8c7

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

test/integration/apiserver/cel/validatingadmissionpolicy_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ import (
6565
authorizationv1 "k8s.io/api/authorization/v1"
6666
v1 "k8s.io/api/core/v1"
6767
rbacv1 "k8s.io/api/rbac/v1"
68+
utilrand "k8s.io/apimachinery/pkg/util/rand"
69+
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
6870
)
6971

7072
// Short term fix to refresh the policy source cache faster for tests
@@ -3004,8 +3006,17 @@ func secondaryAuthorizationServiceAccountClient(t *testing.T, adminClient *clien
30043006

30053007
func withWaitReadyConstraintAndExpression(policy *admissionregistrationv1.ValidatingAdmissionPolicy) *admissionregistrationv1.ValidatingAdmissionPolicy {
30063008
policy = policy.DeepCopy()
3009+
3010+
testMarkerName := fmt.Sprintf("test-marker-%s", utilrand.String(utilvalidation.DNS1123SubdomainMaxLength-len("test-marker-")))
3011+
annotations := policy.GetAnnotations()
3012+
if annotations == nil {
3013+
annotations = make(map[string]string)
3014+
}
3015+
annotations["test-marker-name"] = testMarkerName
3016+
policy.SetAnnotations(annotations)
3017+
30073018
policy.Spec.MatchConstraints.ResourceRules = append(policy.Spec.MatchConstraints.ResourceRules, admissionregistrationv1.NamedRuleWithOperations{
3008-
ResourceNames: []string{"test-marker"},
3019+
ResourceNames: []string{testMarkerName},
30093020
RuleWithOperations: admissionregistrationv1.RuleWithOperations{
30103021
Operations: []admissionregistrationv1.OperationType{
30113022
"UPDATE",
@@ -3024,7 +3035,7 @@ func withWaitReadyConstraintAndExpression(policy *admissionregistrationv1.Valida
30243035
},
30253036
})
30263037
policy.Spec.Validations = append([]admissionregistrationv1.Validation{{
3027-
Expression: "object.metadata.name != 'test-marker'",
3038+
Expression: fmt.Sprintf("object.metadata.name != '%s'", testMarkerName),
30283039
Message: "marker denied; policy is ready",
30293040
}}, policy.Spec.Validations...)
30303041
return policy
@@ -3039,14 +3050,23 @@ func createAndWaitReadyNamespaced(t *testing.T, client clientset.Interface, bind
30393050
}
30403051

30413052
func createAndWaitReadyNamespacedWithWarnHandler(t *testing.T, client clientset.Interface, binding *admissionregistrationv1.ValidatingAdmissionPolicyBinding, matchLabels map[string]string, ns string, handler *warningHandler) error {
3042-
marker := &v1.Endpoints{ObjectMeta: metav1.ObjectMeta{Name: "test-marker", Namespace: ns, Labels: matchLabels}}
3053+
policy, err := client.AdmissionregistrationV1().ValidatingAdmissionPolicies().Get(context.TODO(), binding.Spec.PolicyName, metav1.GetOptions{})
3054+
if err != nil {
3055+
t.Fatal(err)
3056+
}
3057+
testMarkerName := "test-marker"
3058+
if testMarkerNameAnnotation, ok := policy.GetAnnotations()["test-marker-name"]; ok {
3059+
testMarkerName = testMarkerNameAnnotation
3060+
}
3061+
3062+
marker := &v1.Endpoints{ObjectMeta: metav1.ObjectMeta{Name: testMarkerName, Namespace: ns, Labels: matchLabels}}
30433063
defer func() {
30443064
err := client.CoreV1().Endpoints(ns).Delete(context.TODO(), marker.Name, metav1.DeleteOptions{})
30453065
if err != nil {
30463066
t.Logf("error deleting marker: %v", err)
30473067
}
30483068
}()
3049-
marker, err := client.CoreV1().Endpoints(ns).Create(context.TODO(), marker, metav1.CreateOptions{})
3069+
marker, err = client.CoreV1().Endpoints(ns).Create(context.TODO(), marker, metav1.CreateOptions{})
30503070
if err != nil {
30513071
return err
30523072
}

0 commit comments

Comments
 (0)