| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package confighttp |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"io" |
| 10 |
"net/http" |
| 11 |
"net/http/httptest" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
|
| 18 |
"go.opentelemetry.io/collector/component" |
| 19 |
"go.opentelemetry.io/collector/component/componenttest" |
| 20 |
"go.opentelemetry.io/collector/config/configmiddleware" |
| 21 |
"go.opentelemetry.io/collector/extension" |
| 22 |
"go.opentelemetry.io/collector/extension/extensionmiddleware" |
| 23 |
"go.opentelemetry.io/collector/extension/extensionmiddleware/extensionmiddlewaretest" |
| 24 |
) |
| 25 |
|
| 26 |
// testClientMiddleware is a test middleware that appends a string to the response body |
| 27 |
type testClientMiddleware struct { |
| 28 |
extension.Extension |
| 29 |
extensionmiddleware.GetHTTPRoundTripperFunc |
| 30 |
} |
| 31 |
|
| 32 |
func newTestClientMiddleware(name string) component.Component { |
| 33 |
return &testClientMiddleware{ |
| 34 |
Extension: extensionmiddlewaretest.NewNop(), |
| 35 |
GetHTTPRoundTripperFunc: func(transport http.RoundTripper) (http.RoundTripper, error) { |
| 36 |
return extensionmiddlewaretest.RoundTripperFunc( |
| 37 |
func(req *http.Request) (*http.Response, error) { |
| 38 |
resp, err := transport.RoundTrip(req) |
| 39 |
if err != nil { |
| 40 |
return resp, err |
| 41 |
} |
| 42 |
|
| 43 |
// Read the original body |
| 44 |
body, err := io.ReadAll(resp.Body) |
| 45 |
if err != nil { |
| 46 |
return resp, err |
| 47 |
} |
| 48 |
_ = resp.Body.Close() |
| 49 |
|
| 50 |
// Create a new body with the appended text |
| 51 |
newBody := string(body) + "\r\noutput by " + name |
| 52 |
|
| 53 |
// Replace the response body |
| 54 |
resp.Body = io.NopCloser(strings.NewReader(newBody)) |
| 55 |
resp.ContentLength = int64(len(newBody)) |
| 56 |
|
| 57 |
return resp, nil |
| 58 |
}), nil |
| 59 |
}, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func newTestClientConfig(name string) configmiddleware.Config { |
| 64 |
return configmiddleware.Config{ |
| 65 |
ID: component.MustNewID(name), |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
func TestClientMiddlewares(t *testing.T) { |
| 70 |
// Create a test server that returns "OK" |
| 71 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 72 |
w.WriteHeader(http.StatusOK) |
| 73 |
_, _ = w.Write([]byte("OK")) |
| 74 |
})) |
| 75 |
defer server.Close() |
| 76 |
|
| 77 |
// Register two test extensions |
| 78 |
extensions := map[component.ID]component.Component{ |
| 79 |
component.MustNewID("test1"): newTestClientMiddleware("test1"), |
| 80 |
component.MustNewID("test2"): newTestClientMiddleware("test2"), |
| 81 |
} |
| 82 |
|
| 83 |
// Test with different middleware configurations |
| 84 |
testCases := []struct { |
| 85 |
name string |
| 86 |
middlewares []configmiddleware.Config |
| 87 |
expectedOutput string |
| 88 |
}{ |
| 89 |
{ |
| 90 |
name: "no_middlewares", |
| 91 |
middlewares: nil, |
| 92 |
expectedOutput: "OK", |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "single_middleware", |
| 96 |
middlewares: []configmiddleware.Config{ |
| 97 |
newTestClientConfig("test1"), |
| 98 |
}, |
| 99 |
expectedOutput: "OK\r\noutput by test1", |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "multiple_middlewares", |
| 103 |
middlewares: []configmiddleware.Config{ |
| 104 |
newTestClientConfig("test1"), |
| 105 |
newTestClientConfig("test2"), |
| 106 |
}, |
| 107 |
expectedOutput: "OK\r\noutput by test2\r\noutput by test1", |
| 108 |
}, |
| 109 |
} |
| 110 |
|
| 111 |
for _, tc := range testCases { |
| 112 |
t.Run(tc.name, func(t *testing.T) { |
| 113 |
// Create HTTP client config with the test middlewares |
| 114 |
clientConfig := ClientConfig{ |
| 115 |
Endpoint: server.URL, |
| 116 |
Middlewares: tc.middlewares, |
| 117 |
} |
| 118 |
|
| 119 |
// Create the client |
| 120 |
client, err := clientConfig.ToClient(context.Background(), extensions, componenttest.NewNopTelemetrySettings()) |
| 121 |
require.NoError(t, err) |
| 122 |
|
| 123 |
// Create a request to the test server |
| 124 |
req, err := http.NewRequest(http.MethodGet, server.URL, http.NoBody) |
| 125 |
require.NoError(t, err) |
| 126 |
|
| 127 |
// Send the request |
| 128 |
resp, err := client.Do(req) |
| 129 |
require.NoError(t, err) |
| 130 |
defer resp.Body.Close() |
| 131 |
|
| 132 |
// Check the response |
| 133 |
body, err := io.ReadAll(resp.Body) |
| 134 |
require.NoError(t, err) |
| 135 |
assert.Equal(t, tc.expectedOutput, string(body)) |
| 136 |
}) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func TestClientMiddlewareErrors(t *testing.T) { |
| 141 |
// Create a test server that returns "OK" |
| 142 |
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 143 |
w.WriteHeader(http.StatusOK) |
| 144 |
_, _ = w.Write([]byte("OK")) |
| 145 |
})) |
| 146 |
defer server.Close() |
| 147 |
|
| 148 |
// Test cases for HTTP client middleware errors |
| 149 |
httpTests := []struct { |
| 150 |
name string |
| 151 |
extensions map[component.ID]component.Component |
| 152 |
config ClientConfig |
| 153 |
errText string |
| 154 |
}{ |
| 155 |
{ |
| 156 |
name: "extension_not_found", |
| 157 |
extensions: map[component.ID]component.Component{}, |
| 158 |
config: ClientConfig{ |
| 159 |
Endpoint: server.URL, |
| 160 |
Middlewares: []configmiddleware.Config{ |
| 161 |
{ |
| 162 |
ID: component.MustNewID("nonexistent"), |
| 163 |
}, |
| 164 |
}, |
| 165 |
}, |
| 166 |
errText: "failed to resolve middleware \"nonexistent\": middleware not found", |
| 167 |
}, |
| 168 |
{ |
| 169 |
name: "get_round_tripper_fails", |
| 170 |
extensions: map[component.ID]component.Component{ |
| 171 |
component.MustNewID("errormw"): extensionmiddlewaretest.NewErr(errors.New("http middleware error")), |
| 172 |
}, |
| 173 |
config: ClientConfig{ |
| 174 |
Endpoint: server.URL, |
| 175 |
Middlewares: []configmiddleware.Config{ |
| 176 |
{ |
| 177 |
ID: component.MustNewID("errormw"), |
| 178 |
}, |
| 179 |
}, |
| 180 |
}, |
| 181 |
errText: "http middleware error", |
| 182 |
}, |
| 183 |
} |
| 184 |
|
| 185 |
for _, tc := range httpTests { |
| 186 |
t.Run(tc.name, func(t *testing.T) { |
| 187 |
// Trying to create the client should fail |
| 188 |
_, err := tc.config.ToClient(context.Background(), tc.extensions, componenttest.NewNopTelemetrySettings()) |
| 189 |
require.Error(t, err) |
| 190 |
assert.Contains(t, err.Error(), tc.errText) |
| 191 |
}) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
// Test failures for gRPC client middlewares by creating a mock implementation |
| 196 |
// that can fail in similar ways to HTTP clients |
| 197 |
func TestGRPCClientMiddlewareErrors(t *testing.T) { |
| 198 |
// Test cases for gRPC client middleware errors |
| 199 |
grpcTests := []struct { |
| 200 |
name string |
| 201 |
extensions map[component.ID]component.Component |
| 202 |
config ClientConfig |
| 203 |
errText string |
| 204 |
}{ |
| 205 |
{ |
| 206 |
name: "grpc_extension_not_found", |
| 207 |
extensions: map[component.ID]component.Component{}, |
| 208 |
config: ClientConfig{ |
| 209 |
Endpoint: "localhost:1234", |
| 210 |
Middlewares: []configmiddleware.Config{ |
| 211 |
{ |
| 212 |
ID: component.MustNewID("nonexistent"), |
| 213 |
}, |
| 214 |
}, |
| 215 |
}, |
| 216 |
errText: "failed to resolve middleware \"nonexistent\": middleware not found", |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "grpc_get_client_options_fails", |
| 220 |
extensions: map[component.ID]component.Component{ |
| 221 |
component.MustNewID("errormw"): extensionmiddlewaretest.NewErr(errors.New("grpc middleware error")), |
| 222 |
}, |
| 223 |
config: ClientConfig{ |
| 224 |
Endpoint: "localhost:1234", |
| 225 |
Middlewares: []configmiddleware.Config{ |
| 226 |
{ |
| 227 |
ID: component.MustNewID("errormw"), |
| 228 |
}, |
| 229 |
}, |
| 230 |
}, |
| 231 |
errText: "grpc middleware error", |
| 232 |
}, |
| 233 |
} |
| 234 |
|
| 235 |
for _, tc := range grpcTests { |
| 236 |
t.Run(tc.name, func(t *testing.T) { |
| 237 |
// For gRPC, we need to use the configgrpc.ClientConfig structure |
| 238 |
// We'll test the middleware failure path here using the HTTP client approach, |
| 239 |
// as the middleware resolution logic is the same |
| 240 |
_, err := tc.config.ToClient(context.Background(), tc.extensions, componenttest.NewNopTelemetrySettings()) |
| 241 |
require.Error(t, err) |
| 242 |
assert.Contains(t, err.Error(), tc.errText) |
| 243 |
}) |
| 244 |
} |
| 245 |
} |