-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
test_backupstorage.go
116 lines (90 loc) · 3.29 KB
/
test_backupstorage.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
/*
Copyright 2021 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testutil
import (
"context"
"fmt"
"sort"
"vitess.io/vitess/go/vt/mysqlctl/backupstorage"
)
type backupStorage struct {
backupstorage.BackupStorage
// Backups is a mapping of directory to list of backup names stored in that
// directory.
Backups map[string][]string
// ListBackupsError is returned from ListBackups when it is non-nil.
ListBackupsError error
}
// ListBackups is part of the backupstorage.BackupStorage interface.
func (bs *backupStorage) ListBackups(ctx context.Context, dir string) ([]backupstorage.BackupHandle, error) {
if bs.ListBackupsError != nil {
return nil, bs.ListBackupsError
}
handles := []backupstorage.BackupHandle{}
for k, v := range bs.Backups {
if k == dir {
for _, name := range v {
handles = append(handles, &backupHandle{directory: k, name: name})
}
}
}
sort.Sort(handlesByName(handles))
return handles, nil
}
// RemoveBackup is part of the backupstorage.BackupStorage interface.
func (bs *backupStorage) RemoveBackup(ctx context.Context, dir string, name string) error {
bucket, ok := bs.Backups[dir]
if !ok {
return fmt.Errorf("no bucket for key %s in testutil.BackupStorage", dir)
}
idx := -1
for i, backup := range bucket {
if backup == name {
idx = i
break
}
}
if idx == -1 {
return fmt.Errorf("no backup found for %s/%s", dir, name)
}
bs.Backups[dir] = append(bucket[:idx], bucket[idx+1:]...)
return nil
}
// Close is part of the backupstorage.BackupStorage interface.
func (bs *backupStorage) Close() error { return nil }
// backupHandle implements a subset of the backupstorage.backupHandle interface.
type backupHandle struct {
backupstorage.BackupHandle
directory string
name string
}
func (bh *backupHandle) Directory() string { return bh.directory }
func (bh *backupHandle) Name() string { return bh.name }
// handlesByName implements the sort interface for backup handles by Name().
type handlesByName []backupstorage.BackupHandle
func (a handlesByName) Len() int { return len(a) }
func (a handlesByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a handlesByName) Less(i, j int) bool { return a[i].Name() < a[j].Name() }
// BackupStorageImplementation is the name this package registers its test
// backupstorage.BackupStorage implementation as. Users should set
// *backupstorage.BackupStorageImplementation to this value before use.
const BackupStorageImplementation = "grpcvtctldserver.testutil"
// BackupStorage is the singleton test backupstorage.BackupStorage intastnce. It
// is public and singleton to allow tests to both mutate and assert against its
// state.
var BackupStorage = &backupStorage{
Backups: map[string][]string{},
}
func init() {
backupstorage.BackupStorageMap[BackupStorageImplementation] = BackupStorage
}