-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scraper_test.go
173 lines (133 loc) · 5.49 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build windows
package activedirectorydsreceiver
import (
"context"
"errors"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/receiver/receivertest"
"go.opentelemetry.io/collector/scraper/scrapererror"
"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/pkg/winperfcounters"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/activedirectorydsreceiver/internal/metadata"
)
var (
goldenScrapePath = filepath.Join("testdata", "golden_scrape.yaml")
partialScrapePath = filepath.Join("testdata", "partial_scrape.yaml")
)
func TestScrape(t *testing.T) {
t.Run("Fully successful scrape", func(t *testing.T) {
t.Parallel()
mockWatchers, err := getWatchers(&mockCounterCreater{
availableCounterNames: getAvailableCounters(t),
})
require.NoError(t, err)
scraper := &activeDirectoryDSScraper{
mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopSettings()),
w: mockWatchers,
}
scrapeData, err := scraper.scrape(context.Background())
require.NoError(t, err)
expectedMetrics, err := golden.ReadMetrics(goldenScrapePath)
require.NoError(t, err)
require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, scrapeData, pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(), pmetrictest.IgnoreMetricDataPointsOrder()))
err = scraper.shutdown(context.Background())
require.NoError(t, err)
})
t.Run("Scrape with errors", func(t *testing.T) {
t.Parallel()
fullSyncObjectsRemainingErr := errors.New("failed to scrape sync objects remaining")
draInboundValuesDNErr := errors.New("failed to scrape sync inbound value DNs")
mockWatchers, err := getWatchers(&mockCounterCreater{
availableCounterNames: getAvailableCounters(t),
})
require.NoError(t, err)
mockWatchers.counterNameToWatcher[draInboundFullSyncObjectsRemaining].(*mockPerfCounterWatcher).scrapeErr = fullSyncObjectsRemainingErr
mockWatchers.counterNameToWatcher[draInboundValuesDNs].(*mockPerfCounterWatcher).scrapeErr = draInboundValuesDNErr
scraper := &activeDirectoryDSScraper{
mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopSettings()),
w: mockWatchers,
}
scrapeData, err := scraper.scrape(context.Background())
require.Error(t, err)
require.True(t, scrapererror.IsPartialScrapeError(err))
require.ErrorContains(t, err, fullSyncObjectsRemainingErr.Error())
require.ErrorContains(t, err, draInboundValuesDNErr.Error())
expectedMetrics, err := golden.ReadMetrics(partialScrapePath)
require.NoError(t, err)
require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, scrapeData, pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(), pmetrictest.IgnoreMetricDataPointsOrder()))
err = scraper.shutdown(context.Background())
require.NoError(t, err)
})
t.Run("Close with errors", func(t *testing.T) {
t.Parallel()
fullSyncObjectsRemainingErr := errors.New("failed to close sync objects remaining")
draInboundValuesDNErr := errors.New("failed to close sync inbound value DNs")
mockWatchers, err := getWatchers(&mockCounterCreater{
availableCounterNames: getAvailableCounters(t),
})
require.NoError(t, err)
mockWatchers.counterNameToWatcher[draInboundFullSyncObjectsRemaining].(*mockPerfCounterWatcher).closeErr = fullSyncObjectsRemainingErr
mockWatchers.counterNameToWatcher[draInboundValuesDNs].(*mockPerfCounterWatcher).closeErr = draInboundValuesDNErr
scraper := &activeDirectoryDSScraper{
mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopSettings()),
w: mockWatchers,
}
err = scraper.shutdown(context.Background())
require.ErrorContains(t, err, fullSyncObjectsRemainingErr.Error())
require.ErrorContains(t, err, draInboundValuesDNErr.Error())
})
t.Run("Double shutdown does not error", func(t *testing.T) {
t.Parallel()
mockWatchers, err := getWatchers(&mockCounterCreater{
availableCounterNames: getAvailableCounters(t),
})
require.NoError(t, err)
scraper := &activeDirectoryDSScraper{
mb: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopSettings()),
w: mockWatchers,
}
err = scraper.shutdown(context.Background())
require.NoError(t, err)
err = scraper.shutdown(context.Background())
require.NoError(t, err)
})
}
type mockPerfCounterWatcher struct {
val float64
scrapeErr error
closeErr error
closed bool
}
// Reset panics; it should not be called
func (mockPerfCounterWatcher) Reset() error {
panic("mockPerfCounterWatcher::Reset is not implemented")
}
// Path panics; It should not be called
func (mockPerfCounterWatcher) Path() string {
panic("mockPerfCounterWatcher::Path is not implemented")
}
// ScrapeData returns scrapeErr if it's set, otherwise it returns a single countervalue with the mock's val
func (w mockPerfCounterWatcher) ScrapeData() ([]winperfcounters.CounterValue, error) {
if w.scrapeErr != nil {
return nil, w.scrapeErr
}
return []winperfcounters.CounterValue{
{
Value: w.val,
},
}, nil
}
// Close all counters/handles related to the query and free all associated memory.
func (w mockPerfCounterWatcher) Close() error {
if w.closed {
panic("mockPerfCounterWatcher was already closed!")
}
return w.closeErr
}