Skip to content

Commit

Permalink
Extract method getMesosDnsId and add a few tests. Still learning Conv…
Browse files Browse the repository at this point in the history
…ey syntax
  • Loading branch information
Damian Martinez committed May 6, 2016
1 parent 3bb6388 commit d825591
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
26 changes: 14 additions & 12 deletions services/marathon/marathon.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,10 @@ func createApps(tasksById map[string]marathonTaskList, marathonApps map[string]m
appPath = "/" + appId
}

// split up groups and recombine for how mesos-dns/consul/etc use service name
// "/nested/group/app" -> "app-group-nested"
groups := strings.Split(appPath, "/")
reverseGroups := []string{}
for i := len(groups) - 1; i >= 0; i-- {
if groups[i] != "" {
reverseGroups = append(reverseGroups, groups[i])
}
}
mesosId := strings.Join(reverseGroups, "-")

// build App from marathonApp
app := App{
Id: appPath,
MesosDnsId: mesosId,
MesosDnsId: getMesosDnsId(appPath),
EscapedId: strings.Replace(appId, "/", "::", -1),
HealthCheckPath: parseHealthCheckPath(mApp.HealthChecks),
Env: mApp.Env,
Expand Down Expand Up @@ -249,6 +238,19 @@ func createApps(tasksById map[string]marathonTaskList, marathonApps map[string]m
return apps
}

func getMesosDnsId(appPath string) string {
// split up groups and recombine for how mesos-dns/consul/etc use service name
// "/nested/group/app" -> "app-group-nested"
groups := strings.Split(appPath, "/")
reverseGroups := []string{}
for i := len(groups) - 1; i >= 0; i-- {
if groups[i] != "" {
reverseGroups = append(reverseGroups, groups[i])
}
}
return strings.Join(reverseGroups, "-")
}

func parseHealthCheckPath(checks []marathonHealthCheck) string {
for _, check := range checks {
if check.Protocol != "HTTP" {
Expand Down
20 changes: 20 additions & 0 deletions services/marathon/marathon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,26 @@ import (
"testing"
)

func TestGetMesosDnsId_Simple(t *testing.T) {
Convey("#getMesosDnsId", t, func() {
Convey("should return simple appname", func() {
So(getMesosDnsId("appname"), ShouldEqual, "appname")
})

Convey("should return simple appname if slash prefixed", func() {
So(getMesosDnsId("/appname"), ShouldEqual, "appname")
})

Convey("should return groups reverse-added to appname", func() {
So(getMesosDnsId("/group/appname"), ShouldEqual, "appname-group")
})

Convey("should return groups reverse-added to appname but no blanks", func() {
So(getMesosDnsId("//group/again//appname/"), ShouldEqual, "appname-again-group")
})
})
}

func TestParseHealthCheckPathTCP(t *testing.T) {
Convey("#parseHealthCheckPath", t, func() {
checks := []marathonHealthCheck{
Expand Down

0 comments on commit d825591

Please sign in to comment.