Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
fixed string replacement for first service occurrence
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Aug 18, 2017
1 parent e42ff51 commit 2aafdff
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
24 changes: 18 additions & 6 deletions pkg/converter/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 10 additions & 11 deletions pkg/converter/linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/converter/testdata/docker-compose-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ services:
driver: syslog
options:
syslog-address: "tcp://192.168.0.42:123"
mongo:
image: mongodb
volumes:
db:
content:
2 changes: 2 additions & 0 deletions pkg/converter/testdata/docker-compose-v3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ services:
driver: syslog
options:
syslog-address: "tcp://192.168.0.42:123"
mongo:
image: mongodb
volumes:
db:
content:
4 changes: 4 additions & 0 deletions pkg/converter/testdata/golden0.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/converter/testdata/test.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
VAR_A=1
VAR_B=test
FOO_URL=mongodb://mongo:27017/db

0 comments on commit 2aafdff

Please sign in to comment.