From 2aafdff7f34a7c91a085c528fc8e672f426c13f8 Mon Sep 17 00:00:00 2001 From: Marcel Ludwig Date: Fri, 18 Aug 2017 16:57:23 +0200 Subject: [PATCH] fixed string replacement for first service occurrence --- pkg/converter/linker.go | 24 +++++++++++++++----- pkg/converter/linker_test.go | 21 ++++++++--------- pkg/converter/testdata/docker-compose-v2.yml | 2 ++ pkg/converter/testdata/docker-compose-v3.yml | 2 ++ pkg/converter/testdata/golden0.yml | 4 ++++ pkg/converter/testdata/test.env | 1 + 6 files changed, 37 insertions(+), 17 deletions(-) diff --git a/pkg/converter/linker.go b/pkg/converter/linker.go index 0fc7f33..d3c6f8a 100644 --- a/pkg/converter/linker.go +++ b/pkg/converter/linker.go @@ -68,12 +68,24 @@ func (l *Linker) Resolve(cf *ComposeFile, sf *SloppyFile) error { continue } - targetVar := strings.Replace( - app.EnvVars[key], - match, - targetLink.fqdn, - 1, - ) + var targetVar string + schemeIdx := strings.Index(app.EnvVars[key], "://") + if schemeIdx != -1 { + targetVar = app.EnvVars[key][:schemeIdx] + strings.Replace( + app.EnvVars[key][schemeIdx:], + match, + targetLink.fqdn, + 1, + ) + } else { + targetVar = strings.Replace( + app.EnvVars[key], + match, + targetLink.fqdn, + 1, + ) + } + app.EnvVars[key] = targetVar // also consider special sloppy Env field diff --git a/pkg/converter/linker_test.go b/pkg/converter/linker_test.go index 2f5df5f..4ad64a1 100644 --- a/pkg/converter/linker_test.go +++ b/pkg/converter/linker_test.go @@ -12,25 +12,24 @@ import ( func TestLinker_FindService(t *testing.T) { cases := map[string]struct { value string + expected string shouldMatch bool }{ - "FOO": {"bar", false}, - "BAR": {"foo:80", true}, - "SHORT": {"s:80", true}, - "FOO_BAR": {"another.foo:443", true}, - "FOO_HOST": {"bar", true}, - "FOO_URL": {"mongodb://whatever:4444/db", true}, - "BAR_URL": {"foo://nope", true}, + "FOO": {"bar", "", false}, + "BAR": {"foo:80", "foo", true}, + "SHORT": {"s:80", "s", true}, + "FOO_BAR": {"another.foo:443", "another.foo", true}, + "FOO_HOST": {"bar", "bar", true}, + "FOO_URL": {"mongodb://whatever:4444/db", "whatever", true}, + "BAR_URL": {"foo://nope", "nope", true}, } l := converter.Linker{} for envKey, caseVal := range cases { match := l.FindServiceString(envKey, caseVal.value) - if caseVal.shouldMatch && match == "" { - t.Errorf("Expected an match for %q, got nothing.", caseVal.value) - } else if match != "" && !caseVal.shouldMatch { - t.Errorf("Expected no match for %q, got: %v", caseVal.value, match) + if caseVal.shouldMatch && match != caseVal.expected { + t.Errorf("Expected a match for %q, got nothing.", caseVal.value) } } } diff --git a/pkg/converter/testdata/docker-compose-v2.yml b/pkg/converter/testdata/docker-compose-v2.yml index 13e18c7..113bfcc 100644 --- a/pkg/converter/testdata/docker-compose-v2.yml +++ b/pkg/converter/testdata/docker-compose-v2.yml @@ -34,6 +34,8 @@ services: driver: syslog options: syslog-address: "tcp://192.168.0.42:123" + mongo: + image: mongodb volumes: db: content: diff --git a/pkg/converter/testdata/docker-compose-v3.yml b/pkg/converter/testdata/docker-compose-v3.yml index da3fcbd..fbdabe7 100644 --- a/pkg/converter/testdata/docker-compose-v3.yml +++ b/pkg/converter/testdata/docker-compose-v3.yml @@ -55,6 +55,8 @@ services: driver: syslog options: syslog-address: "tcp://192.168.0.42:123" + mongo: + image: mongodb volumes: db: content: diff --git a/pkg/converter/testdata/golden0.yml b/pkg/converter/testdata/golden0.yml index de2550a..14e6b4d 100644 --- a/pkg/converter/testdata/golden0.yml +++ b/pkg/converter/testdata/golden0.yml @@ -5,8 +5,10 @@ services: busy_env: cmd: sleep 20 dependencies: + - ../apps/mongo - ../apps/wordpress env: + - FOO_URL: mongodb://mongo.apps.sloppy-test:27017/db - VAR_A: "1" - VAR_B: test image: busybox @@ -24,6 +26,8 @@ services: syslog-address: tcp://192.168.0.42:123 volumes: - container_path: /var/lib/mysql + mongo: + image: mongodb wordpress: dependencies: - ../apps/db diff --git a/pkg/converter/testdata/test.env b/pkg/converter/testdata/test.env index cf36ad7..7f79fea 100644 --- a/pkg/converter/testdata/test.env +++ b/pkg/converter/testdata/test.env @@ -1,2 +1,3 @@ VAR_A=1 VAR_B=test +FOO_URL=mongodb://mongo:27017/db