Repository: kubernetes/kubernetes
File: pkg/registry/rbac/clusterrole/declarative_validation_test.go (Go)

Code before:
	}
}

func TestValidateUpdateForDeclarative(t *testing.T) {
	for _, apiVersion := range apiVersions {
		testValidateUpdateForDeclarative(t, apiVersion)
	}
}

func testValidateUpdateForDeclarative(t *testing.T, apiVersion string) {
	ctx := genericapirequest.WithRequestInfo(genericapirequest.NewDefaultContext(), &genericapirequest.RequestInfo{
		APIGroup:   "rbac.authorization.k8s.io",
		APIVersion: apiVersion,
	})
	testCases := map[string]struct {
		old          rbac.ClusterRole
		update       rbac.ClusterRole
		expectedErrs field.ErrorList
	}{
		"valid update adding resources": {
			old:    mkValidClusterRole(tweakResourceVersion("1")),
			update: mkValidClusterRole(tweakResourceVersion("2"), tweakAddResource("services")),
		},
		"invalid update clearing resources": {
			old:    mkValidClusterRole(tweakResourceVersion("1")),
			update: mkValidClusterRole(tweakResourceVersion("2"), tweakResources([]string{})),
			expectedErrs: field.ErrorList{
				field.Required(field.NewPath("rules").Index(0).Child("resources"), ""),
			},
		},
		// TODO: Add more test cases
	}
	for k, tc := range testCases {
		t.Run(k, func(t *testing.T) {
			apitesting.VerifyUpdateValidationEquivalence(t, ctx, &tc.update, &tc.old, Strategy.ValidateUpdate, tc.expectedErrs)
		})
	}
}

func mkValidClusterRole(tweaks ...func(*rbac.ClusterRole)) rbac.ClusterRole {
	cr := rbac.ClusterRole{
		ObjectMeta: metav1.ObjectMeta{
			Name: "valid-cluster-role",
		},
		Rules: []rbac.PolicyRule{{
			APIGroups: []string{"rbac.authorization.k8s.io"},
			Resources: []string{"pods"},
			Verbs:     []string{"get", "list"},
		}},
	}


Review comment (suggestion):
This tweakResourceVersion can be moved to update test. It is kind of a common  operation in all update test cases.

Code after:
	}
}

func TestValidateUpdateForDeclarative(t *testing.T) {
	for _, apiVersion := range apiVersions {
		testValidateUpdateForDeclarative(t, apiVersion)
	}
}

func testValidateUpdateForDeclarative(t *testing.T, apiVersion string) {
	ctx := genericapirequest.WithRequestInfo(genericapirequest.NewDefaultContext(), &genericapirequest.RequestInfo{
		APIGroup:   "rbac.authorization.k8s.io",
		APIVersion: apiVersion,
	})
	testCases := map[string]struct {
		old          rbac.ClusterRole
		update       rbac.ClusterRole
		expectedErrs field.ErrorList
	}{
		"valid update": {
			old:    mkValidClusterRole(),
			update: mkValidClusterRole(),
		},
		"invalid update clearing verbs": {
			old:    mkValidClusterRole(),
			update: mkValidClusterRole(tweakVerbs([]string{})),
			expectedErrs: field.ErrorList{
				field.Required(field.NewPath("rules").Index(0).Child("verbs"), ""),
			},
		},
		// TODO: Add more test cases
	}
	for k, tc := range testCases {
		t.Run(k, func(t *testing.T) {
			tc.old.ResourceVersion = "1"
			tc.update.ResourceVersion = "2"
			apitesting.VerifyUpdateValidationEquivalence(t, ctx, &tc.update, &tc.old, Strategy.ValidateUpdate, tc.expectedErrs)
		})
	}
}

func mkValidClusterRole(tweaks ...func(*rbac.ClusterRole)) rbac.ClusterRole {
	cr := rbac.ClusterRole{
		ObjectMeta: metav1.ObjectMeta{
			Name: "valid-cluster-role",
		},
		Rules: []rbac.PolicyRule{{
			APIGroups: []string{"rbac.authorization.k8s.io"},
			Resources: []string{"pods"},
			Verbs:     []string{"get", "list"},
		}},