Skip to content

Commit

Permalink
reorder feature id and simplify metric tracking test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianyi Wang committed Dec 18, 2024
1 parent 4dd0258 commit 25d943c
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 76 deletions.
8 changes: 4 additions & 4 deletions aws/middleware/user_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ const (
UserAgentFeatureS3AccessGrants = "K" // not yet implemented
UserAgentFeatureGZIPRequestCompression = "L"
UserAgentFeatureProtocolRPCV2CBOR = "M"
UserAgentFeatureRequestChecksumWhenSupported = "Z"
UserAgentFeatureRequestChecksumWhenRequired = "a"
UserAgentFeatureResponseChecksumWhenSupported = "b"
UserAgentFeatureResponseChecksumWhenRequired = "c"
UserAgentFeatureRequestChecksumCRC32 = "U"
UserAgentFeatureRequestChecksumCRC32C = "V"
UserAgentFeatureRequestChecksumCRC64 = "W"
UserAgentFeatureRequestChecksumSHA1 = "X"
UserAgentFeatureRequestChecksumSHA256 = "Y"
UserAgentFeatureRequestChecksumWhenSupported = "Z"
UserAgentFeatureRequestChecksumWhenRequired = "a"
UserAgentFeatureResponseChecksumWhenSupported = "b"
UserAgentFeatureResponseChecksumWhenRequired = "c"
)

// RequestUserAgent is a build middleware that set the User-Agent for the request.
Expand Down
2 changes: 1 addition & 1 deletion feature/s3/manager/integ_upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func TestInteg_UploadPresetChecksum(t *testing.T) {
},
},
"auto multipart part": {
"no checksum": {
"no checksum algorithm passed": {
payload: bytes.NewReader(multiPartBytes),
expectParts: []s3types.CompletedPart{
{
Expand Down
4 changes: 2 additions & 2 deletions service/internal/checksum/algorithms_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build go1.16
// +build go1.16
//go:build go1.21
// +build go1.21

package checksum

Expand Down
4 changes: 2 additions & 2 deletions service/internal/checksum/aws_chunked_encoding_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build go1.16
// +build go1.16
//go:build go1.21
// +build go1.21

package checksum

Expand Down
4 changes: 2 additions & 2 deletions service/internal/checksum/middleware_add_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build go1.16
// +build go1.16
//go:build go1.21
// +build go1.21

package checksum

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build go1.16
// +build go1.16
//go:build go1.21
// +build go1.21

package checksum

Expand All @@ -10,56 +10,10 @@ import (
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
"net/http"
"runtime"
"strings"
"testing"
)

var expectedAgent = aws.SDKName + "/" + aws.SDKVersion +
" ua/2.1" +
" os/" + getNormalizedOSName() +
" lang/go#" + strings.Map(rules, languageVersion) + // normalize as the user-agent builder will
" md/GOOS#" + runtime.GOOS +
" md/GOARCH#" + runtime.GOARCH

var languageVersion = strings.TrimPrefix(runtime.Version(), "go")

func getNormalizedOSName() (os string) {
switch runtime.GOOS {
case "android":
os = "android"
case "linux":
os = "linux"
case "windows":
os = "windows"
case "darwin":
os = "macos"
case "ios":
os = "ios"
default:
os = "other"
}
return os
}

var validChars = map[rune]bool{
'!': true, '#': true, '$': true, '%': true, '&': true, '\'': true, '*': true, '+': true,
'-': true, '.': true, '^': true, '_': true, '`': true, '|': true, '~': true,
}

func rules(r rune) rune {
switch {
case r >= '0' && r <= '9':
return r
case r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z':
return r
case validChars[r]:
return r
default:
return '-'
}
}

func TestRequestChecksumMetricsTracking(t *testing.T) {
cases := map[string]struct {
requestChecksumCalculation aws.RequestChecksumCalculation
Expand All @@ -69,34 +23,34 @@ func TestRequestChecksumMetricsTracking(t *testing.T) {
"default": {
requestChecksumCalculation: aws.RequestChecksumCalculationWhenSupported,
reqHeaders: map[string][]string{},
expectedUserAgentHeader: expectedAgent + " m/Z",
expectedUserAgentHeader: " m/Z",
},
"calculate checksum when required": {
requestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired,
reqHeaders: map[string][]string{},
expectedUserAgentHeader: expectedAgent + " m/a",
expectedUserAgentHeader: " m/a",
},
"default with crc32 checksum": {
requestChecksumCalculation: aws.RequestChecksumCalculationWhenSupported,
reqHeaders: map[string][]string{
"X-Amz-Checksum-Crc32": {"aa"},
},
expectedUserAgentHeader: expectedAgent + " m/U,Z",
expectedUserAgentHeader: " m/U,Z",
},
"calculate checksum when required with sha256 checksum": {
requestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired,
reqHeaders: map[string][]string{
"X-Amz-Checksum-Sha256": {"aa"},
},
expectedUserAgentHeader: expectedAgent + " m/Y,a",
expectedUserAgentHeader: " m/Y,a",
},
"default with crc32c and crc64": {
requestChecksumCalculation: aws.RequestChecksumCalculationWhenSupported,
reqHeaders: map[string][]string{
"X-Amz-Checksum-Crc32c": {"aa"},
"X-Amz-Checksum-Crc64nvme": {"aa"},
},
expectedUserAgentHeader: expectedAgent + " m/V,W,Z",
expectedUserAgentHeader: " m/V,W,Z",
},
}

Expand All @@ -120,8 +74,8 @@ func TestRequestChecksumMetricsTracking(t *testing.T) {
return
}))

if e, a := c.expectedUserAgentHeader, req.Header["User-Agent"][0]; e != a {
t.Errorf("expected user agent header to be %s, got %s", e, a)
if e, a := c.expectedUserAgentHeader, req.Header["User-Agent"][0]; !strings.Contains(a, e) {
t.Errorf("expected user agent header to include %s, got %s", e, a)
}
})
}
Expand All @@ -134,11 +88,11 @@ func TestResponseChecksumMetricsTracking(t *testing.T) {
}{
"default": {
responseChecksumValidation: aws.ResponseChecksumValidationWhenSupported,
expectedUserAgentHeader: expectedAgent + " m/b",
expectedUserAgentHeader: " m/b",
},
"validate checksum when required": {
responseChecksumValidation: aws.ResponseChecksumValidationWhenRequired,
expectedUserAgentHeader: expectedAgent + " m/c",
expectedUserAgentHeader: " m/c",
},
}

Expand All @@ -161,8 +115,8 @@ func TestResponseChecksumMetricsTracking(t *testing.T) {
return
}))

if e, a := c.expectedUserAgentHeader, req.Header["User-Agent"][0]; e != a {
t.Errorf("expected user agent header to be %s, got %s", e, a)
if e, a := c.expectedUserAgentHeader, req.Header["User-Agent"][0]; !strings.Contains(a, e) {
t.Errorf("expected user agent header to contain %s, got %s", e, a)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build go1.16
// +build go1.16
//go:build go1.21
// +build go1.21

package checksum

Expand Down
4 changes: 2 additions & 2 deletions service/internal/checksum/middleware_setup_context_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build go1.16
// +build go1.16
//go:build go1.21
// +build go1.21

package checksum

Expand Down
4 changes: 2 additions & 2 deletions service/internal/checksum/middleware_validate_output_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//go:build go1.16
// +build go1.16
//go:build go1.21
// +build go1.21

package checksum

Expand Down

0 comments on commit 25d943c

Please sign in to comment.