forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for Custom Transport on Ruler s3 Client (grafana#2891)
* stuff Signed-off-by: Joe Elliott <number101010@gmail.com> * Added Middleware test Signed-off-by: Joe Elliott <number101010@gmail.com> * Better naming. Split New methods to not break existing calls Signed-off-by: Joe Elliott <number101010@gmail.com> * Added top level rule storage module Signed-off-by: Joe Elliott <number101010@gmail.com> * Fixed test Signed-off-by: Joe Elliott <number101010@gmail.com> * Don't start ruler or rulerstorage if not configured Signed-off-by: Joe Elliott <number101010@gmail.com> * Made injectRequestMiddleware a configuration option Signed-off-by: Joe Elliott <number101010@gmail.com>
- Loading branch information
1 parent
a673f74
commit 0ed1041
Showing
2 changed files
with
104 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type RoundTripperFunc func(*http.Request) (*http.Response, error) | ||
|
||
func (f RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) { | ||
return f(req) | ||
} | ||
|
||
func TestRequestMiddleware(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintln(w, r.Header.Get("echo-me")) | ||
})) | ||
defer ts.Close() | ||
|
||
cfg := S3Config{ | ||
Endpoint: ts.URL, | ||
BucketNames: "buck-o", | ||
S3ForcePathStyle: true, | ||
Insecure: true, | ||
AccessKeyID: "key", | ||
SecretAccessKey: "secret", | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
fn InjectRequestMiddleware | ||
expected string | ||
}{ | ||
{ | ||
name: "Test Nil", | ||
fn: nil, | ||
expected: "", | ||
}, | ||
{ | ||
name: "Test Header Injection", | ||
fn: func(next http.RoundTripper) http.RoundTripper { | ||
return RoundTripperFunc(func(req *http.Request) (*http.Response, error) { | ||
req.Header["echo-me"] = []string{"blerg"} | ||
return next.RoundTrip(req) | ||
}) | ||
}, | ||
expected: "blerg", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
cfg.Inject = tt.fn | ||
client, err := NewS3ObjectClient(cfg, "/") | ||
require.NoError(t, err) | ||
|
||
readCloser, err := client.GetObject(context.Background(), "key") | ||
require.NoError(t, err) | ||
|
||
buffer := make([]byte, 100) | ||
_, err = readCloser.Read(buffer) | ||
if err != io.EOF { | ||
require.NoError(t, err) | ||
} | ||
|
||
assert.Equal(t, tt.expected, strings.Trim(string(buffer), "\n\x00")) | ||
}) | ||
} | ||
} |