mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-10-18 18:49:05 +02:00
Co-authored-by: Claude <noreply@anthropic.com> Signed-off-by: Claude <noreply@anthropic.com>
96 lines
3.5 KiB
Go
96 lines
3.5 KiB
Go
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
// Copyright The Prometheus Authors
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//go:build windows
|
|
|
|
package smbclient
|
|
|
|
import (
|
|
"math"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestParseInstanceName(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
server string
|
|
share string
|
|
ok bool
|
|
}{
|
|
{name: `\\fileserver\data`, server: "fileserver", share: "data", ok: true},
|
|
// Windows 11 publishes the instances with a single leading backslash.
|
|
{name: `\localhost\C$`, server: "localhost", share: "C$", ok: true},
|
|
{name: `\\domain\dfs\folder`},
|
|
{name: `_Total`},
|
|
{name: ``},
|
|
} {
|
|
server, share, ok := parseInstanceName(tc.name)
|
|
|
|
require.Equal(t, tc.ok, ok, tc.name)
|
|
require.Equal(t, tc.server, server, tc.name)
|
|
require.Equal(t, tc.share, share, tc.name)
|
|
}
|
|
}
|
|
|
|
func TestExtendCounters32(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
c := &Collector{counters32: make(map[string]*shareCounters32)}
|
|
|
|
// internal/pdh delivers the timer sums in seconds of a 10 MHz time base.
|
|
frequencies := [numTimers32]float64{1e7, 1e7, 1e7}
|
|
|
|
sample := func(data perfDataCounterValues) perfDataCounterValues {
|
|
c.generation++
|
|
|
|
data.Name = `\\server\share`
|
|
c.extendCounters32(&data, frequencies)
|
|
c.pruneCounters32()
|
|
|
|
return data
|
|
}
|
|
|
|
got := sample(perfDataCounterValues{AvgSecPerRead: (1 << 20) / 1e7, ReadRequestsPerSec: 7, ReadBytesPerSec: 100})
|
|
require.Equal(t, (1<<20)/1e7, got.AvgSecPerRead) //nolint:testifylint
|
|
require.Equal(t, 7.0, got.ReadRequestsPerSec) //nolint:testifylint
|
|
|
|
// A timer sum can wrap from anywhere: 2^32 ticks are about 430 s of
|
|
// summed request time.
|
|
got = sample(perfDataCounterValues{AvgSecPerRead: (1 << 10) / 1e7, ReadRequestsPerSec: 9, ReadBytesPerSec: 200})
|
|
require.Equal(t, (float64(math.MaxUint32)+1+(1<<10))/1e7, got.AvgSecPerRead) //nolint:testifylint
|
|
require.Equal(t, 9.0, got.ReadRequestsPerSec) //nolint:testifylint
|
|
|
|
// Fractions of a second survive the round trip through ticks.
|
|
got = sample(perfDataCounterValues{AvgSecPerRead: 0.1234567, ReadRequestsPerSec: 10, ReadBytesPerSec: 300})
|
|
require.Equal(t, (float64(math.MaxUint32)+1+1234567)/1e7, got.AvgSecPerRead) //nolint:testifylint
|
|
|
|
// A drop of a 64-bit byte counter shows a reconnect, even if DataBytes grew.
|
|
got = sample(perfDataCounterValues{AvgSecPerRead: 5 / 1e7, ReadRequestsPerSec: 1, ReadBytesPerSec: 50, DataBytesPerSec: 1000})
|
|
require.Equal(t, 5/1e7, got.AvgSecPerRead) //nolint:testifylint
|
|
require.Equal(t, 1.0, got.ReadRequestsPerSec) //nolint:testifylint
|
|
|
|
// A metadata-only share has no byte counter to show the reconnect; a
|
|
// request count dropping from below 2^31 is taken as one.
|
|
got = sample(perfDataCounterValues{MetadataRequestsPerSec: 50, ReadBytesPerSec: 50, DataBytesPerSec: 1000})
|
|
require.Equal(t, 50.0, got.MetadataRequestsPerSec) //nolint:testifylint
|
|
|
|
got = sample(perfDataCounterValues{MetadataRequestsPerSec: 3, ReadBytesPerSec: 50, DataBytesPerSec: 1000})
|
|
require.Equal(t, 3.0, got.MetadataRequestsPerSec) //nolint:testifylint
|
|
}
|