Files
windows_exporter/internal/collector/printer/printer_test.go
T

56 lines
1.8 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 printer_test
import (
"log/slog"
"testing"
"time"
"github.com/prometheus-community/windows_exporter/internal/collector/printer"
"github.com/prometheus-community/windows_exporter/internal/utils/testutils"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func BenchmarkCollector(b *testing.B) {
testutils.FuncBenchmarkCollector(b, "printer", printer.NewWithFlags)
}
func TestCollector(t *testing.T) {
metrics := testutils.TestCollector(t, printer.New, nil)
testutils.RequireFixtureMetric(t, metrics, printer.Name, "windows_printer_job_count", prometheus.Labels{"printer": "CIPrinter"})
}
// TestCollectorWithoutMISession checks that the collector does not depend on WMI.
// Win32_Printer can block for close to a minute on an offline IPP printer.
func TestCollectorWithoutMISession(t *testing.T) {
t.Parallel()
c := printer.New(nil)
t.Cleanup(func() { assert.NoError(t, c.Close()) })
require.NoError(t, c.Build(slog.New(slog.DiscardHandler), nil))
ch := make(chan prometheus.Metric, 1000)
require.NoError(t, testutils.Collect(c, ch, time.Second))
}