chore: added tests (#1800)

Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
Jan-Otto Kröpke
2024-12-01 21:50:49 +01:00
committed by GitHub
parent 0ad8e01e0e
commit 109c34f572
34 changed files with 196 additions and 11 deletions

2
go.mod
View File

@@ -3,7 +3,6 @@ module github.com/prometheus-community/windows_exporter
go 1.23
require (
github.com/Microsoft/go-winio v0.6.2
github.com/Microsoft/hcsshim v0.12.9
github.com/alecthomas/kingpin/v2 v2.4.0
github.com/bmatcuk/doublestar/v4 v4.7.1
@@ -20,6 +19,7 @@ require (
)
require (
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/alecthomas/units v0.0.0-20240927000941-0f3dac36c52b // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, ad.Name, ad.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, ad.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, adcs.Name, adcs.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, adcs.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, adfs.Name, adfs.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, adfs.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, cache.Name, cache.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, cache.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, container.Name, container.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, container.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, cs.Name, cs.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, cs.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, dfsr.Name, dfsr.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, dfsr.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, dhcp.Name, dhcp.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, dhcp.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, diskdrive.Name, diskdrive.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, diskdrive.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, dns.Name, dns.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, dns.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, exchange.Name, exchange.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, exchange.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, fsrmquota.Name, fsrmquota.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, fsrmquota.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, hyperv.Name, hyperv.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, hyperv.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, iis.Name, iis.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, iis.New, nil)
}

View File

@@ -0,0 +1,31 @@
// Copyright 2024 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 mscluster_test
import (
"testing"
"github.com/prometheus-community/windows_exporter/internal/collector/mscluster"
"github.com/prometheus-community/windows_exporter/internal/utils/testutils"
)
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, mscluster.Name, mscluster.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, mscluster.New, nil)
}

View File

@@ -26,3 +26,7 @@ func BenchmarkCollector(b *testing.B) {
// No context name required as Collector source is WMI
testutils.FuncBenchmarkCollector(b, msmq.Name, msmq.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, msmq.New, nil)
}

View File

@@ -272,6 +272,10 @@ func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
// Collect sends the metric values for each metric
// to the provided prometheus Metric channel.
func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
if len(c.mssqlInstances) == 0 {
return fmt.Errorf("no SQL instances found: %w", perfdata.ErrNoData)
}
errCh := make(chan error, len(c.collectorFns))
errs := make([]error, 0, len(c.collectorFns))

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, mssql.Name, mssql.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, mssql.New, nil)
}

View File

@@ -26,3 +26,7 @@ func BenchmarkCollector(b *testing.B) {
// No context name required as Collector source is WMI
testutils.FuncBenchmarkCollector(b, netframework.Name, netframework.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, netframework.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, nps.Name, nps.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, nps.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, remote_fx.Name, remote_fx.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, remote_fx.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, scheduled_task.Name, scheduled_task.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, scheduled_task.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, smb.Name, smb.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, smb.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, smbclient.Name, smbclient.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, smbclient.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, smtp.Name, smtp.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, smtp.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, system.Name, system.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, system.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, terminal_services.Name, terminal_services.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, terminal_services.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, thermalzone.Name, thermalzone.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, thermalzone.New, nil)
}

View File

@@ -47,7 +47,10 @@ var ConfigDefaults = Config{
scrapeInterval: 6 * time.Hour,
}
var ErrNoUpdates = errors.New("pending gather update metrics")
var (
ErrNoUpdates = errors.New("pending gather update metrics")
ErrUpdateServiceDisabled = errors.New("windows updates service is disabled")
)
type Collector struct {
config Config
@@ -234,7 +237,7 @@ func (c *Collector) scheduleUpdateStatus(ctx context.Context, logger *slog.Logge
}(hc)
if err != nil {
initErrCh <- fmt.Errorf("windows updates service is disabled: %w", err)
initErrCh <- ErrUpdateServiceDisabled
return
}

View File

@@ -23,5 +23,9 @@ import (
)
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, "printer", update.NewWithFlags)
testutils.FuncBenchmarkCollector(b, update.Name, update.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, update.New, nil)
}

View File

@@ -25,3 +25,7 @@ import (
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, vmware.Name, vmware.NewWithFlags)
}
func TestCollector(t *testing.T) {
testutils.TestCollector(t, vmware.New, nil)
}

View File

@@ -312,9 +312,17 @@ func (c *Collector) Close() {
c.handle = 0
close(c.collectCh)
close(c.counterValuesCh)
close(c.errorCh)
if c.collectCh != nil {
close(c.collectCh)
}
if c.counterValuesCh != nil {
close(c.counterValuesCh)
}
if c.errorCh != nil {
close(c.errorCh)
}
c.counterValuesCh = nil
c.collectCh = nil

View File

@@ -16,17 +16,22 @@
package testutils
import (
"errors"
"io"
"log/slog"
"os"
"sync"
"testing"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus-community/windows_exporter/internal/collector/update"
"github.com/prometheus-community/windows_exporter/internal/mi"
"github.com/prometheus-community/windows_exporter/internal/perfdata"
"github.com/prometheus-community/windows_exporter/pkg/collector"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"golang.org/x/sys/windows"
)
func FuncBenchmarkCollector[C collector.Collector](b *testing.B, name string, collectFunc collector.BuilderWithFlags[C]) {
@@ -86,15 +91,37 @@ func TestCollector[C collector.Collector, V interface{}](t *testing.T, fn func(*
}
}()
require.NoError(t, c.Build(logger, miSession))
err = c.Build(logger, miSession)
switch {
case err == nil:
case errors.Is(err, mi.MI_RESULT_INVALID_NAMESPACE),
errors.Is(err, perfdata.NewPdhError(perfdata.PdhCstatusNoCounter)),
errors.Is(err, perfdata.NewPdhError(perfdata.PdhCstatusNoObject)),
errors.Is(err, update.ErrUpdateServiceDisabled),
errors.Is(err, os.ErrNotExist):
default:
require.NoError(t, err)
}
time.Sleep(1 * time.Second)
require.NoError(t, c.Collect(ch))
err = c.Collect(ch)
switch {
// container collector
case errors.Is(err, windows.Errno(2151088411)),
errors.Is(err, perfdata.ErrPerformanceCounterNotInitialized),
errors.Is(err, perfdata.ErrNoData),
errors.Is(err, mi.MI_RESULT_INVALID_NAMESPACE),
errors.Is(err, mi.MI_RESULT_INVALID_QUERY),
errors.Is(err, update.ErrNoUpdates):
t.Skip("collector not supported on this system")
default:
require.NoError(t, err)
}
close(ch)
wg.Wait()
require.NotEmpty(t, metrics)
}