From 109c34f5728f9d90916ca7d794e9cb5d91013532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Sun, 1 Dec 2024 21:50:49 +0100 Subject: [PATCH] chore: added tests (#1800) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- go.mod | 2 +- internal/collector/ad/ad_test.go | 4 +++ internal/collector/adcs/adcs_test.go | 4 +++ internal/collector/adfs/adfs_test.go | 4 +++ internal/collector/cache/cache_test.go | 4 +++ .../collector/container/container_test.go | 4 +++ internal/collector/cs/cs_test.go | 4 +++ internal/collector/dfsr/dfsr_test.go | 4 +++ internal/collector/dhcp/dhcp_test.go | 4 +++ .../collector/diskdrive/diskdrive_test.go | 4 +++ internal/collector/dns/dns_test.go | 4 +++ internal/collector/exchange/exchange_test.go | 4 +++ .../collector/fsrmquota/fsrmquota_test.go | 4 +++ internal/collector/hyperv/hyperv_test.go | 4 +++ internal/collector/iis/iis_bench_test.go | 4 +++ .../collector/mscluster/mscluster_test.go | 31 ++++++++++++++++ internal/collector/msmq/msmq_test.go | 4 +++ internal/collector/mssql/mssql.go | 4 +++ internal/collector/mssql/mssql_test.go | 4 +++ .../netframework/netframework_test.go | 4 +++ internal/collector/nps/nps_test.go | 4 +++ .../collector/remote_fx/remote_fx_test.go | 4 +++ .../scheduled_task/scheduled_task_test.go | 4 +++ internal/collector/smb/smb_test.go | 4 +++ .../collector/smbclient/smbclient_test.go | 4 +++ internal/collector/smtp/smtp_test.go | 4 +++ internal/collector/system/system_test.go | 4 +++ .../terminal_services_test.go | 4 +++ .../collector/thermalzone/thermalzone_test.go | 4 +++ internal/collector/update/update.go | 7 ++-- internal/collector/update/update_test.go | 6 +++- internal/collector/vmware/vmware_test.go | 4 +++ internal/perfdata/collector.go | 14 ++++++-- internal/utils/testutils/testutils.go | 35 ++++++++++++++++--- 34 files changed, 196 insertions(+), 11 deletions(-) create mode 100644 internal/collector/mscluster/mscluster_test.go diff --git a/go.mod b/go.mod index bc8a35d3..08cd6acd 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/internal/collector/ad/ad_test.go b/internal/collector/ad/ad_test.go index 13398544..f87ae2bf 100644 --- a/internal/collector/ad/ad_test.go +++ b/internal/collector/ad/ad_test.go @@ -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) +} diff --git a/internal/collector/adcs/adcs_test.go b/internal/collector/adcs/adcs_test.go index 296b4545..2f41b491 100644 --- a/internal/collector/adcs/adcs_test.go +++ b/internal/collector/adcs/adcs_test.go @@ -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) +} diff --git a/internal/collector/adfs/adfs_test.go b/internal/collector/adfs/adfs_test.go index 25596f2a..ebcc8fdb 100644 --- a/internal/collector/adfs/adfs_test.go +++ b/internal/collector/adfs/adfs_test.go @@ -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) +} diff --git a/internal/collector/cache/cache_test.go b/internal/collector/cache/cache_test.go index ea098caf..5cc1bd0d 100644 --- a/internal/collector/cache/cache_test.go +++ b/internal/collector/cache/cache_test.go @@ -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) +} diff --git a/internal/collector/container/container_test.go b/internal/collector/container/container_test.go index c691ff4e..3aee05c6 100644 --- a/internal/collector/container/container_test.go +++ b/internal/collector/container/container_test.go @@ -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) +} diff --git a/internal/collector/cs/cs_test.go b/internal/collector/cs/cs_test.go index e8d917e8..431d3e6c 100644 --- a/internal/collector/cs/cs_test.go +++ b/internal/collector/cs/cs_test.go @@ -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) +} diff --git a/internal/collector/dfsr/dfsr_test.go b/internal/collector/dfsr/dfsr_test.go index a3e6c7ce..9717f176 100644 --- a/internal/collector/dfsr/dfsr_test.go +++ b/internal/collector/dfsr/dfsr_test.go @@ -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) +} diff --git a/internal/collector/dhcp/dhcp_test.go b/internal/collector/dhcp/dhcp_test.go index 4145abed..95e77f80 100644 --- a/internal/collector/dhcp/dhcp_test.go +++ b/internal/collector/dhcp/dhcp_test.go @@ -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) +} diff --git a/internal/collector/diskdrive/diskdrive_test.go b/internal/collector/diskdrive/diskdrive_test.go index bcda5a05..b9ee8331 100644 --- a/internal/collector/diskdrive/diskdrive_test.go +++ b/internal/collector/diskdrive/diskdrive_test.go @@ -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) +} diff --git a/internal/collector/dns/dns_test.go b/internal/collector/dns/dns_test.go index 19482b0e..32b0cd27 100644 --- a/internal/collector/dns/dns_test.go +++ b/internal/collector/dns/dns_test.go @@ -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) +} diff --git a/internal/collector/exchange/exchange_test.go b/internal/collector/exchange/exchange_test.go index 95e8bf58..5e80305b 100644 --- a/internal/collector/exchange/exchange_test.go +++ b/internal/collector/exchange/exchange_test.go @@ -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) +} diff --git a/internal/collector/fsrmquota/fsrmquota_test.go b/internal/collector/fsrmquota/fsrmquota_test.go index 0a056d98..bc64c70e 100644 --- a/internal/collector/fsrmquota/fsrmquota_test.go +++ b/internal/collector/fsrmquota/fsrmquota_test.go @@ -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) +} diff --git a/internal/collector/hyperv/hyperv_test.go b/internal/collector/hyperv/hyperv_test.go index c1268226..c30244cd 100644 --- a/internal/collector/hyperv/hyperv_test.go +++ b/internal/collector/hyperv/hyperv_test.go @@ -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) +} diff --git a/internal/collector/iis/iis_bench_test.go b/internal/collector/iis/iis_bench_test.go index c2c5e21a..5929e1e8 100644 --- a/internal/collector/iis/iis_bench_test.go +++ b/internal/collector/iis/iis_bench_test.go @@ -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) +} diff --git a/internal/collector/mscluster/mscluster_test.go b/internal/collector/mscluster/mscluster_test.go new file mode 100644 index 00000000..242a0e28 --- /dev/null +++ b/internal/collector/mscluster/mscluster_test.go @@ -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) +} diff --git a/internal/collector/msmq/msmq_test.go b/internal/collector/msmq/msmq_test.go index d8813275..a183e81d 100644 --- a/internal/collector/msmq/msmq_test.go +++ b/internal/collector/msmq/msmq_test.go @@ -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) +} diff --git a/internal/collector/mssql/mssql.go b/internal/collector/mssql/mssql.go index b5382111..db87764d 100644 --- a/internal/collector/mssql/mssql.go +++ b/internal/collector/mssql/mssql.go @@ -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)) diff --git a/internal/collector/mssql/mssql_test.go b/internal/collector/mssql/mssql_test.go index da503684..b8271986 100644 --- a/internal/collector/mssql/mssql_test.go +++ b/internal/collector/mssql/mssql_test.go @@ -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) +} diff --git a/internal/collector/netframework/netframework_test.go b/internal/collector/netframework/netframework_test.go index c5051b82..498450fc 100644 --- a/internal/collector/netframework/netframework_test.go +++ b/internal/collector/netframework/netframework_test.go @@ -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) +} diff --git a/internal/collector/nps/nps_test.go b/internal/collector/nps/nps_test.go index 07acde67..c5c8de31 100644 --- a/internal/collector/nps/nps_test.go +++ b/internal/collector/nps/nps_test.go @@ -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) +} diff --git a/internal/collector/remote_fx/remote_fx_test.go b/internal/collector/remote_fx/remote_fx_test.go index 9911e223..e88d3dd0 100644 --- a/internal/collector/remote_fx/remote_fx_test.go +++ b/internal/collector/remote_fx/remote_fx_test.go @@ -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) +} diff --git a/internal/collector/scheduled_task/scheduled_task_test.go b/internal/collector/scheduled_task/scheduled_task_test.go index 6f09ab08..e8140e78 100644 --- a/internal/collector/scheduled_task/scheduled_task_test.go +++ b/internal/collector/scheduled_task/scheduled_task_test.go @@ -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) +} diff --git a/internal/collector/smb/smb_test.go b/internal/collector/smb/smb_test.go index dcc2dc54..6232899e 100644 --- a/internal/collector/smb/smb_test.go +++ b/internal/collector/smb/smb_test.go @@ -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) +} diff --git a/internal/collector/smbclient/smbclient_test.go b/internal/collector/smbclient/smbclient_test.go index 5259ccf0..268ac905 100644 --- a/internal/collector/smbclient/smbclient_test.go +++ b/internal/collector/smbclient/smbclient_test.go @@ -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) +} diff --git a/internal/collector/smtp/smtp_test.go b/internal/collector/smtp/smtp_test.go index ed1b7e74..b6878050 100644 --- a/internal/collector/smtp/smtp_test.go +++ b/internal/collector/smtp/smtp_test.go @@ -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) +} diff --git a/internal/collector/system/system_test.go b/internal/collector/system/system_test.go index 4ace7986..444a6bb0 100644 --- a/internal/collector/system/system_test.go +++ b/internal/collector/system/system_test.go @@ -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) +} diff --git a/internal/collector/terminal_services/terminal_services_test.go b/internal/collector/terminal_services/terminal_services_test.go index 6940108c..0904f157 100644 --- a/internal/collector/terminal_services/terminal_services_test.go +++ b/internal/collector/terminal_services/terminal_services_test.go @@ -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) +} diff --git a/internal/collector/thermalzone/thermalzone_test.go b/internal/collector/thermalzone/thermalzone_test.go index 863f6f12..4fa6387c 100644 --- a/internal/collector/thermalzone/thermalzone_test.go +++ b/internal/collector/thermalzone/thermalzone_test.go @@ -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) +} diff --git a/internal/collector/update/update.go b/internal/collector/update/update.go index 0a2d7f09..861d9bf4 100644 --- a/internal/collector/update/update.go +++ b/internal/collector/update/update.go @@ -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 } diff --git a/internal/collector/update/update_test.go b/internal/collector/update/update_test.go index 4ffa2e29..1c730eff 100644 --- a/internal/collector/update/update_test.go +++ b/internal/collector/update/update_test.go @@ -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) } diff --git a/internal/collector/vmware/vmware_test.go b/internal/collector/vmware/vmware_test.go index 2982284d..a5f04f38 100644 --- a/internal/collector/vmware/vmware_test.go +++ b/internal/collector/vmware/vmware_test.go @@ -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) +} diff --git a/internal/perfdata/collector.go b/internal/perfdata/collector.go index f5b3a45e..c949b0e9 100644 --- a/internal/perfdata/collector.go +++ b/internal/perfdata/collector.go @@ -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 diff --git a/internal/utils/testutils/testutils.go b/internal/utils/testutils/testutils.go index e5482d45..90e9e97d 100644 --- a/internal/utils/testutils/testutils.go +++ b/internal/utils/testutils/testutils.go @@ -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) }