forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper_test.go
137 lines (115 loc) · 3.77 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
// 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/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()
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.NewNopCreateSettings())
metrics, err := scraper.scrape(ctx)
require.NoError(t, err)
require.NotEqual(t, metrics.MetricCount(), 0)
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 TestScrape_NoClient(t *testing.T) {
ctx := context.Background()
scraper := &vcenterMetricScraper{
client: nil,
config: &Config{
Endpoint: "http://vcsa.localnet",
},
mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopCreateSettings()),
logger: zap.NewNop(),
}
metrics, err := scraper.scrape(ctx)
require.ErrorContains(t, err, "unable to connect to vSphere SDK")
require.Equal(t, metrics.MetricCount(), 0)
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.NewNopCreateSettings())
err := scraper.Start(ctx, nil)
if tc.err != nil {
require.ErrorContains(t, err, tc.err.Error())
} else {
require.NoError(t, err)
}
}
}