1// Copyright 2019 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
14package model
15
16import (
17	"encoding/json"
18	"testing"
19)
20
21func TestUnmarshalJSONLabelSet(t *testing.T) {
22	type testConfig struct {
23		LabelSet LabelSet `yaml:"labelSet,omitempty"`
24	}
25
26	// valid LabelSet JSON
27	labelSetJSON := `{
28	"labelSet": {
29		"monitor": "codelab",
30		"foo": "bar"
31	}
32}`
33	var c testConfig
34	err := json.Unmarshal([]byte(labelSetJSON), &c)
35
36	if err != nil {
37		t.Errorf("unexpected error while marshalling JSON : %s", err.Error())
38	}
39
40	labelSetString := c.LabelSet.String()
41
42	expected := `{foo="bar", monitor="codelab"}`
43
44	if expected != labelSetString {
45		t.Errorf("expected %s but got %s", expected, labelSetString)
46	}
47
48	// invalid LabelSet JSON
49	invalidlabelSetJSON := `{
50	"labelSet": {
51		"1nvalid_23name": "codelab",
52		"foo": "bar"
53	}
54}`
55
56	err = json.Unmarshal([]byte(invalidlabelSetJSON), &c)
57	expectedErr := `"1nvalid_23name" is not a valid label name`
58	if err == nil || err.Error() != expectedErr {
59		t.Errorf("expected an error with message '%s' to be thrown", expectedErr)
60	}
61}
62
63func TestLabelSetClone(t *testing.T) {
64	labelSet := LabelSet{
65		"monitor": "codelab",
66		"foo":     "bar",
67		"bar":     "baz",
68	}
69
70	cloneSet := labelSet.Clone()
71
72	if len(labelSet) != len(cloneSet) {
73		t.Errorf("expected the length of the cloned Label set to be %d, but got %d",
74			len(labelSet), len(cloneSet))
75	}
76
77	for ln, lv := range labelSet {
78		expected := cloneSet[ln]
79		if expected != lv {
80			t.Errorf("expected to get LabelValue %s, but got %s for LabelName %s", expected, lv, ln)
81		}
82	}
83}
84
85func TestLabelSetMerge(t *testing.T) {
86	labelSet := LabelSet{
87		"monitor": "codelab",
88		"foo":     "bar",
89		"bar":     "baz",
90	}
91
92	labelSet2 := LabelSet{
93		"monitor": "codelab",
94		"dolor":   "mi",
95		"lorem":   "ipsum",
96	}
97
98	expectedSet := LabelSet{
99		"monitor": "codelab",
100		"foo":     "bar",
101		"bar":     "baz",
102		"dolor":   "mi",
103		"lorem":   "ipsum",
104	}
105
106	mergedSet := labelSet.Merge(labelSet2)
107
108	if len(mergedSet) != len(expectedSet) {
109		t.Errorf("expected the length of the cloned Label set to be %d, but got %d",
110			len(expectedSet), len(mergedSet))
111	}
112
113	for ln, lv := range mergedSet {
114		expected := expectedSet[ln]
115		if expected != lv {
116			t.Errorf("expected to get LabelValue %s, but got %s for LabelName %s", expected, lv, ln)
117		}
118	}
119
120}
121