Skip to content

Commit

Permalink
feat(teams) Update unit test and update (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
gciezkowski-acc committed Dec 12, 2024
1 parent 7c12ec9 commit 98a8368
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
17 changes: 6 additions & 11 deletions pkg/controllers/team/team_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}
41 changes: 40 additions & 1 deletion pkg/controllers/team/team_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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: "[email protected]",
},
{
ID: "d2a72c04-42d2-426a-942d-af9609c4cd00",
FirstName: "Test",
LastName: "User2",
Email: "[email protected]",
},
},
},
})
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)
Expand Down

0 comments on commit 98a8368

Please sign in to comment.