Files
windows_exporter/internal/collector/textfile/textfile_test_test.go
renovate[bot] 3d0587d28c chore(deps): update dependency golangci/golangci-lint to v2.6.0 (#2246)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Jan-Otto Kröpke <mail@jkroepke.de>
2025-11-02 19:41:27 +01:00

115 lines
2.9 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 textfile_test
import (
"fmt"
"io"
"log/slog"
"strings"
"testing"
"github.com/prometheus-community/windows_exporter/internal/collector/textfile"
"github.com/prometheus-community/windows_exporter/pkg/collector"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
)
//nolint:gochecknoglobals
var baseDir = "../../../tools/textfile-test"
//nolint:paralleltest
func TestMultipleDirectories(t *testing.T) {
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
testDir := baseDir + "/multiple-dirs"
testDirs := fmt.Sprintf("%[1]s/dir1,%[1]s/dir2,%[1]s/dir3", testDir)
textFileCollector := textfile.New(&textfile.Config{
TextFileDirectories: strings.Split(testDirs, ","),
})
collectors := collector.New(map[string]collector.Collector{textfile.Name: textFileCollector})
require.NoError(t, collectors.Build(t.Context(), logger))
metrics := make(chan prometheus.Metric)
got := ""
errCh := make(chan error, 1)
go func() {
errCh <- textFileCollector.Collect(metrics)
close(metrics)
}()
for val := range metrics {
var metric dto.Metric
err := val.Write(&metric)
require.NoError(t, err)
//nolint:modernize,perfsprint
got += metric.String()
}
require.NoError(t, <-errCh)
for _, f := range []string{"dir1", "dir2", "dir3", "dir3sub"} {
require.Contains(t, got, f)
}
}
//nolint:paralleltest
func TestDuplicateFileName(t *testing.T) {
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
testDir := baseDir + "/duplicate-filename"
textFileCollector := textfile.New(&textfile.Config{
TextFileDirectories: []string{testDir},
})
collectors := collector.New(map[string]collector.Collector{textfile.Name: textFileCollector})
require.NoError(t, collectors.Build(t.Context(), logger))
metrics := make(chan prometheus.Metric)
got := ""
errCh := make(chan error, 1)
go func() {
errCh <- textFileCollector.Collect(metrics)
close(metrics)
}()
for val := range metrics {
var metric dto.Metric
err := val.Write(&metric)
require.NoError(t, err)
//nolint:perfsprint
got += metric.String()
}
require.ErrorContains(t, <-errCh, "duplicate filename detected")
require.Contains(t, got, "file")
require.NotContains(t, got, "sub_file")
}