| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package configauth |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/component" |
| 14 |
"go.opentelemetry.io/collector/extension" |
| 15 |
"go.opentelemetry.io/collector/extension/extensionauth/extensionauthtest" |
| 16 |
) |
| 17 |
|
| 18 |
var mockID = component.MustNewID("mock") |
| 19 |
|
| 20 |
func TestGetServer(t *testing.T) { |
| 21 |
testCases := []struct { |
| 22 |
name string |
| 23 |
authenticator extension.Extension |
| 24 |
expected error |
| 25 |
}{ |
| 26 |
{ |
| 27 |
name: "obtain server authenticator", |
| 28 |
authenticator: extensionauthtest.NewNopServer(), |
| 29 |
expected: nil, |
| 30 |
}, |
| 31 |
{ |
| 32 |
name: "not a server authenticator", |
| 33 |
authenticator: extensionauthtest.NewNopClient(), |
| 34 |
expected: errNotServer, |
| 35 |
}, |
| 36 |
} |
| 37 |
for _, tt := range testCases { |
| 38 |
t.Run(tt.name, func(t *testing.T) { |
| 39 |
// prepare |
| 40 |
cfg := &Config{ |
| 41 |
AuthenticatorID: mockID, |
| 42 |
} |
| 43 |
ext := map[component.ID]component.Component{ |
| 44 |
mockID: tt.authenticator, |
| 45 |
} |
| 46 |
|
| 47 |
authenticator, err := cfg.GetServerAuthenticator(context.Background(), ext) |
| 48 |
|
| 49 |
// verify |
| 50 |
if tt.expected != nil { |
| 51 |
require.ErrorIs(t, err, tt.expected) |
| 52 |
assert.Nil(t, authenticator) |
| 53 |
} else { |
| 54 |
require.NoError(t, err) |
| 55 |
assert.NotNil(t, authenticator) |
| 56 |
} |
| 57 |
}) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestGetServerFails(t *testing.T) { |
| 62 |
cfg := &Config{ |
| 63 |
AuthenticatorID: component.MustNewID("does_not_exist"), |
| 64 |
} |
| 65 |
|
| 66 |
authenticator, err := cfg.GetServerAuthenticator(context.Background(), map[component.ID]component.Component{}) |
| 67 |
require.ErrorIs(t, err, errAuthenticatorNotFound) |
| 68 |
assert.Nil(t, authenticator) |
| 69 |
} |
| 70 |
|
| 71 |
func TestGetHTTPClient(t *testing.T) { |
| 72 |
testCases := []struct { |
| 73 |
name string |
| 74 |
authenticator extension.Extension |
| 75 |
expected error |
| 76 |
}{ |
| 77 |
{ |
| 78 |
name: "obtain client authenticator", |
| 79 |
authenticator: extensionauthtest.NewNopClient(), |
| 80 |
expected: nil, |
| 81 |
}, |
| 82 |
{ |
| 83 |
name: "not a client authenticator", |
| 84 |
authenticator: extensionauthtest.NewNopServer(), |
| 85 |
expected: errNotHTTPClient, |
| 86 |
}, |
| 87 |
} |
| 88 |
for _, tt := range testCases { |
| 89 |
t.Run(tt.name, func(t *testing.T) { |
| 90 |
// prepare |
| 91 |
cfg := &Config{ |
| 92 |
AuthenticatorID: mockID, |
| 93 |
} |
| 94 |
ext := map[component.ID]component.Component{ |
| 95 |
mockID: tt.authenticator, |
| 96 |
} |
| 97 |
|
| 98 |
authenticator, err := cfg.GetHTTPClientAuthenticator(context.Background(), ext) |
| 99 |
|
| 100 |
// verify |
| 101 |
if tt.expected != nil { |
| 102 |
require.ErrorIs(t, err, tt.expected) |
| 103 |
assert.Nil(t, authenticator) |
| 104 |
} else { |
| 105 |
require.NoError(t, err) |
| 106 |
assert.NotNil(t, authenticator) |
| 107 |
} |
| 108 |
}) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
func TestGetGRPCClientFails(t *testing.T) { |
| 113 |
cfg := &Config{ |
| 114 |
AuthenticatorID: component.MustNewID("does_not_exist"), |
| 115 |
} |
| 116 |
authenticator, err := cfg.GetGRPCClientAuthenticator(context.Background(), map[component.ID]component.Component{}) |
| 117 |
require.ErrorIs(t, err, errAuthenticatorNotFound) |
| 118 |
assert.Nil(t, authenticator) |
| 119 |
} |