From 93fff5ff2e66a32a3580371520a5c63ad1eff2c2 Mon Sep 17 00:00:00 2001 From: Len Smith Date: Sat, 7 Jan 2017 14:48:29 -0600 Subject: [PATCH] Added in test for following redirects. https://github.com/influxdata/telegraf/issues/2191 This depends on code/PR for influxDB https://github.com/influxdata/influxdb/pull/7803 --- plugins/outputs/influxdb/influxdb.go | 20 +++++-- plugins/outputs/influxdb/influxdb_test.go | 72 +++++++++++++++++++++++ 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/plugins/outputs/influxdb/influxdb.go b/plugins/outputs/influxdb/influxdb.go index 8c23b2c5a..059f126d6 100644 --- a/plugins/outputs/influxdb/influxdb.go +++ b/plugins/outputs/influxdb/influxdb.go @@ -38,6 +38,9 @@ type InfluxDB struct { // Use SSL but skip chain & host verification InsecureSkipVerify bool + // Follow Redirects even for puts/posts. This can be insuecure + InsecureFollowRedirect bool + // Precision is only here for legacy support. It will be ignored. Precision string @@ -117,17 +120,22 @@ func (i *InfluxDB) Connect() error { default: // If URL doesn't start with "udp", assume HTTP client c, err := client.NewHTTPClient(client.HTTPConfig{ - Addr: u, - Username: i.Username, - Password: i.Password, - UserAgent: i.UserAgent, - Timeout: i.Timeout.Duration, - TLSConfig: tlsCfg, + Addr: u, + Username: i.Username, + Password: i.Password, + UserAgent: i.UserAgent, + Timeout: i.Timeout.Duration, + InsecureFollowRedirect: i.InsecureFollowRedirect, + TLSConfig: tlsCfg, }) if err != nil { return err } + if i.InsecureFollowRedirect { + c.Ping(10) + } + err = createDatabase(c, i.Database) if err != nil { log.Println("E! Database creation failed: " + err.Error()) diff --git a/plugins/outputs/influxdb/influxdb_test.go b/plugins/outputs/influxdb/influxdb_test.go index 1414fa839..a90bff810 100644 --- a/plugins/outputs/influxdb/influxdb_test.go +++ b/plugins/outputs/influxdb/influxdb_test.go @@ -39,3 +39,75 @@ func TestHTTPInflux(t *testing.T) { err = i.Write(testutil.MockMetrics()) require.NoError(t, err) } + +// TestHTTPInfluxFollow301RedirectFalse verifies that the default behavior is for +// InsecureFollowRedirect is false and that it will generate an error. +func TestHTTPInfluxFollow301RedirectFalse(t *testing.T) { + + // The influxDB HTTP server + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, `{"results":[{}]}`) + })) + defer ts.Close() + + // A intermediate HTTP server which sends a redirect + redirectServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "test/html") + w.Header().Set("Location", ts.URL) + w.WriteHeader(http.StatusMovedPermanently) + fmt.Fprintln(w, `301 Moved Permanently`) + fmt.Fprintln(w, `

301 Moved Permanently


nginx/1.10.0 (Ubuntu)
`) + })) + defer redirectServer.Close() + + i := InfluxDB{ + URLs: []string{redirectServer.URL}, + } + + err := i.Connect() + require.NoError(t, err) + + // This should be empty because we have one URL, with a redirect. Since we don't + // follow it, the connection is not added. + if len(i.conns) != 0 { + t.Errorf("Did not get an empty list of connections: %s\n", i.conns) + } + +} + +// TestHTTPInfluxFollow301RedirectTrue verifies that if InsecureFollowRedirect +// is set to true, then Ping() will be called and the CheckRedirect function will +// be enabled and will set URL to the last redirect in the chain. Up to 10 +// redirects max will be followed ( This is net/http behavior ) +func TestHTTPInfluxFollow301RedirectTrue(t *testing.T) { + + // The influxDB HTTP server + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, `{"results":[{}]}`) + })) + defer ts.Close() + + // A intermediate HTTP server which sends a redirect + redirectServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "test/html") + w.Header().Set("Location", ts.URL) + w.WriteHeader(http.StatusMovedPermanently) + fmt.Fprintln(w, `301 Moved Permanently`) + fmt.Fprintln(w, `

301 Moved Permanently


nginx/1.10.0 (Ubuntu)
`) + })) + defer redirectServer.Close() + + i := InfluxDB{ + URLs: []string{redirectServer.URL}, + InsecureFollowRedirect: true, + } + + err := i.Connect() + + require.NoError(t, err) + +}