Skip to content

Commit

Permalink
Revert "Use range check"
Browse files Browse the repository at this point in the history
This reverts commit 1bcbacf.
  • Loading branch information
valaparthvi committed Apr 17, 2023
1 parent 1bcbacf commit fb31c33
Showing 1 changed file with 17 additions and 47 deletions.
64 changes: 17 additions & 47 deletions pkg/portForward/kubeportforward/portForward_test.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
package kubeportforward

import (
"fmt"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/google/go-cmp/cmp"
"github.com/redhat-developer/odo/pkg/api"
"strconv"
"strings"
"testing"
)

func Test_getCompleteCustomPortPairs(t *testing.T) {
const (
acceptablePortRange = "[20001-30001]"
acceptableMinPort = 20001
acceptableMaxPort = 30001
)
type args struct {
definedPorts []api.ForwardedPort
ceMapping map[string][]v1alpha2.Endpoint
}
tests := []struct {
name string
args args
// wantPortPairs format: {containerName: {containerPort: localPort}}
wantPortPairs map[string]map[string]string
name string
args args
wantPortPairs map[string][]string
}{
// TODO: Add test cases.
{
name: "ports are provided with container name",
args: args{
definedPorts: []api.ForwardedPort{
{ContainerName: "runtime", LocalPort: 8080, ContainerPort: 8000},
{ContainerName: "tools", LocalPort: 5000, ContainerPort: 5000},
},
ceMapping: map[string][]v1alpha2.Endpoint{
"runtime": {{TargetPort: 8000}, {TargetPort: 9000}},
"tools": {{TargetPort: 5000}},
},
},
wantPortPairs: map[string]map[string]string{
"runtime": {"8000": "8080", "9000": acceptablePortRange},
"tools": {"5000": acceptablePortRange},
wantPortPairs: map[string][]string{
"runtime": {"8080:8000", "20001:9000"},
"tools": {"5000:5000"},
},
},
{
Expand All @@ -54,57 +47,34 @@ func Test_getCompleteCustomPortPairs(t *testing.T) {
"tools": {{TargetPort: 5000}},
},
},
wantPortPairs: map[string]map[string]string{
"runtime": {"8000": "8080", "9000": acceptablePortRange},
"tools": {"5000": "5000"},
wantPortPairs: map[string][]string{
"runtime": {"8080:8000", "20001:9000"},
"tools": {"5000:5000"},
},
},
{
name: "local ports in range [20001-30001] are provided as custom forward ports",
args: args{
definedPorts: []api.ForwardedPort{
{LocalPort: 25001, ContainerPort: 8000},
{LocalPort: 25002, ContainerPort: 9000},
{LocalPort: 20001, ContainerPort: 8000},
{LocalPort: 20002, ContainerPort: 9000},
{LocalPort: 5000, ContainerPort: 5000},
},
ceMapping: map[string][]v1alpha2.Endpoint{
"runtime": {{TargetPort: 8000}, {TargetPort: 9000}},
"tools": {{TargetPort: 5000}, {TargetPort: 8080}},
},
},
wantPortPairs: map[string]map[string]string{
"runtime": {"8000": "25001", "9000": "25002"},
"tools": {"5000": "5000", "8080": acceptablePortRange},
wantPortPairs: map[string][]string{
"runtime": {"20001:8000", "20002:9000"},
"tools": {"5000:5000", "20003:8080"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPortPairs := getCustomPortPairs(tt.args.definedPorts, tt.args.ceMapping)

validatePortPairs := func(gotPortPairs map[string][]string, wantPortPairs map[string]map[string]string) (diff string) {
for container, portPairs := range gotPortPairs {
wantPortPair := wantPortPairs[container]
for _, portPair := range portPairs {
portMap := strings.Split(portPair, ":")
lPort, cPort := portMap[0], portMap[1]
wantLPort := wantPortPair[cPort]
if wantLPort == acceptablePortRange {
if intLPort, _ := strconv.Atoi(lPort); intLPort >= acceptableMinPort && intLPort <= acceptableMaxPort {
continue
} else {
diff += fmt.Sprintf("[container %q] %s:%s is not in range %s\n", container, cPort, lPort, acceptablePortRange)
}
} else if wantLPort == lPort {
continue
} else {
diff += fmt.Sprintf("[container %q] %s:%s does not match %s:%s\n", container, cPort, lPort, cPort, wantLPort)
}
}
}
return diff
}
if diff := validatePortPairs(gotPortPairs, tt.wantPortPairs); diff != "" {
if diff := cmp.Diff(gotPortPairs, tt.wantPortPairs); diff != "" {
t.Errorf("getCompleteCustomPortPairs() (got vs want) diff = %v", diff)
}
})
Expand Down

0 comments on commit fb31c33

Please sign in to comment.