resource_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2018, OpenCensus Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package resource
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "reflect"
  20. "testing"
  21. )
  22. func TestMerge(t *testing.T) {
  23. cases := []struct {
  24. a, b, want *Resource
  25. }{
  26. {
  27. a: &Resource{
  28. Type: "t1",
  29. Labels: map[string]string{"a": "1", "b": "2"},
  30. },
  31. b: &Resource{
  32. Type: "t2",
  33. Labels: map[string]string{"a": "1", "b": "3", "c": "4"},
  34. },
  35. want: &Resource{
  36. Type: "t1",
  37. Labels: map[string]string{"a": "1", "b": "2", "c": "4"},
  38. },
  39. },
  40. {
  41. a: nil,
  42. b: &Resource{
  43. Type: "t1",
  44. Labels: map[string]string{"a": "1"},
  45. },
  46. want: &Resource{
  47. Type: "t1",
  48. Labels: map[string]string{"a": "1"},
  49. },
  50. },
  51. {
  52. a: &Resource{
  53. Type: "t1",
  54. Labels: map[string]string{"a": "1"},
  55. },
  56. b: nil,
  57. want: &Resource{
  58. Type: "t1",
  59. Labels: map[string]string{"a": "1"},
  60. },
  61. },
  62. }
  63. for i, c := range cases {
  64. t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
  65. res := merge(c.a, c.b)
  66. if !reflect.DeepEqual(res, c.want) {
  67. t.Fatalf("unwanted result: want %+v, got %+v", c.want, res)
  68. }
  69. })
  70. }
  71. }
  72. func TestDecodeLabels(t *testing.T) {
  73. cases := []struct {
  74. encoded string
  75. wantLabels map[string]string
  76. wantFail bool
  77. }{
  78. {
  79. encoded: `example.org/test-1="test $ \"" , Abc="Def"`,
  80. wantLabels: map[string]string{"example.org/test-1": "test $ \"", "Abc": "Def"},
  81. }, {
  82. encoded: `single="key"`,
  83. wantLabels: map[string]string{"single": "key"},
  84. },
  85. {encoded: `invalid-char-ü="test"`, wantFail: true},
  86. {encoded: `invalid-char="ü-test"`, wantFail: true},
  87. {encoded: `missing="trailing-quote`, wantFail: true},
  88. {encoded: `missing=leading-quote"`, wantFail: true},
  89. {encoded: `extra="chars", a`, wantFail: true},
  90. }
  91. for i, c := range cases {
  92. t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
  93. res, err := DecodeLabels(c.encoded)
  94. if err != nil && !c.wantFail {
  95. t.Fatalf("unwanted error: %s", err)
  96. }
  97. if c.wantFail && err == nil {
  98. t.Fatalf("wanted failure but got none, result: %v", res)
  99. }
  100. if !reflect.DeepEqual(res, c.wantLabels) {
  101. t.Fatalf("wanted result %v, got %v", c.wantLabels, res)
  102. }
  103. })
  104. }
  105. }
  106. func TestEncodeLabels(t *testing.T) {
  107. got := EncodeLabels(map[string]string{
  108. "example.org/test-1": "test ¥ \"",
  109. "un": "quøted",
  110. "Abc": "Def",
  111. })
  112. if want := `Abc="Def",example.org/test-1="test ¥ \"",un="quøted"`; got != want {
  113. t.Fatalf("got %q, want %q", got, want)
  114. }
  115. }
  116. func TestMultiDetector(t *testing.T) {
  117. got, err := MultiDetector(
  118. func(context.Context) (*Resource, error) {
  119. return &Resource{
  120. Type: "t1",
  121. Labels: map[string]string{"a": "1", "b": "2"},
  122. }, nil
  123. },
  124. func(context.Context) (*Resource, error) {
  125. return &Resource{
  126. Type: "t2",
  127. Labels: map[string]string{"a": "11", "c": "3"},
  128. }, nil
  129. },
  130. )(context.Background())
  131. if err != nil {
  132. t.Fatalf("unexpected error: %s", err)
  133. }
  134. want := &Resource{
  135. Type: "t1",
  136. Labels: map[string]string{"a": "1", "b": "2", "c": "3"},
  137. }
  138. if !reflect.DeepEqual(got, want) {
  139. t.Fatalf("unexpected resource: want %v, got %v", want, got)
  140. }
  141. wantErr := errors.New("err1")
  142. _, err = MultiDetector(
  143. func(context.Context) (*Resource, error) {
  144. return &Resource{
  145. Type: "t1",
  146. Labels: map[string]string{"a": "1", "b": "2"},
  147. }, nil
  148. },
  149. func(context.Context) (*Resource, error) {
  150. return nil, wantErr
  151. },
  152. )(context.Background())
  153. if err != wantErr {
  154. t.Fatalf("unexpected error: want %v, got %v", wantErr, err)
  155. }
  156. }