Repository: kubernetes/kubernetes
File: test/integration/apiserver/cel/admission_policy_test.go (Go)

Code before:
			PolicyName:        convertPolicy.GetName(),
			ValidationActions: []admissionregistrationv1.ValidationAction{admissionregistrationv1.Warn},
			ParamRef: &admissionregistrationv1.ParamRef{
				Name:                    configurationConvert.GetName(),
				Namespace:               configurationConvert.GetNamespace(),
				ParameterNotFoundAction: &denyAction,
			},
		},
	}, metav1.CreateOptions{})
	if err != nil {
		return err
	}

	return nil
}

// This test shows that policy intercepts all requests for all resources,
// subresources, verbs, and input versions of policy/binding.
// The test emulates v1.33 as that was the last version before v1beta1 resource was removed.
// Remove this test once v1.33 cannot be emulated in v1.37.
//
// This test tries to mirror very closely the same test for webhook admission
// test/integration/apiserver/admissionwebhook/admission_test.go testWebhookAdmission
func TestPolicyAdmissionV1beta1(t *testing.T) {
	featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, utilfeature.DefaultFeatureGate, version.MustParse(vapV1beta1LastEmulatableVersion))
	testPolicyAdmission(t, true)
}

// This test shows that policy intercepts all requests for all resources,
// subresources, verbs, and input versions of policy/binding.
//
// This test tries to mirror very closely the same test for webhook admission
// test/integration/apiserver/admissionwebhook/admission_test.go testWebhookAdmission
func TestPolicyAdmission(t *testing.T) {
	testPolicyAdmission(t, false)
}

func testPolicyAdmission(t *testing.T, supportV1Beta1 bool) {
	supportedVersions := []string{"v1"}
	if supportV1Beta1 {
		supportedVersions = append(supportedVersions, "v1beta1")
	}

	holder := &policyExpectationHolder{
		supportedVersions: supportedVersions,
		holder: holder{
			t:                 t,
			supportedVersions: supportedVersions,
			gvrToConvertedGVR: map[metav1.GroupVersionResource]metav1.GroupVersionResource{},
			gvrToConvertedGVK: map[metav1.GroupVersionResource]schema.GroupVersionKind{},
		},

Review comment (refactor):
I am generally ok with having separate test for v1beta1 till 1.37 for emulative version. However, isn't the current solution runs duplicated test for v1? Raising this because there was concern raised earlier about the time this kind of test takes.

Code after:
			PolicyName:        convertPolicy.GetName(),
			ValidationActions: []admissionregistrationv1.ValidationAction{admissionregistrationv1.Warn},
			ParamRef: &admissionregistrationv1.ParamRef{
				Name:                    configurationConvert.GetName(),
				Namespace:               configurationConvert.GetNamespace(),
				ParameterNotFoundAction: &denyAction,
			},
		},
	}, metav1.CreateOptions{})
	if err != nil {
		return err
	}

	return nil
}

// This test shows that policy intercepts all requests for all resources,
// subresources, verbs, and input versions of policy/binding.
// The test emulates v1.33 as that was the last version before v1beta1 resource was removed.
// Remove this test once v1.33 cannot be emulated in v1.37.
//
// This test tries to mirror very closely the same test for webhook admission
// test/integration/apiserver/admissionwebhook/admission_test.go testWebhookAdmission
func TestPolicyAdmissionV1beta1(t *testing.T) {
	featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, utilfeature.DefaultFeatureGate, version.MustParse(vapV1beta1LastEmulatableVersion))
	testPolicyAdmission(t, true)
}

// This test shows that policy intercepts all requests for all resources,
// subresources, verbs, and input versions of policy/binding.
//
// This test tries to mirror very closely the same test for webhook admission
// test/integration/apiserver/admissionwebhook/admission_test.go testWebhookAdmission
func TestPolicyAdmission(t *testing.T) {
	testPolicyAdmission(t, false)
}

func testPolicyAdmission(t *testing.T, supportV1Beta1 bool) {
	supportedVersions := []string{}
	if supportV1Beta1 {
		supportedVersions = append(supportedVersions, "v1beta1")
	} else {
		supportedVersions = append(supportedVersions, "v1")
	}

	holder := &policyExpectationHolder{
		supportedVersions: supportedVersions,
		holder: holder{
			t:                 t,
			supportedVersions: supportedVersions,
			gvrToConvertedGVR: map[metav1.GroupVersionResource]metav1.GroupVersionResource{},