forked from wal-g/wal-g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup_test.go
81 lines (66 loc) · 2.64 KB
/
backup_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
package test
import (
"bytes"
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/wal-g/wal-g/internal"
"github.com/wal-g/wal-g/utility"
"testing"
)
func TestCheckExistenceWhenBackupExists(t *testing.T) {
folder := createMockStorageFolder()
backup := internal.NewBackup(folder.GetSubFolder(utility.BaseBackupPath), "base_000")
exists, err := backup.CheckExistence()
assert.NoError(t, err)
assert.True(t, exists)
}
func TestCheckExistenceWhenBackupNotExists(t *testing.T) {
folder := createMockStorageFolder()
backup := internal.NewBackup(folder.GetSubFolder(utility.BaseBackupPath), "base_321")
exists, err := backup.CheckExistence()
assert.NoError(t, err)
assert.False(t, exists)
}
func TestGetTarNames(t *testing.T) {
folder := createMockStorageFolder()
backup := internal.NewBackup(folder.GetSubFolder(utility.BaseBackupPath), "base_456")
tarNames, err := backup.GetTarNames()
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"1", "2", "3"}, tarNames)
}
func TestIsPgControlRequired(t *testing.T) {
folder := createMockStorageFolder()
backup := internal.NewBackup(folder.GetSubFolder(utility.BaseBackupPath), "base_456")
dto, err := backup.FetchSentinel()
assert.NoError(t, err)
assert.True(t, internal.IsPgControlRequired(backup, dto))
}
func TestIsPgControlNotRequiredForWALEBackups(t *testing.T) {
folder := createMockStorageFolder()
backup := internal.NewBackup(folder.GetSubFolder(utility.BaseBackupPath), "base_000000010000DD170000000C_00743784")
assert.False(t, internal.IsPgControlRequired(backup, internal.BackupSentinelDto{}))
}
func TestFetchSentinel(t *testing.T) {
folder := createMockStorageFolder()
expectedSentinel := internal.BackupSentinelDto{}
expectedSentinelJson, _ := json.Marshal(expectedSentinel)
folder.PutObject("base_789454598_backup_stop_sentinel.json", bytes.NewReader(expectedSentinelJson))
backup := internal.NewBackup(folder, "base_789454598")
actualSentinel, err := backup.FetchSentinel()
assert.NoError(t, err)
assert.Equal(t, expectedSentinel, actualSentinel)
}
func TestFetchSentinelReturnErrorWhenSentinelNotExist(t *testing.T) {
folder := createMockStorageFolder()
backup := internal.NewBackup(folder.GetSubFolder(utility.BaseBackupPath), "base_78934085033849")
_, err := backup.FetchSentinel()
assert.Error(t, err)
}
func TestFetchSentinelReturnErrorWhenSentinelUnmarshallable(t *testing.T) {
folder := createMockStorageFolder()
backup := internal.NewBackup(folder.GetSubFolder(utility.BaseBackupPath), "base_000")
errorMessage := "failed to unmarshal sentinel"
_, err := backup.FetchSentinel()
assert.Error(t, err)
assert.Equal(t, errorMessage, err.Error()[:len(errorMessage)])
}