2019-02-22 19:02:03 +00:00
|
|
|
package prometheus_client_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"fmt"
|
|
|
|
"github.com/influxdata/telegraf/plugins/outputs/prometheus_client"
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
|
|
"github.com/influxdata/toml"
|
2019-02-26 18:34:50 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-02-22 19:02:03 +00:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-02-22 22:18:36 +00:00
|
|
|
var pki = testutil.NewPKI("../../../testutil/pki")
|
|
|
|
|
2019-02-22 19:02:03 +00:00
|
|
|
var configWithTLS = fmt.Sprintf(`
|
|
|
|
listen = "127.0.0.1:9090"
|
2019-02-22 22:18:36 +00:00
|
|
|
tls_allowed_cacerts = ["%s"]
|
2019-02-22 19:02:03 +00:00
|
|
|
tls_cert = "%s"
|
|
|
|
tls_key = "%s"
|
2019-02-22 22:18:36 +00:00
|
|
|
`, pki.TLSServerConfig().TLSAllowedCACerts[0], pki.TLSServerConfig().TLSCert, pki.TLSServerConfig().TLSKey)
|
2019-02-22 19:02:03 +00:00
|
|
|
|
|
|
|
var configWithoutTLS = `
|
|
|
|
listen = "127.0.0.1:9090"
|
|
|
|
`
|
|
|
|
|
|
|
|
type PrometheusClientTestContext struct {
|
|
|
|
Output *prometheus_client.PrometheusClient
|
|
|
|
Accumulator *testutil.Accumulator
|
|
|
|
Client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWorksWithoutTLS(t *testing.T) {
|
|
|
|
tc := buildTestContext(t, []byte(configWithoutTLS))
|
|
|
|
err := tc.Output.Connect()
|
|
|
|
defer tc.Output.Close()
|
|
|
|
|
2019-02-26 18:34:50 +00:00
|
|
|
require.NoError(t, err)
|
2019-02-22 19:02:03 +00:00
|
|
|
|
2019-02-26 18:34:50 +00:00
|
|
|
response, err := tc.Client.Get("http://localhost:9090/metrics")
|
|
|
|
require.NoError(t, err)
|
2019-02-22 19:02:03 +00:00
|
|
|
|
2019-02-26 18:34:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, response.StatusCode, http.StatusOK)
|
2019-02-22 19:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWorksWithTLS(t *testing.T) {
|
|
|
|
tc := buildTestContext(t, []byte(configWithTLS))
|
|
|
|
err := tc.Output.Connect()
|
|
|
|
defer tc.Output.Close()
|
2019-02-26 18:34:50 +00:00
|
|
|
require.NoError(t, err)
|
2019-02-22 19:02:03 +00:00
|
|
|
|
2019-02-26 18:34:50 +00:00
|
|
|
response, err := tc.Client.Get("https://localhost:9090/metrics")
|
|
|
|
require.NoError(t, err)
|
2019-02-22 19:02:03 +00:00
|
|
|
|
2019-02-26 18:34:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, response.StatusCode, http.StatusOK)
|
2019-02-22 19:02:03 +00:00
|
|
|
|
|
|
|
response, err = tc.Client.Get("http://localhost:9090/metrics")
|
2019-02-26 18:34:50 +00:00
|
|
|
require.Error(t, err)
|
2019-02-22 19:02:03 +00:00
|
|
|
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
response, err = client.Get("https://localhost:9090/metrics")
|
|
|
|
|
2019-02-26 18:34:50 +00:00
|
|
|
require.Error(t, err)
|
2019-02-22 19:02:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func buildTestContext(t *testing.T, config []byte) *PrometheusClientTestContext {
|
|
|
|
output := prometheus_client.NewClient()
|
|
|
|
err := toml.Unmarshal(config, output)
|
2019-02-26 18:34:50 +00:00
|
|
|
require.NoError(t, err)
|
2019-02-22 19:02:03 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
httpClient *http.Client
|
|
|
|
)
|
|
|
|
|
2019-02-22 22:18:36 +00:00
|
|
|
if len(output.TLSAllowedCACerts) != 0 {
|
2019-02-26 18:34:50 +00:00
|
|
|
httpClient = buildClientWithTLS(t, output)
|
2019-02-22 19:02:03 +00:00
|
|
|
} else {
|
|
|
|
httpClient = buildClientWithoutTLS()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &PrometheusClientTestContext{
|
|
|
|
Output: output,
|
|
|
|
Accumulator: &testutil.Accumulator{},
|
|
|
|
Client: httpClient,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildClientWithoutTLS() *http.Client {
|
|
|
|
return &http.Client{}
|
|
|
|
}
|
|
|
|
|
2019-02-26 18:34:50 +00:00
|
|
|
func buildClientWithTLS(t *testing.T, output *prometheus_client.PrometheusClient) *http.Client {
|
2019-02-22 22:18:36 +00:00
|
|
|
tlsConfig, err := pki.TLSClientConfig().TLSConfig()
|
2019-02-26 18:34:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-02-22 19:02:03 +00:00
|
|
|
transport := &http.Transport{TLSClientConfig: tlsConfig}
|
|
|
|
return &http.Client{Transport: transport}
|
|
|
|
}
|