forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counters_test.go
68 lines (51 loc) · 1.49 KB
/
counters_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build windows
package activedirectorydsreceiver
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/winperfcounters"
)
func TestGetWatchers(t *testing.T) {
c := &mockCounterCreater{
availableCounterNames: getAvailableCounters(t),
}
watchers, err := getWatchers(c)
require.NoError(t, err)
require.NotNil(t, watchers)
}
func getAvailableCounters(t *testing.T) []string {
prefix := fmt.Sprintf(`\%s(*)\`, object)
f, err := os.ReadFile(filepath.Join("testdata", "counters.txt"))
require.NoError(t, err)
lines := regexp.MustCompile("\r?\n").Split(string(f), -1)
linesOut := make([]string, 0, len(lines))
for _, line := range lines {
if line != "" {
linesOut = append(linesOut, strings.TrimPrefix(line, prefix))
}
}
return linesOut
}
type mockCounterCreater struct {
created int
availableCounterNames []string
}
func (m *mockCounterCreater) Create(counterName string) (winperfcounters.PerfCounterWatcher, error) {
for _, availableCounter := range m.availableCounterNames {
if counterName == availableCounter {
watcher := &mockPerfCounterWatcher{
val: float64(m.created),
}
m.created++
return watcher, nil
}
}
return nil, fmt.Errorf("counter %s is not available\navailable counters:\n\t%s", counterName, strings.Join(m.availableCounterNames, "\n\t"))
}