This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontainer_test.go
119 lines (105 loc) · 3 KB
/
container_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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package api
import (
"bytes"
"crypto/md5"
"fmt"
"testing"
"time"
)
func generateRandomBucketName() string {
return fmt.Sprintf("scaleway-test-%d-%s", time.Now().Unix(), fmt.Sprintf("%x", md5.Sum([]byte(time.Now().String()))))
}
func TestScalewaAPI_ManageBucket(t *testing.T) {
client, err := buildClient("ams1")
if err != nil {
t.Fatalf("Unable to create a client for AMS1: %v", err.Error())
}
desiredBucketName := generateRandomBucketName()
bucket, err := client.CreateBucket(&CreateBucketRequest{
Name: desiredBucketName,
Organization: client.Organization,
})
if err != nil {
t.Fatalf("Expected request to succeed, but didn't: %v", err.Error())
}
defer func() {
client.DeleteBucket(desiredBucketName)
}()
if bucket.Name != desiredBucketName {
t.Errorf("Expected bucket name of %s, but got %s", desiredBucketName, bucket.Name)
}
if bucket.NumberOfObjects != 0 || bucket.Size != 0 {
t.Errorf("Expected to create a fresh bucket, but got %d items, %d size", bucket.NumberOfObjects, bucket.Size)
}
_, err = client.ListObjects(bucket.Name)
if err != nil {
t.Fatalf("Expected request to succeed, but didn't: %v", err.Error())
}
}
func TestScalewaAPI_ManageObject(t *testing.T) {
client, err := buildClient("ams1")
if err != nil {
t.Fatalf("Unable to create a client for AMS1: %v", err.Error())
}
bucket, err := client.CreateBucket(&CreateBucketRequest{
Name: generateRandomBucketName(),
Organization: client.Organization,
})
if err != nil {
t.Fatalf("test requires bucket, but creation failed: %v", err.Error())
}
defer func() {
client.DeleteBucket(bucket.Name)
}()
desiredName := "just-a-test"
t.Run("Put object", func(t *testing.T) {
tmpfile := bytes.NewBuffer([]byte("temporary file's content"))
object, err := client.PutObject(&PutObjectRequest{
BucketName: bucket.Name,
ObjectName: desiredName,
}, tmpfile)
if err != nil {
t.Fatalf(err.Error())
}
if object == nil {
t.Fatalf("Expected object, but got %v", object)
}
if object.Name != desiredName {
t.Fatalf("Expected %s as name, but got %s", desiredName, object.Name)
}
})
t.Run("Get object", func(t *testing.T) {
object, err := client.GetObject(bucket.Name, desiredName)
if err != nil {
t.Fatalf(err.Error())
}
if object == nil {
t.Fatalf("Expected object, but got %v", object)
}
if object.Name != desiredName {
t.Fatalf("Expected %s as name, but got %s", desiredName, object.Name)
}
})
t.Run("List objects", func(t *testing.T) {
containers, err := client.ListObjects(bucket.Name)
if err != nil {
t.Fatalf(err.Error())
}
if len(containers) != 1 {
t.Fatalf("Expected 1 object, got %d", len(containers))
}
})
t.Run("Delete objects", func(t *testing.T) {
err := client.DeleteObject(bucket.Name, desiredName)
if err != nil {
t.Fatalf(err.Error())
}
containers, err := client.ListObjects(bucket.Name)
if err != nil {
t.Fatalf(err.Error())
}
if len(containers) != 0 {
t.Fatalf("Expected 0 object, got %d", len(containers))
}
})
}