Skip to content

Commit e085777

Browse files
committed
[extensionauthtest] Remove extensionauthtest.MockClient
1 parent 97be125 commit e085777

File tree

5 files changed

+77
-199
lines changed

5 files changed

+77
-199
lines changed

.chloggen/mx-psi_seal-them-all.yaml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: breaking
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: extensionauthtest
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Remove the `extensionauthtest.MockClient` struct.
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [12567]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
Use the standard `extensionauth.NewClient` constructor to create a client with a specific mock implementation. Use `extensionauthtest.NewErrorClient` to create a client that always returns an error.
20+
21+
# Optional: The change log or logs in which this entry should be included.
22+
# e.g. '[user]' or '[user, api]'
23+
# Include 'user' if the change is relevant to end users.
24+
# Include 'api' if there is a change to a library API.
25+
# Default: '[user]'
26+
change_logs: [api]

config/configgrpc/configgrpc_test.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"go.opentelemetry.io/collector/config/configopaque"
3131
"go.opentelemetry.io/collector/config/configtls"
3232
"go.opentelemetry.io/collector/extension/extensionauth"
33-
"go.opentelemetry.io/collector/extension/extensionauth/extensionauthtest"
3433
"go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp"
3534
)
3635

@@ -41,6 +40,13 @@ func mustNewServerAuth(t *testing.T, opts ...extensionauth.ServerOption) extensi
4140
return srv
4241
}
4342

43+
func mustNewClientAuth(t *testing.T, opts ...extensionauth.ClientOption) extensionauth.Client {
44+
t.Helper()
45+
client, err := extensionauth.NewClient(opts...)
46+
require.NoError(t, err)
47+
return client
48+
}
49+
4450
func TestNewDefaultKeepaliveClientConfig(t *testing.T) {
4551
expectedKeepaliveClientConfig := &KeepaliveClientConfig{
4652
Time: time.Second * 10,
@@ -171,7 +177,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
171177
},
172178
host: &mockHost{
173179
ext: map[component.ID]component.Component{
174-
testAuthID: &extensionauthtest.MockClient{},
180+
testAuthID: mustNewClientAuth(t),
175181
},
176182
},
177183
},
@@ -200,7 +206,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
200206
},
201207
host: &mockHost{
202208
ext: map[component.ID]component.Component{
203-
testAuthID: &extensionauthtest.MockClient{},
209+
testAuthID: mustNewClientAuth(t),
204210
},
205211
},
206212
},
@@ -229,7 +235,7 @@ func TestAllGrpcClientSettings(t *testing.T) {
229235
},
230236
host: &mockHost{
231237
ext: map[component.ID]component.Component{
232-
testAuthID: &extensionauthtest.MockClient{},
238+
testAuthID: mustNewClientAuth(t),
233239
},
234240
},
235241
},

config/confighttp/confighttp_test.go

+29-27
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,11 @@ import (
3434
"go.opentelemetry.io/collector/extension/extensionauth/extensionauthtest"
3535
)
3636

