|
| 1 | +// Copyright 2024 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +//go:build !go1.21 |
| 15 | + |
| 16 | +package model |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "testing" |
| 21 | +) |
| 22 | + |
| 23 | +func TestUnmarshalJSONLabelSet(t *testing.T) { |
| 24 | + type testConfig struct { |
| 25 | + LabelSet LabelSet `yaml:"labelSet,omitempty"` |
| 26 | + } |
| 27 | + |
| 28 | + // valid LabelSet JSON |
| 29 | + labelSetJSON := `{ |
| 30 | + "labelSet": { |
| 31 | + "monitor": "codelab", |
| 32 | + "foo": "bar", |
| 33 | + "foo2": "bar", |
| 34 | + "abc": "prometheus", |
| 35 | + "foo11": "bar11" |
| 36 | + } |
| 37 | +}` |
| 38 | + var c testConfig |
| 39 | + err := json.Unmarshal([]byte(labelSetJSON), &c) |
| 40 | + if err != nil { |
| 41 | + t.Errorf("unexpected error while marshalling JSON : %s", err.Error()) |
| 42 | + } |
| 43 | + |
| 44 | + labelSetString := c.LabelSet.String() |
| 45 | + |
| 46 | + expected := `{abc="prometheus", foo="bar", foo11="bar11", foo2="bar", monitor="codelab"}` |
| 47 | + |
| 48 | + if expected != labelSetString { |
| 49 | + t.Errorf("expected %s but got %s", expected, labelSetString) |
| 50 | + } |
| 51 | + |
| 52 | + // invalid LabelSet JSON |
| 53 | + invalidlabelSetJSON := `{ |
| 54 | + "labelSet": { |
| 55 | + "1nvalid_23name": "codelab", |
| 56 | + "foo": "bar" |
| 57 | + } |
| 58 | +}` |
| 59 | + |
| 60 | + NameValidationScheme = LegacyValidation |
| 61 | + err = json.Unmarshal([]byte(invalidlabelSetJSON), &c) |
| 62 | + expectedErr := `"1nvalid_23name" is not a valid label name` |
| 63 | + if err == nil || err.Error() != expectedErr { |
| 64 | + t.Errorf("expected an error with message '%s' to be thrown", expectedErr) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestLabelSetClone(t *testing.T) { |
| 69 | + labelSet := LabelSet{ |
| 70 | + "monitor": "codelab", |
| 71 | + "foo": "bar", |
| 72 | + "bar": "baz", |
| 73 | + } |
| 74 | + |
| 75 | + cloneSet := labelSet.Clone() |
| 76 | + |
| 77 | + if len(labelSet) != len(cloneSet) { |
| 78 | + t.Errorf("expected the length of the cloned Label set to be %d, but got %d", |
| 79 | + len(labelSet), len(cloneSet)) |
| 80 | + } |
| 81 | + |
| 82 | + for ln, lv := range labelSet { |
| 83 | + expected := cloneSet[ln] |
| 84 | + if expected != lv { |
| 85 | + t.Errorf("expected to get LabelValue %s, but got %s for LabelName %s", expected, lv, ln) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestLabelSetMerge(t *testing.T) { |
| 91 | + labelSet := LabelSet{ |
| 92 | + "monitor": "codelab", |
| 93 | + "foo": "bar", |
| 94 | + "bar": "baz", |
| 95 | + } |
| 96 | + |
| 97 | + labelSet2 := LabelSet{ |
| 98 | + "monitor": "codelab", |
| 99 | + "dolor": "mi", |
| 100 | + "lorem": "ipsum", |
| 101 | + } |
| 102 | + |
| 103 | + expectedSet := LabelSet{ |
| 104 | + "monitor": "codelab", |
| 105 | + "foo": "bar", |
| 106 | + "bar": "baz", |
| 107 | + "dolor": "mi", |
| 108 | + "lorem": "ipsum", |
| 109 | + } |
| 110 | + |
| 111 | + mergedSet := labelSet.Merge(labelSet2) |
| 112 | + |
| 113 | + if len(mergedSet) != len(expectedSet) { |
| 114 | + t.Errorf("expected the length of the cloned Label set to be %d, but got %d", |
| 115 | + len(expectedSet), len(mergedSet)) |
| 116 | + } |
| 117 | + |
| 118 | + for ln, lv := range mergedSet { |
| 119 | + expected := expectedSet[ln] |
| 120 | + if expected != lv { |
| 121 | + t.Errorf("expected to get LabelValue %s, but got %s for LabelName %s", expected, lv, ln) |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// Benchmark Results for LabelSet's String() method |
| 127 | +// --------------------------------------------------------------------------------------------------------- |
| 128 | +// goos: linux |
| 129 | +// goarch: amd64 |
| 130 | +// pkg: github.com/prometheus/common/model |
| 131 | +// cpu: 11th Gen Intel(R) Core(TM) i5-1145G7 @ 2.60GHz |
| 132 | +// BenchmarkLabelSetStringMethod-8 732376 1532 ns/op |
| 133 | + |
| 134 | +func BenchmarkLabelSetStringMethod(b *testing.B) { |
| 135 | + ls := make(LabelSet) |
| 136 | + ls["monitor"] = "codelab" |
| 137 | + ls["foo2"] = "bar" |
| 138 | + ls["foo"] = "bar" |
| 139 | + ls["abc"] = "prometheus" |
| 140 | + ls["foo11"] = "bar11" |
| 141 | + for i := 0; i < b.N; i++ { |
| 142 | + _ = ls.String() |
| 143 | + } |
| 144 | +} |
0 commit comments