@@ -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
2524func TestPortsUpdateState (t * testing.T ) {
@@ -556,7 +555,7 @@ func TestPortsUpdateState(t *testing.T) {
556555 for _ , c := range test .Changes {
557556 if c .Config != nil {
558557 change := & Configs {}
559- change .workspaceConfigs = parseWorkspaceConfigs (c .Config .workspace )
558+ change .workspaceConfigs , change . workspaceConfigsOrder = parseWorkspaceConfigs (c .Config .workspace )
560559 portConfigs , rangeConfigs := parseInstanceConfigs (c .Config .instance )
561560 change .instancePortConfigs = portConfigs
562561 change .instanceRangeConfigs = rangeConfigs
@@ -746,3 +745,95 @@ func TestPortsConcurrentSubscribe(t *testing.T) {
746745
747746 wg .Wait ()
748747}
748+
749+ func TestManager_getStatus (t * testing.T ) {
750+ type fields struct {
751+ orderInYaml []uint32
752+ state map [uint32 ]bool
753+ }
754+ tests := []struct {
755+ name string
756+ fields fields
757+ want []uint32
758+ }{
759+ {
760+ name : "happy path" ,
761+ fields : fields {
762+ orderInYaml : []uint32 {1002 , 1000 , 1001 },
763+ state : map [uint32 ]bool {
764+ 1000 : true ,
765+ 1001 : true ,
766+ 1002 : true ,
767+ 4002 : true ,
768+ 4000 : true ,
769+ 5000 : false ,
770+ 5005 : true ,
771+ },
772+ },
773+ want : []uint32 {1002 , 1000 , 1001 , 4000 , 4002 , 5005 },
774+ },
775+ {
776+ name : "ignore ports that not served and not exists in yaml" ,
777+ fields : fields {
778+ orderInYaml : []uint32 {1002 , 1000 , 1001 },
779+ state : map [uint32 ]bool {
780+ 1000 : true ,
781+ 1001 : true ,
782+ 1002 : true ,
783+ 4000 : false ,
784+ },
785+ },
786+ want : []uint32 {1002 , 1000 , 1001 },
787+ },
788+ {
789+ name : "not ignore ports that not served but exists in yaml" ,
790+ fields : fields {
791+ orderInYaml : []uint32 {1002 , 1000 , 1001 },
792+ state : map [uint32 ]bool {1002 : false , 1000 : false , 1001 : false },
793+ },
794+ want : []uint32 {1002 , 1000 , 1001 },
795+ },
796+ {
797+ name : "served ports order by number ASC" ,
798+ fields : fields {
799+ orderInYaml : []uint32 {},
800+ state : map [uint32 ]bool {
801+ 4000 : true ,
802+ 4003 : true ,
803+ 4007 : true ,
804+ 4001 : true ,
805+ 4006 : false ,
806+ },
807+ },
808+ want : []uint32 {4000 , 4001 , 4003 , 4007 },
809+ },
810+ }
811+ for _ , tt := range tests {
812+ t .Run (tt .name , func (t * testing.T ) {
813+ state := make (map [uint32 ]* managedPort )
814+ for port , served := range tt .fields .state {
815+ state [port ] = & managedPort {
816+ Served : served ,
817+ LocalhostPort : port ,
818+ TunneledTargetPort : port ,
819+ TunneledClients : map [string ]uint32 {},
820+ }
821+ }
822+ pm := & Manager {
823+ configs : & Configs {
824+ workspaceConfigsOrder : tt .fields .orderInYaml ,
825+ },
826+ state : state ,
827+ }
828+ got := pm .getStatus ()
829+ if len (got ) != len (tt .want ) {
830+ t .Errorf ("Manager.getStatus() length = %v, want %v" , len (got ), len (tt .want ))
831+ }
832+ for i := 0 ; i < len (got ); i ++ {
833+ if got [i ].LocalPort != tt .want [i ] {
834+ t .Errorf ("Manager.getStatus() %v, want %v" , got , tt .want )
835+ }
836+ }
837+ })
838+ }
839+ }
0 commit comments