step_create_ssm_tunnel: Add port availability check for LocalPortNumber
This commit is contained in:
parent
08dc2cb847
commit
3ae7ab994b
|
@ -109,13 +109,17 @@ func (s *StepCreateSSMTunnel) Cleanup(state multistep.StateBag) {
|
|||
// ConfigureLocalHostPort finds an available port on the localhost that can be used for the remote tunnel.
|
||||
// Defaults to using s.LocalPortNumber if it is set.
|
||||
func (s *StepCreateSSMTunnel) ConfigureLocalHostPort(ctx context.Context) error {
|
||||
minPortNumber, maxPortNumber := 8000, 9000
|
||||
|
||||
if s.LocalPortNumber != 0 {
|
||||
return nil
|
||||
minPortNumber = s.LocalPortNumber
|
||||
maxPortNumber = minPortNumber
|
||||
}
|
||||
|
||||
// Find an available TCP port for our HTTP server
|
||||
l, err := net.ListenRangeConfig{
|
||||
Min: 8000,
|
||||
Max: 9000,
|
||||
Min: minPortNumber,
|
||||
Max: maxPortNumber,
|
||||
Addr: "0.0.0.0",
|
||||
Network: "tcp",
|
||||
}.Listen(ctx)
|
||||
|
|
|
@ -34,15 +34,27 @@ func TestStepCreateSSMTunnel_BuildTunnelInputForInstance(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestStepCreateSSMTunnel_ConfigureLocalHostPort(t *testing.T) {
|
||||
tun := StepCreateSSMTunnel{}
|
||||
|
||||
ctx := context.TODO()
|
||||
if err := tun.ConfigureLocalHostPort(ctx); err != nil {
|
||||
t.Errorf("failed to configure a port on localhost")
|
||||
tt := []struct {
|
||||
Name string
|
||||
Step StepCreateSSMTunnel
|
||||
PortCheck func(int) bool
|
||||
}{
|
||||
{"WithLocalPortNumber", StepCreateSSMTunnel{LocalPortNumber: 9001}, func(port int) bool { return port == 9001 }},
|
||||
{"WithNoLocalPortNumber", StepCreateSSMTunnel{}, func(port int) bool { return port >= 8000 && port <= 9000 }},
|
||||
}
|
||||
|
||||
if tun.LocalPortNumber == 0 {
|
||||
t.Errorf("failed to configure a port on localhost")
|
||||
for _, tc := range tt {
|
||||
tc := tc
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
step := tc.Step
|
||||
if err := step.ConfigureLocalHostPort(context.TODO()); err != nil {
|
||||
t.Errorf("failed to configure a port on localhost")
|
||||
}
|
||||
|
||||
if !tc.PortCheck(step.LocalPortNumber) {
|
||||
t.Errorf("failed to configure a port on localhost")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue