@@ -23,3 +23,137 @@ func TestCompleteMemoryGiB(t *testing.T) {
2323 assert .DeepEqual (t , []float32 {1 , 2 , 4 }, completeMemoryGiB (8 << 30 ))
2424 assert .DeepEqual (t , []float32 {1 , 2 , 4 , 8 , 10 }, completeMemoryGiB (20 << 30 ))
2525}
26+
27+ func TestBuildPortForwardExpression (t * testing.T ) {
28+ tests := []struct {
29+ name string
30+ portForwards []string
31+ expected string
32+ expectError bool
33+ }{
34+ {
35+ name : "empty port forwards" ,
36+ portForwards : []string {},
37+ expected : "" ,
38+ },
39+ {
40+ name : "single dynamic port forward" ,
41+ portForwards : []string {"8080:80" },
42+ expected : `.portForwards += [{"guestPort": "80", "hostPort": "8080", "static": false}]` ,
43+ },
44+ {
45+ name : "single static port forward" ,
46+ portForwards : []string {"8080:80,static=true" },
47+ expected : `.portForwards += [{"guestPort": "80", "hostPort": "8080", "static": true}]` ,
48+ },
49+ {
50+ name : "multiple mixed port forwards" ,
51+ portForwards : []string {"8080:80" , "2222:22,static=true" , "3000:3000" },
52+ expected : `.portForwards += [{"guestPort": "80", "hostPort": "8080", "static": false},{"guestPort": "22", "hostPort": "2222", "static": true},{"guestPort": "3000", "hostPort": "3000", "static": false}]` ,
53+ },
54+ {
55+ name : "invalid format - missing colon" ,
56+ portForwards : []string {"8080" },
57+ expectError : true ,
58+ },
59+ {
60+ name : "invalid format - too many colons" ,
61+ portForwards : []string {"8080:80:extra" },
62+ expectError : true ,
63+ },
64+ {
65+ name : "invalid static parameter" ,
66+ portForwards : []string {"8080:80,invalid=true" },
67+ expectError : true ,
68+ },
69+ {
70+ name : "too many parameters" ,
71+ portForwards : []string {"8080:80,static=true,extra=value" },
72+ expectError : true ,
73+ },
74+ {
75+ name : "whitespace handling" ,
76+ portForwards : []string {" 8080 : 80 , static=true " },
77+ expected : `.portForwards += [{"guestPort": "80", "hostPort": "8080", "static": true}]` ,
78+ },
79+ }
80+
81+ for _ , tt := range tests {
82+ t .Run (tt .name , func (t * testing.T ) {
83+ result , err := BuildPortForwardExpression (tt .portForwards )
84+ if tt .expectError {
85+ assert .Check (t , err != nil )
86+ } else {
87+ assert .NilError (t , err )
88+ assert .Equal (t , tt .expected , result )
89+ }
90+ })
91+ }
92+ }
93+
94+ func TestParsePortForward (t * testing.T ) {
95+ tests := []struct {
96+ name string
97+ spec string
98+ hostPort string
99+ guestPort string
100+ isStatic bool
101+ expectError bool
102+ }{
103+ {
104+ name : "dynamic port forward" ,
105+ spec : "8080:80" ,
106+ hostPort : "8080" ,
107+ guestPort : "80" ,
108+ isStatic : false ,
109+ },
110+ {
111+ name : "static port forward" ,
112+ spec : "8080:80,static=true" ,
113+ hostPort : "8080" ,
114+ guestPort : "80" ,
115+ isStatic : true ,
116+ },
117+ {
118+ name : "whitespace handling" ,
119+ spec : " 8080 : 80 , static=true " ,
120+ hostPort : "8080" ,
121+ guestPort : "80" ,
122+ isStatic : true ,
123+ },
124+ {
125+ name : "invalid format - missing colon" ,
126+ spec : "8080" ,
127+ expectError : true ,
128+ },
129+ {
130+ name : "invalid format - too many colons" ,
131+ spec : "8080:80:extra" ,
132+ expectError : true ,
133+ },
134+ {
135+ name : "invalid parameter" ,
136+ spec : "8080:80,invalid=true" ,
137+ expectError : true ,
138+ },
139+ {
140+ name : "too many parameters" ,
141+ spec : "8080:80,static=true,extra=value" ,
142+ expectError : true ,
143+ },
144+ }
145+
146+ for _ , tt := range tests {
147+ t .Run (tt .name , func (t * testing.T ) {
148+ hostPort , guestPort , isStatic , err := ParsePortForward (tt .spec )
149+ if tt .expectError {
150+ assert .Check (t , err != nil )
151+ } else {
152+ assert .NilError (t , err )
153+ assert .Equal (t , tt .hostPort , hostPort )
154+ assert .Equal (t , tt .guestPort , guestPort )
155+ assert .Equal (t , tt .isStatic , isStatic )
156+ }
157+ })
158+ }
159+ }
0 commit comments