forked from compose-spec/compose-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaults_test.go
More file actions
318 lines (298 loc) · 6.86 KB
/
defaults_test.go
File metadata and controls
318 lines (298 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
Copyright 2020 The Compose Specification Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package transform
import (
"fmt"
"strings"
"testing"
"github.com/compose-spec/compose-go/v2/tree"
"go.yaml.in/yaml/v3"
"gotest.tools/v3/assert"
)
// exampleEnhancedPortDefaults is an example of an enhanced port defaults function
// that supports additional protocols and domain names in the published field.
// This function can be used as a replacement for the default portDefaults
// by registering it with RegisterDefaultValue.
//
// Example usage:
//
// RegisterDefaultValue("services.*.ports.*", exampleEnhancedPortDefaults)
//
// This function supports:
// - Additional protocols: "http", "https", "tcp", "udp"
// - Domain names in the published field (e.g., "example.com:80")
// - All existing functionality of the original portDefaults
func exampleEnhancedPortDefaults(data any, _ tree.Path, _ bool) (any, error) {
switch v := data.(type) {
case map[string]any:
// Set default protocol if not specified
if _, ok := v["protocol"]; !ok {
v["protocol"] = "tcp"
}
// Set default mode if not specified
if _, ok := v["mode"]; !ok {
v["mode"] = "ingress"
}
// Enhanced protocol handling with app_protocol
protocol, _ := v["protocol"].(string)
switch protocol {
case "http":
if _, ok := v["app_protocol"]; !ok {
v["app_protocol"] = "http1.1"
}
case "https":
if _, ok := v["app_protocol"]; !ok {
v["app_protocol"] = "http2"
}
}
// Auto-generate port name based on target port
if _, ok := v["name"]; !ok {
if target, ok := v["target"].(int); ok {
switch target {
case 80:
v["name"] = "http"
case 443:
v["name"] = "https"
case 3306:
v["name"] = "mysql"
case 5432:
v["name"] = "postgres"
case 6379:
v["name"] = "redis"
case 27017:
v["name"] = "mongodb"
default:
v["name"] = fmt.Sprintf("port-%d", target)
}
}
}
// Handle domain names in published field
if published, ok := v["published"].(string); ok {
// Check if published contains a domain name, very KISS check for this example
if strings.Contains(published, ".") && strings.Contains(published, ":") {
parts := strings.SplitN(published, ":", 2)
if len(parts) == 2 {
v["x-published-domain"] = parts[0]
v["x-published-port"] = parts[1]
}
}
}
// Normalize host_ip shortcuts
if hostIP, ok := v["host_ip"].(string); ok {
switch hostIP {
case "localhost":
v["host_ip"] = "127.0.0.1"
case "*":
v["host_ip"] = "0.0.0.0"
}
}
// Add monitoring metadata for common ports
if target, ok := v["target"].(int); ok {
switch target {
case 80, 443, 8080, 8443:
v["x-metrics-enabled"] = true
v["x-metrics-path"] = "/metrics"
case 9090: // Prometheus
v["x-metrics-enabled"] = true
v["x-metrics-type"] = "prometheus"
}
}
return v, nil
default:
return data, nil
}
}
func TestRegisterDefaultValue(t *testing.T) {
// Save original transformers, so as not to break possible other tests
originalTransformers := make(map[tree.Path]Func)
for k, v := range DefaultValues {
originalTransformers[k] = v
}
t.Cleanup(func() {
DefaultValues = originalTransformers
})
// Register the function
RegisterDefaultValue("services.*.ports.*", exampleEnhancedPortDefaults)
testCases := []struct {
name string
inputYAML string
expectedYAML string
}{
{
name: "basic port with defaults and auto-generated name",
inputYAML: `
services:
web:
ports:
- target: 80
`,
expectedYAML: `
services:
web:
ports:
- target: 80
protocol: tcp
mode: ingress
name: http
x-metrics-enabled: true
x-metrics-path: /metrics
`,
},
{
name: "port with https protocol and app_protocol",
inputYAML: `
services:
web:
ports:
- target: 443
protocol: https
`,
expectedYAML: `
services:
web:
ports:
- target: 443
protocol: https
app_protocol: http2
mode: ingress
name: https
x-metrics-enabled: true
x-metrics-path: /metrics
`,
},
{
name: "port with domain name in published field",
inputYAML: `
services:
web:
ports:
- target: 80
published: "example.com:8080"
`,
expectedYAML: `
services:
web:
ports:
- target: 80
published: "example.com:8080"
protocol: tcp
mode: ingress
name: http
x-published-domain: example.com
x-published-port: "8080"
x-metrics-enabled: true
x-metrics-path: /metrics
`,
},
{
name: "database port with auto-generated name",
inputYAML: `
services:
db:
ports:
- target: 3306
`,
expectedYAML: `
services:
db:
ports:
- target: 3306
protocol: tcp
mode: ingress
name: mysql
`,
},
{
name: "host_ip normalization",
inputYAML: `
services:
web:
ports:
- target: 8080
host_ip: localhost
- target: 8081
host_ip: "*"
`,
expectedYAML: `
services:
web:
ports:
- target: 8080
host_ip: "127.0.0.1"
protocol: tcp
mode: ingress
name: port-8080
x-metrics-enabled: true
x-metrics-path: /metrics
- target: 8081
host_ip: "0.0.0.0"
protocol: tcp
mode: ingress
name: port-8081
`,
},
{
name: "prometheus port with monitoring metadata",
inputYAML: `
services:
prometheus:
ports:
- target: 9090
`,
expectedYAML: `
services:
prometheus:
ports:
- target: 9090
protocol: tcp
mode: ingress
name: port-9090
x-metrics-enabled: true
x-metrics-type: prometheus
`,
},
{
name: "http protocol with app_protocol",
inputYAML: `
services:
web:
ports:
- target: 3000
protocol: http
`,
expectedYAML: `
services:
web:
ports:
- target: 3000
protocol: http
app_protocol: http1.1
mode: ingress
name: port-3000
`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var input map[string]any
err := yaml.Unmarshal([]byte(tc.inputYAML), &input)
assert.NilError(t, err)
var expected map[string]any
err = yaml.Unmarshal([]byte(tc.expectedYAML), &expected)
assert.NilError(t, err)
result, err := SetDefaultValues(input)
assert.NilError(t, err)
assert.DeepEqual(t, result, expected)
})
}
}