Skip to content

Commit

Permalink
Remove pester dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
janisz committed Feb 1, 2016
1 parent bf93d8d commit 7088ccc
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 48 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ consul-ssl-cert | | Path to an SSL client certifica
consul-ssl-verify | `true` | Verify certificates when connecting via SSL
consul-token | | The Consul ACL token
consul-tag | `marathon` | Common tag name added to every service registered in Consul, should be unique for every Marathon-cluster connected to Consul
consul-timeout | `1s` | Time limit for requests made by the Consul HTTP client. A Timeout of zero means no timeout
consul-timeout | `3s` | Time limit for requests made by the Consul HTTP client. A Timeout of zero means no timeout
listen | `:4000` | Accept connections at this address
log-level | `info` | Log level: panic, fatal, error, warn, info, or debug
log-format | `text` | Log format: JSON, text
Expand All @@ -79,6 +79,7 @@ marathon-location | `localhost:8080` | Marathon URL
marathon-password | | Marathon password for basic auth
marathon-protocol | `http` | Marathon protocol (http or https)
marathon-username | | Marathon username for basic auth
marathon-timeout | `30s` | Time limit for requests made by the Consul HTTP client. A Timeout of zero means no timeout
metrics-interval | `30s` | Metrics reporting [interval](https://golang.org/pkg/time/#Duration) **Note:** While using file configuration intervals should be provided in *nanoseconds*
metrics-location | | Graphite URL (used when metrics-target is set to graphite)
metrics-prefix | `default` | Metrics prefix (resolved to `<hostname>.<app_name>` by default)
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (config *Config) parseFlags() {
flag.StringVar(&config.Consul.SslCaCert, "consul-ssl-ca-cert", "", "Path to a CA certificate file, containing one or more CA certificates to use to validate the certificate sent by the Consul server to us")
flag.StringVar(&config.Consul.Token, "consul-token", "", "The Consul ACL token")
flag.StringVar(&config.Consul.Tag, "consul-tag", "marathon", "Common tag name added to every service registered in Consul, should be unique for every Marathon-cluster connected to Consul")
flag.DurationVar(&config.Consul.Timeout, "consul-timeout", time.Second, "Time limit for requests made by the Consul HTTP client. A Timeout of zero means no timeout")
flag.DurationVar(&config.Consul.Timeout, "consul-timeout", 3*time.Second, "Time limit for requests made by the Consul HTTP client. A Timeout of zero means no timeout")

// Web
flag.StringVar(&config.Web.Listen, "listen", ":4000", "accept connections at this address")
Expand All @@ -85,6 +85,7 @@ func (config *Config) parseFlags() {
flag.StringVar(&config.Marathon.Username, "marathon-username", "", "Marathon username for basic auth")
flag.StringVar(&config.Marathon.Password, "marathon-password", "", "Marathon password for basic auth")
flag.BoolVar(&config.Marathon.VerifySsl, "marathon-ssl-verify", true, "Verify certificates when connecting via SSL")
flag.DurationVar(&config.Marathon.Timeout, "marathon-timeout", 30*time.Second, "Time limit for requests made by the Consul HTTP client. A Timeout of zero means no timeout")

// Metrics
flag.StringVar(&config.Metrics.Target, "metrics-target", "stdout", "Metrics destination stdout or graphite (empty string disables metrics)")
Expand Down
5 changes: 3 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestConfig_ShouldBeMergedWithFileDefaultsAndFlags(t *testing.T) {
SslCaCert: "",
Token: "",
Tag: "marathon",
Timeout: 1 * time.Second,
Timeout: 3 * time.Second,
},
Web: struct{ Listen string }{Listen: ":4000"},
Sync: sync.Config{
Expand All @@ -107,7 +107,8 @@ func TestConfig_ShouldBeMergedWithFileDefaultsAndFlags(t *testing.T) {
Protocol: "http",
Username: "",
Password: "",
VerifySsl: true},
VerifySsl: true,
Timeout: 30 * time.Second},
Metrics: metrics.Config{Target: "stdout",
Prefix: "default",
Interval: 30 * time.Second,
Expand Down
5 changes: 3 additions & 2 deletions debian/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"SslCaCert": "",
"Token": "",
"Tag": "marathon",
"Timeout": 1000000000
"Timeout": 3000000000
},
"Web": {
"Listen": ":4000"
Expand All @@ -28,7 +28,8 @@
"Protocol": "http",
"Username": "",
"Password": "",
"VerifySsl": true
"VerifySsl": true,
"Timeout": 30000000000
},
"Metrics": {
"Target": "stdout",
Expand Down
3 changes: 3 additions & 0 deletions marathon/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package marathon

import "time"

type Config struct {
Location string
Protocol string
Username string
Password string
VerifySsl bool
Timeout time.Duration
}
33 changes: 15 additions & 18 deletions marathon/marathon.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
log "github.com/Sirupsen/logrus"
"github.com/allegro/marathon-consul/apps"
"github.com/allegro/marathon-consul/metrics"
"github.com/sethgrid/pester"
"io/ioutil"
"net/http"
"net/url"
Expand All @@ -22,10 +21,10 @@ type Marathoner interface {
}

type Marathon struct {
Location string
Protocol string
Auth *url.Userinfo
transport http.RoundTripper
Location string
Protocol string
Auth *url.Userinfo
client *http.Client
}

type LeaderResponse struct {
Expand All @@ -39,15 +38,19 @@ func New(config Config) (*Marathon, error) {
} else {
auth = url.UserPassword(config.Username, config.Password)
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !config.VerifySsl,
},
}
return &Marathon{
Location: config.Location,
Protocol: config.Protocol,
Auth: auth,
transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !config.VerifySsl,
},
client: &http.Client{
Transport: transport,
Timeout: config.Timeout,
},
}, nil
}
Expand Down Expand Up @@ -103,7 +106,6 @@ func (m Marathon) Leader() (string, error) {
}

func (m Marathon) get(url string) ([]byte, error) {
client := m.getClient()
request, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Error(err.Error())
Expand All @@ -118,12 +120,13 @@ func (m Marathon) get(url string) ([]byte, error) {
}).Debug("Sending GET request to marathon")

var response *http.Response
metrics.Time("marathon.get", func() { response, err = client.Do(request) })
metrics.Time("marathon.get", func() { response, err = m.client.Do(request) })
if err != nil {
metrics.Mark("marathon.get.error")
m.logHTTPError(response, err)
return nil, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
metrics.Mark("marathon.get.error")
metrics.Mark(fmt.Sprintf("marathon.get.error.%d", response.StatusCode))
Expand Down Expand Up @@ -162,9 +165,3 @@ func (m Marathon) urlWithQuery(path string, query string) string {
}
return marathon.String()
}

func (m Marathon) getClient() *pester.Client {
client := pester.New()
client.Transport = m.transport
return client
}
48 changes: 24 additions & 24 deletions marathon/marathon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestMarathon_AppsWhenMarathonReturnEmptyList(t *testing.T) {

url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
apps, err := m.Apps()
//then
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestMarathon_AppsWhenServerIsNotResponding(t *testing.T) {
assert.Nil(t, apps)
}

func TestMarathon_AppsWhenMarathonConnectionFailedShouldRetry(t *testing.T) {
func TestMarathon_AppsWhenMarathonConnectionFailedShouldNotRetry(t *testing.T) {
t.Parallel()
// given
calls := 0
Expand All @@ -62,16 +62,16 @@ func TestMarathon_AppsWhenMarathonConnectionFailedShouldRetry(t *testing.T) {

url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
apps, err := m.Apps()
//then
assert.Error(t, err)
assert.Empty(t, apps)
assert.Equal(t, 3, calls)
assert.Equal(t, 1, calls)
}

func TestMarathon_TasksWhenMarathonConnectionFailedShouldRetry(t *testing.T) {
func TestMarathon_TasksWhenMarathonConnectionFailedShouldNotRetry(t *testing.T) {
t.Parallel()
// given
calls := 0
Expand All @@ -83,16 +83,16 @@ func TestMarathon_TasksWhenMarathonConnectionFailedShouldRetry(t *testing.T) {

url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
tasks, err := m.Tasks("/app/id")
//then
assert.Error(t, err)
assert.Empty(t, tasks)
assert.Equal(t, 3, calls)
assert.Equal(t, 1, calls)
}

func TestMarathon_AppWhenMarathonConnectionFailedShouldRetry(t *testing.T) {
func TestMarathon_AppWhenMarathonConnectionFailedShouldNotRetry(t *testing.T) {
t.Parallel()
// given
calls := 0
Expand All @@ -104,13 +104,13 @@ func TestMarathon_AppWhenMarathonConnectionFailedShouldRetry(t *testing.T) {

url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
app, err := m.App("/app/id")
//then
assert.Error(t, err)
assert.Nil(t, app)
assert.Equal(t, 3, calls)
assert.Equal(t, 1, calls)
}

func TestMarathon_AppsWhenMarathonReturnEmptyResponse(t *testing.T) {
Expand All @@ -121,7 +121,7 @@ func TestMarathon_AppsWhenMarathonReturnEmptyResponse(t *testing.T) {

url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
apps, err := m.Apps()
//then
Expand All @@ -137,7 +137,7 @@ func TestMarathon_AppsWhenMarathonReturnMalformedJsonResponse(t *testing.T) {

url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
app, err := m.App("/test/app")
//then
Expand All @@ -153,7 +153,7 @@ func TestMarathon_AppWhenMarathonReturnEmptyApp(t *testing.T) {

url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
app, err := m.App("/test/app")
//then
Expand All @@ -169,7 +169,7 @@ func TestMarathon_AppWhenMarathonReturnEmptyResponse(t *testing.T) {

url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
app, err := m.App("/test/app")
//then
Expand All @@ -185,7 +185,7 @@ func TestMarathon_AppWhenMarathonReturnMalformedJsonResponse(t *testing.T) {

url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
apps, err := m.Apps()
//then
Expand All @@ -207,7 +207,7 @@ func TestMarathon_TasksWhenMarathonReturnEmptyList(t *testing.T) {
defer server.Close()
url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
tasks, err := m.Tasks("//test/app")
//then
Expand All @@ -222,7 +222,7 @@ func TestMarathon_TasksWhenMarathonReturnEmptyResponse(t *testing.T) {
defer server.Close()
url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
tasks, err := m.Tasks("/test/app")
//then
Expand All @@ -237,7 +237,7 @@ func TestMarathon_TasksWhenMarathonReturnMalformedJsonResponse(t *testing.T) {
defer server.Close()
url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport
// when
tasks, err := m.Tasks("/test/app")
//then
Expand All @@ -252,7 +252,7 @@ func TestConfig_transport(t *testing.T) {
// when
marathon, _ := New(config)
// then
transport, ok := marathon.transport.(*http.Transport)
transport, ok := marathon.client.Transport.(*http.Transport)
assert.True(t, ok)
assert.True(t, transport.TLSClientConfig.InsecureSkipVerify)
}
Expand Down Expand Up @@ -285,7 +285,7 @@ func TestLeader_SuccessfulResponse(t *testing.T) {
defer server.Close()
url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport

// when
leader, err := m.Leader()
Expand All @@ -303,7 +303,7 @@ func TestLeader_ErrorOnMalformedJsonResponse(t *testing.T) {
defer server.Close()
url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport

// when
leader, err := m.Leader()
Expand All @@ -313,7 +313,7 @@ func TestLeader_ErrorOnMalformedJsonResponse(t *testing.T) {
assert.Empty(t, leader)
}

func TestLeader_RetryOnFailingResponse(t *testing.T) {
func TestLeader_NotRetryOnFailingResponse(t *testing.T) {
t.Parallel()

// given
Expand All @@ -325,14 +325,14 @@ func TestLeader_RetryOnFailingResponse(t *testing.T) {
defer server.Close()
url, _ := url.Parse(server.URL)
m, _ := New(Config{Location: url.Host, Protocol: "HTTP"})
m.transport = transport
m.client.Transport = transport

// when
leader, err := m.Leader()

//then
assert.Error(t, err)
assert.Equal(t, 3, calls)
assert.Equal(t, 1, calls)
assert.Empty(t, leader)
}

Expand Down

0 comments on commit 7088ccc

Please sign in to comment.