@@ -18,8 +18,7 @@ import (
18
18
"github.com/stretchr/testify/require"
19
19
)
20
20
21
- func TestCaddyfileGenerator (t * testing.T ) {
22
- caddyfileHeader := `# This file is autogenerated by Uncloud based on the configuration of running services.
21
+ const testCaddyfileHeader = `# This file is autogenerated by Uncloud based on the configuration of running services.
23
22
# Do not edit manually. Any manual changes will be overwritten on the next update.
24
23
25
24
# Health check endpoint to verify Caddy reachability on this machine.
@@ -38,6 +37,7 @@ http:// {
38
37
}
39
38
`
40
39
40
+ func TestCaddyfileGenerator (t * testing.T ) {
41
41
tests := []struct {
42
42
name string
43
43
containers []store.ContainerRecord
@@ -47,14 +47,14 @@ http:// {
47
47
{
48
48
name : "empty containers" ,
49
49
containers : []store.ContainerRecord {},
50
- want : caddyfileHeader ,
50
+ want : testCaddyfileHeader ,
51
51
},
52
52
{
53
53
name : "HTTP container" ,
54
54
containers : []store.ContainerRecord {
55
55
newContainerRecord (newContainer ("10.210.0.2" , "app.example.com:8080/http" ), "mach1" ),
56
56
},
57
- want : caddyfileHeader + `
57
+ want : testCaddyfileHeader + `
58
58
# Sites generated from service ports.
59
59
60
60
http://app.example.com {
@@ -71,7 +71,7 @@ http://app.example.com {
71
71
newContainerRecord (newContainer ("10.210.0.2" , "app.example.com:8080/http" ), "mach1" ),
72
72
newContainerRecord (newContainer ("10.210.0.3" , "app.example.com:8080/http" ), "mach1" ),
73
73
},
74
- want : caddyfileHeader + `
74
+ want : testCaddyfileHeader + `
75
75
# Sites generated from service ports.
76
76
77
77
http://app.example.com {
@@ -87,7 +87,7 @@ http://app.example.com {
87
87
containers : []store.ContainerRecord {
88
88
newContainerRecord (newContainer ("10.210.0.2" , "secure.example.com:8000/https" ), "mach1" ),
89
89
},
90
- want : caddyfileHeader + `
90
+ want : testCaddyfileHeader + `
91
91
# Sites generated from service ports.
92
92
93
93
https://secure.example.com {
@@ -127,7 +127,7 @@ https://secure.example.com {
127
127
"mach1" ,
128
128
),
129
129
},
130
- want : caddyfileHeader + `
130
+ want : testCaddyfileHeader + `
131
131
# Sites generated from service ports.
132
132
133
133
http://app.example.com {
@@ -157,14 +157,14 @@ https://secure.example.com {
157
157
containers : []store.ContainerRecord {
158
158
newContainerRecord (newContainerWithoutNetwork ("ignored.example.com:8080/http" ), "mach1" ),
159
159
},
160
- want : caddyfileHeader ,
160
+ want : testCaddyfileHeader ,
161
161
},
162
162
{
163
163
name : "container with invalid port ignored" ,
164
164
containers : []store.ContainerRecord {
165
165
newContainerRecord (newContainer ("10.210.0.2" , "invalid-port" ), "mach1" ),
166
166
},
167
- want : caddyfileHeader ,
167
+ want : testCaddyfileHeader ,
168
168
},
169
169
{
170
170
name : "containers with unsupported protocols and host mode ignored" ,
@@ -173,7 +173,7 @@ https://secure.example.com {
173
173
newContainerRecord (newContainer ("10.210.0.3" , "5000/udp" ), "mach1" ),
174
174
newContainerRecord (newContainer ("10.210.0.4" , "80:8080/tcp@host" ), "mach1" ),
175
175
},
176
- want : caddyfileHeader ,
176
+ want : testCaddyfileHeader ,
177
177
},
178
178
}
179
179
@@ -183,7 +183,7 @@ https://secure.example.com {
183
183
// Validator is not expected to be called in these tests.
184
184
generator := NewCaddyfileGenerator ("test-machine-id" , nil , nil )
185
185
186
- config , err := generator .Generate (ctx , tt .containers )
186
+ config , err := generator .Generate (ctx , tt .containers , true )
187
187
188
188
if tt .wantErr {
189
189
assert .Error (t , err )
@@ -197,25 +197,6 @@ https://secure.example.com {
197
197
}
198
198
199
199
func TestCaddyfileGeneratorWithCustomConfigs (t * testing.T ) {
200
- caddyfileBase := `# This file is autogenerated by Uncloud based on the configuration of running services.
201
- # Do not edit manually. Any manual changes will be overwritten on the next update.
202
-
203
- # Health check endpoint to verify Caddy reachability on this machine.
204
- http:// {
205
- handle /.uncloud-verify {
206
- respond "test-machine-id" 200
207
- }
208
- log
209
- }
210
-
211
- (common_proxy) {
212
- # Retry failed requests up to lb_retries times against other available upstreams.
213
- lb_retries 3
214
- # Upstreams are marked unhealthy for fail_duration after a failed request (passive health checking).
215
- fail_duration 30s
216
- }
217
- `
218
-
219
200
tests := []struct {
220
201
name string
221
202
containers []store.ContainerRecord
@@ -275,7 +256,7 @@ web.example.com {
275
256
time .Now (),
276
257
),
277
258
},
278
- want : caddyfileBase + `
259
+ want : testCaddyfileHeader + `
279
260
# User-defined config for service 'web'.
280
261
# Custom config for web service
281
262
web.example.com {
@@ -297,7 +278,7 @@ bad.config.com {
297
278
time .Now (),
298
279
),
299
280
},
300
- want : caddyfileBase + `
281
+ want : testCaddyfileHeader + `
301
282
# Skipped invalid user-defined configs:
302
283
# - service 'bad-service': validation failed: invalid config detected
303
284
` ,
@@ -316,7 +297,7 @@ bad.template.com {
316
297
time .Now (),
317
298
),
318
299
},
319
- want : caddyfileBase + `
300
+ want : testCaddyfileHeader + `
320
301
# Skipped invalid user-defined configs:
321
302
# - service 'bad-template': failed to render template: parse config as Go template: template: Caddyfile:3: unexpected "}" in operand
322
303
` ,
@@ -335,7 +316,7 @@ localhost {
335
316
time .Now (),
336
317
),
337
318
},
338
- want : caddyfileBase + `
319
+ want : testCaddyfileHeader + `
339
320
# Skipped invalid user-defined configs:
340
321
# - service 'caddy': validation failed: invalid config detected
341
322
` ,
@@ -354,7 +335,7 @@ localhost {
354
335
time .Now (),
355
336
),
356
337
},
357
- want : caddyfileBase ,
338
+ want : testCaddyfileHeader ,
358
339
},
359
340
{
360
341
name : "multiple services with mixed valid and invalid configs" ,
@@ -388,7 +369,7 @@ bad.example.com {
388
369
time .Now (),
389
370
),
390
371
},
391
- want : caddyfileBase + `
372
+ want : testCaddyfileHeader + `
392
373
# User-defined config for service 'api'.
393
374
api.example.com {
394
375
reverse_proxy api:8080
@@ -490,7 +471,7 @@ api.example.com {
490
471
"test-machine-id" ,
491
472
),
492
473
},
493
- want : caddyfileBase + `
474
+ want : testCaddyfileHeader + `
494
475
# Sites generated from service ports.
495
476
496
477
http://api.example.com {
@@ -530,7 +511,7 @@ new.example.com {
530
511
time .Now (),
531
512
),
532
513
},
533
- want : caddyfileBase + `
514
+ want : testCaddyfileHeader + `
534
515
# User-defined config for service 'web'.
535
516
# New config
536
517
new.example.com {
@@ -815,7 +796,7 @@ invalid.example.com {
815
796
time .Now (),
816
797
),
817
798
},
818
- want : caddyfileBase + `
799
+ want : testCaddyfileHeader + `
819
800
# User-defined config for service 'valid'.
820
801
valid.example.com {
821
802
respond "Valid config"
@@ -843,7 +824,7 @@ valid.example.com {
843
824
t .Run (tt .name , func (t * testing.T ) {
844
825
generator := NewCaddyfileGenerator ("test-machine-id" , validator , nil )
845
826
846
- config , err := generator .Generate (ctx , tt .containers )
827
+ config , err := generator .Generate (ctx , tt .containers , true )
847
828
848
829
if tt .wantErr {
849
830
assert .Error (t , err )
@@ -899,6 +880,94 @@ func newContainerRecordWithCaddyConfig(serviceName, ip, caddyConfig, machineID s
899
880
}
900
881
}
901
882
883
+ func TestCaddyfileGeneratorWithoutCustomConfigs (t * testing.T ) {
884
+ // Test that when includeCustom is false (Caddy not available), x-caddy configs are skipped.
885
+ tests := []struct {
886
+ name string
887
+ containers []store.ContainerRecord
888
+ want string
889
+ }{
890
+ {
891
+ name : "x-caddy configs are skipped" ,
892
+ containers : []store.ContainerRecord {
893
+ newContainerRecordWithCaddyConfig (
894
+ "caddy" ,
895
+ "10.210.0.1" ,
896
+ `# Global config
897
+ {
898
+ global directive
899
+ }` ,
900
+ "test-machine-id" ,
901
+ time .Now (),
902
+ ),
903
+ newContainerRecordWithCaddyConfig (
904
+ "web" ,
905
+ "10.210.0.2" ,
906
+ `web.example.com {
907
+ reverse_proxy web:3000
908
+ }` ,
909
+ "test-machine-id" ,
910
+ time .Now (),
911
+ ),
912
+ newContainerRecordWithPorts (
913
+ "api" ,
914
+ "10.210.0.3" ,
915
+ []string {"api.example.com:8080/http" },
916
+ "test-machine-id" ,
917
+ ),
918
+ },
919
+ want : testCaddyfileHeader + `
920
+ # Sites generated from service ports.
921
+
922
+ http://api.example.com {
923
+ reverse_proxy 10.210.0.3:8080 {
924
+ import common_proxy
925
+ }
926
+ log
927
+ }
928
+
929
+ # NOTE: User-defined configs for services were skipped because Caddy is not running on this machine.
930
+ ` ,
931
+ },
932
+ {
933
+ name : "no containers with x-caddy configs" ,
934
+ containers : []store.ContainerRecord {
935
+ newContainerRecordWithPorts (
936
+ "api" ,
937
+ "10.210.0.3" ,
938
+ []string {"api.example.com:8080/http" },
939
+ "test-machine-id" ,
940
+ ),
941
+ },
942
+ want : testCaddyfileHeader + `
943
+ # Sites generated from service ports.
944
+
945
+ http://api.example.com {
946
+ reverse_proxy 10.210.0.3:8080 {
947
+ import common_proxy
948
+ }
949
+ log
950
+ }
951
+
952
+ # NOTE: User-defined configs for services were skipped because Caddy is not running on this machine.
953
+ ` ,
954
+ },
955
+ }
956
+
957
+ ctx := context .Background ()
958
+ for _ , tt := range tests {
959
+ t .Run (tt .name , func (t * testing.T ) {
960
+ // Validator is not expected to be called in these tests.
961
+ generator := NewCaddyfileGenerator ("test-machine-id" , nil , nil )
962
+
963
+ config , err := generator .Generate (ctx , tt .containers , false )
964
+ require .NoError (t , err )
965
+
966
+ assert .Equal (t , tt .want , config , "Generated Caddyfile doesn't match" )
967
+ })
968
+ }
969
+ }
970
+
902
971
func newContainerRecordWithPorts (serviceName , ip string , ports []string , machineID string ) store.ContainerRecord {
903
972
portsLabel := strings .Join (ports , "," )
904
973
return store.ContainerRecord {
0 commit comments