forked from wal-g/wal-g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs_test.go
234 lines (179 loc) · 4.7 KB
/
structs_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package walg_test
import (
"archive/tar"
"testing"
"github.com/wal-g/wal-g"
"os"
"time"
)
// Tests S3 get and set methods.
func TestS3TarBall(t *testing.T) {
tarBallCounter := 0
bundle := &walg.Bundle{
MinSize: int64(10),
}
bundle.Tbm = &walg.S3TarBallMaker{
BaseDir: "tmp",
Trim: "/usr/local",
BkupName: "test",
}
bundle.NewTarBall(true)
tarBallCounter += 1
if bundle.Tb == nil {
t.Errorf("make: Did not successfully create a new tarball.")
}
tarBall := bundle.Tb
if tarBall.BaseDir() != "tmp" {
t.Errorf("make: Expected base directory to be '%s' but got '%s'", "tmp", tarBall.BaseDir())
}
if tarBall.Trim() != "/usr/local" {
t.Errorf("make: Expected trim to be '%s' but got '%s'", "/usr/local", tarBall.Trim())
}
if tarBall.Nop() {
t.Errorf("make: S3TarBall expected NOP to be false but got %v", tarBall.Nop())
}
if tarBall.Number() != tarBallCounter {
t.Errorf("make: Expected tarball number to be %d but got %d", tarBallCounter, tarBall.Number())
}
if tarBall.Size() != 0 {
t.Errorf("make: Expected tarball initial size to be 0 but got %d", tarBall.Size())
}
increase := 1024
tarBall.AddSize(int64(increase))
if tarBall.Size() != 1024 {
t.Errorf("make: Tarball size expected to increase to %d but got %d", increase, tarBall.Size())
}
if tarBall.Tw() != nil {
t.Errorf("make: Tarball writer should not be set up without calling SetUp()")
}
bundle.NewTarBall(true)
tarBallCounter += 1
if tarBall == bundle.Tb {
t.Errorf("make: Did not successfully create a new tarball")
}
if bundle.Tb.Number() != tarBallCounter {
t.Errorf("make: Expected tarball number to increase to %d but got %d", tarBallCounter, tarBall.Number())
}
}
// Tests S3 dependent functions for S3TarBall such as
// SetUp(), CloseTar() and Finish().
func TestS3DependentFunctions(t *testing.T) {
bundle := &walg.Bundle{
MinSize: 100,
}
tu := walg.NewTarUploader(&mockS3Client{}, "bucket", "server", "region", 1, float64(1))
tu.Upl = &mockS3Uploader{}
bundle.Tbm = &walg.S3TarBallMaker{
BaseDir: "mockDirectory",
Trim: "",
BkupName: "mockBackup",
Tu: tu,
}
bundle.NewTarBall(true)
tarBall := bundle.Tb
tarBall.SetUp(walg.MockArmedCrypter())
tarWriter := tarBall.Tw()
one := []byte("a")
// Write mock header.
hdr := &tar.Header{
Name: "mock",
Size: int64(1),
}
err := tarWriter.WriteHeader(hdr)
if err != nil {
t.Log(err)
}
// Write body.
_, err = tarWriter.Write(one)
if err != nil {
t.Errorf("structs: expected to write 1 byte but got %s", err)
}
tarBall.CloseTar()
// Handle write after close.
_, err = tarBall.Tw().Write(one)
if err == nil {
t.Errorf("structs: expected WriteAfterClose error but got '<nil>'")
}
err = tarBall.Finish(&walg.S3TarBallSentinelDto{})
if err != nil {
t.Errorf("structs: tarball did not finish correctly with error %s", err)
}
// Test naming property of SetUp().
bundle.NewTarBall(true)
tarBall = bundle.Tb
tarBall.SetUp(walg.MockArmedCrypter(), "mockTarball")
tarBall.CloseTar()
err = tarBall.Finish(&walg.S3TarBallSentinelDto{})
if err != nil {
t.Errorf("structs: tarball did not finish correctly with error %s", err)
}
}
func TestEmptyBundleQueue(t *testing.T) {
bundle := &walg.Bundle{
MinSize: 100,
}
tu := walg.NewTarUploader(&mockS3Client{}, "bucket", "server", "region", 1, float64(1))
tu.Upl = &mockS3Uploader{}
bundle.Tbm = &walg.S3TarBallMaker{
BaseDir: "mockDirectory",
Trim: "",
BkupName: "mockBackup",
Tu: tu,
}
bundle.StartQueue()
err := bundle.FinishQueue()
if err != nil {
t.Log(err)
}
}
func TestBundleQueue(t *testing.T) {
queueTest(t)
}
func TestBundleQueueHC(t *testing.T) {
os.Setenv("WALG_UPLOAD_CONCURRENCY", "100")
queueTest(t)
os.Unsetenv("WALG_UPLOAD_CONCURRENCY")
}
func TestBundleQueueLC(t *testing.T) {
os.Setenv("WALG_UPLOAD_CONCURRENCY", "1")
queueTest(t)
os.Unsetenv("WALG_UPLOAD_CONCURRENCY")
}
func queueTest(t *testing.T) {
bundle := &walg.Bundle{
MinSize: 100,
}
tu := walg.NewTarUploader(&mockS3Client{}, "bucket", "server", "region", 1, float64(1))
tu.Upl = &mockS3Uploader{}
bundle.Tbm = &walg.S3TarBallMaker{
BaseDir: "mockDirectory",
Trim: "",
BkupName: "mockBackup",
Tu: tu,
}
f := false
tr := true
// For tests there must be at leaest 3 workers
bundle.StartQueue()
go func() {
a := bundle.Deque()
time.Sleep(10*time.Millisecond)
bundle.EnqueueBack(a, &tr)
time.Sleep(10*time.Millisecond)
bundle.EnqueueBack(a, &f)
}()
go func() {
c := bundle.Deque()
time.Sleep(10*time.Millisecond)
bundle.CheckSizeAndEnqueueBack(c)
}()
go func() {
b := bundle.Deque()
time.Sleep(10*time.Millisecond)
bundle.EnqueueBack(b, &f)
}()
err := bundle.FinishQueue()
if err != nil {
t.Log(err)
}
}