test(outputs.instrumental): Allow setting custom port (#13576)

This commit is contained in:
Joshua Powers 2023-07-10 06:48:54 -06:00 committed by GitHub
parent 908c6a551f
commit 00b0ae68e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

@ -28,6 +28,7 @@ var (
type Instrumental struct {
Host string `toml:"host"`
Port int `toml:"port"`
APIToken config.Secret `toml:"api_token"`
Prefix string `toml:"prefix"`
DataFormat string `toml:"data_format"`
@ -44,6 +45,7 @@ type Instrumental struct {
const (
DefaultHost = "collector.instrumentalapp.com"
DefaultPort = 8000
HelloMessage = "hello version go/telegraf/1.1\n"
AuthFormat = "authenticate %s\n"
HandshakeFormat = HelloMessage + AuthFormat
@ -70,7 +72,8 @@ func (i *Instrumental) Init() error {
}
func (i *Instrumental) Connect() error {
connection, err := net.DialTimeout("tcp", i.Host+":8000", time.Duration(i.Timeout))
addr := fmt.Sprintf("%s:%d", i.Host, i.Port)
connection, err := net.DialTimeout("tcp", addr, time.Duration(i.Timeout))
if err != nil {
i.conn = nil
@ -203,6 +206,7 @@ func init() {
outputs.Add("instrumental", func() telegraf.Output {
return &Instrumental{
Host: DefaultHost,
Port: DefaultPort,
Template: graphite.DefaultTemplate,
}
})

View File

@ -18,10 +18,11 @@ import (
func TestWrite(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
TCPServer(t, &wg)
port := TCPServer(t, &wg)
i := Instrumental{
Host: "127.0.0.1",
Port: port,
APIToken: config.NewSecret([]byte("abc123token")),
Prefix: "my.prefix",
}
@ -80,8 +81,8 @@ func TestWrite(t *testing.T) {
wg.Wait()
}
func TCPServer(t *testing.T, wg *sync.WaitGroup) {
tcpServer, err := net.Listen("tcp", "127.0.0.1:8000")
func TCPServer(t *testing.T, wg *sync.WaitGroup) int {
tcpServer, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
go func() {
@ -132,4 +133,6 @@ func TCPServer(t *testing.T, wg *sync.WaitGroup) {
err = conn.Close()
require.NoError(t, err)
}()
return tcpServer.Addr().(*net.TCPAddr).Port
}