@@ -12,14 +12,13 @@ import (
12
12
"testing"
13
13
"time"
14
14
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"
15
18
"github.com/google/go-cmp/cmp"
16
19
"github.com/google/go-cmp/cmp/cmpopts"
17
20
"github.com/sirupsen/logrus"
18
21
"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"
23
22
)
24
23
25
24
func TestPortsUpdateState (t * testing.T ) {
@@ -64,8 +63,8 @@ func TestPortsUpdateState(t *testing.T) {
64
63
[]* api.PortsStatus {{LocalPort : 8080 , Served : true , OnOpen : api .PortsStatus_notify_private }},
65
64
[]* 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" }}},
66
65
[]* 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
+ { },
69
68
},
70
69
},
71
70
{
@@ -86,15 +85,18 @@ func TestPortsUpdateState(t *testing.T) {
86
85
{
87
86
Desc : "basic port publically exposed" ,
88
87
Changes : []Change {
89
- {Exposed : []ExposedPort {{LocalPort : 8080 , Public : false , URL : "foobar" }}},
90
- {Exposed : []ExposedPort {{LocalPort : 8080 , Public : true , URL : "foobar" }}},
91
88
{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 },
92
94
},
93
95
ExpectedUpdates : UpdateExpectation {
94
96
{},
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 }},
97
98
[]* 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 }}},
98
100
},
99
101
},
100
102
{
@@ -746,3 +748,89 @@ func TestPortsConcurrentSubscribe(t *testing.T) {
746
748
747
749
wg .Wait ()
748
750
}
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