Skip to content

Commit b49d237

Browse files
committed
Use net.JoinHostPort
1 parent fca46fc commit b49d237

File tree

7 files changed

+20
-14
lines changed

7 files changed

+20
-14
lines changed

heartbeat/hbtest/hbtestutil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func ResolveChecks(ip string) validator.Validator {
213213
func SimpleURLChecks(t *testing.T, scheme string, host string, port uint16) validator.Validator {
214214
hostPort := host
215215
if port != 0 {
216-
hostPort = fmt.Sprintf("%s:%d", host, port)
216+
hostPort = net.JoinHostPort(host, strconv.Itoa(int(port)))
217217
}
218218

219219
u, err := url.Parse(fmt.Sprintf("%s://%s", scheme, hostPort))

heartbeat/monitors/active/http/http_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"os"
3333
"path"
3434
"reflect"
35+
"strconv"
3536
"sync"
3637
"testing"
3738
"time"
@@ -620,7 +621,7 @@ func TestConnRefusedJob(t *testing.T) {
620621
lookslike.Strict(lookslike.Compose(
621622
hbtest.BaseChecks(ip, "down", "http"),
622623
hbtest.SummaryStateChecks(0, 1),
623-
hbtest.ECSErrCodeChecks(ecserr.CODE_NET_COULD_NOT_CONNECT, fmt.Sprintf("%s:%d", ip, port)),
624+
hbtest.ECSErrCodeChecks(ecserr.CODE_NET_COULD_NOT_CONNECT, net.JoinHostPort(ip, strconv.Itoa(int(port)))),
624625
urlChecks(url),
625626
)),
626627
event.Fields,
@@ -642,7 +643,7 @@ func TestUnreachableJob(t *testing.T) {
642643
lookslike.Strict(lookslike.Compose(
643644
hbtest.BaseChecks(ip, "down", "http"),
644645
hbtest.SummaryStateChecks(0, 1),
645-
hbtest.ECSErrCodeChecks(ecserr.CODE_NET_COULD_NOT_CONNECT, fmt.Sprintf("%s:%d", ip, port)),
646+
hbtest.ECSErrCodeChecks(ecserr.CODE_NET_COULD_NOT_CONNECT, net.JoinHostPort(ip, strconv.Itoa(int(port)))),
646647
urlChecks(url),
647648
)),
648649
event.Fields,

libbeat/processors/add_kubernetes_metadata/indexers.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package add_kubernetes_metadata
1919

2020
import (
2121
"fmt"
22+
"net"
23+
"strconv"
2224

2325
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
2426
"github.com/elastic/elastic-agent-autodiscover/kubernetes/metadata"
@@ -255,7 +257,7 @@ func (h *IPPortIndexer) GetMetadata(pod *kubernetes.Pod) []MetadataIndex {
255257
if port.ContainerPort != 0 {
256258

257259
m = append(m, MetadataIndex{
258-
Index: fmt.Sprintf("%s:%d", pod.Status.PodIP, port.ContainerPort),
260+
Index: net.JoinHostPort(pod.Status.PodIP, strconv.Itoa(int(port.ContainerPort))),
259261
Data: h.metaGen.Generate(
260262
pod,
261263
metadata.WithFields("container.name", container.Name),
@@ -287,7 +289,7 @@ func (h *IPPortIndexer) GetIndexes(pod *kubernetes.Pod) []string {
287289

288290
for _, port := range ports {
289291
if port.ContainerPort != 0 {
290-
hostPorts = append(hostPorts, fmt.Sprintf("%s:%d", pod.Status.PodIP, port.ContainerPort))
292+
hostPorts = append(hostPorts, net.JoinHostPort(pod.Status.PodIP, strconv.Itoa(int(port.ContainerPort))))
291293
}
292294
}
293295
}

libbeat/processors/add_kubernetes_metadata/indexers_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package add_kubernetes_metadata
1919

2020
import (
2121
"fmt"
22+
"net"
23+
"strconv"
2224
"testing"
2325

2426
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
@@ -468,12 +470,12 @@ func TestIpPortIndexer(t *testing.T) {
468470
indexers = ipIndexer.GetMetadata(&pod)
469471
assert.Equal(t, 2, len(indexers))
470472
assert.Equal(t, ip, indexers[0].Index)
471-
assert.Equal(t, fmt.Sprintf("%s:%d", ip, port), indexers[1].Index)
473+
assert.Equal(t, net.JoinHostPort(ip, strconv.Itoa(int(port))), indexers[1].Index)
472474

473475
indices = ipIndexer.GetIndexes(&pod)
474476
assert.Equal(t, 2, len(indices))
475477
assert.Equal(t, ip, indices[0])
476-
assert.Equal(t, fmt.Sprintf("%s:%d", ip, port), indices[1])
478+
assert.Equal(t, net.JoinHostPort(ip, strconv.Itoa(int(port))), indices[1])
477479

478480
assert.Equal(t, expected.String(), indexers[0].Data.String())
479481
expected.Put("kubernetes.container",

metricbeat/helper/server/tcp/tcp_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
package tcp
2121

2222
import (
23-
"fmt"
2423
"net"
24+
"strconv"
2525
"testing"
2626

2727
"github.com/stretchr/testify/assert"
@@ -31,7 +31,7 @@ import (
3131
)
3232

3333
func GetTestTcpServer(host string, port int) (server.Server, error) {
34-
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", host, port))
34+
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(host, strconv.Itoa(int(port))))
3535

3636
if err != nil {
3737
return nil, err
@@ -80,7 +80,7 @@ func TestTcpServer(t *testing.T) {
8080
}
8181

8282
func writeToServer(t *testing.T, message, host string, port int) {
83-
servAddr := fmt.Sprintf("%s:%d", host, port)
83+
servAddr := net.JoinHostPort(host, strconv.Itoa(int(port)))
8484
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
8585
if err != nil {
8686
t.Error(err)

metricbeat/helper/server/udp/udp_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
package udp
2121

2222
import (
23-
"fmt"
2423
"net"
24+
"strconv"
2525
"testing"
2626

2727
"github.com/stretchr/testify/assert"
@@ -31,7 +31,7 @@ import (
3131
)
3232

3333
func GetTestUdpServer(host string, port int) (server.Server, error) {
34-
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", host, port))
34+
addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(host, strconv.Itoa(int(port))))
3535

3636
if err != nil {
3737
return nil, err
@@ -78,7 +78,7 @@ func TestUdpServer(t *testing.T) {
7878
}
7979

8080
func writeToServer(t *testing.T, message, host string, port int) {
81-
servAddr := fmt.Sprintf("%s:%d", host, port)
81+
servAddr := net.JoinHostPort(host, strconv.Itoa(int(port)))
8282
conn, err := net.Dial("udp", servAddr)
8383
if err != nil {
8484
t.Error(err)

x-pack/metricbeat/module/airflow/statsd/data_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"net"
1010
"runtime"
11+
"strconv"
1112
"sync"
1213
"testing"
1314

@@ -43,7 +44,7 @@ func getConfig() map[string]interface{} {
4344
}
4445

4546
func createEvent(data string, t *testing.T) {
46-
udpAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", STATSD_HOST, STATSD_PORT))
47+
udpAddr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(STATSD_HOST, strconv.Itoa(int(STATSD_PORT))))
4748
require.NoError(t, err)
4849

4950
conn, err := net.DialUDP("udp", nil, udpAddr)

0 commit comments

Comments
 (0)