|
4 | 4 | package aws |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "bytes" |
| 8 | + "log" |
| 9 | + "os" |
7 | 10 | "testing" |
8 | 11 |
|
9 | | - "github.com/aws/aws-sdk-go/aws" |
| 12 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 13 | + "github.com/aws/smithy-go/logging" |
| 14 | + "github.com/stretchr/testify/assert" |
10 | 15 | ) |
11 | 16 |
|
12 | 17 | func TestSetSDKLogLevel(t *testing.T) { |
13 | | - cases := []struct { |
14 | | - sdkLogLevelString string |
15 | | - expectedVal aws.LogLevelType |
| 18 | + testCases := []struct { |
| 19 | + input string |
| 20 | + want aws.ClientLogMode |
16 | 21 | }{ |
17 | | - // sdkLogLevelString does not match |
18 | | - {"FOO", aws.LogOff}, |
| 22 | + // Invalid input |
| 23 | + {input: "FOO", want: aws.ClientLogMode(0)}, |
19 | 24 | // Wrong case. |
20 | | - {"logDEBUG", aws.LogOff}, |
| 25 | + {input: "logrequest", want: aws.ClientLogMode(0)}, |
21 | 26 | // Extra char. |
22 | | - {"LogDebug1", aws.LogOff}, |
| 27 | + {input: "LogRequest1", want: aws.ClientLogMode(0)}, |
23 | 28 | // Single match. |
24 | | - {"LogDebug", aws.LogDebug}, |
25 | | - {"LogDebugWithEventStreamBody", aws.LogDebugWithEventStreamBody}, |
26 | | - {"LogDebugWithHTTPBody", aws.LogDebugWithHTTPBody}, |
27 | | - {"LogDebugWithRequestRetries", aws.LogDebugWithRequestRetries}, |
28 | | - {"LogDebugWithRequestErrors", aws.LogDebugWithRequestErrors}, |
29 | | - {"LogDebugWithEventStreamBody", aws.LogDebugWithEventStreamBody}, |
| 29 | + {input: "LogRequest", want: aws.LogRequest}, |
| 30 | + {input: "LogResponse", want: aws.LogResponse}, |
| 31 | + {input: "LogSigning", want: aws.LogSigning}, |
| 32 | + {input: "LogRequestWithBody", want: aws.LogRequestWithBody}, |
| 33 | + {input: "LogResponseWithBody", want: aws.LogResponseWithBody}, |
| 34 | + {input: "LogRetries", want: aws.LogRetries}, |
| 35 | + {input: "LogRequestEventMessage", want: aws.LogRequestEventMessage}, |
| 36 | + {input: "LogResponseEventMessage", want: aws.LogResponseEventMessage}, |
| 37 | + {input: "LogDeprecatedUsage", want: aws.LogDeprecatedUsage}, |
| 38 | + // v1 compatibility |
| 39 | + {input: "LogDebug", want: aws.LogRequest | aws.LogResponse}, |
| 40 | + {input: "LogDebugWithSigning", want: aws.LogRequest | aws.LogResponse | aws.LogSigning}, |
| 41 | + {input: "LogDebugWithHTTPBody", want: aws.LogRequestWithBody | aws.LogResponseWithBody}, |
| 42 | + {input: "LogDebugWithRequestRetries", want: aws.LogRequest | aws.LogResponse | aws.LogRetries}, |
| 43 | + {input: "LogDebugWithRequestErrors", want: aws.LogRequest | aws.LogResponse}, |
| 44 | + {input: "LogDebugWithEventStreamBody", want: aws.LogRequestEventMessage | aws.LogResponseEventMessage}, |
30 | 45 | // Extra space around is allowed. |
31 | | - {" LogDebug ", aws.LogDebug}, |
| 46 | + {input: " LogRequest ", want: aws.LogRequest}, |
32 | 47 | // Multiple matches. |
33 | | - {"LogDebugWithEventStreamBody|LogDebugWithHTTPBody", |
34 | | - aws.LogDebugWithEventStreamBody | aws.LogDebugWithHTTPBody}, |
35 | | - {" LogDebugWithHTTPBody | LogDebugWithEventStreamBody ", |
36 | | - aws.LogDebugWithEventStreamBody | aws.LogDebugWithHTTPBody}, |
37 | | - {"LogDebugWithRequestRetries|LogDebugWithEventStreamBody", |
38 | | - aws.LogDebugWithEventStreamBody | aws.LogDebugWithRequestRetries}, |
39 | | - {"LogDebugWithRequestRetries|LogDebugWithRequestErrors", |
40 | | - aws.LogDebugWithRequestRetries | aws.LogDebugWithRequestErrors}, |
41 | | - {"LogDebugWithRequestRetries|LogDebugWithRequestErrors|LogDebugWithEventStreamBody", |
42 | | - aws.LogDebugWithRequestRetries | aws.LogDebugWithRequestErrors | aws.LogDebugWithEventStreamBody}, |
| 48 | + {input: "LogRequest|LogResponse", want: aws.LogRequest | aws.LogResponse}, |
| 49 | + {input: " LogRequestWithBody | LogResponseWithBody ", want: aws.LogRequestWithBody | aws.LogResponseWithBody}, |
| 50 | + {input: "LogRetries|LogSigning", want: aws.LogRetries | aws.LogSigning}, |
| 51 | + {input: "LogRequest|LogResponse|LogSigning", want: aws.LogRequest | aws.LogResponse | aws.LogSigning}, |
43 | 52 | } |
44 | 53 |
|
45 | | - for _, tc := range cases { |
46 | | - SetSDKLogLevel(tc.sdkLogLevelString) |
47 | | - // check the internal var |
48 | | - if *SDKLogLevel() != tc.expectedVal { |
49 | | - t.Errorf("input: %v, actual: %v", tc, sdkLogLevel) |
50 | | - } |
| 54 | + for _, testCase := range testCases { |
| 55 | + SetSDKLogLevel(testCase.input) |
| 56 | + assert.Equal(t, testCase.want, SDKLogLevel()) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestSDKLogger(t *testing.T) { |
| 61 | + var buf bytes.Buffer |
| 62 | + log.SetOutput(&buf) |
| 63 | + defer log.SetOutput(os.Stderr) |
| 64 | + |
| 65 | + logger := SDKLogger{} |
| 66 | + |
| 67 | + tests := []struct { |
| 68 | + classification logging.Classification |
| 69 | + expectedPrefix string |
| 70 | + }{ |
| 71 | + {classification: logging.Debug, expectedPrefix: "D!"}, |
| 72 | + {classification: logging.Warn, expectedPrefix: "W!"}, |
| 73 | + {classification: logging.Classification("TEST"), expectedPrefix: "I!"}, |
| 74 | + } |
| 75 | + |
| 76 | + for _, tt := range tests { |
| 77 | + t.Run(string(tt.classification), func(t *testing.T) { |
| 78 | + buf.Reset() |
| 79 | + logger.Logf(tt.classification, "test message: %s", "arg") |
| 80 | + |
| 81 | + output := buf.String() |
| 82 | + assert.Contains(t, output, tt.expectedPrefix) |
| 83 | + assert.Contains(t, output, "test message: arg") |
| 84 | + }) |
51 | 85 | } |
52 | 86 | } |
0 commit comments