-
Notifications
You must be signed in to change notification settings - Fork 0
Refactoring of LBManager.nextFreeSocket #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
25c7e89
Refactor the implementation of nextFreePort
eberlep 52a1f47
Further refactoring
eberlep 17a7c9f
Merge branch 'main' of https://github.com/fi-ts/postgreslet into refa…
eberlep d36fe7d
Add tests
LimKianAn 54af2e4
Add additional test
eberlep 65c2f4b
Small tweak
eberlep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package lbmanager | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
api "github.com/fi-ts/postgreslet/api/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
func TestLBManager_nextFreePort(t *testing.T) { | ||
portRangeStart := int32(0) | ||
portRangeSize := int32(5) | ||
|
||
tests := []struct { | ||
name string | ||
lbMgr *LBManager | ||
want int32 | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "no svc in the cluster", | ||
lbMgr: &LBManager{ | ||
Client: fake.NewClientBuilder().WithScheme(scheme()).WithLists(svcListWithPorts()).Build(), | ||
LBIP: "0.0.0.0", | ||
PortRangeStart: portRangeStart, | ||
PortRangeSize: portRangeSize, | ||
}, | ||
want: 0, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "one svc already in the cluster", | ||
lbMgr: &LBManager{ | ||
Client: fake.NewClientBuilder().WithScheme(scheme()).WithLists(svcListWithPorts(0)).Build(), | ||
LBIP: "0.0.0.0", | ||
PortRangeStart: portRangeStart, | ||
PortRangeSize: portRangeSize, | ||
}, | ||
want: 1, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "last free port left", | ||
lbMgr: &LBManager{ | ||
Client: fake.NewClientBuilder().WithScheme(scheme()).WithLists(svcListWithPorts(0, 1, 2, 3)).Build(), | ||
LBIP: "0.0.0.0", | ||
PortRangeStart: portRangeStart, | ||
PortRangeSize: portRangeSize, | ||
}, | ||
want: 4, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "no free port", | ||
lbMgr: &LBManager{ | ||
Client: fake.NewClientBuilder().WithScheme(scheme()).WithLists(svcListWithPorts(0, 1, 2, 3, 4)).Build(), | ||
LBIP: "0.0.0.0", | ||
PortRangeStart: portRangeStart, | ||
PortRangeSize: portRangeSize, | ||
}, | ||
want: 0, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "re-use releaased port", | ||
lbMgr: &LBManager{ | ||
Client: fake.NewClientBuilder().WithScheme(scheme()).WithLists(svcListWithPorts(0, 2, 3)).Build(), | ||
LBIP: "0.0.0.0", | ||
PortRangeStart: portRangeStart, | ||
PortRangeSize: portRangeSize, | ||
}, | ||
want: 1, | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.lbMgr.nextFreePort(context.Background()) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("LBManager.nextFreePort() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("LBManager.nextFreePort() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func scheme() *runtime.Scheme { | ||
scheme := runtime.NewScheme() | ||
_ = corev1.AddToScheme(scheme) | ||
|
||
return scheme | ||
} | ||
|
||
// svcListWithPorts generates a `ServiceList` containing `Service`s with ports respectively | ||
func svcListWithPorts(ports ...int32) *corev1.ServiceList { | ||
svcList := &corev1.ServiceList{} | ||
for _, port := range ports { | ||
svcList.Items = append(svcList.Items, *svcWithPort(port)) | ||
} | ||
return svcList | ||
} | ||
|
||
func svcWithPort(port int32) *corev1.Service { | ||
svc := corev1.Service{} | ||
svc.Name = fmt.Sprintf("svc-with-port-%d", port) | ||
svc.Labels = api.SvcLoadBalancerLabel | ||
svc.Spec.Ports = []corev1.ServicePort{ | ||
{ | ||
Port: port, | ||
}, | ||
} | ||
return &svc | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about factoring this logic out into a testable function ?