2024-11-24 13:11:14 +00:00
|
|
|
// 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.
|
|
|
|
|
2024-11-17 20:51:12 +00:00
|
|
|
//go:build windows
|
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
package textfile
|
2018-05-15 18:52:11 +00:00
|
|
|
|
|
|
|
import (
|
2023-07-29 18:03:51 +00:00
|
|
|
"io"
|
2018-11-29 16:31:48 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2021-04-18 03:04:39 +00:00
|
|
|
|
2023-04-22 10:17:51 +00:00
|
|
|
"github.com/dimchansky/utfbom"
|
2021-04-18 03:04:39 +00:00
|
|
|
dto "github.com/prometheus/client_model/go"
|
2018-05-15 18:52:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCRFilter(t *testing.T) {
|
2024-08-10 20:05:33 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-05-15 18:52:11 +00:00
|
|
|
sr := strings.NewReader("line 1\r\nline 2")
|
2018-11-29 16:31:48 +00:00
|
|
|
cr := carriageReturnFilteringReader{r: sr}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2023-07-29 18:03:51 +00:00
|
|
|
b, err := io.ReadAll(cr)
|
2018-05-15 18:52:11 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(b) != "line 1\nline 2" {
|
|
|
|
t.Errorf("Unexpected output %q", b)
|
|
|
|
}
|
2018-11-29 16:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckBOM(t *testing.T) {
|
2024-08-10 20:05:33 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-11-29 16:31:48 +00:00
|
|
|
testdata := []struct {
|
|
|
|
encoding utfbom.Encoding
|
|
|
|
err string
|
|
|
|
}{
|
|
|
|
{utfbom.Unknown, ""},
|
|
|
|
{utfbom.UTF8, ""},
|
|
|
|
{utfbom.UTF16BigEndian, "UTF16BigEndian"},
|
|
|
|
{utfbom.UTF16LittleEndian, "UTF16LittleEndian"},
|
|
|
|
{utfbom.UTF32BigEndian, "UTF32BigEndian"},
|
|
|
|
{utfbom.UTF32LittleEndian, "UTF32LittleEndian"},
|
|
|
|
}
|
|
|
|
for _, d := range testdata {
|
|
|
|
err := checkBOM(d.encoding)
|
|
|
|
if d.err == "" && err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2018-11-29 16:31:48 +00:00
|
|
|
if d.err != "" && err == nil {
|
|
|
|
t.Errorf("Missing expected error %s", d.err)
|
|
|
|
}
|
2024-09-10 22:34:10 +00:00
|
|
|
|
2018-11-29 16:31:48 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), d.err) {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-18 03:04:39 +00:00
|
|
|
|
|
|
|
func TestDuplicateMetricEntry(t *testing.T) {
|
2024-08-10 20:05:33 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-04-18 03:04:39 +00:00
|
|
|
metric_name := "windows_sometest"
|
|
|
|
metric_help := "This is a Test."
|
|
|
|
metric_type := dto.MetricType_GAUGE
|
|
|
|
|
|
|
|
gauge_value := 1.0
|
|
|
|
|
|
|
|
gauge := dto.Gauge{
|
|
|
|
Value: &gauge_value,
|
|
|
|
}
|
|
|
|
|
|
|
|
label1_name := "display_name"
|
|
|
|
label1_value := "foobar"
|
|
|
|
|
|
|
|
label1 := dto.LabelPair{
|
|
|
|
Name: &label1_name,
|
|
|
|
Value: &label1_value,
|
|
|
|
}
|
|
|
|
|
|
|
|
label2_name := "display_version"
|
|
|
|
label2_value := "13.4.0"
|
|
|
|
|
|
|
|
label2 := dto.LabelPair{
|
|
|
|
Name: &label2_name,
|
|
|
|
Value: &label2_value,
|
|
|
|
}
|
|
|
|
|
|
|
|
metric1 := dto.Metric{
|
|
|
|
Label: []*dto.LabelPair{&label1, &label2},
|
|
|
|
Gauge: &gauge,
|
|
|
|
}
|
|
|
|
|
|
|
|
metric2 := dto.Metric{
|
|
|
|
Label: []*dto.LabelPair{&label1, &label2},
|
|
|
|
Gauge: &gauge,
|
|
|
|
}
|
|
|
|
|
|
|
|
duplicate := dto.MetricFamily{
|
|
|
|
Name: &metric_name,
|
|
|
|
Help: &metric_help,
|
|
|
|
Type: &metric_type,
|
|
|
|
Metric: []*dto.Metric{&metric1, &metric2},
|
|
|
|
}
|
|
|
|
|
2024-08-29 22:26:15 +00:00
|
|
|
var duplicateFamily []*dto.MetricFamily
|
2021-05-16 02:29:09 +00:00
|
|
|
duplicateFamily = append(duplicateFamily, &duplicate)
|
2021-04-18 03:04:39 +00:00
|
|
|
|
|
|
|
// Ensure detection for duplicate metrics
|
|
|
|
if !duplicateMetricEntry(duplicateFamily) {
|
|
|
|
t.Errorf("Duplicate not found in duplicateFamily")
|
|
|
|
}
|
|
|
|
|
|
|
|
label3_name := "test"
|
|
|
|
label3_value := "1.0"
|
|
|
|
|
|
|
|
label3 := dto.LabelPair{
|
|
|
|
Name: &label3_name,
|
|
|
|
Value: &label3_value,
|
|
|
|
}
|
|
|
|
metric3 := dto.Metric{
|
|
|
|
Label: []*dto.LabelPair{&label1, &label2, &label3},
|
|
|
|
Gauge: &gauge,
|
|
|
|
}
|
|
|
|
|
|
|
|
differentLabels := dto.MetricFamily{
|
|
|
|
Name: &metric_name,
|
|
|
|
Help: &metric_help,
|
|
|
|
Type: &metric_type,
|
|
|
|
Metric: []*dto.Metric{&metric1, &metric3},
|
|
|
|
}
|
2021-05-16 02:29:09 +00:00
|
|
|
|
|
|
|
duplicateFamily = []*dto.MetricFamily{}
|
|
|
|
duplicateFamily = append(duplicateFamily, &differentLabels)
|
2021-04-18 03:04:39 +00:00
|
|
|
|
|
|
|
// Additional label on second metric should not be cause for duplicate detection
|
|
|
|
if duplicateMetricEntry(duplicateFamily) {
|
|
|
|
t.Errorf("Unexpected duplicate found in differentLabels")
|
|
|
|
}
|
|
|
|
|
|
|
|
label4_value := "2.0"
|
|
|
|
|
|
|
|
label4 := dto.LabelPair{
|
|
|
|
Name: &label3_name,
|
|
|
|
Value: &label4_value,
|
|
|
|
}
|
|
|
|
metric4 := dto.Metric{
|
|
|
|
Label: []*dto.LabelPair{&label1, &label2, &label4},
|
|
|
|
Gauge: &gauge,
|
|
|
|
}
|
|
|
|
|
|
|
|
differentValues := dto.MetricFamily{
|
|
|
|
Name: &metric_name,
|
|
|
|
Help: &metric_help,
|
|
|
|
Type: &metric_type,
|
|
|
|
Metric: []*dto.Metric{&metric3, &metric4},
|
|
|
|
}
|
2021-05-16 02:29:09 +00:00
|
|
|
duplicateFamily = []*dto.MetricFamily{}
|
|
|
|
duplicateFamily = append(duplicateFamily, &differentValues)
|
2021-04-18 03:04:39 +00:00
|
|
|
|
|
|
|
// Additional label with different values metric should not be cause for duplicate detection
|
|
|
|
if duplicateMetricEntry(duplicateFamily) {
|
|
|
|
t.Errorf("Unexpected duplicate found in differentValues")
|
|
|
|
}
|
|
|
|
}
|