Debian Codegraph @

otelconf/v0.2.0/resource_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelconf
5
6 import (
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 "go.opentelemetry.io/otel/attribute"
12 "go.opentelemetry.io/otel/sdk/resource"
13 )
14
15 func TestNewResource(t *testing.T) {
16 schemaURL := resource.Default().SchemaURL()
17
18 res, err := resource.Merge(resource.Default(),
19 resource.NewWithAttributes(schemaURL,
20 attribute.String("service.name", "service-a"),
21 ))
22 require.NoError(t, err)
23 resWithAttrs, err := resource.Merge(resource.Default(),
24 resource.NewWithAttributes(schemaURL,
25 attribute.String("service.name", "service-a"),
26 attribute.Bool("attr-bool", true),
27 ))
28 require.NoError(t, err)
29 tests := []struct {
30 name string
31 config *Resource
32 wantResource *resource.Resource
33 wantErr error
34 }{
35 {
36 name: "no-resource-configuration",
37 wantResource: resource.Default(),
38 },
39 {
40 name: "resource-no-attributes",
41 config: &Resource{},
42 wantResource: resource.Default(),
43 },
44 {
45 name: "resource-with-attributes-invalid-schema",
46 config: &Resource{
47 SchemaUrl: ptr("https://opentelemetry.io/"),
48 Attributes: Attributes{
49 "service.name": "service-a",
50 },
51 },
52 wantResource: resource.NewSchemaless(res.Attributes()...),
53 wantErr: resource.ErrSchemaURLConflict,
54 },
55 {
56 name: "resource-with-attributes-and-schema",
57 config: &Resource{
58 Attributes: Attributes{
59 "service.name": "service-a",
60 },
61 SchemaUrl: ptr(schemaURL),
62 },
63 wantResource: res,
64 },
65 {
66 name: "resource-with-additional-attributes-and-schema",
67 config: &Resource{
68 Attributes: Attributes{
69 "service.name": "service-a",
70 "attr-bool": true,
71 },
72 SchemaUrl: ptr(schemaURL),
73 },
74 wantResource: resWithAttrs,
75 },
76 }
77 for _, tt := range tests {
78 t.Run(tt.name, func(t *testing.T) {
79 got, err := newResource(tt.config)
80 assert.ErrorIs(t, err, tt.wantErr)
81 assert.Equal(t, tt.wantResource, got)
82 })
83 }
84 }