|  | 
| 1 |  | -package resources | 
|  | 1 | +package resources_test | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"context" | 
|  | 5 | + | 
|  | 6 | +	. "github.com/onsi/ginkgo/v2" | 
|  | 7 | +	. "github.com/onsi/gomega" | 
|  | 8 | +	v1 "k8s.io/api/rbac/v1" | 
|  | 9 | +	"k8s.io/apimachinery/pkg/runtime" | 
|  | 10 | +	"sigs.k8s.io/controller-runtime/pkg/client" | 
|  | 11 | + | 
|  | 12 | +	"github.com/openmcp-project/controller-utils/pkg/resources" | 
|  | 13 | +	"github.com/openmcp-project/controller-utils/pkg/testing" | 
|  | 14 | +) | 
|  | 15 | + | 
|  | 16 | +var _ = Describe("ClusterRoleBindingMutator", func() { | 
|  | 17 | +	var ( | 
|  | 18 | +		ctx         context.Context | 
|  | 19 | +		fakeClient  client.WithWatch | 
|  | 20 | +		scheme      *runtime.Scheme | 
|  | 21 | +		subjects    []v1.Subject | 
|  | 22 | +		roleRef     v1.RoleRef | 
|  | 23 | +		labels      map[string]string | 
|  | 24 | +		annotations map[string]string | 
|  | 25 | +		mutator     resources.Mutator[*v1.ClusterRoleBinding] | 
|  | 26 | +	) | 
|  | 27 | + | 
|  | 28 | +	BeforeEach(func() { | 
|  | 29 | +		ctx = context.TODO() | 
|  | 30 | + | 
|  | 31 | +		// Create a scheme and register the rbac/v1 API | 
|  | 32 | +		scheme = runtime.NewScheme() | 
|  | 33 | +		Expect(v1.AddToScheme(scheme)).To(Succeed()) | 
|  | 34 | + | 
|  | 35 | +		// Initialize the fake client | 
|  | 36 | +		var err error | 
|  | 37 | +		fakeClient, err = testing.GetFakeClient(scheme) | 
|  | 38 | +		Expect(err).ToNot(HaveOccurred()) | 
|  | 39 | + | 
|  | 40 | +		// Define subjects, roleRef, labels, and annotations | 
|  | 41 | +		subjects = []v1.Subject{ | 
|  | 42 | +			{ | 
|  | 43 | +				Kind:      "User", | 
|  | 44 | +				Name:      "test-user", | 
|  | 45 | +				Namespace: "test-namespace", | 
|  | 46 | +			}, | 
|  | 47 | +		} | 
|  | 48 | +		roleRef = resources.NewClusterRoleRef("test-role") | 
|  | 49 | +		labels = map[string]string{"key1": "value1"} | 
|  | 50 | +		annotations = map[string]string{"annotation1": "value1"} | 
|  | 51 | + | 
|  | 52 | +		// Create a cluster role binding mutator | 
|  | 53 | +		mutator = resources.NewClusterRoleBindingMutator("test-clusterrolebinding", subjects, roleRef, labels, annotations) | 
|  | 54 | +	}) | 
|  | 55 | + | 
|  | 56 | +	It("should create an empty cluster role binding with correct metadata", func() { | 
|  | 57 | +		clusterRoleBinding := mutator.Empty() | 
|  | 58 | + | 
|  | 59 | +		Expect(clusterRoleBinding.Name).To(Equal("test-clusterrolebinding")) | 
|  | 60 | +		Expect(clusterRoleBinding.APIVersion).To(Equal("rbac.authorization.k8s.io/v1")) | 
|  | 61 | +		Expect(clusterRoleBinding.Kind).To(Equal("ClusterRoleBinding")) | 
|  | 62 | +	}) | 
|  | 63 | + | 
|  | 64 | +	It("should apply subjects and roleRef using Mutate", func() { | 
|  | 65 | +		clusterRoleBinding := mutator.Empty() | 
|  | 66 | + | 
|  | 67 | +		// Apply the mutator's Mutate method | 
|  | 68 | +		Expect(mutator.Mutate(clusterRoleBinding)).To(Succeed()) | 
|  | 69 | + | 
|  | 70 | +		// Verify that the subjects and roleRef are applied | 
|  | 71 | +		Expect(clusterRoleBinding.Subjects).To(Equal(subjects)) | 
|  | 72 | +		Expect(clusterRoleBinding.RoleRef).To(Equal(roleRef)) | 
|  | 73 | +	}) | 
|  | 74 | + | 
|  | 75 | +	It("should create and retrieve the cluster role binding using the fake client", func() { | 
|  | 76 | +		clusterRoleBinding := mutator.Empty() | 
|  | 77 | +		Expect(mutator.Mutate(clusterRoleBinding)).To(Succeed()) | 
|  | 78 | + | 
|  | 79 | +		// Create the cluster role binding in the fake client | 
|  | 80 | +		Expect(fakeClient.Create(ctx, clusterRoleBinding)).To(Succeed()) | 
|  | 81 | + | 
|  | 82 | +		// Retrieve the cluster role binding from the fake client and verify it | 
|  | 83 | +		retrievedClusterRoleBinding := &v1.ClusterRoleBinding{} | 
|  | 84 | +		Expect(fakeClient.Get(ctx, client.ObjectKey{Name: "test-clusterrolebinding"}, retrievedClusterRoleBinding)).To(Succeed()) | 
|  | 85 | +		Expect(retrievedClusterRoleBinding).To(Equal(clusterRoleBinding)) | 
|  | 86 | +	}) | 
|  | 87 | +}) | 
0 commit comments