forked from FiloSottile/b2
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathb2_test.go
141 lines (124 loc) · 2.64 KB
/
b2_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package b2_test
import (
"crypto/rand"
"crypto/tls"
"encoding/hex"
"errors"
"flag"
"log"
"net/http"
"os"
"strings"
"sync"
"testing"
"github.com/FiloSottile/b2"
)
var client *b2.Client
var clientMu sync.Mutex
func getClient(t *testing.T) *b2.Client {
accountID := os.Getenv("ACCOUNT_ID")
applicationKey := os.Getenv("APPLICATION_KEY")
if accountID == "" || applicationKey == "" {
t.Fatal("Missing ACCOUNT_ID or APPLICATION_KEY")
}
clientMu.Lock()
defer clientMu.Unlock()
if client != nil {
return client
}
c, err := b2.NewClient(accountID, applicationKey, &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
})
if err != nil {
t.Fatal("While authenticating:", err)
}
client = c
return c
}
var cleanup = flag.Bool("cleanup", false, "Delete all test-* buckets on start.")
func TestMain(m *testing.M) {
flag.Parse()
if *cleanup {
c := getClient(nil)
buckets, err := c.Buckets()
if err != nil {
log.Fatal(err)
}
for _, b := range buckets {
if !strings.HasPrefix(b.Name, "test-") {
continue
}
log.Println("Deleting bucket", b.Name)
l := b.ListFilesVersions("", "")
for l.Next() {
fi := l.FileInfo()
if err := c.DeleteFile(fi.ID, fi.Name); err != nil {
log.Fatal(err)
}
}
if err := l.Err(); err != nil {
log.Fatal(err)
}
if err := b.Delete(); err != nil {
log.Fatal(err)
}
}
}
os.Exit(m.Run())
}
func TestBucketLifecycle(t *testing.T) {
c := getClient(t)
r := make([]byte, 6)
rand.Read(r)
name := "test-" + hex.EncodeToString(r)
if _, err := c.BucketByName(name, false); err == nil {
t.Fatal("bucket exists?")
}
b, err := c.BucketByName(name, true)
if err != nil {
t.Fatal(err)
}
buckets, err := c.Buckets()
if err != nil {
t.Fatal(err)
}
found := false
for _, bb := range buckets {
if bb.Name == name {
found = true
if bb.ID != b.ID {
t.Fatal("Bucket ID mismatch:", b.ID, bb.ID)
}
if bb.Type != "allPrivate" {
t.Fatal("Bucket type mismatch:", bb.Type)
}
}
}
if !found {
t.Fatal("Bucket did not appear in Buckets()")
}
if err := b.Delete(); err != nil {
t.Fatal(err)
}
if _, err := c.BucketByName(name, false); err == nil {
t.Fatal("Bucket did not disappear")
}
}
func TestUnwrapError(t *testing.T) {
c := getClient(t)
_, err := c.GetFileInfoByID("jhgvcfgcgvhjhbjvghcf")
if e, ok := b2.UnwrapError(err); !ok || e == nil {
t.Fatalf("%T %v", err, err)
}
if err, ok := b2.UnwrapError(nil); ok || err != nil {
t.Fatal(err)
}
if err, ok := b2.UnwrapError(errors.New("test")); ok || err != nil {
t.Fatal(err)
}
}