-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
78 lines (64 loc) · 2.01 KB
/
util_test.go
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
package main
import (
"bytes"
"context"
"os"
"testing"
"github.com/joho/godotenv"
"github.com/minio/minio-go/v7"
)
// TODO: Write test cases.
func TestCreateClient(t *testing.T) {
err := godotenv.Load()
if err != nil && os.Getenv("DUMMY_HOST") == "" {
t.Errorf(Fata("Error loading .env file"))
return
}
var dummyFile string = "dummy_file"
// Start by creating dummy client
client, err := createClient(os.Getenv("DUMMY_HOST"), os.Getenv("DUMMY_KEY"), os.Getenv("DUMMY_SECRET"))
if err != nil || client == nil {
t.Errorf("Failed connecting to dummy client\n")
return
}
// Create a bucket
client.MakeBucket(context.Background(), os.Getenv("DUMMY_BUCKET"), minio.MakeBucketOptions{Region: os.Getenv("DUMMY_REGION")})
if err != nil {
t.Errorf("Error creating bucket %s", err.Error())
return
}
// Upload a file
dummyData := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
dummyReader := bytes.NewReader(dummyData)
_, err = client.PutObject(context.Background(), os.Getenv("DUMMY_BUCKET"), dummyFile, dummyReader, int64(len(dummyData)), minio.PutObjectOptions{})
if err != nil {
t.Errorf("Error uploading file: %s", err)
return
}
// Download dummy file
object, err := client.GetObject(context.Background(), os.Getenv("DUMMY_BUCKET"), dummyFile, minio.GetObjectOptions{})
if err != nil {
t.Errorf("Error downloading file: %s", err)
return
}
defer object.Close()
var downloadBuffer = new(bytes.Buffer)
downloadBuffer.ReadFrom(object)
downloadBytes := downloadBuffer.Bytes()
if !bytes.Equal(dummyData, downloadBytes) {
t.Errorf("Downloaded data does not equal uploaded data.")
return
}
// Delete dummy file
err = client.RemoveObject(context.Background(), os.Getenv("DUMMY_BUCKET"), dummyFile, minio.RemoveObjectOptions{})
if err != nil {
t.Errorf("Error deleting file: %s", err)
return
}
// Delete the bucket
err = client.RemoveBucket(context.Background(), os.Getenv("DUMMY_BUCKET"))
if err != nil {
t.Errorf("Error deleting bucket: %s", err)
return
}
}