Fixes #4090. Marathon service discovery for 5XX http response (#4091)

Signed-off-by: rohit01 <hello@rohit.io>
This commit is contained in:
Rohit Gupta 2018-04-17 13:58:06 +05:30 committed by Brian Brazil
parent d13db89548
commit 30c3e02864
2 changed files with 33 additions and 0 deletions

View file

@ -336,6 +336,10 @@ func fetchApps(client *http.Client, url string) (*AppList, error) {
return nil, err
}
if (resp.StatusCode < 200) || (resp.StatusCode >= 300) {
return nil, fmt.Errorf("Non 2xx status '%v' response during marathon service discovery", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err

View file

@ -16,7 +16,9 @@ package marathon
import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
@ -326,6 +328,33 @@ func TestMarathonZeroTaskPorts(t *testing.T) {
}
}
func Test500ErrorHttpResponseWithValidJSONBody(t *testing.T) {
var (
ch = make(chan []*targetgroup.Group, 1)
client = fetchApps
)
// Simulate 500 error with a valid JSON response.
respHandler := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
io.WriteString(w, `{}`)
}
// Create a test server with mock HTTP handler.
ts := httptest.NewServer(http.HandlerFunc(respHandler))
defer ts.Close()
// Backup conf for future tests.
backupConf := conf
defer func() {
conf = backupConf
}()
// Setup conf for the test case.
conf = SDConfig{Servers: []string{ts.URL}}
// Execute test case and validate behaviour.
if err := testUpdateServices(client, ch); err == nil {
t.Fatalf("Expected error for 5xx HTTP response from marathon server")
}
}
func marathonTestAppListWithoutPortMappings(labels map[string]string, runningTasks int) *AppList {
var (
task = Task{