| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Package client contains generic representations of clients connecting to |
| 5 |
// different receivers |
| 6 |
package client |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"net" |
| 11 |
"slices" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
) |
| 16 |
|
| 17 |
func TestNewContext(t *testing.T) { |
| 18 |
testCases := []struct { |
| 19 |
name string |
| 20 |
cl Info |
| 21 |
}{ |
| 22 |
{ |
| 23 |
name: "valid client", |
| 24 |
cl: Info{ |
| 25 |
Addr: &net.IPAddr{ |
| 26 |
IP: net.IPv4(1, 2, 3, 4), |
| 27 |
}, |
| 28 |
}, |
| 29 |
}, |
| 30 |
{ |
| 31 |
name: "nil client", |
| 32 |
cl: Info{}, |
| 33 |
}, |
| 34 |
} |
| 35 |
for _, tt := range testCases { |
| 36 |
t.Run(tt.name, func(t *testing.T) { |
| 37 |
ctx := NewContext(context.Background(), tt.cl) |
| 38 |
assert.Equal(t, ctx.Value(ctxKey{}), tt.cl) |
| 39 |
}) |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
func TestFromContext(t *testing.T) { |
| 44 |
testCases := []struct { |
| 45 |
name string |
| 46 |
input context.Context |
| 47 |
expected Info |
| 48 |
}{ |
| 49 |
{ |
| 50 |
name: "context with client", |
| 51 |
input: context.WithValue(context.Background(), ctxKey{}, Info{ |
| 52 |
Addr: &net.IPAddr{ |
| 53 |
IP: net.IPv4(1, 2, 3, 4), |
| 54 |
}, |
| 55 |
}), |
| 56 |
expected: Info{ |
| 57 |
Addr: &net.IPAddr{ |
| 58 |
IP: net.IPv4(1, 2, 3, 4), |
| 59 |
}, |
| 60 |
}, |
| 61 |
}, |
| 62 |
{ |
| 63 |
name: "context without client", |
| 64 |
input: context.Background(), |
| 65 |
expected: Info{}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "context with something else in the key", |
| 69 |
input: context.WithValue(context.Background(), ctxKey{}, "unexpected!"), |
| 70 |
expected: Info{}, |
| 71 |
}, |
| 72 |
} |
| 73 |
for _, tt := range testCases { |
| 74 |
t.Run(tt.name, func(t *testing.T) { |
| 75 |
assert.Equal(t, tt.expected, FromContext(tt.input)) |
| 76 |
}) |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestMetadata(t *testing.T) { |
| 81 |
source := map[string][]string{"test-key": {"test-val"}, "TEST-KEY-2": {"test-val"}} |
| 82 |
md := NewMetadata(source) |
| 83 |
assert.Equal(t, []string{"test-key", "test-key-2"}, slices.Sorted(md.Keys())) |
| 84 |
assert.Equal(t, []string{"test-val"}, md.Get("test-key")) |
| 85 |
assert.Equal(t, []string{"test-val"}, md.Get("test-KEY")) // case insensitive lookup |
| 86 |
assert.Equal(t, []string{"test-val"}, md.Get("test-key-2")) // case insensitive lookup |
| 87 |
|
| 88 |
// test if copy. In regular use, source cannot change |
| 89 |
val := md.Get("test-key") |
| 90 |
source["test-key"][0] = "abc" |
| 91 |
assert.Equal(t, []string{"test-val"}, val) |
| 92 |
|
| 93 |
assert.Empty(t, md.Get("non-existent-key")) |
| 94 |
} |
| 95 |
|
| 96 |
func TestUninstantiatedMetadata(t *testing.T) { |
| 97 |
i := Info{} |
| 98 |
assert.Empty(t, slices.Collect(i.Metadata.Keys())) |
| 99 |
assert.Empty(t, i.Metadata.Get("test")) |
| 100 |
} |