-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy paths3_client_test.go
More file actions
81 lines (70 loc) · 1.96 KB
/
s3_client_test.go
File metadata and controls
81 lines (70 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright 2022-present Kuei-chun Chen. All rights reserved.
* s3_client_test.go
*/
package hatchet
import (
"bytes"
"fmt"
"os"
"testing"
)
const (
S3Profile = "default"
testEndpoint = "http://localhost:9090"
testRegion = "us-east-1"
testAccessKey = "test-access-key"
testSecretKey = "test-secret-key"
testBucket = "test-bucket"
testKey = "test-key"
)
func TestAWS(t *testing.T) {
_, err := NewS3Client(S3Profile)
if err != nil {
t.Skipf("skipping test: AWS not configured: %v", err)
}
}
func TestNewS3Client(t *testing.T) {
s3client, err := NewS3Client(S3Profile, testEndpoint)
if err != nil {
t.Skipf("skipping test: S3 client not available: %v", err)
}
// create a new S3 bucket
bucketName := "test-bucket"
s3client.DeleteBucket(bucketName) // just in case
err = s3client.CreateBucket(bucketName)
if err != nil {
t.Skipf("skipping test: S3 bucket creation failed (S3 endpoint not available): %v", err)
}
// upload a file to S3
fileName := "test-file.txt"
filePath := "./testdata/" + fileName
err = s3client.PutObject(bucketName, fileName, filePath)
if err != nil {
t.Fatalf("failed to upload file to S3: %v", err)
}
// download the file from S3
var buf []byte
buf, err = s3client.GetObject(fmt.Sprintf("%v/%v", bucketName, fileName))
if err != nil {
t.Fatalf("failed to download file from S3: %v", err)
}
// check that the contents of the downloaded file match the original file
originalBytes, err := os.ReadFile(filePath)
if err != nil {
t.Fatalf("failed to read original file: %v", err)
}
if !bytes.Equal(buf, originalBytes) {
t.Fatalf("file contents do not match: expected '%s', got '%s'", string(originalBytes), string(buf))
}
t.Log(string(buf))
// delete the file from S3
err = s3client.DeleteObject(bucketName, fileName)
if err != nil {
t.Fatalf("failed to delete file from S3: %v", err)
}
err = s3client.DeleteBucket(bucketName)
if err != nil {
t.Fatalf("failed to delete S3 bucket: %v", err)
}
}