-
Notifications
You must be signed in to change notification settings - Fork 903
feat: Support for comma-separation and ranges in port-forward #4166
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
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,90 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_parsePortForwards(t *testing.T) { | ||
t.Parallel() | ||
|
||
portForwardSpecToString := func(v []portForwardSpec) (out []string) { | ||
for _, p := range v { | ||
require.Equal(t, p.listenNetwork, p.dialNetwork) | ||
out = append(out, fmt.Sprintf("%s:%s", strings.Replace(p.listenAddress, "127.0.0.1:", "", 1), strings.Replace(p.dialAddress, "127.0.0.1:", "", 1))) | ||
} | ||
return out | ||
} | ||
type args struct { | ||
tcpSpecs []string | ||
udpSpecs []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "TCP mixed ports and ranges", | ||
args: args{ | ||
tcpSpecs: []string{ | ||
"8000,8080:8081,9000-9002,9003-9004:9005-9006", | ||
"10000", | ||
}, | ||
}, | ||
want: []string{ | ||
"8000:8000", | ||
"8080:8081", | ||
"9000:9000", | ||
"9001:9001", | ||
"9002:9002", | ||
"9003:9005", | ||
"9004:9006", | ||
"10000:10000", | ||
}, | ||
}, | ||
{ | ||
name: "UDP with port range", | ||
args: args{ | ||
udpSpecs: []string{"8000,8080-8081"}, | ||
}, | ||
want: []string{ | ||
"8000:8000", | ||
"8080:8080", | ||
"8081:8081", | ||
}, | ||
}, | ||
{ | ||
name: "Bad port range", | ||
args: args{ | ||
tcpSpecs: []string{"8000-7000"}, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Bad dest port range", | ||
args: args{ | ||
tcpSpecs: []string{"8080-8081:9080-9082"}, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got, err := parsePortForwards(tt.args.tcpSpecs, tt.args.udpSpecs) | ||
if (err != nil) != tt.wantErr { | ||
t.Fatalf("parsePortForwards() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
gotStrings := portForwardSpecToString(got) | ||
require.Equal(t, tt.want, gotStrings) | ||
}) | ||
} | ||
} |
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
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.
Why not just have the function return a portForwardSpec directly? You could pass it a listen and dial network and it would return
[]portForwardSpec
instead of having an intermediary typeThere 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.
My thinking was probably influenced by the previous implementation, and keeping the type "plain until use". Why plain? For instance, if you see the test where we mangle the
portForwardSpec
to make testing more convenient, that's not ideal IMO. In fact I could seeparsePortForwards
returning[]parsedSrcDestPort
if it was updated withprotocol string
field. Then all items could be post-processed intoportForwardSpec
as a last step¯\_(ツ)_/¯
. That code would seem more re-usable if we ever want to parse the port format in other places.I'm not married to this by any means, I can change it, but I don't see a clear benefit to either way of doing it.