Skip to content

Commit 8b42f3e

Browse files
committed
add(restapi): add 'PurgeBackups' func test and some fixes
1 parent dce61ad commit 8b42f3e

File tree

4 files changed

+184
-51
lines changed

4 files changed

+184
-51
lines changed

pkg/restapi/backup_test.go

+118-5
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,40 @@
33
package restapi_test
44

55
import (
6+
"encoding/json"
67
"fmt"
78
"net/http"
89
"net/http/httptest"
910
"net/url"
1011
"testing"
12+
"time"
1113

1214
"github.com/golang/mock/gomock"
1315
"github.com/scylladb/go-log"
1416
"github.com/scylladb/scylla-manager/v3/pkg/restapi"
1517
"github.com/scylladb/scylla-manager/v3/pkg/service/backup"
1618
"github.com/scylladb/scylla-manager/v3/pkg/service/backup/backupspec"
19+
"github.com/scylladb/scylla-manager/v3/pkg/service/scheduler"
1720
"github.com/scylladb/scylla-manager/v3/pkg/util/timeutc"
1821
"github.com/scylladb/scylla-manager/v3/pkg/util/uuid"
1922
)
2023

2124
//go:generate mockgen -destination mock_backupservice_test.go -mock_names BackupService=MockBackupService -package restapi github.com/scylladb/scylla-manager/v3/pkg/restapi BackupService
25+
//go:generate mockgen -destination mock_schedservice_test.go -mock_names SchedService=MockSchedService -package restapi github.com/scylladb/scylla-manager/v3/pkg/restapi SchedService
2226

2327
func listBackupsRequest(clusterID uuid.UUID) *http.Request {
2428
return httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/cluster/%s/backups", clusterID.String()), nil)
2529
}
2630

31+
func listBackupsPurgeRequest(clusterID uuid.UUID) *http.Request {
32+
return httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/api/v1/cluster/%s/backups/purge", clusterID.String()), nil)
33+
}
34+
2735
func listBackupFilesRequest(clusterID uuid.UUID) *http.Request {
2836
return httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/cluster/%s/backups/files", clusterID.String()), nil)
2937
}
3038

