Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: added tests #1800

Merged
merged 3 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions internal/collector/ad/ad_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/adcs/adcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/adfs/adfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/cs/cs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/dfsr/dfsr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/dhcp/dhcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/diskdrive/diskdrive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/exchange/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/fsrmquota/fsrmquota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/hyperv/hyperv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/iis/iis_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
31 changes: 31 additions & 0 deletions internal/collector/mscluster/mscluster_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/msmq/msmq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
4 changes: 4 additions & 0 deletions internal/collector/mssql/mssql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/netframework/netframework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/nps/nps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/remote_fx/remote_fx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/scheduled_task/scheduled_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/smb/smb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/smbclient/smbclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/smtp/smtp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/thermalzone/thermalzone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
7 changes: 5 additions & 2 deletions internal/collector/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion internal/collector/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions internal/collector/vmware/vmware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 11 additions & 3 deletions internal/perfdata/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 31 additions & 4 deletions internal/utils/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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)
}