Skip to content

Commit fbb8a81

Browse files
mustard-mhroboquat
authored andcommitted
[supervisor] add test cases for ports order
1 parent 71889fd commit fbb8a81

File tree

1 file changed

+98
-10
lines changed

1 file changed

+98
-10
lines changed

components/supervisor/pkg/ports/ports_test.go

Lines changed: 98 additions & 10 deletions
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) {
@@ -64,8 +63,8 @@ func TestPortsUpdateState(t *testing.T) {
6463
[]*api.PortsStatus{{LocalPort: 8080, Served: true, OnOpen: api.PortsStatus_notify_private}},
6564
[]*api.PortsStatus{{LocalPort: 8080, Served: true, OnOpen: api.PortsStatus_notify_private, Exposed: &api.ExposedPortInfo{OnExposed: api.OnPortExposedAction_notify_private, Visibility: api.PortVisibility_private, Url: "foobar"}}},
6665
[]*api.PortsStatus{{LocalPort: 8080, Served: true, OnOpen: api.PortsStatus_notify_private, Exposed: &api.ExposedPortInfo{OnExposed: api.OnPortExposedAction_notify_private, Visibility: api.PortVisibility_private, Url: "foobar"}}, {LocalPort: 60000, Served: true}},
67-
[]*api.PortsStatus{{LocalPort: 8080, Served: false, OnOpen: api.PortsStatus_notify_private, Exposed: &api.ExposedPortInfo{OnExposed: api.OnPortExposedAction_notify_private, Visibility: api.PortVisibility_private, Url: "foobar"}}, {LocalPort: 60000, Served: true}},
68-
[]*api.PortsStatus{{LocalPort: 8080, Served: false, OnOpen: api.PortsStatus_notify_private, Exposed: &api.ExposedPortInfo{OnExposed: api.OnPortExposedAction_notify_private, Visibility: api.PortVisibility_private, Url: "foobar"}}},
66+
[]*api.PortsStatus{{LocalPort: 60000, Served: true}},
67+
{},
6968
},
7069
},
7170
{
@@ -86,15 +85,18 @@ func TestPortsUpdateState(t *testing.T) {
8685
{
8786
Desc: "basic port publically exposed",
8887
Changes: []Change{
89-
{Exposed: []ExposedPort{{LocalPort: 8080, Public: false, URL: "foobar"}}},
90-
{Exposed: []ExposedPort{{LocalPort: 8080, Public: true, URL: "foobar"}}},
9188
{Served: []ServedPort{{Port: 8080}}},
89+
{Exposed: []ExposedPort{{LocalPort: 8080, Public: true, URL: "foobar"}}},
90+
{Exposed: []ExposedPort{{LocalPort: 8080, Public: false, URL: "foobar"}}},
91+
},
92+
ExpectedExposure: ExposureExpectation{
93+
{LocalPort: 8080},
9294
},
9395
ExpectedUpdates: UpdateExpectation{
9496
{},
95-
[]*api.PortsStatus{{LocalPort: 8080, OnOpen: api.PortsStatus_notify_private, Exposed: &api.ExposedPortInfo{Visibility: api.PortVisibility_private, Url: "foobar", OnExposed: api.OnPortExposedAction_notify_private}}},
96-
[]*api.PortsStatus{{LocalPort: 8080, OnOpen: api.PortsStatus_notify_private, Exposed: &api.ExposedPortInfo{Visibility: api.PortVisibility_public, Url: "foobar", OnExposed: api.OnPortExposedAction_notify_private}}},
97+
[]*api.PortsStatus{{LocalPort: 8080, Served: true, OnOpen: api.PortsStatus_notify_private}},
9798
[]*api.PortsStatus{{LocalPort: 8080, Served: true, OnOpen: api.PortsStatus_notify_private, Exposed: &api.ExposedPortInfo{Visibility: api.PortVisibility_public, Url: "foobar", OnExposed: api.OnPortExposedAction_notify_private}}},
99+
[]*api.PortsStatus{{LocalPort: 8080, Served: true, OnOpen: api.PortsStatus_notify_private, Exposed: &api.ExposedPortInfo{Visibility: api.PortVisibility_private, Url: "foobar", OnExposed: api.OnPortExposedAction_notify_private}}},
98100
},
99101
},
100102
{
@@ -746,3 +748,89 @@ func TestPortsConcurrentSubscribe(t *testing.T) {
746748

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

0 commit comments

Comments
 (0)