1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-29 23:07:45 +02:00
Files
opentelemetry-go/sdk/resource/resource_test.go
Joshua MacDonald 0bb12d9b1b New api/label package, common label set impl (#651)
* New label set API

* Checkpoint

* Remove label.Labels interface

* Fix trace

* Remove label storage

* Restore metric_test.go

* Tidy tests

* More comments

* More comments

* Same changes as 654

* Checkpoint

* Fix batch labels

* Avoid Resource.Attributes() where possible

* Update comments and restore order in resource.go

* From feedback

* From feedback

* Move iterator_test & feedback

* Strenghten the label.Set test

* Feedback on typos

* Fix the set test per @krnowak

* Nit
2020-04-23 12:10:58 -07:00

225 lines
5.1 KiB
Go

// Copyright The OpenTelemetry Authors
//
// 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 (
"encoding/json"
"fmt"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/api/core"
"go.opentelemetry.io/otel/api/key"
"go.opentelemetry.io/otel/sdk/resource"
)
var (
kv11 = key.String("k1", "v11")
kv12 = key.String("k1", "v12")
kv21 = key.String("k2", "v21")
kv31 = key.String("k3", "v31")
kv41 = key.String("k4", "v41")
)
func TestNew(t *testing.T) {
cases := []struct {
name string
in []core.KeyValue
want []core.KeyValue
}{
{
name: "New with common key order1",
in: []core.KeyValue{kv12, kv11, kv21},
want: []core.KeyValue{kv11, kv21},
},
{
name: "New with common key order2",
in: []core.KeyValue{kv11, kv12, kv21},
want: []core.KeyValue{kv12, kv21},
},
{
name: "New with nil",
in: nil,
want: nil,
},
}
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(
res.Attributes(),
c.want,
cmp.AllowUnexported(core.Value{})); diff != "" {
t.Fatalf("unwanted result: diff %+v,", diff)
}
})
}
}
func TestMerge(t *testing.T) {
cases := []struct {
name string
a, b *resource.Resource
want []core.KeyValue
}{
{
name: "Merge with no overlap, no nil",
a: resource.New(kv11, kv31),
b: resource.New(kv21, kv41),
want: []core.KeyValue{kv11, kv21, kv31, kv41},
},
{
name: "Merge with no overlap, no nil, not interleaved",
a: resource.New(kv11, kv21),
b: resource.New(kv31, kv41),
want: []core.KeyValue{kv11, kv21, kv31, kv41},
},
{
name: "Merge with common key order1",
a: resource.New(kv11),
b: resource.New(kv12, kv21),
want: []core.KeyValue{kv11, kv21},
},
{
name: "Merge with common key order2",
a: resource.New(kv12, kv21),
b: resource.New(kv11),
want: []core.KeyValue{kv12, kv21},
},
{
name: "Merge with common key order4",
a: resource.New(kv11, kv21, kv41),
b: resource.New(kv31, kv41),
want: []core.KeyValue{kv11, kv21, kv31, kv41},
},
{
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),
want: []core.KeyValue{kv21},
},
{
name: "Merge with second resource no keys",
a: resource.New(kv11),
b: resource.New(),
want: []core.KeyValue{kv11},
},
{
name: "Merge with first resource nil",
a: nil,
b: resource.New(kv21),
want: []core.KeyValue{kv21},
},
{
name: "Merge with second resource nil",
a: resource.New(kv11),
b: nil,
want: []core.KeyValue{kv11},
},
}
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(
res.Attributes(),
c.want,
cmp.AllowUnexported(core.Value{})); diff != "" {
t.Fatalf("unwanted result: diff %+v,", diff)
}
})
}
}
func TestString(t *testing.T) {
for _, test := range []struct {
kvs []core.KeyValue
want string
}{
{
kvs: nil,
want: "",
},
{
kvs: []core.KeyValue{},
want: "",
},
{
kvs: []core.KeyValue{kv11},
want: "k1=v11",
},
{
kvs: []core.KeyValue{kv11, kv12},
want: "k1=v12",
},
{
kvs: []core.KeyValue{kv11, kv21},
want: "k1=v11,k2=v21",
},
{
kvs: []core.KeyValue{kv21, kv11},
want: "k1=v11,k2=v21",
},
{
kvs: []core.KeyValue{kv11, kv21, kv31},
want: "k1=v11,k2=v21,k3=v31",
},
{
kvs: []core.KeyValue{kv31, kv11, kv21},
want: "k1=v11,k2=v21,k3=v31",
},
{
kvs: []core.KeyValue{key.String("A", "a"), key.String("B", "b")},
want: "A=a,B=b",
},
{
kvs: []core.KeyValue{key.String("A", "a,B=b")},
want: `A=a\,B\=b`,
},
{
kvs: []core.KeyValue{key.String("A", `a,B\=b`)},
want: `A=a\,B\\\=b`,
},
{
kvs: []core.KeyValue{key.String("A=a,B", `b`)},
want: `A\=a\,B=b`,
},
{
kvs: []core.KeyValue{key.String(`A=a\,B`, `b`)},
want: `A\=a\\\,B=b`,
},
} {
if got := resource.New(test.kvs...).String(); got != test.want {
t.Errorf("Resource(%v).String() = %q, want %q", test.kvs, got, test.want)
}
}
}
func TestMarshalJSON(t *testing.T) {
r := resource.New(key.Int64("A", 1), key.String("C", "D"))
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))
}