@@ -52,7 +52,7 @@ func TestAddContainerWithTemplates(t *testing.T) {
5252 " proxy / 172.17.0.2:5000/api\n " +
5353 "}\n "
5454
55- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
55+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
5656}
5757
5858func TestAddContainerPicksRightNetwork (t * testing.T ) {
@@ -81,7 +81,7 @@ func TestAddContainerPicksRightNetwork(t *testing.T) {
8181 " proxy / 172.17.0.2\n " +
8282 "}\n "
8383
84- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
84+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
8585}
8686
8787func TestAddContainerWithMinimumBasicLabels (t * testing.T ) {
@@ -106,7 +106,7 @@ func TestAddContainerWithMinimumBasicLabels(t *testing.T) {
106106 " proxy / 172.17.0.2\n " +
107107 "}\n "
108108
109- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
109+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
110110}
111111
112112func TestAddContainerWithAllBasicLabels (t * testing.T ) {
@@ -134,7 +134,7 @@ func TestAddContainerWithAllBasicLabels(t *testing.T) {
134134 " proxy / https://172.17.0.2:5000/api\n " +
135135 "}\n "
136136
137- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
137+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
138138}
139139
140140func TestAddContainerFromDifferentNetwork (t * testing.T ) {
@@ -161,7 +161,35 @@ func TestAddContainerFromDifferentNetwork(t *testing.T) {
161161 const expectedLogs = skipCaddyfileText +
162162 "[ERROR] Container CONTAINER-ID and caddy are not in same network\n "
163163
164- testGeneration (t , dockerClient , false , expectedCaddyfile , expectedLogs )
164+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , expectedLogs )
165+ }
166+
167+ func TestAddContainerFromDifferentNetworkSkipValidation (t * testing.T ) {
168+ dockerClient := createBasicDockerClientMock ()
169+ dockerClient .ContainersData = []types.Container {
170+ types.Container {
171+ ID : "CONTAINER-ID" ,
172+ NetworkSettings : & types.SummaryNetworkSettings {
173+ Networks : map [string ]* network.EndpointSettings {
174+ "other-network" : & network.EndpointSettings {
175+ IPAddress : "10.0.0.1" ,
176+ NetworkID : "other-network-id" ,
177+ },
178+ },
179+ },
180+ Labels : map [string ]string {
181+ fmtLabel ("%s.address" ): "service.testdomain.com" ,
182+ },
183+ },
184+ }
185+
186+ const expectedCaddyfile = "service.testdomain.com {\n " +
187+ " proxy / 10.0.0.1\n " +
188+ "}\n "
189+
190+ const expectedLogs = skipCaddyfileText
191+
192+ testGeneration (t , dockerClient , false , false , expectedCaddyfile , expectedLogs )
165193}
166194
167195func TestAddContainerWithMultipleConfigs (t * testing.T ) {
@@ -201,7 +229,7 @@ func TestAddContainerWithMultipleConfigs(t *testing.T) {
201229 " }\n " +
202230 "}\n "
203231
204- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
232+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
205233}
206234
207235func TestAddContainerWithReplicas (t * testing.T ) {
@@ -239,7 +267,7 @@ func TestAddContainerWithReplicas(t *testing.T) {
239267 " proxy / 172.17.0.2 172.17.0.3\n " +
240268 "}\n "
241269
242- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
270+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
243271}
244272
245273func TestDoNotMergeProxiesWithDifferentLabelKey (t * testing.T ) {
@@ -280,7 +308,7 @@ func TestDoNotMergeProxiesWithDifferentLabelKey(t *testing.T) {
280308 " proxy /b service-b\n " +
281309 "}\n "
282310
283- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
311+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
284312}
285313
286314func TestAddContainersWithSnippets (t * testing.T ) {
@@ -329,7 +357,7 @@ func TestAddContainersWithSnippets(t *testing.T) {
329357 " proxy / 172.17.0.3\n " +
330358 "}\n "
331359
332- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
360+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
333361}
334362
335363func TestAddServiceWithTemplates (t * testing.T ) {
@@ -386,7 +414,7 @@ func TestAddServiceWithTemplates(t *testing.T) {
386414 " }\n " +
387415 "}\n "
388416
389- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
417+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
390418}
391419
392420func TestUseTemplatesToGenerateEmptyValues (t * testing.T ) {
@@ -423,7 +451,7 @@ func TestUseTemplatesToGenerateEmptyValues(t *testing.T) {
423451 " }\n " +
424452 "}\n "
425453
426- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
454+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
427455}
428456
429457func TestAddServiceWithMinimumBasicLabels (t * testing.T ) {
@@ -452,7 +480,7 @@ func TestAddServiceWithMinimumBasicLabels(t *testing.T) {
452480 " proxy / service\n " +
453481 "}\n "
454482
455- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
483+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
456484}
457485
458486func TestAddServiceWithAllBasicLabels (t * testing.T ) {
@@ -484,7 +512,7 @@ func TestAddServiceWithAllBasicLabels(t *testing.T) {
484512 " proxy / https://service:5000/api\n " +
485513 "}\n "
486514
487- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
515+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
488516}
489517
490518func TestAddServiceWithMultipleConfigs (t * testing.T ) {
@@ -537,7 +565,7 @@ func TestAddServiceWithMultipleConfigs(t *testing.T) {
537565 " }\n " +
538566 "}\n "
539567
540- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
568+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
541569}
542570
543571func TestAddServiceProxyServiceTasks (t * testing.T ) {
@@ -567,7 +595,7 @@ func TestAddServiceProxyServiceTasks(t *testing.T) {
567595 " proxy / tasks.service:5000\n " +
568596 "}\n "
569597
570- testGeneration (t , dockerClient , true , expectedCaddyfile , skipCaddyfileText )
598+ testGeneration (t , dockerClient , true , true , expectedCaddyfile , skipCaddyfileText )
571599}
572600
573601func TestAddServiceMultipleAddresses (t * testing.T ) {
@@ -596,7 +624,7 @@ func TestAddServiceMultipleAddresses(t *testing.T) {
596624 " proxy / service\n " +
597625 "}\n "
598626
599- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
627+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
600628}
601629
602630func TestAutomaticProxyDoesntOverrideCustomWithSameKey (t * testing.T ) {
@@ -628,7 +656,7 @@ func TestAutomaticProxyDoesntOverrideCustomWithSameKey(t *testing.T) {
628656 " proxy /api external-api\n " +
629657 "}\n "
630658
631- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
659+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
632660}
633661
634662func TestAddServiceFromDifferentNetwork (t * testing.T ) {
@@ -659,7 +687,39 @@ func TestAddServiceFromDifferentNetwork(t *testing.T) {
659687 const expectedLogs = skipCaddyfileText +
660688 "[ERROR] Service SERVICE-ID and caddy are not in same network\n "
661689
662- testGeneration (t , dockerClient , false , expectedCaddyfile , expectedLogs )
690+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , expectedLogs )
691+ }
692+
693+ func TestAddServiceFromDifferentNetworkSkipValidation (t * testing.T ) {
694+ dockerClient := createBasicDockerClientMock ()
695+ dockerClient .ServicesData = []swarm.Service {
696+ swarm.Service {
697+ ID : "SERVICE-ID" ,
698+ Spec : swarm.ServiceSpec {
699+ Annotations : swarm.Annotations {
700+ Name : "service" ,
701+ Labels : map [string ]string {
702+ fmtLabel ("%s.address" ): "service.testdomain.com" ,
703+ },
704+ },
705+ },
706+ Endpoint : swarm.Endpoint {
707+ VirtualIPs : []swarm.EndpointVirtualIP {
708+ swarm.EndpointVirtualIP {
709+ NetworkID : "other-network-id" ,
710+ },
711+ },
712+ },
713+ },
714+ }
715+
716+ const expectedCaddyfile = "service.testdomain.com {\n " +
717+ " proxy / service\n " +
718+ "}\n "
719+
720+ const expectedLogs = skipCaddyfileText
721+
722+ testGeneration (t , dockerClient , false , false , expectedCaddyfile , expectedLogs )
663723}
664724
665725func TestAddServiceSwarmDisable (t * testing.T ) {
@@ -696,7 +756,7 @@ func TestAddServiceSwarmDisable(t *testing.T) {
696756 "[INFO] Skipping services because swarm is not available\n " +
697757 "[INFO] Skipping configs because swarm is not available\n "
698758
699- testGeneration (t , dockerClient , false , expectedCaddyfile , expectedLogs )
759+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , expectedLogs )
700760}
701761
702762func TestAddDockerConfigContent (t * testing.T ) {
@@ -723,7 +783,7 @@ func TestAddDockerConfigContent(t *testing.T) {
723783 " tls off+\n " +
724784 "}\n "
725785
726- testGeneration (t , dockerClient , false , expectedCaddyfile , skipCaddyfileText )
786+ testGeneration (t , dockerClient , false , true , expectedCaddyfile , skipCaddyfileText )
727787}
728788
729789func TestIgnoreLabelsWithoutCaddyPrefix (t * testing.T ) {
@@ -753,13 +813,14 @@ func TestIgnoreLabelsWithoutCaddyPrefix(t *testing.T) {
753813
754814 const expectedCaddyfile = ""
755815
756- testGeneration (t , dockerClient , true , expectedCaddyfile , skipCaddyfileText )
816+ testGeneration (t , dockerClient , true , true , expectedCaddyfile , skipCaddyfileText )
757817}
758818
759819func testGeneration (
760820 t * testing.T ,
761821 dockerClient DockerClient ,
762822 proxyServiceTasks bool ,
823+ validateNetwork bool ,
763824 expectedCaddyfile string ,
764825 expectedLogs string ,
765826) {
@@ -768,6 +829,7 @@ func testGeneration(
768829 generator := CreateGenerator (dockerClient , dockerUtils , & GeneratorOptions {
769830 labelPrefix : defaultLabelPrefix ,
770831 proxyServiceTasks : proxyServiceTasks ,
832+ validateNetwork : validateNetwork ,
771833 })
772834
773835 caddyfileBytes , logs := generator .GenerateCaddyFile ()
0 commit comments