| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package resource |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/otel/attribute" |
| 14 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 15 |
) |
| 16 |
|
| 17 |
func TestDetectOnePair(t *testing.T) { |
| 18 |
t.Setenv(resourceAttrKey, "key=value") |
| 19 |
|
| 20 |
detector := &fromEnv{} |
| 21 |
res, err := detector.Detect(t.Context()) |
| 22 |
require.NoError(t, err) |
| 23 |
assert.Equal(t, NewSchemaless(attribute.String("key", "value")), res) |
| 24 |
} |
| 25 |
|
| 26 |
func TestDetectURIEncodingOnePair(t *testing.T) { |
| 27 |
t.Setenv(resourceAttrKey, "key=x+y+z?q=123") |
| 28 |
|
| 29 |
detector := &fromEnv{} |
| 30 |
res, err := detector.Detect(t.Context()) |
| 31 |
require.NoError(t, err) |
| 32 |
assert.Equal(t, NewSchemaless(attribute.String("key", "x+y+z?q=123")), res) |
| 33 |
} |
| 34 |
|
| 35 |
func TestDetectMultiPairs(t *testing.T) { |
| 36 |
t.Setenv("x", "1") |
| 37 |
t.Setenv(resourceAttrKey, "key=value, k = v , a= x, a=z, b=c%2Fd") |
| 38 |
|
| 39 |
detector := &fromEnv{} |
| 40 |
res, err := detector.Detect(t.Context()) |
| 41 |
require.NoError(t, err) |
| 42 |
assert.Equal(t, NewSchemaless( |
| 43 |
attribute.String("key", "value"), |
| 44 |
attribute.String("k", "v"), |
| 45 |
attribute.String("a", "x"), |
| 46 |
attribute.String("a", "z"), |
| 47 |
attribute.String("b", "c/d"), |
| 48 |
), res) |
| 49 |
} |
| 50 |
|
| 51 |
func TestDetectURIEncodingMultiPairs(t *testing.T) { |
| 52 |
t.Setenv("x", "1") |
| 53 |
t.Setenv(resourceAttrKey, "key=x+y+z,namespace=localhost/test&verify") |
| 54 |
detector := &fromEnv{} |
| 55 |
res, err := detector.Detect(t.Context()) |
| 56 |
require.NoError(t, err) |
| 57 |
assert.Equal(t, NewSchemaless( |
| 58 |
attribute.String("key", "x+y+z"), |
| 59 |
attribute.String("namespace", "localhost/test&verify"), |
| 60 |
), res) |
| 61 |
} |
| 62 |
|
| 63 |
func TestEmpty(t *testing.T) { |
| 64 |
t.Setenv(resourceAttrKey, " ") |
| 65 |
detector := &fromEnv{} |
| 66 |
res, err := detector.Detect(t.Context()) |
| 67 |
require.NoError(t, err) |
| 68 |
assert.Equal(t, Empty(), res) |
| 69 |
} |
| 70 |
|
| 71 |
func TestNoResourceAttributesSet(t *testing.T) { |
| 72 |
t.Setenv(svcNameKey, "bar") |
| 73 |
detector := &fromEnv{} |
| 74 |
res, err := detector.Detect(t.Context()) |
| 75 |
require.NoError(t, err) |
| 76 |
assert.Equal(t, res, NewSchemaless( |
| 77 |
semconv.ServiceName("bar"), |
| 78 |
)) |
| 79 |
} |
| 80 |
|
| 81 |
func TestMissingKeyError(t *testing.T) { |
| 82 |
t.Setenv(resourceAttrKey, "key=value,key") |
| 83 |
detector := &fromEnv{} |
| 84 |
res, err := detector.Detect(t.Context()) |
| 85 |
assert.Error(t, err) |
| 86 |
assert.Equal(t, err, fmt.Errorf("%w: %v", errMissingValue, "[key]")) |
| 87 |
assert.Equal(t, res, NewSchemaless( |
| 88 |
attribute.String("key", "value"), |
| 89 |
)) |
| 90 |
} |
| 91 |
|
| 92 |
func TestInvalidPercentDecoding(t *testing.T) { |
| 93 |
t.Setenv(resourceAttrKey, "key=%invalid") |
| 94 |
detector := &fromEnv{} |
| 95 |
res, err := detector.Detect(t.Context()) |
| 96 |
assert.NoError(t, err) |
| 97 |
assert.Equal(t, NewSchemaless( |
| 98 |
attribute.String("key", "%invalid"), |
| 99 |
), res) |
| 100 |
} |
| 101 |
|
| 102 |
func TestDetectServiceNameFromEnv(t *testing.T) { |
| 103 |
t.Setenv(resourceAttrKey, "key=value,service.name=foo") |
| 104 |
t.Setenv(svcNameKey, "bar") |
| 105 |
detector := &fromEnv{} |
| 106 |
res, err := detector.Detect(t.Context()) |
| 107 |
require.NoError(t, err) |
| 108 |
assert.Equal(t, res, NewSchemaless( |
| 109 |
attribute.String("key", "value"), |
| 110 |
semconv.ServiceName("bar"), |
| 111 |
)) |
| 112 |
} |