From 95653221834e381dc5fc291f09ecffbf18cc0fd5 Mon Sep 17 00:00:00 2001 From: grzegorz-ciezkowski Date: Thu, 12 Dec 2024 12:33:50 +0100 Subject: [PATCH] feat(teams) Update unit test and update (#723) --- pkg/controllers/team/team_controller.go | 17 +++----- pkg/controllers/team/team_controller_test.go | 41 +++++++++++++++++++- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/pkg/controllers/team/team_controller.go b/pkg/controllers/team/team_controller.go index 7c6a91ea0..461cbed78 100644 --- a/pkg/controllers/team/team_controller.go +++ b/pkg/controllers/team/team_controller.go @@ -12,6 +12,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" greenhouseapisv1alpha1 "github.com/cloudoperators/greenhouse/pkg/apis/greenhouse/v1alpha1" + "github.com/cloudoperators/greenhouse/pkg/clientutil" "github.com/cloudoperators/greenhouse/pkg/lifecycle" ) @@ -63,7 +64,11 @@ func (r *TeamReconciler) setStatus() lifecycle.Conditioner { } for _, member := range teamMembershipList.Items { - if !hasOwnerReference(member.OwnerReferences, team.Kind, team.Name) { + var teamReference *v1.OwnerReference + if teamReference = clientutil.GetOwnerReference(&member, "Team"); teamReference == nil { + continue + } + if teamReference.Name != team.Name { continue } @@ -78,13 +83,3 @@ func (r *TeamReconciler) setStatus() lifecycle.Conditioner { } } } - -func hasOwnerReference(ownerReferences []v1.OwnerReference, kind, name string) bool { - for _, ownerReference := range ownerReferences { - if ownerReference.Kind == kind && ownerReference.Name == name { - return true - } - } - - return false -} diff --git a/pkg/controllers/team/team_controller_test.go b/pkg/controllers/team/team_controller_test.go index b6edb4758..f53b56e43 100644 --- a/pkg/controllers/team/team_controller_test.go +++ b/pkg/controllers/team/team_controller_test.go @@ -13,7 +13,10 @@ import ( "github.com/cloudoperators/greenhouse/pkg/test" ) -const teamName = "test-team-0000" +const ( + teamName = "test-team" + anotherTeamName = "another-test-team" +) var _ = Describe("TeamControllerTest", Ordered, func() { It("Should update status of team with members", func() { @@ -63,6 +66,42 @@ var _ = Describe("TeamControllerTest", Ordered, func() { }) Expect(err).ToNot(HaveOccurred()) + err = test.K8sClient.Create(test.Ctx, &greenhouseapisv1alpha1.TeamMembership{ + TypeMeta: metav1.TypeMeta{ + Kind: "TeamMembership", + APIVersion: greenhouseapisv1alpha1.GroupVersion.String(), + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "another-test-membership", + Namespace: test.TestNamespace, + OwnerReferences: []metav1.OwnerReference{ + { + APIVersion: greenhouseapisv1alpha1.GroupVersion.String(), + Kind: "Team", + Name: anotherTeamName, + UID: "uhuiqweqwe", + }, + }, + }, + Spec: greenhouseapisv1alpha1.TeamMembershipSpec{ + Members: []greenhouseapisv1alpha1.User{ + { + ID: "d2a72c04-42d2-426a-942d-af9609c4cd00", + FirstName: "Test", + LastName: "User1", + Email: "u1@example.com", + }, + { + ID: "d2a72c04-42d2-426a-942d-af9609c4cd00", + FirstName: "Test", + LastName: "User2", + Email: "u2@example.com", + }, + }, + }, + }) + Expect(err).ToNot(HaveOccurred()) + Eventually(func(g Gomega) { team := &greenhouseapisv1alpha1.Team{} err := test.K8sClient.Get(test.Ctx, client.ObjectKey{Name: teamName, Namespace: test.TestNamespace}, team)