forked from wal-g/wal-g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtar_ball_test.go
109 lines (90 loc) · 2.44 KB
/
tar_ball_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
package test
import (
"archive/tar"
"bytes"
"github.com/stretchr/testify/assert"
"github.com/wal-g/wal-g/internal"
"github.com/wal-g/wal-g/testtools"
"io"
"strings"
"testing"
)
// TODO : this test is broken now
// Tests S3 get and set methods.
func TestS3TarBall(t *testing.T) {
bundle := &internal.Bundle{
ArchiveDirectory: "/usr/local",
TarSizeThreshold: int64(10),
}
bundle.TarBallMaker = internal.NewStorageTarBallMaker("test", testtools.NewMockUploader(false, false))
bundle.NewTarBall(false)
assert.NotNil(t, bundle.TarBall)
tarBall := bundle.TarBall
assert.Equal(t, int64(0), tarBall.Size())
assert.Nil(t, tarBall.TarWriter())
bundle.NewTarBall(false)
//assert.Equal(t, bundle.TarBall, tarBall)
}
// Tests S3 dependent functions for StorageTarBall such as
// SetUp(), CloseTar() and Finish().
func TestS3DependentFunctions(t *testing.T) {
bundle := &internal.Bundle{
ArchiveDirectory: "",
TarSizeThreshold: 100,
}
uploader := testtools.NewMockUploader(false, false)
bundle.TarBallMaker = internal.NewStorageTarBallMaker("mockBackup", uploader)
bundle.NewTarBall(false)
tarBall := bundle.TarBall
tarBall.SetUp(nil)
tarWriter := tarBall.TarWriter()
mockData := []byte("a")
// Write mock header.
mockHeader := &tar.Header{
Name: "mock",
Size: int64(len(mockData)),
}
err := tarWriter.WriteHeader(mockHeader)
if err != nil {
t.Log(err)
}
// Write body.
_, err = tarWriter.Write(mockData)
assert.NoError(t, err)
err = tarBall.CloseTar()
assert.NoError(t, err)
// Handle write after close.
_, err = tarBall.TarWriter().Write(mockData)
assert.Error(t, err)
}
func TestPackFileTo(t *testing.T) {
mockData := "mock"
mockHeader := &tar.Header{
Name: "mock",
Mode: int64(0600),
Size: int64(len(mockData)),
Typeflag: tar.TypeReg,
}
buffer := bytes.NewBuffer(make([]byte, 0))
tarBallMaker := testtools.BufferTarBallMaker{
BufferToWrite: buffer,
}
tarBall := tarBallMaker.Make(false)
tarBall.SetUp(nil)
size, err := internal.PackFileTo(tarBall, mockHeader, strings.NewReader(mockData))
assert.Equal(t, int64(len(mockData)), size)
assert.NoError(t, err)
assert.Equal(t, tarBall.Size(), size)
reader := tar.NewReader(buffer)
interpreter := testtools.BufferTarInterpreter{}
for {
header, err := reader.Next()
if err == io.EOF {
break
}
assert.NoError(t, err)
err = interpreter.Interpret(reader, header)
assert.NoError(t, err)
}
assert.Equal(t, []byte(mockData), interpreter.Out)
}