1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-05-20 09:29:35 +02:00
Files
opentelemetry-go/sdk/resource/resource_test.go
T

226 lines
5.0 KiB
Go
Raw Normal View History

// Copyright The OpenTelemetry Authors
2020-03-09 09:30:42 -07:00
//
// 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.
package resource_test
import (
2020-04-22 14:32:58 -07:00
"encoding/json"
2020-03-09 09:30:42 -07:00
"fmt"
"testing"
2020-05-14 07:06:03 +08:00
"go.opentelemetry.io/otel/api/kv/value"
2020-03-09 09:30:42 -07:00
"github.com/google/go-cmp/cmp"
2020-04-22 14:32:58 -07:00
"github.com/stretchr/testify/require"
2020-03-09 09:30:42 -07:00
2020-05-14 07:06:03 +08:00
"go.opentelemetry.io/otel/api/kv"
2020-03-09 09:30:42 -07:00
"go.opentelemetry.io/otel/sdk/resource"
)
var (
2020-05-14 07:06:03 +08:00
kv11 = kv.String("k1", "v11")
kv12 = kv.String("k1", "v12")
kv21 = kv.String("k2", "v21")
kv31 = kv.String("k3", "v31")
kv41 = kv.String("k4", "v41")
2020-03-09 09:30:42 -07:00
)
func TestNew(t *testing.T) {
cases := []struct {
name string
2020-05-14 07:06:03 +08:00
in []kv.KeyValue
want []kv.KeyValue
2020-03-09 09:30:42 -07:00
}{
{
2020-05-13 16:21:23 -07:00
name: "Key with common key order1",
2020-05-14 07:06:03 +08:00
in: []kv.KeyValue{kv12, kv11, kv21},
want: []kv.KeyValue{kv11, kv21},
2020-03-09 09:30:42 -07:00
},
{
2020-05-13 16:21:23 -07:00
name: "Key with common key order2",
2020-05-14 07:06:03 +08:00
in: []kv.KeyValue{kv11, kv12, kv21},
want: []kv.KeyValue{kv12, kv21},
2020-03-09 09:30:42 -07:00
},
{
2020-05-13 16:21:23 -07:00
name: "Key with nil",
2020-03-09 09:30:42 -07:00
in: nil,
2020-04-07 12:15:36 -07:00
want: nil,
2020-03-09 09:30:42 -07:00
},
}
for _, c := range cases {
t.Run(fmt.Sprintf("case-%s", c.name), func(t *testing.T) {
res := resource.New(c.in...)
if diff := cmp.Diff(
2020-04-07 12:15:36 -07:00
res.Attributes(),
c.want,
2020-05-14 07:06:03 +08:00
cmp.AllowUnexported(value.Value{})); diff != "" {
2020-03-09 09:30:42 -07:00
t.Fatalf("unwanted result: diff %+v,", diff)
}
})
}
}
func TestMerge(t *testing.T) {
cases := []struct {
name string
a, b *resource.Resource
2020-05-14 07:06:03 +08:00
want []kv.KeyValue
2020-03-09 09:30:42 -07:00
}{
{
name: "Merge with no overlap, no nil",
a: resource.New(kv11, kv31),
b: resource.New(kv21, kv41),
2020-05-14 07:06:03 +08:00
want: []kv.KeyValue{kv11, kv21, kv31, kv41},
2020-03-09 09:30:42 -07:00
},
2020-04-07 12:15:36 -07:00
{
name: "Merge with no overlap, no nil, not interleaved",
a: resource.New(kv11, kv21),
b: resource.New(kv31, kv41),
2020-05-14 07:06:03 +08:00
want: []kv.KeyValue{kv11, kv21, kv31, kv41},
2020-04-07 12:15:36 -07:00
},
2020-03-09 09:30:42 -07:00
{
name: "Merge with common key order1",
a: resource.New(kv11),
b: resource.New(kv12, kv21),
2020-05-14 07:06:03 +08:00
want: []kv.KeyValue{kv11, kv21},
2020-03-09 09:30:42 -07:00
},
{
name: "Merge with common key order2",
a: resource.New(kv12, kv21),
b: resource.New(kv11),
2020-05-14 07:06:03 +08:00
want: []kv.KeyValue{kv12, kv21},
2020-03-09 09:30:42 -07:00
},
2020-04-07 12:15:36 -07:00
{
name: "Merge with common key order4",
a: resource.New(kv11, kv21, kv41),
b: resource.New(kv31, kv41),
2020-05-14 07:06:03 +08:00
want: []kv.KeyValue{kv11, kv21, kv31, kv41},
2020-04-07 12:15:36 -07:00
},
{
name: "Merge with no keys",
a: resource.New(),
b: resource.New(),
want: nil,
},
{
name: "Merge with first resource no keys",
a: resource.New(),
b: resource.New(kv21),
2020-05-14 07:06:03 +08:00
want: []kv.KeyValue{kv21},
2020-04-07 12:15:36 -07:00
},
{
name: "Merge with second resource no keys",
a: resource.New(kv11),
b: resource.New(),
2020-05-14 07:06:03 +08:00
want: []kv.KeyValue{kv11},
2020-04-07 12:15:36 -07:00
},
2020-03-09 09:30:42 -07:00
{
name: "Merge with first resource nil",
a: nil,
b: resource.New(kv21),
2020-05-14 07:06:03 +08:00
want: []kv.KeyValue{kv21},
2020-03-09 09:30:42 -07:00
},
{
name: "Merge with second resource nil",
a: resource.New(kv11),
b: nil,
2020-05-14 07:06:03 +08:00
want: []kv.KeyValue{kv11},
2020-03-09 09:30:42 -07:00
},
}
for _, c := range cases {
t.Run(fmt.Sprintf("case-%s", c.name), func(t *testing.T) {
res := resource.Merge(c.a, c.b)
if diff := cmp.Diff(
2020-04-07 12:15:36 -07:00
res.Attributes(),
c.want,
2020-05-14 07:06:03 +08:00
cmp.AllowUnexported(value.Value{})); diff != "" {
2020-03-09 09:30:42 -07:00
t.Fatalf("unwanted result: diff %+v,", diff)
}
})
}
}
2020-04-07 12:15:36 -07:00
func TestString(t *testing.T) {
for _, test := range []struct {
2020-05-14 07:06:03 +08:00
kvs []kv.KeyValue
2020-04-07 12:15:36 -07:00
want string
}{
{
kvs: nil,
want: "",
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{},
want: "",
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv11},
want: "k1=v11",
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv11, kv12},
want: "k1=v12",
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv11, kv21},
want: "k1=v11,k2=v21",
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv21, kv11},
want: "k1=v11,k2=v21",
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv11, kv21, kv31},
want: "k1=v11,k2=v21,k3=v31",
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv31, kv11, kv21},
want: "k1=v11,k2=v21,k3=v31",
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv.String("A", "a"), kv.String("B", "b")},
want: "A=a,B=b",
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv.String("A", "a,B=b")},
want: `A=a\,B\=b`,
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv.String("A", `a,B\=b`)},
want: `A=a\,B\\\=b`,
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv.String("A=a,B", `b`)},
want: `A\=a\,B=b`,
2020-04-07 12:15:36 -07:00
},
{
2020-05-14 07:06:03 +08:00
kvs: []kv.KeyValue{kv.String(`A=a\,B`, `b`)},
want: `A\=a\\\,B=b`,
2020-04-07 12:15:36 -07:00
},
} {
if got := resource.New(test.kvs...).String(); got != test.want {
t.Errorf("Resource(%v).String() = %q, want %q", test.kvs, got, test.want)
}
}
2020-03-09 09:30:42 -07:00
}
2020-04-22 14:32:58 -07:00
func TestMarshalJSON(t *testing.T) {
2020-05-14 07:06:03 +08:00
r := resource.New(kv.Int64("A", 1), kv.String("C", "D"))
2020-04-22 14:32:58 -07:00
data, err := json.Marshal(r)
require.NoError(t, err)
require.Equal(t,
`[{"Key":"A","Value":{"Type":"INT64","Value":1}},{"Key":"C","Value":{"Type":"STRING","Value":"D"}}]`,
string(data))
}