From 152d7caea5d264dde968b27914d8d982863f0569 Mon Sep 17 00:00:00 2001 From: iamagf Date: Tue, 8 Aug 2023 22:27:19 -0600 Subject: [PATCH 01/16] availableport extended for more params --- ignite/pkg/availableport/availableport.go | 73 +++++++++++++++++++++-- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/ignite/pkg/availableport/availableport.go b/ignite/pkg/availableport/availableport.go index f84e985685..005a0a9483 100644 --- a/ignite/pkg/availableport/availableport.go +++ b/ignite/pkg/availableport/availableport.go @@ -7,17 +7,74 @@ import ( "time" ) +type optionalParameters struct { + WithRandomizer *rand.Rand + WithMinPort int + WithMaxPort int +} + // Find finds n number of unused ports. // it is not guaranteed that these ports will not be allocated to // another program in the time of calling Find(). -func Find(n int) (ports []int, err error) { - min := 44000 - max := 55000 +func Find(n int, moreParameters ...optionalParameters) (ports []int, err error) { + // Defining them before so we can set a value depending on the optionalParameters + var min int + var max int + var r *rand.Rand + if len(moreParameters) != 0 { + extra := moreParameters[0] + if extra.WithMinPort != 0 { + if extra.WithMinPort > -1 { + min = extra.WithMinPort + } else { + // This is not required since the port would become 0 + // but the user could not notice that sent a negative port + return nil, fmt.Errorf("ports can't be negative (negative min port given)") + } + } else { + min = 44000 + } + if extra.WithMaxPort != 0 { + if extra.WithMaxPort > -1 { + max = extra.WithMaxPort + } else { + // This is not required since the port would become 0 + // but the user could not notice that sent a negative port + return nil, fmt.Errorf("ports can't be negative (negative max port given)") + } + } else { + max = 55000 + } + if extra.WithRandomizer != nil { + r = extra.WithRandomizer + } else { + r = rand.New(rand.NewSource(time.Now().UnixNano())) + } + } else { + // If we don't require special conditions, we can + // return to the original parameters + min = 44000 + max = 55000 + r = rand.New(rand.NewSource(time.Now().UnixNano())) + } + + // If the number of ports required is bigger than the range, this stops it + if max < min { + return nil, fmt.Errorf("invalid ports range: max < min (%d < %d)", max, min) + } + + // If the number of ports required is bigger than the range, this stops it + if n > (max - min) { + return nil, fmt.Errorf("invalid amount of ports requested: limit is %d", max-min) + } + + // Marker to point if a port is already added in the list + registered := make(map[int]bool) for i := 0; i < n; i++ { for { - rand.Seed(time.Now().UnixNano()) - port := rand.Intn(max-min+1) + min + // Greater or equal to min and lower than max + port := r.Intn(max-min+1) + min conn, err := net.Dial("tcp", fmt.Sprintf(":%d", port)) // if there is an error, this might mean that no one is listening from this port @@ -26,7 +83,13 @@ func Find(n int) (ports []int, err error) { conn.Close() continue } + // if the port is already registered we skip it to the next one + // otherwise it's added to the ports list and pointed in our map + if registered[port] { + continue + } ports = append(ports, port) + registered[port] = true break } } From 1b733caf74da0f3b20e7f40ea30b72d2c53116f7 Mon Sep 17 00:00:00 2001 From: iamagf Date: Wed, 9 Aug 2023 10:21:41 -0600 Subject: [PATCH 02/16] typo private -> public OptionaParameters --- ignite/pkg/availableport/availableport.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ignite/pkg/availableport/availableport.go b/ignite/pkg/availableport/availableport.go index 005a0a9483..4f6aab07e1 100644 --- a/ignite/pkg/availableport/availableport.go +++ b/ignite/pkg/availableport/availableport.go @@ -7,7 +7,7 @@ import ( "time" ) -type optionalParameters struct { +type OptionalParameters struct { WithRandomizer *rand.Rand WithMinPort int WithMaxPort int @@ -16,8 +16,8 @@ type optionalParameters struct { // Find finds n number of unused ports. // it is not guaranteed that these ports will not be allocated to // another program in the time of calling Find(). -func Find(n int, moreParameters ...optionalParameters) (ports []int, err error) { - // Defining them before so we can set a value depending on the optionalParameters +func Find(n int, moreParameters ...OptionalParameters) (ports []int, err error) { + // Defining them before so we can set a value depending on the OptionalParameters var min int var max int var r *rand.Rand From a79793ceb1f229d6b62dc6e53d7256dd39be5fcc Mon Sep 17 00:00:00 2001 From: iamagf Date: Wed, 9 Aug 2023 10:21:55 -0600 Subject: [PATCH 03/16] Tests for updates in availableport --- .../pkg/availableport/availableport_test.go | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 ignite/pkg/availableport/availableport_test.go diff --git a/ignite/pkg/availableport/availableport_test.go b/ignite/pkg/availableport/availableport_test.go new file mode 100644 index 0000000000..f8440b5985 --- /dev/null +++ b/ignite/pkg/availableport/availableport_test.go @@ -0,0 +1,83 @@ +package availableport_test + +import ( + "fmt" + "math/rand" + "testing" + + "github.com/ignite/cli/ignite/pkg/availableport" + "github.com/stretchr/testify/require" +) + +func TestAvailablePort(t *testing.T) { + + ports, err := availableport.Find(10) + require.Equal(t, nil, err) + require.Equal(t, 10, len(ports)) + + for idx := 0; idx < 9; idx++ { + for jdx := idx + 1; jdx < 10; jdx++ { + require.NotEqual(t, ports[idx], ports[jdx]) + } + } + + // Case no ports generated + options := availableport.OptionalParameters{ + WithMinPort: 1, + WithMaxPort: 1, + } + ports, err = availableport.Find(10, options) + require.Equal(t, fmt.Errorf("invalid amount of ports requested: limit is 0"), err) + require.Equal(t, 0, len(ports)) + + // Case max < min + options = availableport.OptionalParameters{ + WithMinPort: 5, + WithMaxPort: 1, + } + ports, err = availableport.Find(10, options) + require.Equal(t, fmt.Errorf("invalid ports range: max < min (1 < 5)"), err) + require.Equal(t, 0, len(ports)) + + // Case max < min min restriction given + options = availableport.OptionalParameters{ + WithMinPort: 55001, + } + ports, err = availableport.Find(10, options) + require.Equal(t, fmt.Errorf("invalid ports range: max < min (55000 < 55001)"), err) + require.Equal(t, 0, len(ports)) + + // Case max < min max restriction given + options = availableport.OptionalParameters{ + WithMaxPort: 43999, + } + ports, err = availableport.Find(10, options) + require.Equal(t, fmt.Errorf("invalid ports range: max < min (43999 < 44000)"), err) + require.Equal(t, 0, len(ports)) + + // Case negative min + options = availableport.OptionalParameters{ + WithMinPort: -10, + } + ports, err = availableport.Find(10, options) + require.Equal(t, fmt.Errorf("ports can't be negative (negative min port given)"), err) + require.Equal(t, 0, len(ports)) + + // Case negative max + options = availableport.OptionalParameters{ + WithMaxPort: -10, + } + ports, err = availableport.Find(10, options) + require.Equal(t, fmt.Errorf("ports can't be negative (negative max port given)"), err) + require.Equal(t, 0, len(ports)) + + // Case randomizer given + options = availableport.OptionalParameters{ + WithRandomizer: rand.New(rand.NewSource(2023)), + WithMinPort: 100, + WithMaxPort: 200, + } + ports, err = availableport.Find(10, options) + require.Equal(t, 10, len(ports)) + require.Equal(t, []int{195, 129, 150, 108, 120, 169, 166, 121, 131, 160}, ports) +} From 8459bd8ee51007448032608e2a953bd5a42a5a60 Mon Sep 17 00:00:00 2001 From: iamagf Date: Wed, 9 Aug 2023 22:22:01 -0600 Subject: [PATCH 04/16] Corrections by Pantani --- ignite/pkg/availableport/availableport.go | 80 +++++++++++++++-------- 1 file changed, 52 insertions(+), 28 deletions(-) diff --git a/ignite/pkg/availableport/availableport.go b/ignite/pkg/availableport/availableport.go index 4f6aab07e1..cf246ac9d7 100644 --- a/ignite/pkg/availableport/availableport.go +++ b/ignite/pkg/availableport/availableport.go @@ -7,12 +7,32 @@ import ( "time" ) -type OptionalParameters struct { +type optionalParameters struct { WithRandomizer *rand.Rand WithMinPort int WithMaxPort int } +type OptionalParameters func(o *optionalParameters) + +func WithRandomizer(r *rand.Rand) OptionalParameters { + return func(o *optionalParameters) { + o.WithRandomizer = r + } +} + +func WithMaxPort(maxPort int) OptionalParameters { + return func(o *optionalParameters) { + o.WithMaxPort = maxPort + } +} + +func WithMinPort(minPort int) OptionalParameters { + return func(o *optionalParameters) { + o.WithMinPort = minPort + } +} + // Find finds n number of unused ports. // it is not guaranteed that these ports will not be allocated to // another program in the time of calling Find(). @@ -22,40 +42,44 @@ func Find(n int, moreParameters ...OptionalParameters) (ports []int, err error) var max int var r *rand.Rand + options := &optionalParameters{} if len(moreParameters) != 0 { - extra := moreParameters[0] - if extra.WithMinPort != 0 { - if extra.WithMinPort > -1 { - min = extra.WithMinPort - } else { - // This is not required since the port would become 0 - // but the user could not notice that sent a negative port - return nil, fmt.Errorf("ports can't be negative (negative min port given)") - } - } else { - min = 44000 - } - if extra.WithMaxPort != 0 { - if extra.WithMaxPort > -1 { - max = extra.WithMaxPort - } else { - // This is not required since the port would become 0 - // but the user could not notice that sent a negative port - return nil, fmt.Errorf("ports can't be negative (negative max port given)") - } + opt := moreParameters[0] + opt(options) + } else { + // If we don't require special conditions, we can + // return to the original parameters + min = 44000 + max = 55000 + r = rand.New(rand.NewSource(time.Now().UnixNano())) + } + + if options.WithMinPort != 0 { + if options.WithMinPort > -1 { + min = options.WithMinPort } else { - max = 55000 + // This is not required since the port would become 0 + // but the user could not notice that sent a negative port + return nil, fmt.Errorf("ports can't be negative (negative min port given)") } - if extra.WithRandomizer != nil { - r = extra.WithRandomizer + } else { + min = 44000 + } + + if options.WithMaxPort != 0 { + if options.WithMaxPort > -1 { + max = options.WithMaxPort } else { - r = rand.New(rand.NewSource(time.Now().UnixNano())) + // This is not required since the port would become 0 + // but the user could not notice that sent a negative port + return nil, fmt.Errorf("ports can't be negative (negative max port given)") } } else { - // If we don't require special conditions, we can - // return to the original parameters - min = 44000 max = 55000 + } + if options.WithRandomizer != nil { + r = options.WithRandomizer + } else { r = rand.New(rand.NewSource(time.Now().UnixNano())) } From fb3c06f307c4f40cfa7c618996adb1ac61ddd569 Mon Sep 17 00:00:00 2001 From: iamagf Date: Thu, 10 Aug 2023 20:15:39 -0600 Subject: [PATCH 05/16] Removing excessive conditions --- ignite/pkg/availableport/availableport.go | 98 ++++++++--------------- 1 file changed, 33 insertions(+), 65 deletions(-) diff --git a/ignite/pkg/availableport/availableport.go b/ignite/pkg/availableport/availableport.go index cf246ac9d7..bec3ffcde5 100644 --- a/ignite/pkg/availableport/availableport.go +++ b/ignite/pkg/availableport/availableport.go @@ -7,98 +7,64 @@ import ( "time" ) -type optionalParameters struct { - WithRandomizer *rand.Rand - WithMinPort int - WithMaxPort int +type availablePortOptions struct { + randomizer *rand.Rand + minPort uint + maxPort uint } -type OptionalParameters func(o *optionalParameters) +type Options func(o *availablePortOptions) -func WithRandomizer(r *rand.Rand) OptionalParameters { - return func(o *optionalParameters) { - o.WithRandomizer = r +func WithRandomizer(r *rand.Rand) Options { + return func(o *availablePortOptions) { + o.randomizer = r } } -func WithMaxPort(maxPort int) OptionalParameters { - return func(o *optionalParameters) { - o.WithMaxPort = maxPort +func WithMaxPort(maxPort uint) Options { + return func(o *availablePortOptions) { + o.maxPort = maxPort } } -func WithMinPort(minPort int) OptionalParameters { - return func(o *optionalParameters) { - o.WithMinPort = minPort +func WithMinPort(minPort uint) Options { + return func(o *availablePortOptions) { + o.minPort = minPort } } // Find finds n number of unused ports. // it is not guaranteed that these ports will not be allocated to // another program in the time of calling Find(). -func Find(n int, moreParameters ...OptionalParameters) (ports []int, err error) { - // Defining them before so we can set a value depending on the OptionalParameters - var min int - var max int - var r *rand.Rand - - options := &optionalParameters{} - if len(moreParameters) != 0 { - opt := moreParameters[0] - opt(options) - } else { - // If we don't require special conditions, we can - // return to the original parameters - min = 44000 - max = 55000 - r = rand.New(rand.NewSource(time.Now().UnixNano())) - } - - if options.WithMinPort != 0 { - if options.WithMinPort > -1 { - min = options.WithMinPort - } else { - // This is not required since the port would become 0 - // but the user could not notice that sent a negative port - return nil, fmt.Errorf("ports can't be negative (negative min port given)") - } - } else { - min = 44000 +func Find(n uint, options ...Options) (ports []uint, err error) { + // Defining them before so we can set a value depending on the AvailablePortOptions + opts := availablePortOptions{ + minPort: 44000, + maxPort: 55000, + randomizer: rand.New(rand.NewSource(time.Now().UnixNano())), } - if options.WithMaxPort != 0 { - if options.WithMaxPort > -1 { - max = options.WithMaxPort - } else { - // This is not required since the port would become 0 - // but the user could not notice that sent a negative port - return nil, fmt.Errorf("ports can't be negative (negative max port given)") - } - } else { - max = 55000 - } - if options.WithRandomizer != nil { - r = options.WithRandomizer - } else { - r = rand.New(rand.NewSource(time.Now().UnixNano())) + for _, apply := range options { + apply(&opts) } - // If the number of ports required is bigger than the range, this stops it - if max < min { - return nil, fmt.Errorf("invalid ports range: max < min (%d < %d)", max, min) + if opts.maxPort < opts.minPort { + return nil, fmt.Errorf("invalid ports range: max < min (%d < %d)", opts.maxPort, opts.minPort) } // If the number of ports required is bigger than the range, this stops it - if n > (max - min) { - return nil, fmt.Errorf("invalid amount of ports requested: limit is %d", max-min) + if n > (opts.maxPort - opts.minPort) { + return nil, fmt.Errorf("invalid amount of ports requested: limit is %d", opts.maxPort-opts.minPort) } // Marker to point if a port is already added in the list - registered := make(map[int]bool) - for i := 0; i < n; i++ { + registered := make(map[uint]bool) + for i := uint(0); i < n; i++ { for { // Greater or equal to min and lower than max - port := r.Intn(max-min+1) + min + totalPorts := opts.maxPort - opts.minPort + 1 + randomPort := opts.randomizer.Intn(int(totalPorts)) + port := uint(randomPort) + opts.minPort conn, err := net.Dial("tcp", fmt.Sprintf(":%d", port)) // if there is an error, this might mean that no one is listening from this port @@ -107,6 +73,8 @@ func Find(n int, moreParameters ...OptionalParameters) (ports []int, err error) conn.Close() continue } + defer conn.Close() + // if the port is already registered we skip it to the next one // otherwise it's added to the ports list and pointed in our map if registered[port] { From 6988bc9784092ac19149d879fffb8fa527276ecf Mon Sep 17 00:00:00 2001 From: iamagf Date: Thu, 10 Aug 2023 20:30:16 -0600 Subject: [PATCH 06/16] Changelog added --- changelog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog.md b/changelog.md index 4b682d0de0..22d14408cb 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,7 @@ ### Changes +- [#3621](https://github.com/ignite/cli/pull/3621) Refactor `ignite/pkg/availablePort` package to allow custom parametersin `Find` function. - [#3581](https://github.com/ignite/cli/pull/3581) Bump cometbft and cometbft-db in the template - [#3559](https://github.com/ignite/cli/pull/3559) Bump network plugin version to `v0.1.1` - [#3522](https://github.com/ignite/cli/pull/3522) Remove indentation from `chain serve` output From aa0b2d8dc93c43b4c92e5feda89e91b4f8a1d7be Mon Sep 17 00:00:00 2001 From: iamagf Date: Sun, 3 Sep 2023 15:06:57 -0600 Subject: [PATCH 07/16] Avoids nil conn error. --- ignite/pkg/availableport/availableport.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ignite/pkg/availableport/availableport.go b/ignite/pkg/availableport/availableport.go index 4f36aea374..3373cf66f1 100644 --- a/ignite/pkg/availableport/availableport.go +++ b/ignite/pkg/availableport/availableport.go @@ -4,6 +4,7 @@ import ( "fmt" "math/rand" "net" + "time" ) type availablePortOptions struct { @@ -72,7 +73,9 @@ func Find(n uint, options ...Options) (ports []uint, err error) { conn.Close() continue } - defer conn.Close() + if conn != nil { + defer conn.Close() + } // if the port is already registered we skip it to the next one // otherwise it's added to the ports list and pointed in our map From f0d51d99a99681c2b839da5a3ba004e35a2c5517 Mon Sep 17 00:00:00 2001 From: iamagf Date: Sun, 3 Sep 2023 15:07:20 -0600 Subject: [PATCH 08/16] Tests corrected for updated code. --- .../pkg/availableport/availableport_test.go | 63 ++++++++----------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/ignite/pkg/availableport/availableport_test.go b/ignite/pkg/availableport/availableport_test.go index f8440b5985..73dfbf1d85 100644 --- a/ignite/pkg/availableport/availableport_test.go +++ b/ignite/pkg/availableport/availableport_test.go @@ -22,62 +22,49 @@ func TestAvailablePort(t *testing.T) { } // Case no ports generated - options := availableport.OptionalParameters{ - WithMinPort: 1, - WithMaxPort: 1, + options := []availableport.Options{ + availableport.WithMinPort(1), + availableport.WithMaxPort(1), } - ports, err = availableport.Find(10, options) + ports, err = availableport.Find(10, options...) require.Equal(t, fmt.Errorf("invalid amount of ports requested: limit is 0"), err) require.Equal(t, 0, len(ports)) - // Case max < min - options = availableport.OptionalParameters{ - WithMinPort: 5, - WithMaxPort: 1, + // // Case max < min + options = []availableport.Options{ + availableport.WithMinPort(5), + availableport.WithMaxPort(1), } - ports, err = availableport.Find(10, options) + ports, err = availableport.Find(10, options...) require.Equal(t, fmt.Errorf("invalid ports range: max < min (1 < 5)"), err) require.Equal(t, 0, len(ports)) // Case max < min min restriction given - options = availableport.OptionalParameters{ - WithMinPort: 55001, + options = []availableport.Options{ + availableport.WithMinPort(55001), + availableport.WithMaxPort(1), } - ports, err = availableport.Find(10, options) - require.Equal(t, fmt.Errorf("invalid ports range: max < min (55000 < 55001)"), err) + ports, err = availableport.Find(10, options...) + require.Equal(t, fmt.Errorf("invalid ports range: max < min (1 < 55001)"), err) require.Equal(t, 0, len(ports)) - // Case max < min max restriction given - options = availableport.OptionalParameters{ - WithMaxPort: 43999, + // Case max < min max restriction given + options = []availableport.Options{ + availableport.WithMaxPort(43999), } - ports, err = availableport.Find(10, options) + ports, err = availableport.Find(10, options...) require.Equal(t, fmt.Errorf("invalid ports range: max < min (43999 < 44000)"), err) require.Equal(t, 0, len(ports)) - // Case negative min - options = availableport.OptionalParameters{ - WithMinPort: -10, - } - ports, err = availableport.Find(10, options) - require.Equal(t, fmt.Errorf("ports can't be negative (negative min port given)"), err) - require.Equal(t, 0, len(ports)) + // Case randomizer given - // Case negative max - options = availableport.OptionalParameters{ - WithMaxPort: -10, + options = []availableport.Options{ + availableport.WithRandomizer(rand.New(rand.NewSource(2023))), + availableport.WithMinPort(100), + availableport.WithMaxPort(200), } - ports, err = availableport.Find(10, options) - require.Equal(t, fmt.Errorf("ports can't be negative (negative max port given)"), err) - require.Equal(t, 0, len(ports)) - // Case randomizer given - options = availableport.OptionalParameters{ - WithRandomizer: rand.New(rand.NewSource(2023)), - WithMinPort: 100, - WithMaxPort: 200, - } - ports, err = availableport.Find(10, options) + ports, err = availableport.Find(10, options...) require.Equal(t, 10, len(ports)) - require.Equal(t, []int{195, 129, 150, 108, 120, 169, 166, 121, 131, 160}, ports) + require.Equal(t, []uint([]uint{0xc3, 0x81, 0x96, 0x6c, 0x78, 0xa9, 0xa6, 0x79, 0x83, 0xa0}), ports) } From ceba39f25a5d38032e973179e218e474d890d24b Mon Sep 17 00:00:00 2001 From: iamagf Date: Sun, 3 Sep 2023 15:16:37 -0600 Subject: [PATCH 09/16] Param update in `genAddr` to avoid type error --- integration/app.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration/app.go b/integration/app.go index c9c5a0396a..54516c5d7b 100644 --- a/integration/app.go +++ b/integration/app.go @@ -218,7 +218,7 @@ func (a App) RandomizeServerPorts() Hosts { ports, err := availableport.Find(7) require.NoError(a.env.t, err) - genAddr := func(port int) string { + genAddr := func(port uint) string { return fmt.Sprintf("127.0.0.1:%d", port) } From bdadeada32b2f618f7d5fa6787395f94f35105d1 Mon Sep 17 00:00:00 2001 From: iamagf Date: Wed, 6 Sep 2023 10:11:05 -0600 Subject: [PATCH 10/16] Faucet.Port int to uint --- ignite/config/chain/base/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/config/chain/base/config.go b/ignite/config/chain/base/config.go index b71de4e1ef..d7900c022d 100644 --- a/ignite/config/chain/base/config.go +++ b/ignite/config/chain/base/config.go @@ -126,7 +126,7 @@ type Faucet struct { Host string `yaml:"host,omitempty"` // Port number for faucet server to listen at. - Port int `yaml:"port,omitempty"` + Port uint `yaml:"port,omitempty"` } // Init overwrites sdk configurations with given values. From 18b01dbfbda57bc3f89ba8c1308869a993da22b5 Mon Sep 17 00:00:00 2001 From: iamagf Date: Wed, 6 Sep 2023 10:12:31 -0600 Subject: [PATCH 11/16] Editing 0 int to uint for Faucet.Port type --- ignite/config/chain/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ignite/config/chain/config.go b/ignite/config/chain/config.go index e77c65c77d..b19cc7fbe3 100644 --- a/ignite/config/chain/config.go +++ b/ignite/config/chain/config.go @@ -75,7 +75,7 @@ func FaucetHost(cfg *Config) string { // We keep supporting Port option for backward compatibility // TODO: drop this option in the future host := cfg.Faucet.Host - if cfg.Faucet.Port != 0 { + if cfg.Faucet.Port != uint(0) { host = fmt.Sprintf(":%d", cfg.Faucet.Port) } From 571813e3bcbb7c7732ba2d10d96e3de219a9f039 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 13 Oct 2023 02:34:20 +0200 Subject: [PATCH 12/16] add table tests --- .../pkg/availableport/availableport_test.go | 120 ++++++++++-------- 1 file changed, 64 insertions(+), 56 deletions(-) diff --git a/ignite/pkg/availableport/availableport_test.go b/ignite/pkg/availableport/availableport_test.go index 73dfbf1d85..cd42b0b6ff 100644 --- a/ignite/pkg/availableport/availableport_test.go +++ b/ignite/pkg/availableport/availableport_test.go @@ -5,66 +5,74 @@ import ( "math/rand" "testing" - "github.com/ignite/cli/ignite/pkg/availableport" "github.com/stretchr/testify/require" -) - -func TestAvailablePort(t *testing.T) { - - ports, err := availableport.Find(10) - require.Equal(t, nil, err) - require.Equal(t, 10, len(ports)) - - for idx := 0; idx < 9; idx++ { - for jdx := idx + 1; jdx < 10; jdx++ { - require.NotEqual(t, ports[idx], ports[jdx]) - } - } - - // Case no ports generated - options := []availableport.Options{ - availableport.WithMinPort(1), - availableport.WithMaxPort(1), - } - ports, err = availableport.Find(10, options...) - require.Equal(t, fmt.Errorf("invalid amount of ports requested: limit is 0"), err) - require.Equal(t, 0, len(ports)) - // // Case max < min - options = []availableport.Options{ - availableport.WithMinPort(5), - availableport.WithMaxPort(1), - } - ports, err = availableport.Find(10, options...) - require.Equal(t, fmt.Errorf("invalid ports range: max < min (1 < 5)"), err) - require.Equal(t, 0, len(ports)) - - // Case max < min min restriction given - options = []availableport.Options{ - availableport.WithMinPort(55001), - availableport.WithMaxPort(1), - } - ports, err = availableport.Find(10, options...) - require.Equal(t, fmt.Errorf("invalid ports range: max < min (1 < 55001)"), err) - require.Equal(t, 0, len(ports)) + "github.com/ignite/cli/ignite/pkg/availableport" +) - // Case max < min max restriction given - options = []availableport.Options{ - availableport.WithMaxPort(43999), +func TestFind(t *testing.T) { + tests := []struct { + name string + n uint + options []availableport.Options + err error + }{ + { + name: "test 10 ports", + n: 10, + }, + { + name: "invalid port range", + n: 10, + options: []availableport.Options{ + availableport.WithMinPort(5), + availableport.WithMaxPort(1), + }, + err: fmt.Errorf("invalid ports range: max < min (1 < 5)"), + }, + { + name: "invalid maximum port range", + n: 10, + options: []availableport.Options{ + availableport.WithMinPort(55001), + availableport.WithMaxPort(1), + }, + err: fmt.Errorf("invalid ports range: max < min (1 < 55001)"), + }, + { + name: "only invalid maximum port range", + n: 10, + options: []availableport.Options{ + availableport.WithMaxPort(43999), + }, + err: fmt.Errorf("invalid ports range: max < min (43999 < 44000)"), + }, + { + name: "with randomizer", + n: 100, + options: []availableport.Options{ + availableport.WithRandomizer(rand.New(rand.NewSource(2023))), + availableport.WithMinPort(100), + availableport.WithMaxPort(200), + }, + }, } - ports, err = availableport.Find(10, options...) - require.Equal(t, fmt.Errorf("invalid ports range: max < min (43999 < 44000)"), err) - require.Equal(t, 0, len(ports)) - - // Case randomizer given + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := availableport.Find(tt.n, tt.options...) + if tt.err != nil { + require.Equal(t, tt.err, err) + return + } + require.NoError(t, err) + require.Len(t, got, int(tt.n)) - options = []availableport.Options{ - availableport.WithRandomizer(rand.New(rand.NewSource(2023))), - availableport.WithMinPort(100), - availableport.WithMaxPort(200), + seen := make(map[uint]struct{}) + for _, val := range got { + _, ok := seen[val] + require.Falsef(t, ok, "duplicated port %d", val) + seen[val] = struct{}{} + } + }) } - - ports, err = availableport.Find(10, options...) - require.Equal(t, 10, len(ports)) - require.Equal(t, []uint([]uint{0xc3, 0x81, 0x96, 0x6c, 0x78, 0xa9, 0xa6, 0x79, 0x83, 0xa0}), ports) } From 8f9570cd1a04df523f5fa3bc0a69e6f7905ba563 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 13 Oct 2023 02:34:42 +0200 Subject: [PATCH 13/16] remove unused double for --- ignite/pkg/availableport/availableport.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/ignite/pkg/availableport/availableport.go b/ignite/pkg/availableport/availableport.go index 3373cf66f1..b4d461f398 100644 --- a/ignite/pkg/availableport/availableport.go +++ b/ignite/pkg/availableport/availableport.go @@ -84,8 +84,6 @@ func Find(n uint, options ...Options) (ports []uint, err error) { } ports = append(ports, port) registered[port] = true - break - } } return ports, nil } From 1388ce41a5ec95f13ed1ed0fccc3cedb1e04db79 Mon Sep 17 00:00:00 2001 From: Danilo Pantani Date: Fri, 13 Oct 2023 02:34:50 +0200 Subject: [PATCH 14/16] remove unused double for --- ignite/pkg/availableport/availableport.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ignite/pkg/availableport/availableport.go b/ignite/pkg/availableport/availableport.go index b4d461f398..f681fb32c5 100644 --- a/ignite/pkg/availableport/availableport.go +++ b/ignite/pkg/availableport/availableport.go @@ -59,8 +59,7 @@ func Find(n uint, options ...Options) (ports []uint, err error) { // Marker to point if a port is already added in the list registered := make(map[uint]bool) - for i := uint(0); i < n; i++ { - for { + for len(registered) < int(n) { // Greater or equal to min and lower than max totalPorts := opts.maxPort - opts.minPort + 1 randomPort := opts.randomizer.Intn(int(totalPorts)) From bf9f50c18fb5510e73e1636d63cb17d296dcf6cf Mon Sep 17 00:00:00 2001 From: Pantani Date: Fri, 13 Oct 2023 02:35:47 +0200 Subject: [PATCH 15/16] format the code --- ignite/pkg/availableport/availableport.go | 42 +++++++++++------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/ignite/pkg/availableport/availableport.go b/ignite/pkg/availableport/availableport.go index f681fb32c5..77f9445d63 100644 --- a/ignite/pkg/availableport/availableport.go +++ b/ignite/pkg/availableport/availableport.go @@ -60,29 +60,29 @@ func Find(n uint, options ...Options) (ports []uint, err error) { // Marker to point if a port is already added in the list registered := make(map[uint]bool) for len(registered) < int(n) { - // Greater or equal to min and lower than max - totalPorts := opts.maxPort - opts.minPort + 1 - randomPort := opts.randomizer.Intn(int(totalPorts)) - port := uint(randomPort) + opts.minPort + // Greater or equal to min and lower than max + totalPorts := opts.maxPort - opts.minPort + 1 + randomPort := opts.randomizer.Intn(int(totalPorts)) + port := uint(randomPort) + opts.minPort - conn, err := net.Dial("tcp", fmt.Sprintf(":%d", port)) - // if there is an error, this might mean that no one is listening from this port - // which is what we need. - if err == nil { - conn.Close() - continue - } - if conn != nil { - defer conn.Close() - } + conn, err := net.Dial("tcp", fmt.Sprintf(":%d", port)) + // if there is an error, this might mean that no one is listening from this port + // which is what we need. + if err == nil { + conn.Close() + continue + } + if conn != nil { + defer conn.Close() + } - // if the port is already registered we skip it to the next one - // otherwise it's added to the ports list and pointed in our map - if registered[port] { - continue - } - ports = append(ports, port) - registered[port] = true + // if the port is already registered we skip it to the next one + // otherwise it's added to the ports list and pointed in our map + if registered[port] { + continue + } + ports = append(ports, port) + registered[port] = true } return ports, nil } From 1cc6d4cf34e797d77c0ed6864cf48415cbe7f665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jer=C3=B3nimo=20Albi?= Date: Fri, 13 Oct 2023 09:57:30 +0200 Subject: [PATCH 16/16] Update changelog.md --- changelog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog.md b/changelog.md index d8870e6185..6ddc00384f 100644 --- a/changelog.md +++ b/changelog.md @@ -11,7 +11,7 @@ ### Changes -- [#3621](https://github.com/ignite/cli/pull/3621) Refactor `ignite/pkg/availablePort` package to allow custom parametersin `Find` function. +- [#3621](https://github.com/ignite/cli/pull/3621) Change `pkg/availableport` to allow custom parameters in `Find` function and handle duplicated ports - [#3559](https://github.com/ignite/cli/pull/3559) Bump network plugin version to `v0.1.1` - [#3581](https://github.com/ignite/cli/pull/3581) Bump cometbft and cometbft-db in the template - [#3522](https://github.com/ignite/cli/pull/3522) Remove indentation from `chain serve` output