31-
func withForm(r *http.Request, locations []backupspec.Location, filter backup.ListFilter, query string) *http.Request {
39+
func withForm(r *http.Request, dryRun bool, locations []backupspec.Location, filter backup.ListFilter, query string) *http.Request {
3240
r.Form = url.Values{}
3341
for _, l := range locations {
3442
r.Form.Add("locations", l.String())
@@ -41,6 +49,7 @@ func withForm(r *http.Request, locations []backupspec.Location, filter backup.Li
4149
r.Form.Add("min_date", string(a))
4250
b, _ := filter.MaxDate.MarshalText()
4351
r.Form.Add("max_date", string(b))
52+
r.Form.Add("dry_run", fmt.Sprintf("%v", dryRun))
4453

4554
return r
4655
}
@@ -91,15 +100,119 @@ func TestBackupList(t *testing.T) {
91100
cm.EXPECT().GetCluster(gomock.Any(), cluster.ID.String()).Return(cluster, nil)
92101
bm.EXPECT().List(gomock.Any(), cluster.ID, locations, filter).Return(golden, nil)
93102

94-
r := withForm(listBackupsRequest(cluster.ID), locations, filter, cluster.Name)
103+
r := withForm(listBackupsRequest(cluster.ID), false, locations, filter, cluster.Name)
95104
w := httptest.NewRecorder()
96105
h.ServeHTTP(w, r)
97106
assertJsonBody(t, w, golden)
98107
}
99108

100-
func TestBackupListAllClusters(t *testing.T) {
109+
func TestBackupPurge(t *testing.T) {
101110
t.Parallel()
102111

112+
ctrl := gomock.NewController(t)
113+
defer ctrl.Finish()
114+
cm := restapi.NewMockClusterService(ctrl)
115+
bm := restapi.NewMockBackupService(ctrl)
116+
sm := restapi.NewMockSchedService(ctrl)
117+
118+
services := restapi.Services{
119+
Cluster: cm,
120+
Backup: bm,
121+
Scheduler: sm,
122+
}
123+
124+
h := restapi.New(services, log.NewDevelopment())
125+
126+
var (
127+
cluster = givenCluster()
128+
129+
locations = backupspec.Locations{
130+
{Provider: backupspec.S3, Path: "foo"},
131+
{Provider: backupspec.S3, Path: "bar"},
132+
}
133+
134+
taskID1 = uuid.MustRandom()
135+
taskID2 = uuid.MustRandom()
136+
137+
snapshotNow1 = backupspec.SnapshotTagAt(time.Now())
138+
snapshotNow2 = backupspec.SnapshotTagAt(time.Now().Add(time.Minute))
139+
140+
filter = backup.ListFilter{
141+
ClusterID: cluster.ID,
142+
}
143+
144+
manifests = backupspec.Manifests{
145+
{
146+
Location: locations[0],
147+
DC: "",
148+
ClusterID: cluster.ID,
149+
NodeID: "",
150+
TaskID: taskID1,
151+
SnapshotTag: snapshotNow1,
152+
Temporary: false,
153+
},
154+
{
155+
Location: locations[1],
156+
DC: "",
157+
ClusterID: cluster.ID,
158+
NodeID: "",
159+
TaskID: taskID2,
160+
SnapshotTag: snapshotNow2,
161+
Temporary: false,
162+
},
163+
}
164+
expected = restapi.BackupPurgeOut{
165+
Warnings: make([]string, 0),
166+
}
167+
)
168+
169+
task1 := makeTaskItem(cluster.ID, taskID1, locations, 3, 30)
170+
task2 := makeTaskItem(cluster.ID, taskID2, locations, 3, 30)
171+
tasks := []*scheduler.TaskListItem{
172+
task1, task2,
173+
}
174+
cm.EXPECT().GetCluster(gomock.Any(), cluster.ID.String()).Return(cluster, nil)
175+
sm.EXPECT().ListTasks(gomock.Any(), cluster.ID, scheduler.ListFilter{TaskType: []scheduler.TaskType{scheduler.BackupTask}}).Return(tasks, nil)
176+
bm.EXPECT().PurgeBackups(gomock.Any(), cluster.ID, locations, gomock.Any(), gomock.Any()).Return(manifests, expected.Warnings, nil)
177+
178+
expected.Deleted = restapi.ConvertManifestsToListItems(manifests)
179+
r := withForm(listBackupsPurgeRequest(cluster.ID), true, locations, filter, cluster.Name)
180+
w := httptest.NewRecorder()
181+
h.ServeHTTP(w, r)
182+
183+
assertJsonBody(t, w, expected)
184+
}
185+
186+
func makeTaskItem(clusterID, taskID uuid.UUID, locations backupspec.Locations, retention, day int) *scheduler.TaskListItem {
187+
taskProp := backup.TaskProperties{
188+
Location: locations,
189+
RetentionMap: make(backup.RetentionMap, 0),
190+
}
191+
taskProp.RetentionMap.Add(taskID, retention, day)
192+
193+
prop, err := json.Marshal(taskProp)
194+
if err != nil {
195+
return nil
196+
}
197+
198+
return &scheduler.TaskListItem{
199+
Task: scheduler.Task{
200+
ClusterID: clusterID,
201+
ID: taskID,
202+
Type: scheduler.BackupTask,
203+
Enabled: true,
204+
Sched: scheduler.Schedule{StartDate: time.Now()},
205+
Properties: prop,
206+
},
207+
Suspended: false,
208+
NextActivation: nil,
209+
Retry: 0,
210+
}
211+
212+
}
213+
214+
func TestBackupListAllClusters(t *testing.T) {
215+
103216
ctrl := gomock.NewController(t)
104217
defer ctrl.Finish()
105218
cm := restapi.NewMockClusterService(ctrl)
@@ -142,7 +255,7 @@ func TestBackupListAllClusters(t *testing.T) {
142255
cm.EXPECT().GetCluster(gomock.Any(), cluster.ID.String()).Return(cluster, nil)
143256
bm.EXPECT().List(gomock.Any(), cluster.ID, locations, filter).Return(golden, nil)
144257

145-
r := withForm(listBackupsRequest(cluster.ID), locations, filter, "")
258+
r := withForm(listBackupsRequest(cluster.ID), false, locations, filter, "")
146259
w := httptest.NewRecorder()
147260
h.ServeHTTP(w, r)
148261
assertJsonBody(t, w, golden)
@@ -191,7 +304,7 @@ func TestBackupListFiles(t *testing.T) {
191304
cm.EXPECT().GetCluster(gomock.Any(), cluster.ID.String()).Return(cluster, nil)
192305
bm.EXPECT().ListFiles(gomock.Any(), cluster.ID, locations, filter).Return(golden, nil)
193306

194-
r := withForm(listBackupFilesRequest(cluster.ID), locations, filter, cluster.ID.String())
307+
r := withForm(listBackupFilesRequest(cluster.ID), false, locations, filter, cluster.ID.String())
195308
w := httptest.NewRecorder()
196309
h.ServeHTTP(w, r)
197310
assertJsonBody(t, w, golden)

pkg/restapi/main_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ func jsonBody(t testing.TB, v interface{}) *bytes.Reader {
3030
}
3131

3232
func assertJsonBody(t testing.TB, w *httptest.ResponseRecorder, expected interface{}) {
33+
t.Helper()
34+
3335
b, err := json.Marshal(expected)
3436
if err != nil {
3537
t.Fatal(err)

pkg/restapi/mock_backupservice_test.go

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)