Skip to content

Commit fae57be

Browse files
committed
[supervisor] add test cases for ports order
1 parent 03b0dc4 commit fae57be

File tree

1 file changed

+89
-4
lines changed

1 file changed

+89
-4
lines changed

Diff for: components/supervisor/pkg/ports/ports_test.go

+89-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ import (
1212
"testing"
1313
"time"
1414

15+
"github.com/gitpod-io/gitpod/common-go/log"
16+
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
17+
"github.com/gitpod-io/gitpod/supervisor/api"
1518
"github.com/google/go-cmp/cmp"
1619
"github.com/google/go-cmp/cmp/cmpopts"
1720
"github.com/sirupsen/logrus"
1821
"golang.org/x/sync/errgroup"
19-
20-
"github.com/gitpod-io/gitpod/common-go/log"
21-
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
22-
"github.com/gitpod-io/gitpod/supervisor/api"
2322
)
2423

2524
func TestPortsUpdateState(t *testing.T) {
@@ -746,3 +745,89 @@ func TestPortsConcurrentSubscribe(t *testing.T) {
746745

747746
wg.Wait()
748747
}
748+
749+
func TestManager_getStatus(t *testing.T) {
750+
type fields struct {
751+
orderInYaml []any
752+
state []uint32
753+
}
754+
tests := []struct {
755+
name string
756+
fields fields
757+
want []uint32
758+
}{
759+
{
760+
name: "happy path",
761+
fields: fields{
762+
// The port number (e.g. 1337) or range (e.g. 3000-3999) to expose.
763+
orderInYaml: []any{1002, 1000, "3000-3999", 1001},
764+
state: []uint32{1000, 1001, 1002, 3003, 3001, 3002, 4002, 4000, 5000, 5005},
765+
},
766+
want: []uint32{1002, 1000, 3001, 3002, 3003, 1001, 4000, 4002, 5000, 5005},
767+
},
768+
{
769+
name: "order for ranged ports and inside ranged order by number ASC",
770+
fields: fields{
771+
orderInYaml: []any{1002, "3000-3999", 1009, "4000-4999"},
772+
state: []uint32{5000, 1000, 1009, 4000, 4001, 3000, 3009},
773+
},
774+
want: []uint32{3000, 3009, 1009, 4000, 4001, 1000, 5000},
775+
},
776+
{
777+
name: "served ports order by number ASC",
778+
fields: fields{
779+
orderInYaml: []any{},
780+
state: []uint32{4000, 4003, 4007, 4001, 4006},
781+
},
782+
want: []uint32{4000, 4001, 4003, 4006, 4007},
783+
},
784+
785+
// It will not works because we do not `Run` ports Manger
786+
// As ports Manger will autoExpose those ports (but not ranged port) in yaml
787+
// and they will exists in state
788+
// {
789+
// name: "not ignore ports that not served but exists in yaml",
790+
// fields: fields{
791+
// orderInYaml: []any{1002, 1000, 1001},
792+
// state: []uint32{},
793+
// },
794+
// want: []uint32{1002, 1000, 1001},
795+
// },
796+
}
797+
for _, tt := range tests {
798+
t.Run(tt.name, func(t *testing.T) {
799+
state := make(map[uint32]*managedPort)
800+
for _, port := range tt.fields.state {
801+
state[port] = &managedPort{
802+
Served: true,
803+
LocalhostPort: port,
804+
TunneledTargetPort: port,
805+
TunneledClients: map[string]uint32{},
806+
}
807+
}
808+
portsItems := []*gitpod.PortsItems{}
809+
for _, port := range tt.fields.orderInYaml {
810+
portsItems = append(portsItems, &gitpod.PortsItems{Port: port})
811+
}
812+
portsConfig, rangeConfig := parseInstanceConfigs(portsItems)
813+
pm := &Manager{
814+
configs: &Configs{
815+
instancePortConfigs: portsConfig,
816+
instanceRangeConfigs: rangeConfig,
817+
},
818+
state: state,
819+
}
820+
got := pm.getStatus()
821+
if len(got) != len(tt.want) {
822+
t.Errorf("Manager.getStatus() length = %v, want %v", len(got), len(tt.want))
823+
}
824+
gotPorts := []uint32{}
825+
for _, g := range got {
826+
gotPorts = append(gotPorts, g.LocalPort)
827+
}
828+
if diff := cmp.Diff(gotPorts, tt.want); diff != "" {
829+
t.Errorf("unexpected exposures (-want +got):\n%s", diff)
830+
}
831+
})
832+
}
833+
}

0 commit comments

Comments
 (0)