forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorders_test.go
85 lines (68 loc) · 2.48 KB
/
recorders_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go:build windows
package sqlserverreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver"
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// TestPerfCounterRecorders requires each perf counter record created to exist in the available counters.
func TestPerfCounterRecorders(t *testing.T) {
t.Run("default", func(t *testing.T) {
expectedCounters := getAvailableCounters(t)
for _, counter := range perfCounterRecorders {
for counterPath := range counter.recorders {
counterFullName := fmt.Sprintf("%s:%s %s", defaultObjectName, counter.object, counterPath)
_, ok := expectedCounters[counterFullName]
require.True(t, ok, fmt.Sprintf("counter %s not found", counterFullName))
}
}
})
t.Run("named", func(t *testing.T) {
expectedCounters := getAvailableNamedInstanceCounters(t)
for _, counter := range perfCounterRecorders {
for counterPath := range counter.recorders {
counterFullName := fmt.Sprintf("MSSQL$TEST_NAME:%s %s", counter.object, counterPath)
_, ok := expectedCounters[counterFullName]
require.True(t, ok, fmt.Sprintf("counter %s not found", counterFullName))
}
}
})
}
// getAvailableCounters populates a map containing all available counters.
func getAvailableCounters(t *testing.T) map[string]bool {
file, err := os.Open(filepath.Join("testdata", "counters.txt"))
require.NoError(t, err)
defer file.Close()
scanner := bufio.NewScanner(file)
counterNames := map[string]bool{}
for scanner.Scan() {
if scanner.Text() != "" {
nameNoBackslash := strings.ReplaceAll(scanner.Text(), "\\", " ")
nameNoInstance := strings.TrimSpace(strings.ReplaceAll(nameNoBackslash, "(*)", ""))
counterNames[nameNoInstance] = true
}
}
return counterNames
}
// getAvailableCounters populates a map containing all available counters.
func getAvailableNamedInstanceCounters(t *testing.T) map[string]bool {
file, err := os.Open(filepath.Join("testdata", "named-instance-counters.txt"))
require.NoError(t, err)
defer file.Close()
scanner := bufio.NewScanner(file)
counterNames := map[string]bool{}
for scanner.Scan() {
if scanner.Text() != "" {
nameNoBackslash := strings.ReplaceAll(scanner.Text(), "\\", " ")
nameNoInstance := strings.TrimSpace(strings.ReplaceAll(nameNoBackslash, "(*)", ""))
counterNames[nameNoInstance] = true
}
}
return counterNames
}