|
| 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.HTTPClientFunc( |
| 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 | + host := &mockHost{ |
| 79 | + ext: map[component.ID]component.Component{ |
| 80 | + component.MustNewID("test1"): newTestClientMiddleware("test1"), |
| 81 | + component.MustNewID("test2"): newTestClientMiddleware("test2"), |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + // Test with different middleware configurations |
| 86 | + testCases := []struct { |
| 87 | + name string |
| 88 | + middlewares []configmiddleware.Config |
| 89 | + expectedOutput string |
| 90 | + }{ |
| 91 | + { |
| 92 | + name: "no_middlewares", |
| 93 | + middlewares: nil, |
| 94 | + expectedOutput: "OK", |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "single_middleware", |
| 98 | + middlewares: []configmiddleware.Config{ |
| 99 | + newTestClientConfig("test1"), |
| 100 | + }, |
| 101 | + expectedOutput: "OK\r\noutput by test1", |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "multiple_middlewares", |
| 105 | + middlewares: []configmiddleware.Config{ |
| 106 | + newTestClientConfig("test1"), |
| 107 | + newTestClientConfig("test2"), |
| 108 | + }, |
| 109 | + expectedOutput: "OK\r\noutput by test2\r\noutput by test1", |
| 110 | + }, |
| 111 | + } |
| 112 | + |
| 113 | + for _, tc := range testCases { |
| 114 | + t.Run(tc.name, func(t *testing.T) { |
| 115 | + // Create HTTP client config with the test middlewares |
| 116 | + clientConfig := ClientConfig{ |
| 117 | + Endpoint: server.URL, |
| 118 | + Middlewares: tc.middlewares, |
| 119 | + } |
| 120 | + |
| 121 | + // Create the client |
| 122 | + client, err := clientConfig.ToClient(context.Background(), host, componenttest.NewNopTelemetrySettings()) |
| 123 | + require.NoError(t, err) |
| 124 | + |
| 125 | + // Create a request to the test server |
| 126 | + req, err := http.NewRequest(http.MethodGet, server.URL, nil) |
| 127 | + require.NoError(t, err) |
| 128 | + |
| 129 | + // Send the request |
| 130 | + resp, err := client.Do(req) |
| 131 | + require.NoError(t, err) |
| 132 | + defer resp.Body.Close() |
| 133 | + |
| 134 | + // Check the response |
| 135 | + body, err := io.ReadAll(resp.Body) |
| 136 | + require.NoError(t, err) |
| 137 | + assert.Equal(t, tc.expectedOutput, string(body)) |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestClientMiddlewareErrors(t *testing.T) { |
| 143 | + // Create a test server that returns "OK" |
| 144 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 145 | + w.WriteHeader(http.StatusOK) |
| 146 | + _, _ = w.Write([]byte("OK")) |
| 147 | + })) |
| 148 | + defer server.Close() |
| 149 | + |
| 150 | + // Test cases for HTTP client middleware errors |
| 151 | + httpTests := []struct { |
| 152 | + name string |
| 153 | + host component.Host |
| 154 | + config ClientConfig |
| 155 | + errText string |
| 156 | + }{ |
| 157 | + { |
| 158 | + name: "extension_not_found", |
| 159 | + host: &mockHost{ |
| 160 | + ext: map[component.ID]component.Component{}, |
| 161 | + }, |
| 162 | + config: ClientConfig{ |
| 163 | + Endpoint: server.URL, |
| 164 | + Middlewares: []configmiddleware.Config{ |
| 165 | + { |
| 166 | + ID: component.MustNewID("nonexistent"), |
| 167 | + }, |
| 168 | + }, |
| 169 | + }, |
| 170 | + errText: "failed to resolve middleware \"nonexistent\": middleware not found", |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "get_round_tripper_fails", |
| 174 | + host: &mockHost{ |
| 175 | + ext: map[component.ID]component.Component{ |
| 176 | + component.MustNewID("errormw"): extensionmiddlewaretest.NewErr(errors.New("http middleware error")), |
| 177 | + }, |
| 178 | + }, |
| 179 | + config: ClientConfig{ |
| 180 | + Endpoint: server.URL, |
| 181 | + Middlewares: []configmiddleware.Config{ |
| 182 | + { |
| 183 | + ID: component.MustNewID("errormw"), |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + errText: "http middleware error", |
| 188 | + }, |
| 189 | + } |
| 190 | + |
| 191 | + for _, tc := range httpTests { |
| 192 | + t.Run(tc.name, func(t *testing.T) { |
| 193 | + // Trying to create the client should fail |
| 194 | + _, err := tc.config.ToClient(context.Background(), tc.host, componenttest.NewNopTelemetrySettings()) |
| 195 | + require.Error(t, err) |
| 196 | + assert.Contains(t, err.Error(), tc.errText) |
| 197 | + }) |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +// Test failures for gRPC client middlewares by creating a mock implementation |
| 202 | +// that can fail in similar ways to HTTP clients |
| 203 | +func TestGRPCClientMiddlewareErrors(t *testing.T) { |
| 204 | + // Test cases for gRPC client middleware errors |
| 205 | + grpcTests := []struct { |
| 206 | + name string |
| 207 | + host component.Host |
| 208 | + config ClientConfig |
| 209 | + errText string |
| 210 | + }{ |
| 211 | + { |
| 212 | + name: "grpc_extension_not_found", |
| 213 | + host: &mockHost{ |
| 214 | + ext: map[component.ID]component.Component{}, |
| 215 | + }, |
| 216 | + config: ClientConfig{ |
| 217 | + Endpoint: "localhost:1234", |
| 218 | + Middlewares: []configmiddleware.Config{ |
| 219 | + { |
| 220 | + ID: component.MustNewID("nonexistent"), |
| 221 | + }, |
| 222 | + }, |
| 223 | + }, |
| 224 | + errText: "failed to resolve middleware \"nonexistent\": middleware not found", |
| 225 | + }, |
| 226 | + { |
| 227 | + name: "grpc_get_client_options_fails", |
| 228 | + host: &mockHost{ |
| 229 | + ext: map[component.ID]component.Component{ |
| 230 | + component.MustNewID("errormw"): extensionmiddlewaretest.NewErr(errors.New("grpc middleware error")), |
| 231 | + }, |
| 232 | + }, |
| 233 | + config: ClientConfig{ |
| 234 | + Endpoint: "localhost:1234", |
| 235 | + Middlewares: []configmiddleware.Config{ |
| 236 | + { |
| 237 | + ID: component.MustNewID("errormw"), |
| 238 | + }, |
| 239 | + }, |
| 240 | + }, |
| 241 | + errText: "grpc middleware error", |
| 242 | + }, |
| 243 | + } |
| 244 | + |
| 245 | + for _, tc := range grpcTests { |
| 246 | + t.Run(tc.name, func(t *testing.T) { |
| 247 | + // For gRPC, we need to use the configgrpc.ClientConfig structure |
| 248 | + // We'll test the middleware failure path here using the HTTP client approach, |
| 249 | + // as the middleware resolution logic is the same |
| 250 | + _, err := tc.config.ToClient(context.Background(), tc.host, componenttest.NewNopTelemetrySettings()) |
| 251 | + require.Error(t, err) |
| 252 | + assert.Contains(t, err.Error(), tc.errText) |
| 253 | + }) |
| 254 | + } |
| 255 | +} |
0 commit comments