Skip to content

Commit

Permalink
Add tests for role_arn
Browse files Browse the repository at this point in the history
  • Loading branch information
bonclay7 committed Mar 12, 2021
1 parent 3cf3439 commit 5059b56
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
64 changes: 39 additions & 25 deletions exporter/awsprometheusremotewriteexporter/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,51 @@ import (
)

func TestRequestSignature(t *testing.T) {
// Some form of AWS credentials must be set up for tests to succeed
awsCreds := fetchMockCredentials()
authConfig := AuthConfig{Region: "region", Service: "service"}

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := v4.GetSignedRequestSignature(r)
assert.NoError(t, err)
w.WriteHeader(200)
}))
defer server.Close()
tests := []struct {
authConfig AuthConfig
}{
{
AuthConfig{Region: "region", Service: "service"},
},
{
AuthConfig{Region: "region", Service: "service", RoleArn: "arn:aws:iam::123456789012:role/IAMRole"},
},
}

serverURL, err := url.Parse(server.URL)
assert.NoError(t, err)
for _, tt := range tests {
// Some form of AWS credentials must be set up for tests to succeed
awsCreds := fetchMockCredentials()
authConfig := tt.authConfig
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := v4.GetSignedRequestSignature(r)
assert.NoError(t, err)
w.WriteHeader(200)
}))
defer server.Close()

setting := confighttp.HTTPClientSettings{
Endpoint: serverURL.String(),
TLSSetting: configtls.TLSClientSetting{},
ReadBufferSize: 0,
WriteBufferSize: 0,
Timeout: 0,
CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) {
return createSigningRoundTripperWithCredentials(authConfig, awsCreds, next)
},
serverURL, err := url.Parse(server.URL)
assert.NoError(t, err)

setting := confighttp.HTTPClientSettings{
Endpoint: serverURL.String(),
TLSSetting: configtls.TLSClientSetting{},
ReadBufferSize: 0,
WriteBufferSize: 0,
Timeout: 0,
CustomRoundTripper: func(next http.RoundTripper) (http.RoundTripper, error) {
return createSigningRoundTripperWithCredentials(authConfig, awsCreds, next)
},
}
client, _ := setting.ToClient()
req, err := http.NewRequest("POST", setting.Endpoint, strings.NewReader("a=1&b=2"))
assert.NoError(t, err)
_, err = client.Do(req)
assert.NoError(t, err)
}
client, _ := setting.ToClient()
req, err := http.NewRequest("POST", setting.Endpoint, strings.NewReader("a=1&b=2"))
assert.NoError(t, err)
_, err = client.Do(req)
assert.NoError(t, err)
}


type ErrorRoundTripper struct{}

func (ert *ErrorRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
Expand Down
1 change: 1 addition & 0 deletions exporter/awsprometheusremotewriteexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func TestLoadConfig(t *testing.T) {
AuthConfig: AuthConfig{
Region: "us-west-2",
Service: "service-name",
RoleArn: "arn:aws:iam::123456789012:role/IAMRole",
},
}
// testing function equality is not supported in Go hence these will be ignored for this test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
receivers:
nop:

processors:
nop:

exporters:
awsprometheusremotewrite:
awsprometheusremotewrite/2:
Expand All @@ -26,6 +26,7 @@ exporters:
aws_auth:
region: "us-west-2"
service: "service-name"
role_arn: "arn:aws:iam::123456789012:role/IAMRole"
external_labels:
key1: value1
key2: value2
Expand All @@ -35,5 +36,5 @@ service:
receivers: [nop]
processors: [nop]
exporters: [awsprometheusremotewrite]


0 comments on commit 5059b56

Please sign in to comment.