2018-02-16 08:00:10 +08:00
|
|
|
package http_test
|
|
|
|
|
|
|
|
|
|
import (
|
2018-12-12 11:12:00 +08:00
|
|
|
"compress/gzip"
|
|
|
|
|
"fmt"
|
2021-09-29 05:16:32 +08:00
|
|
|
"io"
|
2023-10-18 20:44:36 +08:00
|
|
|
"math/rand"
|
|
|
|
|
"net"
|
2018-02-16 08:00:10 +08:00
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2021-04-23 21:37:27 +08:00
|
|
|
"net/url"
|
2023-10-18 20:44:36 +08:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
2018-02-16 08:00:10 +08:00
|
|
|
"testing"
|
2022-02-04 00:15:38 +08:00
|
|
|
"time"
|
2018-02-16 08:00:10 +08:00
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
2022-02-04 00:15:38 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2021-04-23 21:37:27 +08:00
|
|
|
httpconfig "github.com/influxdata/telegraf/plugins/common/http"
|
2021-10-26 21:45:03 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/common/oauth"
|
|
|
|
|
httpplugin "github.com/influxdata/telegraf/plugins/inputs/http"
|
2022-02-04 00:15:38 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/csv"
|
2022-07-29 04:30:36 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/influx"
|
2022-06-22 23:56:51 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/json"
|
2022-06-30 04:46:43 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/value"
|
2018-02-16 08:00:10 +08:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
|
)
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
func TestHTTPWithJSONFormat(t *testing.T) {
|
2018-02-16 08:00:10 +08:00
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/endpoint" {
|
|
|
|
|
_, _ = w.Write([]byte(simpleJSON))
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
address := fakeServer.URL + "/endpoint"
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-02-16 08:00:10 +08:00
|
|
|
}
|
|
|
|
|
metricName := "metricName"
|
2018-08-23 10:26:48 +08:00
|
|
|
|
2022-02-04 00:15:38 +08:00
|
|
|
plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
2022-06-22 23:56:51 +08:00
|
|
|
p := &json.Parser{MetricName: "metricName"}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
return p, err
|
2018-08-23 10:26:48 +08:00
|
|
|
})
|
2018-02-16 08:00:10 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, plugin.Init())
|
2018-02-16 08:00:10 +08:00
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
|
|
|
|
|
require.Len(t, acc.Metrics, 1)
|
|
|
|
|
|
|
|
|
|
// basic check to see if we got the right field, value and tag
|
|
|
|
|
var metric = acc.Metrics[0]
|
|
|
|
|
require.Equal(t, metric.Measurement, metricName)
|
|
|
|
|
require.Len(t, acc.Metrics[0].Fields, 1)
|
2023-11-02 04:09:55 +08:00
|
|
|
require.Equal(t, 1.2, acc.Metrics[0].Fields["a"])
|
2021-10-26 21:45:03 +08:00
|
|
|
require.Equal(t, acc.Metrics[0].Tags["url"], address)
|
2018-02-16 08:00:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestHTTPHeaders(t *testing.T) {
|
|
|
|
|
header := "X-Special-Header"
|
|
|
|
|
headerValue := "Special-Value"
|
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/endpoint" {
|
|
|
|
|
if r.Header.Get(header) == headerValue {
|
|
|
|
|
_, _ = w.Write([]byte(simpleJSON))
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
address := fakeServer.URL + "/endpoint"
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2018-02-16 08:00:10 +08:00
|
|
|
Headers: map[string]string{header: headerValue},
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-02-16 08:00:10 +08:00
|
|
|
}
|
2018-08-23 10:26:48 +08:00
|
|
|
|
2022-02-04 00:15:38 +08:00
|
|
|
plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
2022-06-22 23:56:51 +08:00
|
|
|
p := &json.Parser{MetricName: "metricName"}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
return p, err
|
2018-08-23 10:26:48 +08:00
|
|
|
})
|
2018-02-16 08:00:10 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, plugin.Init())
|
2018-02-16 08:00:10 +08:00
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-19 03:06:56 +08:00
|
|
|
func TestHTTPContentLengthHeader(t *testing.T) {
|
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/endpoint" {
|
|
|
|
|
if r.Header.Get("Content-Length") != "" {
|
|
|
|
|
_, _ = w.Write([]byte(simpleJSON))
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
|
|
|
|
address := fakeServer.URL + "/endpoint"
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
|
|
|
|
Headers: map[string]string{},
|
|
|
|
|
Body: "{}",
|
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
2022-06-22 23:56:51 +08:00
|
|
|
p := &json.Parser{MetricName: "metricName"}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
return p, err
|
2022-05-19 03:06:56 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
require.NoError(t, plugin.Init())
|
|
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 08:00:10 +08:00
|
|
|
func TestInvalidStatusCode(t *testing.T) {
|
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
address := fakeServer.URL + "/endpoint"
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-02-16 08:00:10 +08:00
|
|
|
}
|
|
|
|
|
|
2022-02-04 00:15:38 +08:00
|
|
|
plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
2022-06-22 23:56:51 +08:00
|
|
|
p := &json.Parser{MetricName: "metricName"}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
return p, err
|
2018-08-23 10:26:48 +08:00
|
|
|
})
|
2018-02-16 08:00:10 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, plugin.Init())
|
2018-02-16 08:00:10 +08:00
|
|
|
require.Error(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 05:23:36 +08:00
|
|
|
func TestSuccessStatusCodes(t *testing.T) {
|
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusAccepted)
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
address := fakeServer.URL + "/endpoint"
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2019-10-22 05:23:36 +08:00
|
|
|
SuccessStatusCodes: []int{200, 202},
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2019-10-22 05:23:36 +08:00
|
|
|
}
|
|
|
|
|
|
2022-02-04 00:15:38 +08:00
|
|
|
plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
2022-06-22 23:56:51 +08:00
|
|
|
p := &json.Parser{MetricName: "metricName"}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
return p, err
|
2019-10-22 05:23:36 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, plugin.Init())
|
2019-10-22 05:23:36 +08:00
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 10:36:01 +08:00
|
|
|
func TestMethod(t *testing.T) {
|
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin := &httpplugin.HTTP{
|
2018-02-16 10:36:01 +08:00
|
|
|
URLs: []string{fakeServer.URL},
|
|
|
|
|
Method: "POST",
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-02-16 10:36:01 +08:00
|
|
|
}
|
|
|
|
|
|
2022-02-04 00:15:38 +08:00
|
|
|
plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
2022-06-22 23:56:51 +08:00
|
|
|
p := &json.Parser{MetricName: "metricName"}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
return p, err
|
2018-08-23 10:26:48 +08:00
|
|
|
})
|
2018-02-16 10:36:01 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, plugin.Init())
|
2018-02-16 10:36:01 +08:00
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 08:00:10 +08:00
|
|
|
const simpleJSON = `
|
|
|
|
|
{
|
|
|
|
|
"a": 1.2
|
|
|
|
|
}
|
|
|
|
|
`
|
2022-02-04 00:15:38 +08:00
|
|
|
const simpleCSVWithHeader = `
|
|
|
|
|
# Simple CSV with header(s)
|
|
|
|
|
a,b,c
|
|
|
|
|
1.2,3.1415,ok
|
|
|
|
|
`
|
2018-12-12 11:12:00 +08:00
|
|
|
|
|
|
|
|
func TestBodyAndContentEncoding(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.NotFoundHandler())
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
2021-10-26 21:45:03 +08:00
|
|
|
address := fmt.Sprintf("http://%s", ts.Listener.Addr().String())
|
2018-12-12 11:12:00 +08:00
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin *httpplugin.HTTP
|
2018-12-12 11:12:00 +08:00
|
|
|
queryHandlerFunc func(t *testing.T, w http.ResponseWriter, r *http.Request)
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "no body",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
2018-12-12 11:12:00 +08:00
|
|
|
Method: "POST",
|
2021-10-26 21:45:03 +08:00
|
|
|
URLs: []string{address},
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-12-12 11:12:00 +08:00
|
|
|
},
|
|
|
|
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
2021-09-29 05:16:32 +08:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2018-12-12 11:12:00 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, []byte(""), body)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "post body",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2018-12-12 11:12:00 +08:00
|
|
|
Method: "POST",
|
|
|
|
|
Body: "test",
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-12-12 11:12:00 +08:00
|
|
|
},
|
|
|
|
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
2021-09-29 05:16:32 +08:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2018-12-12 11:12:00 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, []byte("test"), body)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "get method body is sent",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2018-12-12 11:12:00 +08:00
|
|
|
Method: "GET",
|
|
|
|
|
Body: "test",
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-12-12 11:12:00 +08:00
|
|
|
},
|
|
|
|
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
2021-09-29 05:16:32 +08:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2018-12-12 11:12:00 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, []byte("test"), body)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "gzip encoding",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
2018-12-12 11:12:00 +08:00
|
|
|
Method: "GET",
|
|
|
|
|
Body: "test",
|
|
|
|
|
ContentEncoding: "gzip",
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2018-12-12 11:12:00 +08:00
|
|
|
},
|
|
|
|
|
queryHandlerFunc: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
2023-11-02 04:09:55 +08:00
|
|
|
require.Equal(t, "gzip", r.Header.Get("Content-Encoding"))
|
2018-12-12 11:12:00 +08:00
|
|
|
|
|
|
|
|
gr, err := gzip.NewReader(r.Body)
|
|
|
|
|
require.NoError(t, err)
|
2021-09-29 05:16:32 +08:00
|
|
|
body, err := io.ReadAll(gr)
|
2018-12-12 11:12:00 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, []byte("test"), body)
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
tt.queryHandlerFunc(t, w, r)
|
|
|
|
|
})
|
|
|
|
|
|
2022-02-04 00:15:38 +08:00
|
|
|
tt.plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
2022-07-29 04:30:36 +08:00
|
|
|
parser := &influx.Parser{}
|
|
|
|
|
err := parser.Init()
|
|
|
|
|
return parser, err
|
2022-02-04 00:15:38 +08:00
|
|
|
})
|
2018-12-12 11:12:00 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2021-04-09 00:43:39 +08:00
|
|
|
require.NoError(t, tt.plugin.Init())
|
2022-02-04 00:15:38 +08:00
|
|
|
require.NoError(t, tt.plugin.Gather(&acc))
|
2018-12-12 11:12:00 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-23 21:37:27 +08:00
|
|
|
|
|
|
|
|
type TestHandlerFunc func(t *testing.T, w http.ResponseWriter, r *http.Request)
|
|
|
|
|
|
|
|
|
|
func TestOAuthClientCredentialsGrant(t *testing.T) {
|
|
|
|
|
ts := httptest.NewServer(http.NotFoundHandler())
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
var token = "2YotnFZFEjr1zCsicMWpAA"
|
|
|
|
|
|
|
|
|
|
u, err := url.Parse(fmt.Sprintf("http://%s", ts.Listener.Addr().String()))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin *httpplugin.HTTP
|
2021-04-23 21:37:27 +08:00
|
|
|
tokenHandler TestHandlerFunc
|
|
|
|
|
handler TestHandlerFunc
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "no credentials",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
2021-04-23 21:37:27 +08:00
|
|
|
URLs: []string{u.String()},
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2021-04-23 21:37:27 +08:00
|
|
|
},
|
|
|
|
|
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
2023-10-26 05:09:13 +08:00
|
|
|
require.Empty(t, r.Header["Authorization"])
|
2021-04-23 21:37:27 +08:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "success",
|
2021-10-26 21:45:03 +08:00
|
|
|
plugin: &httpplugin.HTTP{
|
2021-04-23 21:37:27 +08:00
|
|
|
URLs: []string{u.String() + "/write"},
|
|
|
|
|
HTTPClientConfig: httpconfig.HTTPClientConfig{
|
|
|
|
|
OAuth2Config: oauth.OAuth2Config{
|
|
|
|
|
ClientID: "howdy",
|
|
|
|
|
ClientSecret: "secret",
|
|
|
|
|
TokenURL: u.String() + "/token",
|
|
|
|
|
Scopes: []string{"urn:opc:idm:__myscopes__"},
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-01-12 00:22:49 +08:00
|
|
|
Log: testutil.Logger{},
|
2021-04-23 21:37:27 +08:00
|
|
|
},
|
|
|
|
|
tokenHandler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
values := url.Values{}
|
|
|
|
|
values.Add("access_token", token)
|
|
|
|
|
values.Add("token_type", "bearer")
|
|
|
|
|
values.Add("expires_in", "3600")
|
|
|
|
|
_, err := w.Write([]byte(values.Encode()))
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
},
|
|
|
|
|
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
require.Equal(t, []string{"Bearer " + token}, r.Header["Authorization"])
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
switch r.URL.Path {
|
|
|
|
|
case "/write":
|
|
|
|
|
tt.handler(t, w, r)
|
|
|
|
|
case "/token":
|
|
|
|
|
tt.tokenHandler(t, w, r)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2022-02-04 00:15:38 +08:00
|
|
|
tt.plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
2022-06-30 04:46:43 +08:00
|
|
|
p := &value.Parser{
|
|
|
|
|
MetricName: "metric",
|
|
|
|
|
DataType: "string",
|
|
|
|
|
}
|
|
|
|
|
err := p.Init()
|
|
|
|
|
return p, err
|
2022-02-04 00:15:38 +08:00
|
|
|
})
|
|
|
|
|
|
2021-04-23 21:37:27 +08:00
|
|
|
err = tt.plugin.Init()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
err = tt.plugin.Gather(&acc)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-02-04 00:15:38 +08:00
|
|
|
|
|
|
|
|
func TestHTTPWithCSVFormat(t *testing.T) {
|
|
|
|
|
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/endpoint" {
|
|
|
|
|
_, _ = w.Write([]byte(simpleCSVWithHeader))
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
defer fakeServer.Close()
|
|
|
|
|
|
|
|
|
|
address := fakeServer.URL + "/endpoint"
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
|
|
|
|
parser := &csv.Parser{
|
|
|
|
|
MetricName: "metricName",
|
2022-02-24 09:28:16 +08:00
|
|
|
SkipRows: 3,
|
2022-02-04 00:15:38 +08:00
|
|
|
ColumnNames: []string{"a", "b", "c"},
|
|
|
|
|
TagColumns: []string{"c"},
|
|
|
|
|
}
|
|
|
|
|
err := parser.Init()
|
|
|
|
|
return parser, err
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expected := []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric("metricName",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"url": address,
|
|
|
|
|
"c": "ok",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"a": 1.2,
|
|
|
|
|
"b": 3.1415,
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
require.NoError(t, plugin.Init())
|
|
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
|
|
|
|
|
|
|
|
|
// Run the parser a second time to test for correct stateful handling
|
|
|
|
|
acc.ClearMetrics()
|
|
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
|
|
|
|
}
|
2023-10-18 20:44:36 +08:00
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
httpOverUnixScheme = "http+unix"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestConnectionOverUnixSocket(t *testing.T) {
|
|
|
|
|
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path == "/data" {
|
|
|
|
|
w.Header().Set("Content-Type", "text/csv")
|
|
|
|
|
_, _ = w.Write([]byte(simpleCSVWithHeader))
|
|
|
|
|
} else {
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
unixListenAddr := filepath.Join(os.TempDir(), fmt.Sprintf("httptestserver.%d.sock", rand.Intn(1_000_000)))
|
|
|
|
|
t.Cleanup(func() { os.Remove(unixListenAddr) })
|
|
|
|
|
|
|
|
|
|
unixListener, err := net.Listen("unix", unixListenAddr)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
ts.Listener = unixListener
|
|
|
|
|
ts.Start()
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
// NOTE: Remove ":" from windows filepath and replace all "\" with "/".
|
|
|
|
|
// This is *required* so that the unix socket path plays well with unixtransport.
|
|
|
|
|
replacer := strings.NewReplacer(":", "", "\\", "/")
|
|
|
|
|
sockPath := replacer.Replace(unixListenAddr)
|
|
|
|
|
|
|
|
|
|
address := fmt.Sprintf("%s://%s:/data", httpOverUnixScheme, sockPath)
|
|
|
|
|
plugin := &httpplugin.HTTP{
|
|
|
|
|
URLs: []string{address},
|
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plugin.SetParserFunc(func() (telegraf.Parser, error) {
|
|
|
|
|
parser := &csv.Parser{
|
|
|
|
|
MetricName: "metricName",
|
|
|
|
|
SkipRows: 3,
|
|
|
|
|
ColumnNames: []string{"a", "b", "c"},
|
|
|
|
|
TagColumns: []string{"c"},
|
|
|
|
|
}
|
|
|
|
|
err := parser.Init()
|
|
|
|
|
return parser, err
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expected := []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric("metricName",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"url": address,
|
|
|
|
|
"c": "ok",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"a": 1.2,
|
|
|
|
|
"b": 3.1415,
|
|
|
|
|
},
|
|
|
|
|
time.Unix(22000, 0),
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
require.NoError(t, plugin.Init())
|
|
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
|
|
|
|
|
|
|
|
|
// Run the parser a second time to test for correct stateful handling
|
|
|
|
|
acc.ClearMetrics()
|
|
|
|
|
require.NoError(t, acc.GatherError(plugin.Gather))
|
|
|
|
|
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
|
|
|
|
}
|