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.
|
// 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.
|
// Defaults to using s.LocalPortNumber if it is set.
|
||||||
func (s *StepCreateSSMTunnel) ConfigureLocalHostPort(ctx context.Context) error {
|
func (s *StepCreateSSMTunnel) ConfigureLocalHostPort(ctx context.Context) error {
|
||||||
|
minPortNumber, maxPortNumber := 8000, 9000
|
||||||
|
|
||||||
if s.LocalPortNumber != 0 {
|
if s.LocalPortNumber != 0 {
|
||||||
return nil
|
minPortNumber = s.LocalPortNumber
|
||||||
|
maxPortNumber = minPortNumber
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find an available TCP port for our HTTP server
|
// Find an available TCP port for our HTTP server
|
||||||
l, err := net.ListenRangeConfig{
|
l, err := net.ListenRangeConfig{
|
||||||
Min: 8000,
|
Min: minPortNumber,
|
||||||
Max: 9000,
|
Max: maxPortNumber,
|
||||||
Addr: "0.0.0.0",
|
Addr: "0.0.0.0",
|
||||||
Network: "tcp",
|
Network: "tcp",
|
||||||
}.Listen(ctx)
|
}.Listen(ctx)
|
||||||
|
|
|
@ -34,15 +34,27 @@ func TestStepCreateSSMTunnel_BuildTunnelInputForInstance(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStepCreateSSMTunnel_ConfigureLocalHostPort(t *testing.T) {
|
func TestStepCreateSSMTunnel_ConfigureLocalHostPort(t *testing.T) {
|
||||||
tun := StepCreateSSMTunnel{}
|
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 }},
|
||||||
|
}
|
||||||
|
|
||||||
ctx := context.TODO()
|
for _, tc := range tt {
|
||||||
if err := tun.ConfigureLocalHostPort(ctx); err != nil {
|
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")
|
t.Errorf("failed to configure a port on localhost")
|
||||||
}
|
}
|
||||||
|
|
||||||
if tun.LocalPortNumber == 0 {
|
if !tc.PortCheck(step.LocalPortNumber) {
|
||||||
t.Errorf("failed to configure a port on localhost")
|
t.Errorf("failed to configure a port on localhost")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue