test: fix flaky S3 volume encryption test (#8083)
Specifically: - Use bytes.NewReader for binary data instead of strings.NewReader - Increase binary test data from 8 bytes to 1KB to avoid edge cases - Add 50ms delay between subtests to prevent overwhelming the server
This commit is contained in:
@@ -42,6 +42,8 @@ func TestS3VolumeEncryptionRoundtrip(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
key string
|
key string
|
||||||
content string
|
content string
|
||||||
|
binaryContent []byte
|
||||||
|
isBinary bool
|
||||||
rangeReq string // Optional range request
|
rangeReq string // Optional range request
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -57,7 +59,15 @@ func TestS3VolumeEncryptionRoundtrip(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "binary content",
|
name: "binary content",
|
||||||
key: "binary.bin",
|
key: "binary.bin",
|
||||||
content: string([]byte{0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD, 0x00, 0x80}),
|
binaryContent: func() []byte {
|
||||||
|
// Create a 1KB binary file with predictable pattern including null bytes
|
||||||
|
data := make([]byte, 1024)
|
||||||
|
for i := range data {
|
||||||
|
data[i] = byte(i % 256)
|
||||||
|
}
|
||||||
|
return data
|
||||||
|
}(),
|
||||||
|
isBinary: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "range request",
|
name: "range request",
|
||||||
@@ -68,12 +78,26 @@ func TestS3VolumeEncryptionRoundtrip(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
tc := tc // Capture range variable for closure
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
// Upload
|
// Add small delay to avoid rapid consecutive requests during volume encryption
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
|
||||||
|
// Upload - use appropriate reader for binary vs text content
|
||||||
|
var uploadBody io.ReadSeeker
|
||||||
|
var expectedData []byte
|
||||||
|
if tc.isBinary {
|
||||||
|
uploadBody = bytes.NewReader(tc.binaryContent)
|
||||||
|
expectedData = tc.binaryContent
|
||||||
|
} else {
|
||||||
|
uploadBody = strings.NewReader(tc.content)
|
||||||
|
expectedData = []byte(tc.content)
|
||||||
|
}
|
||||||
|
|
||||||
_, err := svc.PutObject(&s3.PutObjectInput{
|
_, err := svc.PutObject(&s3.PutObjectInput{
|
||||||
Bucket: aws.String(bucket),
|
Bucket: aws.String(bucket),
|
||||||
Key: aws.String(tc.key),
|
Key: aws.String(tc.key),
|
||||||
Body: strings.NewReader(tc.content),
|
Body: uploadBody,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("PutObject failed: %v", err)
|
t.Fatalf("PutObject failed: %v", err)
|
||||||
@@ -100,14 +124,14 @@ func TestS3VolumeEncryptionRoundtrip(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Verify content
|
// Verify content
|
||||||
expected := tc.content
|
expected := expectedData
|
||||||
if tc.rangeReq != "" {
|
if tc.rangeReq != "" {
|
||||||
// For "bytes=5-10", we expect characters at positions 5-10 (inclusive)
|
// For "bytes=5-10", we expect bytes at positions 5-10 (inclusive)
|
||||||
expected = tc.content[5:11]
|
expected = expectedData[5:11]
|
||||||
}
|
}
|
||||||
|
|
||||||
if string(data) != expected {
|
if !bytes.Equal(data, expected) {
|
||||||
t.Errorf("Content mismatch:\n expected: %q\n got: %q", expected, string(data))
|
t.Errorf("Content mismatch:\n expected: %v\n got: %v", expected, data)
|
||||||
} else {
|
} else {
|
||||||
t.Logf("Successfully uploaded and downloaded %s (%d bytes)", tc.key, len(data))
|
t.Logf("Successfully uploaded and downloaded %s (%d bytes)", tc.key, len(data))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user