37-
type customRoundTripper struct{}
38-
39-
var _ http.RoundTripper = (*customRoundTripper)(nil)
40-
41-
func (c *customRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
42-
return nil, nil
43-
}
44-
45-
func mustNewServerAuth(t *testing.T, opts ...extensionauth.ServerOption) extensionauth.Server {
37+
func build[T any, O any](t *testing.T, builder func(...O) (T, error), opts ...O) T {
4638
t.Helper()
47-
srv, err := extensionauth.NewServer(opts...)
39+
obj, err := builder(opts...)
4840
require.NoError(t, err)
49-
return srv
41+
return obj
5042
}
5143

5244
var (
@@ -61,7 +53,7 @@ var (
6153
func TestAllHTTPClientSettings(t *testing.T) {
6254
host := &mockHost{
6355
ext: map[component.ID]component.Component{
64-
testAuthID: &extensionauthtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
56+
testAuthID: build(t, extensionauth.NewClient),
6557
},
6658
}
6759

@@ -187,7 +179,7 @@ func TestAllHTTPClientSettings(t *testing.T) {
187179
func TestPartialHTTPClientSettings(t *testing.T) {
188180
host := &mockHost{
189181
ext: map[component.ID]component.Component{
190-
testAuthID: &extensionauthtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
182+
testAuthID: build(t, extensionauth.NewClient),
191183
},
192184
}
193185

@@ -339,7 +331,21 @@ func TestHTTPClientSettingsError(t *testing.T) {
339331
}
340332
}
341333

334+
var _ http.RoundTripper = &customRoundTripper{}
335+
336+
type customRoundTripper struct{}
337+
338+
func (c *customRoundTripper) RoundTrip(*http.Request) (*http.Response, error) {
339+
return nil, nil
340+
}
341+
342342
func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
343+
344+
customRoundTripperOpt := extensionauth.WithClientRoundTripper(
345+
func(http.RoundTripper) (http.RoundTripper, error) {
346+
return &customRoundTripper{}, nil
347+
})
348+
343349
tests := []struct {
344350
name string
345351
shouldErr bool
@@ -355,9 +361,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
355361
shouldErr: false,
356362
host: &mockHost{
357363
ext: map[component.ID]component.Component{
358-
mockID: &extensionauthtest.MockClient{
359-
ResultRoundTripper: &customRoundTripper{},
360-
},
364+
mockID: build(t, extensionauth.NewClient),
361365
},
362366
},
363367
},
@@ -370,7 +374,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
370374
shouldErr: true,
371375
host: &mockHost{
372376
ext: map[component.ID]component.Component{
373-
mockID: &extensionauthtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
377+
mockID: build(t, extensionauth.NewClient),
374378
},
375379
},
376380
},
@@ -392,7 +396,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
392396
shouldErr: false,
393397
host: &mockHost{
394398
ext: map[component.ID]component.Component{
395-
mockID: &extensionauthtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
399+
mockID: build(t, extensionauth.NewClient, customRoundTripperOpt),
396400
},
397401
},
398402
},
@@ -406,7 +410,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
406410
shouldErr: false,
407411
host: &mockHost{
408412
ext: map[component.ID]component.Component{
409-
mockID: &extensionauthtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
413+
mockID: build(t, extensionauth.NewClient, customRoundTripperOpt),
410414
},
411415
},
412416
},
@@ -420,7 +424,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
420424
shouldErr: false,
421425
host: &mockHost{
422426
ext: map[component.ID]component.Component{
423-
mockID: &extensionauthtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
427+
mockID: build(t, extensionauth.NewClient, customRoundTripperOpt),
424428
},
425429
},
426430
},
@@ -433,9 +437,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
433437
shouldErr: true,
434438
host: &mockHost{
435439
ext: map[component.ID]component.Component{
436-
mockID: &extensionauthtest.MockClient{
437-
ResultRoundTripper: &customRoundTripper{}, MustError: true,
438-
},
440+
mockID: build(t, extensionauthtest.NewErrorClient),
439441
},
440442
},
441443
},
@@ -832,7 +834,7 @@ func TestHttpCorsWithSettings(t *testing.T) {
832834

833835
host := &mockHost{
834836
ext: map[component.ID]component.Component{
835-
mockID: mustNewServerAuth(t,
837+
mockID: build(t, extensionauth.NewServer,
836838
extensionauth.WithServerAuthenticate(func(ctx context.Context, _ map[string][]string) (context.Context, error) {
837839
return ctx, errors.New("Settings failed")
838840
}),
@@ -1144,7 +1146,7 @@ func TestServerAuth(t *testing.T) {
11441146

11451147
host := &mockHost{
11461148
ext: map[component.ID]component.Component{
1147-
mockID: mustNewServerAuth(t,
1149+
mockID: build(t, extensionauth.NewServer,
11481150
extensionauth.WithServerAuthenticate(func(ctx context.Context, _ map[string][]string) (context.Context, error) {
11491151
authCalled = true
11501152
return ctx, nil
@@ -1195,7 +1197,7 @@ func TestFailedServerAuth(t *testing.T) {
11951197
}
11961198
host := &mockHost{
11971199
ext: map[component.ID]component.Component{
1198-
mockID: mustNewServerAuth(t,
1200+
mockID: build(t, extensionauth.NewServer,
11991201
extensionauth.WithServerAuthenticate(func(ctx context.Context, _ map[string][]string) (context.Context, error) {
12001202
return ctx, errors.New("Settings failed")
12011203
}),
@@ -1376,7 +1378,7 @@ func TestAuthWithQueryParams(t *testing.T) {
13761378

13771379
host := &mockHost{
13781380
ext: map[component.ID]component.Component{
1379-
mockID: mustNewServerAuth(t,
1381+
mockID: build(t, extensionauth.NewServer,
13801382
extensionauth.WithServerAuthenticate(func(ctx context.Context, sources map[string][]string) (context.Context, error) {
13811383
require.Len(t, sources, 1)
13821384
assert.Equal(t, "1", sources["auth"][0])

extension/extensionauth/extensionauthtest/mock_clientauth.go

+12-39
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,25 @@
44
package extensionauthtest // import "go.opentelemetry.io/collector/extension/extensionauth/extensionauthtest"
55

66
import (
7-
"context"
87
"errors"
98
"net/http"
109

1110
"google.golang.org/grpc/credentials"
1211

13-
"go.opentelemetry.io/collector/component"
1412
"go.opentelemetry.io/collector/extension/extensionauth"
1513
)
1614

17-
var (
18-
_ extensionauth.Client = (*MockClient)(nil)
19-
errMockError = errors.New("mock Error")
20-
)
21-
22-
// MockClient provides a mock implementation of GRPCClient and HTTPClient interfaces
23-
type MockClient struct {
24-
ResultRoundTripper http.RoundTripper
25-
ResultPerRPCCredentials credentials.PerRPCCredentials
26-
MustError bool
27-
}
28-
29-
// Start for the MockClient does nothing
30-
func (m *MockClient) Start(context.Context, component.Host) error {
31-
return nil
32-
}
33-
34-
// Shutdown for the MockClient does nothing
35-
func (m *MockClient) Shutdown(context.Context) error {
36-
return nil
37-
}
38-
39-
// RoundTripper for the MockClient either returns error if the mock authenticator is forced to or
40-
// returns the supplied resultRoundTripper.
41-
func (m *MockClient) RoundTripper(http.RoundTripper) (http.RoundTripper, error) {
42-
if m.MustError {
43-
return nil, errMockError
44-
}
45-
return m.ResultRoundTripper, nil
46-
}
47-
48-
// PerRPCCredentials for the MockClient either returns error if the mock authenticator is forced to or
49-
// returns the supplied resultPerRPCCredentials.
50-
func (m *MockClient) PerRPCCredentials() (credentials.PerRPCCredentials, error) {
51-
if m.MustError {
52-
return nil, errMockError
15+
var errMockError = errors.New("mock Error")
16+
17+
// NewErrorClient returns a new extensionauth.Client that always returns an error on any of the client methods.
18+
func NewErrorClient(opts ...extensionauth.ClientOption) (extensionauth.Client, error) {
19+
errorOpts := []extensionauth.ClientOption{
20+
extensionauth.WithClientRoundTripper(func(base http.RoundTripper) (http.RoundTripper, error) {
21+
return nil, errMockError
22+
}),
23+
extensionauth.WithClientPerRPCCredentials(func() (credentials.PerRPCCredentials, error) {
24+
return nil, errMockError
25+
}),
5326
}
54-
return m.ResultPerRPCCredentials, nil
27+
return extensionauth.NewClient(append(errorOpts, opts...)...)
5528
}

0 commit comments

Comments
 (0)