Skip to content

Commit bfb9ff2

Browse files
authored
test(testutil): Improve performance and code quality (#17022)
1 parent fecfefd commit bfb9ff2

File tree

2 files changed

+29
-41
lines changed

2 files changed

+29
-41
lines changed

testutil/testutil.go

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
"github.com/influxdata/telegraf/plugins/serializers/influx"
1717
)
1818

19-
var localhost = "localhost"
20-
2119
const (
2220
DefaultDelta = 0.001
2321
DefaultEpsilon = 0.1
@@ -26,21 +24,22 @@ const (
2624
// GetLocalHost returns the DOCKER_HOST environment variable, parsing
2725
// out any scheme or ports so that only the IP address is returned.
2826
func GetLocalHost() string {
29-
if dockerHostVar := os.Getenv("DOCKER_HOST"); dockerHostVar != "" {
30-
u, err := url.Parse(dockerHostVar)
31-
if err != nil {
32-
return dockerHostVar
33-
}
34-
35-
// split out the ip addr from the port
36-
host, _, err := net.SplitHostPort(u.Host)
37-
if err != nil {
38-
return dockerHostVar
39-
}
40-
41-
return host
27+
dockerHostVar := os.Getenv("DOCKER_HOST")
28+
if dockerHostVar == "" {
29+
return "localhost"
30+
}
31+
32+
u, err := url.Parse(dockerHostVar)
33+
if err != nil {
34+
return dockerHostVar
35+
}
36+
37+
host, _, err := net.SplitHostPort(u.Host)
38+
if err != nil {
39+
return dockerHostVar
4240
}
43-
return localhost
41+
42+
return host
4443
}
4544

4645
// GetRandomString returns a random alphanumerical string of the given length.
@@ -49,32 +48,23 @@ func GetLocalHost() string {
4948
// host which might be drained e.g. in CI pipelines. This is useful to e.g.
5049
// create random passwords for tests where security is not a concern.
5150
func GetRandomString(chars int) string {
52-
charset := []byte("abcdefghijklmnopqrstABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
53-
54-
nchars := len(charset)
51+
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
5552
buffer := make([]byte, chars)
56-
for i := range chars {
53+
for i := range buffer {
5754
//nolint:gosec // Using a weak random number generator on purpose to not drain entropy
58-
buffer[i] = charset[rand.Intn(nchars)]
55+
buffer[i] = charset[rand.Intn(len(charset))]
5956
}
60-
6157
return string(buffer)
6258
}
6359

6460
// MockMetrics returns a mock []telegraf.Metric object for using in unit tests
6561
// of telegraf output sinks.
6662
func MockMetrics() []telegraf.Metric {
67-
metrics := make([]telegraf.Metric, 0)
68-
// Create a new point batch
69-
metrics = append(metrics, TestMetric(1.0))
70-
return metrics
63+
return []telegraf.Metric{TestMetric(1.0)}
7164
}
7265

7366
func MockMetricsWithValue(value float64) []telegraf.Metric {
74-
metrics := make([]telegraf.Metric, 0)
75-
// Create a new point batch
76-
metrics = append(metrics, TestMetric(value))
77-
return metrics
67+
return []telegraf.Metric{TestMetric(value)}
7868
}
7969

8070
// TestMetric Returns a simple test point:
@@ -91,20 +81,21 @@ func TestMetric(value interface{}, name ...string) telegraf.Metric {
9181
if len(name) > 0 {
9282
measurement = name[0]
9383
}
94-
tags := map[string]string{"tag1": "value1"}
95-
pt := metric.New(
84+
85+
return metric.New(
9686
measurement,
97-
tags,
87+
map[string]string{"tag1": "value1"},
9888
map[string]interface{}{"value": value},
9989
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
10090
)
101-
return pt
10291
}
10392

10493
// OnlyTags returns an option for keeping only "Tags" for a given Metric
10594
func OnlyTags() cmp.Option {
106-
f := func(p cmp.Path) bool { return p.String() != "Tags" && p.String() != "" }
107-
return cmp.FilterPath(f, cmp.Ignore())
95+
return cmp.FilterPath(func(p cmp.Path) bool {
96+
path := p.String()
97+
return path != "Tags" && path != ""
98+
}, cmp.Ignore())
10899
}
109100

110101
func PrintMetrics(m []telegraf.Metric) {
@@ -130,8 +121,5 @@ func DefaultSampleConfig(sampleConfig string) []byte {
130121
}
131122

132123
func WithinDefaultDelta(dt float64) bool {
133-
if dt < -DefaultDelta || dt > DefaultDelta {
134-
return false
135-
}
136-
return true
124+
return dt >= -DefaultDelta && dt <= DefaultDelta
137125
}

testutil/testutil_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestDockerHost(t *testing.T) {
1414

1515
host := GetLocalHost()
1616

17-
if host != localhost {
17+
if host != "localhost" {
1818
t.Fatalf("Host should be localhost when DOCKER_HOST is not set. Current value [%s]", host)
1919
}
2020
})

0 commit comments

Comments
 (0)