1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package resource
16
17 import (
18 "context"
19 "errors"
20 "fmt"
21 "reflect"
22 "testing"
23 )
24
25 func TestMerge(t *testing.T) {
26 cases := []struct {
27 a, b, want *Resource
28 }{
29 {
30 a: &Resource{
31 Type: "t1",
32 Labels: map[string]string{"a": "1", "b": "2"},
33 },
34 b: &Resource{
35 Type: "t2",
36 Labels: map[string]string{"a": "1", "b": "3", "c": "4"},
37 },
38 want: &Resource{
39 Type: "t1",
40 Labels: map[string]string{"a": "1", "b": "2", "c": "4"},
41 },
42 },
43 {
44 a: nil,
45 b: &Resource{
46 Type: "t1",
47 Labels: map[string]string{"a": "1"},
48 },
49 want: &Resource{
50 Type: "t1",
51 Labels: map[string]string{"a": "1"},
52 },
53 },
54 {
55 a: &Resource{
56 Type: "t1",
57 Labels: map[string]string{"a": "1"},
58 },
59 b: nil,
60 want: &Resource{
61 Type: "t1",
62 Labels: map[string]string{"a": "1"},
63 },
64 },
65 }
66 for i, c := range cases {
67 t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
68 res := merge(c.a, c.b)
69 if !reflect.DeepEqual(res, c.want) {
70 t.Fatalf("unwanted result: want %+v, got %+v", c.want, res)
71 }
72 })
73 }
74 }
75
76 func TestDecodeLabels(t *testing.T) {
77 cases := []struct {
78 encoded string
79 wantLabels map[string]string
80 wantFail bool
81 }{
82 {
83 encoded: `example.org/test-1="test $ \"" , Abc="Def"`,
84 wantLabels: map[string]string{"example.org/test-1": "test $ \"", "Abc": "Def"},
85 }, {
86 encoded: `single="key"`,
87 wantLabels: map[string]string{"single": "key"},
88 },
89 {encoded: `invalid-char-ü="test"`, wantFail: true},
90 {encoded: `invalid-char="ü-test"`, wantFail: true},
91 {encoded: `missing="trailing-quote`, wantFail: true},
92 {encoded: `missing=leading-quote"`, wantFail: true},
93 {encoded: `extra="chars", a`, wantFail: true},
94 }
95 for i, c := range cases {
96 t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
97 res, err := DecodeLabels(c.encoded)
98 if err != nil && !c.wantFail {
99 t.Fatalf("unwanted error: %s", err)
100 }
101 if c.wantFail && err == nil {
102 t.Fatalf("wanted failure but got none, result: %v", res)
103 }
104 if !reflect.DeepEqual(res, c.wantLabels) {
105 t.Fatalf("wanted result %v, got %v", c.wantLabels, res)
106 }
107 })
108 }
109 }
110
111 func TestEncodeLabels(t *testing.T) {
112 got := EncodeLabels(map[string]string{
113 "example.org/test-1": "test ¥ \"",
114 "un": "quøted",
115 "Abc": "Def",
116 })
117 if want := `Abc="Def",example.org/test-1="test ¥ \"",un="quøted"`; got != want {
118 t.Fatalf("got %q, want %q", got, want)
119 }
120 }
121
122 func TestMultiDetector(t *testing.T) {
123 got, err := MultiDetector(
124 func(context.Context) (*Resource, error) {
125 return &Resource{
126 Type: "t1",
127 Labels: map[string]string{"a": "1", "b": "2"},
128 }, nil
129 },
130 func(context.Context) (*Resource, error) {
131 return &Resource{
132 Type: "t2",
133 Labels: map[string]string{"a": "11", "c": "3"},
134 }, nil
135 },
136 )(context.Background())
137 if err != nil {
138 t.Fatalf("unexpected error: %s", err)
139 }
140 want := &Resource{
141 Type: "t1",
142 Labels: map[string]string{"a": "1", "b": "2", "c": "3"},
143 }
144 if !reflect.DeepEqual(got, want) {
145 t.Fatalf("unexpected resource: want %v, got %v", want, got)
146 }
147
148 wantErr := errors.New("err1")
149 _, err = MultiDetector(
150 func(context.Context) (*Resource, error) {
151 return &Resource{
152 Type: "t1",
153 Labels: map[string]string{"a": "1", "b": "2"},
154 }, nil
155 },
156 func(context.Context) (*Resource, error) {
157 return nil, wantErr
158 },
159 )(context.Background())
160 if err != wantErr {
161 t.Fatalf("unexpected error: want %v, got %v", wantErr, err)
162 }
163 }
164
View as plain text