Skip to content

Commit

Permalink
Use random available port in prometheus output tests (#5555)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored Mar 8, 2019
1 parent bdb9d5c commit 91cd17f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
26 changes: 25 additions & 1 deletion plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
Expand Down Expand Up @@ -70,6 +71,7 @@ type PrometheusClient struct {
tlsint.ServerConfig

server *http.Server
url string

sync.Mutex
// fam is the non-expired MetricFamily by Prometheus metric name.
Expand Down Expand Up @@ -107,7 +109,7 @@ var sampleConfig = `
## If set, enable TLS with the given certificate.
# tls_cert = "/etc/ssl/telegraf.crt"
# tls_key = "/etc/ssl/telegraf.key"
## Set one or more allowed client CA certificate file names to
## enable mutually authenticated TLS connections
# tls_allowed_cacerts = ["/etc/telegraf/clientca.pem"]
Expand Down Expand Up @@ -213,6 +215,8 @@ func (p *PrometheusClient) Connect() error {
return err
}

p.url = createURL(tlsConfig, listener, p.Path)

go func() {
err := p.server.Serve(listener)
if err != nil && err != http.ErrServerClosed {
Expand All @@ -224,11 +228,31 @@ func (p *PrometheusClient) Connect() error {
return nil
}

// Address returns the address the plugin is listening on. If not listening
// an empty string is returned.
func (p *PrometheusClient) URL() string {
return p.url
}

func createURL(tlsConfig *tls.Config, listener net.Listener, path string) string {
u := url.URL{
Scheme: "http",
Host: listener.Addr().String(),
Path: path,
}

if tlsConfig != nil {
u.Scheme = "https"
}
return u.String()
}

func (p *PrometheusClient) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
err := p.server.Shutdown(ctx)
prometheus.Unregister(p)
p.url = ""
return err
}

Expand Down
23 changes: 10 additions & 13 deletions plugins/outputs/prometheus_client/prometheus_client_tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ package prometheus_client_test
import (
"crypto/tls"
"fmt"
"net/http"
"testing"

"github.com/influxdata/telegraf/plugins/outputs/prometheus_client"
"github.com/influxdata/telegraf/testutil"
"github.com/influxdata/toml"
"github.com/stretchr/testify/require"
"net/http"
"testing"
)

var pki = testutil.NewPKI("../../../testutil/pki")

var configWithTLS = fmt.Sprintf(`
listen = "127.0.0.1:9090"
listen = "127.0.0.1:0"
tls_allowed_cacerts = ["%s"]
tls_cert = "%s"
tls_key = "%s"
`, pki.TLSServerConfig().TLSAllowedCACerts[0], pki.TLSServerConfig().TLSCert, pki.TLSServerConfig().TLSKey)

var configWithoutTLS = `
listen = "127.0.0.1:9090"
listen = "127.0.0.1:0"
`

type PrometheusClientTestContext struct {
Expand All @@ -33,11 +34,10 @@ type PrometheusClientTestContext struct {
func TestWorksWithoutTLS(t *testing.T) {
tc := buildTestContext(t, []byte(configWithoutTLS))
err := tc.Output.Connect()
defer tc.Output.Close()

require.NoError(t, err)
defer tc.Output.Close()

response, err := tc.Client.Get("http://localhost:9090/metrics")
response, err := tc.Client.Get(tc.Output.URL())
require.NoError(t, err)

require.NoError(t, err)
Expand All @@ -47,24 +47,21 @@ func TestWorksWithoutTLS(t *testing.T) {
func TestWorksWithTLS(t *testing.T) {
tc := buildTestContext(t, []byte(configWithTLS))
err := tc.Output.Connect()
defer tc.Output.Close()
require.NoError(t, err)
defer tc.Output.Close()

response, err := tc.Client.Get("https://localhost:9090/metrics")
response, err := tc.Client.Get(tc.Output.URL())
require.NoError(t, err)

require.NoError(t, err)
require.Equal(t, response.StatusCode, http.StatusOK)

response, err = tc.Client.Get("http://localhost:9090/metrics")
require.Error(t, err)

tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

client := &http.Client{Transport: tr}
response, err = client.Get("https://localhost:9090/metrics")
response, err = client.Get(tc.Output.URL())

require.Error(t, err)
}
Expand Down

0 comments on commit 91cd17f

Please sign in to comment.