Skip to content

Commit 5dbeb2a

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

File tree

8 files changed

+77
-214
lines changed

8 files changed

+77
-214
lines changed

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

Lines changed: 26 additions & 0 deletions
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

Lines changed: 10 additions & 4 deletions
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/configgrpc/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ require (
1414
go.opentelemetry.io/collector/config/configopaque v1.27.0
1515
go.opentelemetry.io/collector/config/configtls v1.27.0
1616
go.opentelemetry.io/collector/extension/extensionauth v0.121.0
17-
go.opentelemetry.io/collector/extension/extensionauth/extensionauthtest v0.121.0
1817
go.opentelemetry.io/collector/pdata v1.27.0
1918
go.opentelemetry.io/collector/pdata/testdata v0.121.0
2019
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0

config/confighttp/confighttp_test.go

Lines changed: 28 additions & 27 deletions
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,20 @@ 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+
customRoundTripperOpt := extensionauth.WithClientRoundTripper(
344+
func(http.RoundTripper) (http.RoundTripper, error) {
345+
return &customRoundTripper{}, nil
346+
})
347+
343348
tests := []struct {
344349
name string
345350
shouldErr bool
@@ -355,9 +360,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
355360
shouldErr: false,
356361
host: &mockHost{
357362
ext: map[component.ID]component.Component{
358-
mockID: &extensionauthtest.MockClient{
359-
ResultRoundTripper: &customRoundTripper{},
360-
},
363+
mockID: build(t, extensionauth.NewClient),
361364
},
362365
},
363366
},
@@ -370,7 +373,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
370373
shouldErr: true,
371374
host: &mockHost{
372375
ext: map[component.ID]component.Component{
373-
mockID: &extensionauthtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
376+
mockID: build(t, extensionauth.NewClient),
374377
},
375378
},
376379
},
@@ -392,7 +395,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
392395
shouldErr: false,
393396
host: &mockHost{
394397
ext: map[component.ID]component.Component{
395-
mockID: &extensionauthtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
398+
mockID: build(t, extensionauth.NewClient, customRoundTripperOpt),
396399
},
397400
},
398401
},
@@ -406,7 +409,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
406409
shouldErr: false,
407410
host: &mockHost{
408411
ext: map[component.ID]component.Component{
409-
mockID: &extensionauthtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
412+
mockID: build(t, extensionauth.NewClient, customRoundTripperOpt),
410413
},
411414
},
412415
},
@@ -420,7 +423,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
420423
shouldErr: false,
421424
host: &mockHost{
422425
ext: map[component.ID]component.Component{
423-
mockID: &extensionauthtest.MockClient{ResultRoundTripper: &customRoundTripper{}},
426+
mockID: build(t, extensionauth.NewClient, customRoundTripperOpt),
424427
},
425428
},
426429
},
@@ -433,9 +436,7 @@ func TestHTTPClientSettingWithAuthConfig(t *testing.T) {
433436
shouldErr: true,
434437
host: &mockHost{
435438
ext: map[component.ID]component.Component{
436-
mockID: &extensionauthtest.MockClient{
437-
ResultRoundTripper: &customRoundTripper{}, MustError: true,
438-
},
439+
mockID: build(t, extensionauthtest.NewErrorClient),
439440
},
440441
},
441442
},
@@ -832,7 +833,7 @@ func TestHttpCorsWithSettings(t *testing.T) {
832833

833834
host := &mockHost{
834835
ext: map[component.ID]component.Component{
835-
mockID: mustNewServerAuth(t,
836+
mockID: build(t, extensionauth.NewServer,
836837
extensionauth.WithServerAuthenticate(func(ctx context.Context, _ map[string][]string) (context.Context, error) {
837838
return ctx, errors.New("Settings failed")
838839
}),
@@ -1144,7 +1145,7 @@ func TestServerAuth(t *testing.T) {
11441145

11451146
host := &mockHost{
11461147
ext: map[component.ID]component.Component{
1147-
mockID: mustNewServerAuth(t,
1148+
mockID: build(t, extensionauth.NewServer,
11481149
extensionauth.WithServerAuthenticate(func(ctx context.Context, _ map[string][]string) (context.Context, error) {
11491150
authCalled = true
11501151
return ctx, nil
@@ -1195,7 +1196,7 @@ func TestFailedServerAuth(t *testing.T) {
11951196
}
11961197
host := &mockHost{
11971198
ext: map[component.ID]component.Component{
1198-
mockID: mustNewServerAuth(t,
1199+
mockID: build(t, extensionauth.NewServer,
11991200
extensionauth.WithServerAuthenticate(func(ctx context.Context, _ map[string][]string) (context.Context, error) {
12001201
return ctx, errors.New("Settings failed")
12011202
}),
@@ -1376,7 +1377,7 @@ func TestAuthWithQueryParams(t *testing.T) {
13761377

13771378
host := &mockHost{
13781379
ext: map[component.ID]component.Component{
1379-
mockID: mustNewServerAuth(t,
1380+
mockID: build(t, extensionauth.NewServer,
13801381
extensionauth.WithServerAuthenticate(func(ctx context.Context, sources map[string][]string) (context.Context, error) {
13811382
require.Len(t, sources, 1)
13821383
assert.Equal(t, "1", sources["auth"][0])

extension/extensionauth/extensionauthtest/go.mod

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,14 @@ module go.opentelemetry.io/collector/extension/extensionauth/extensionauthtest
33
go 1.23.0
44

55
require (
6-
github.com/stretchr/testify v1.10.0
7-
go.opentelemetry.io/collector/component v1.27.0
86
go.opentelemetry.io/collector/extension/extensionauth v0.121.0
97
go.uber.org/goleak v1.3.0
108
google.golang.org/grpc v1.71.0
119
)
1210

1311
require (
14-
github.com/davecgh/go-spew v1.1.1 // indirect
1512
github.com/gogo/protobuf v1.3.2 // indirect
16-
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
go.opentelemetry.io/collector/component v1.27.0 // indirect
1714
go.opentelemetry.io/collector/extension v1.27.0 // indirect
1815
go.opentelemetry.io/collector/pdata v1.27.0 // indirect
1916
go.opentelemetry.io/otel v1.34.0 // indirect
@@ -26,7 +23,6 @@ require (
2623
golang.org/x/text v0.21.0 // indirect
2724
google.golang.org/genproto/googleapis/rpc v0.0.0-20250115164207-1a7da9e5054f // indirect
2825
google.golang.org/protobuf v1.36.5 // indirect
29-
gopkg.in/yaml.v3 v3.0.1 // indirect
3026
)
3127

3228
replace go.opentelemetry.io/collector/extension/extensionauth => ..

extension/extensionauth/extensionauthtest/go.sum

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extension/extensionauth/extensionauthtest/mock_clientauth.go

Lines changed: 12 additions & 39 deletions
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)