Repository: allegro/marathon-consul
Pull request #274 (2017-10-18 13:09:01)

Use port from marathon locathion.
Add support to use port from marathon location in leader discovery logic so can support port other than 8080

Based on #264 

coveralls: 
[![Coverage Status](https://coveralls.io/builds/13773551/badge)](https://coveralls.io/builds/13773551)

Changes Unknown when pulling **7c61b0d8613332f87930d000f3370b865b6db8fb on fix_264** into ** on master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/13789028/badge)](https://coveralls.io/builds/13789028)

Changes Unknown when pulling **9a348d1b93a6a1eac05adc7bd9a7b35d84e2522f on fix_264** into ** on master**.


Patch:
diff --git a/marathon/marathon.go b/marathon/marathon.go
index d83119e..2c35a78 100644
--- a/marathon/marathon.go
+++ b/marathon/marathon.go
@@ -262,7 +262,13 @@ func (m *Marathon) resolveHostname() error {
 	if err != nil {
 		return err
 	}
-	m.MyLeader = fmt.Sprintf("%s:8080", hostname)
+
+	u, err := url.Parse(m.url(""))
+	if err != nil {
+		return fmt.Errorf("Could not parse marathon location (%s): %v", m.Location, err)
+	}
+
+	m.MyLeader = fmt.Sprintf("%s:%s", hostname, u.Port())
 	log.WithField("Leader", m.MyLeader).Info("Marathon Leader mode set to resolved hostname")
 	return nil
 }


Test patch:
diff --git a/consul/consul_test.go b/consul/consul_test.go
index bfa0c3e..23d8447 100644
--- a/consul/consul_test.go
+++ b/consul/consul_test.go
@@ -1104,4 +1104,4 @@ func TestTokenIsUsedToConnectToConsul(t *testing.T) {
 
 	// then
 	assert.Len(t, services, 0, "Reading services list without ACL token should yield empty response")
-}
\ No newline at end of file
+}
diff --git a/consul/consul_test_server.go b/consul/consul_test_server.go
index 666cde6..cbc453f 100644
--- a/consul/consul_test_server.go
+++ b/consul/consul_test_server.go
@@ -124,4 +124,4 @@ func secureConsulClientAtAddress(host string, port int) *Consul {
 	// initialize the agents cache with a single client pointing at provided location
 	consul.AddAgent(host)
 	return consul
-}
\ No newline at end of file
+}
diff --git a/marathon/marathon_test.go b/marathon/marathon_test.go
index fc9b3a4..1eb8480 100644
--- a/marathon/marathon_test.go
+++ b/marathon/marathon_test.go
@@ -408,11 +408,44 @@ func TestIsLeader_NotPassingMyLeaderIsEmptyStringShouldFillItWithLocalHostname(t
 	leading, err := m.IsLeader()
 
 	//then
-	assert.Equal(t, m.MyLeader, fmt.Sprintf("%s:8080", myHostname))
+	assert.Equal(t, m.MyLeader, fmt.Sprintf("%s:%s", myHostname, url.Port()))
 	assert.False(t, leading)
 	assert.NoError(t, err)
 }
 
+func TestIsLeader_ShouldUsePortFromMarathonConfigToConnectToLocalLeader(t *testing.T) {
+	t.Parallel()
+
+	// given
+	server, _ := stubServer("/v2/leader", `{"leader": "other.leader:8080"}`)
+	defer server.Close()
+	url, _ := url.Parse(server.URL)
+	m, _ := New(Config{Location: url.Host, Protocol: "HTTP", Leader: ""})
+
+	// when
+	leading, err := m.IsLeader()
+
+	//then
+	assert.False(t, leading)
+	assert.NoError(t, err)
+}
+
+func TestIsLeader_ShouldReturnErrorWhenCanNotParseLocation(t *testing.T) {
+	t.Parallel()
+
+	// given
+	server, _ := stubServer("/v2/leader", `{"leader": "other.leader:8080"}`)
+	defer server.Close()
+	m, _ := New(Config{Location: "invalid", Protocol: "1337", Leader: ""})
+
+	// when
+	leading, err := m.IsLeader()
+
+	//then
+	assert.False(t, leading)
+	assert.EqualError(t, err, `Could not resolve hostname: Could not parse marathon location (invalid): parse 1337://invalid: first path segment in URL cannot contain colon`)
+}
+
 func TestEventStream_PassingStreamerCreated(t *testing.T) {
 	t.Parallel()