Skip to content

Commit

Permalink
rename lister func
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrancisc committed Oct 2, 2023
1 parent 3c91f37 commit 94054ae
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions pkg/spacebinding/spacebindinglister.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ func NewLister(listSpaceBindingsFunc func(spaceName string) ([]toolchainv1alpha1
}
}

// RecursiveListForSpace recursively searches all the SpaceBindings of a given Space,
// ListForSpace recursively searches all the SpaceBindings of a given Space,
// it will use the name of the parentSpace for the research if there is one.
func (l *Lister) RecursiveListForSpace(space *toolchainv1alpha1.Space, foundBindings []toolchainv1alpha1.SpaceBinding) ([]toolchainv1alpha1.SpaceBinding, error) {
func (l *Lister) ListForSpace(space *toolchainv1alpha1.Space, foundBindings []toolchainv1alpha1.SpaceBinding) ([]toolchainv1alpha1.SpaceBinding, error) {
parentBindings, err := l.ListSpaceBindingsFunc(space.Name)
if err != nil {
return foundBindings, err
Expand All @@ -42,7 +42,7 @@ func (l *Lister) RecursiveListForSpace(space *toolchainv1alpha1.Space, foundBind
return foundBindings, errs.Wrap(err, "unable to get parent-space")
}

return l.RecursiveListForSpace(parentSpace, foundBindings)
return l.ListForSpace(parentSpace, foundBindings)
}

func mergeSpaceBindings(foundBindings []toolchainv1alpha1.SpaceBinding, parentBindings []toolchainv1alpha1.SpaceBinding) []toolchainv1alpha1.SpaceBinding {
Expand Down
19 changes: 10 additions & 9 deletions pkg/spacebinding/spacebindinglister_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package spacebinding
package spacebinding_test

import (
"fmt"
"testing"

toolchainv1alpha1 "github.com/codeready-toolchain/api/api/v1alpha1"
"github.com/codeready-toolchain/toolchain-common/pkg/spacebinding"
"github.com/codeready-toolchain/toolchain-common/pkg/test"
. "github.com/codeready-toolchain/toolchain-common/pkg/test/masteruserrecord"
spacetest "github.com/codeready-toolchain/toolchain-common/pkg/test/space"
Expand All @@ -31,19 +32,19 @@ func TestNewSpaceBindingLister(t *testing.T) {
mur := NewMasterUserRecord(t, "johnny", TargetCluster(test.MemberClusterName), TierName("deactivate90"))
listSpaceBindingsFunc := func(spaceName string) ([]toolchainv1alpha1.SpaceBinding, error) {
return []toolchainv1alpha1.SpaceBinding{
*NewSpaceBinding(mur, space, "johnny", WithRole("admin")),
*spacebinding.NewSpaceBinding(mur, space, "johnny", spacebinding.WithRole("admin")),
}, nil
}
getSpaceFunc := func(spaceName string) (*toolchainv1alpha1.Space, error) {
return space, nil
}

// when
spaceBindingLister := NewLister(listSpaceBindingsFunc, getSpaceFunc)
spaceBindingLister := spacebinding.NewLister(listSpaceBindingsFunc, getSpaceFunc)

// then
// listing the spacebindings for the sub-space should return the spacebinding from parent-space
spaceBindings, err := spaceBindingLister.RecursiveListForSpace(subSpace, []toolchainv1alpha1.SpaceBinding{})
spaceBindings, err := spaceBindingLister.ListForSpace(subSpace, []toolchainv1alpha1.SpaceBinding{})
assert.NoError(t, err)
assert.Len(t, spaceBindings, 1, "invalid number of spacebidings")

Expand All @@ -69,10 +70,10 @@ func TestNewSpaceBindingLister(t *testing.T) {
}

// when
spaceBindingLister := NewLister(listSpaceBindingsFunc, getSpaceFunc)
spaceBindingLister := spacebinding.NewLister(listSpaceBindingsFunc, getSpaceFunc)

// then
spaceBindings, err := spaceBindingLister.RecursiveListForSpace(myspace, []toolchainv1alpha1.SpaceBinding{})
spaceBindings, err := spaceBindingLister.ListForSpace(myspace, []toolchainv1alpha1.SpaceBinding{})
assert.EqualError(t, err, "error listing spacebindings")
assert.Len(t, spaceBindings, 0, "invalid number of spacebindings")
})
Expand All @@ -83,18 +84,18 @@ func TestNewSpaceBindingLister(t *testing.T) {
myspace := spacetest.NewSpace(test.HostOperatorNs, "myspace", spacetest.WithSpecParentSpace("myparentspace"))
listSpaceBindingsFunc := func(spaceName string) ([]toolchainv1alpha1.SpaceBinding, error) {
return []toolchainv1alpha1.SpaceBinding{
*NewSpaceBinding(mur, myspace, "johnny", WithRole("admin")),
*spacebinding.NewSpaceBinding(mur, myspace, "johnny", spacebinding.WithRole("admin")),
}, nil
}
getSpaceFunc := func(spaceName string) (*toolchainv1alpha1.Space, error) {
return nil, fmt.Errorf("mock error")
}

// when
spaceBindingLister := NewLister(listSpaceBindingsFunc, getSpaceFunc)
spaceBindingLister := spacebinding.NewLister(listSpaceBindingsFunc, getSpaceFunc)

// then
spaceBindings, err := spaceBindingLister.RecursiveListForSpace(myspace, []toolchainv1alpha1.SpaceBinding{})
spaceBindings, err := spaceBindingLister.ListForSpace(myspace, []toolchainv1alpha1.SpaceBinding{})
assert.EqualError(t, err, "unable to get parent-space: mock error")
assert.Len(t, spaceBindings, 1, "invalid number of spacebindings") // expect 1 spacebinding to be returned
})
Expand Down

0 comments on commit 94054ae

Please sign in to comment.