-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scraper_test.go
156 lines (132 loc) · 4.25 KB
/
scraper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package vcenterreceiver // import github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver
import (
"context"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver/internal/metadata"
mock "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/vcenterreceiver/internal/mockserver"
)
func TestScrape(t *testing.T) {
ctx := context.Background()
mockServer := mock.MockServer(t, false)
defer mockServer.Close()
cfg := &Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
Endpoint: mockServer.URL,
Username: mock.MockUsername,
Password: mock.MockPassword,
}
testScrape(ctx, t, cfg, "expected.yaml")
}
func TestScrapeConfigsEnabled(t *testing.T) {
ctx := context.Background()
mockServer := mock.MockServer(t, false)
defer mockServer.Close()
optConfigs := metadata.DefaultMetricsBuilderConfig()
setResourcePoolMemoryUsageAttrFeatureGate(t, true)
cfg := &Config{
MetricsBuilderConfig: optConfigs,
Endpoint: mockServer.URL,
Username: mock.MockUsername,
Password: mock.MockPassword,
}
testScrape(ctx, t, cfg, "expected-all-enabled.yaml")
}
func TestScrape_TLS(t *testing.T) {
ctx := context.Background()
mockServer := mock.MockServer(t, true)
defer mockServer.Close()
cfg := &Config{
MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(),
Endpoint: mockServer.URL,
Username: mock.MockUsername,
Password: mock.MockPassword,
}
cfg.Insecure = true
cfg.InsecureSkipVerify = true
testScrape(ctx, t, cfg, "expected.yaml")
}
func testScrape(ctx context.Context, t *testing.T, cfg *Config, fileName string) {
scraper := newVmwareVcenterScraper(zap.NewNop(), cfg, receivertest.NewNopSettings())
metrics, err := scraper.scrape(ctx)
require.NoError(t, err)
require.NotEqual(t, 0, metrics.MetricCount())
goldenPath := filepath.Join("testdata", "metrics", fileName)
expectedMetrics, err := golden.ReadMetrics(goldenPath)
require.NoError(t, err)
err = pmetrictest.CompareMetrics(expectedMetrics, metrics,
pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricDataPointsOrder(),
)
require.NoError(t, err)
require.NoError(t, scraper.Shutdown(ctx))
}
func setResourcePoolMemoryUsageAttrFeatureGate(t *testing.T, val bool) {
wasEnabled := enableResourcePoolMemoryUsageAttr.IsEnabled()
err := featuregate.GlobalRegistry().Set(
enableResourcePoolMemoryUsageAttr.ID(),
val,
)
require.NoError(t, err)
t.Cleanup(func() {
err := featuregate.GlobalRegistry().Set(
enableResourcePoolMemoryUsageAttr.ID(),
wasEnabled,
)
require.NoError(t, err)
})
}
func TestScrape_NoClient(t *testing.T) {
ctx := context.Background()
scraper := &vcenterMetricScraper{
client: nil,
config: &Config{
Endpoint: "http://vcsa.localnet",
},
mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopSettings()),
logger: zap.NewNop(),
}
metrics, err := scraper.scrape(ctx)
require.ErrorContains(t, err, "unable to connect to vSphere SDK")
require.Equal(t, 0, metrics.MetricCount())
require.NoError(t, scraper.Shutdown(ctx))
}
func TestStartFailures_Metrics(t *testing.T) {
cases := []struct {
desc string
cfg *Config
err error
}{
{
desc: "bad client connect",
cfg: &Config{
Endpoint: "http://no-host",
},
},
{
desc: "unparsable endpoint",
cfg: &Config{
Endpoint: "<protocol>://some-host",
},
},
}
ctx := context.Background()
for _, tc := range cases {
scraper := newVmwareVcenterScraper(zap.NewNop(), tc.cfg, receivertest.NewNopSettings())
err := scraper.Start(ctx, nil)
if tc.err != nil {
require.ErrorContains(t, err, tc.err.Error())
} else {
require.NoError(t, err)
}
}
}