-
Notifications
You must be signed in to change notification settings - Fork 7
/
storage_backup_test.go
108 lines (101 loc) · 3.25 KB
/
storage_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
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
package gsclient
import (
"encoding/json"
"fmt"
"net/http"
"path"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClient_GetStorageBackupList(t *testing.T) {
server, client, mux := setupTestClient(true)
defer server.Close()
uri := path.Join(apiStorageBase, dummyUUID, "backups")
mux.HandleFunc(uri, func(writer http.ResponseWriter, request *http.Request) {
assert.Equal(t, http.MethodGet, request.Method)
writer.Header().Set(requestUUIDHeader, dummyRequestUUID)
fmt.Fprintf(writer, prepareStorageBackupListHTTPGet())
})
for _, test := range uuidCommonTestCases {
res, err := client.GetStorageBackupList(emptyCtx, test.testUUID)
if test.isFailed {
assert.NotNil(t, err)
} else {
assert.Nil(t, err, "GetStorageBackupList returned an error %v", err)
assert.Equal(t, 1, len(res))
assert.Equal(t, fmt.Sprintf("[%v]", getMockStorageBackup()), fmt.Sprintf("%v", res))
}
}
}
func TestClient_DeleteStorageBackup(t *testing.T) {
server, client, mux := setupTestClient(true)
defer server.Close()
var isFailed bool
uri := path.Join(apiStorageBase, dummyUUID, "backups", dummyUUID)
mux.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(requestUUIDHeader, dummyRequestUUID)
if isFailed {
w.WriteHeader(400)
} else {
if r.Method == http.MethodDelete {
fmt.Fprintf(w, "")
} else if r.Method == http.MethodGet {
w.WriteHeader(404)
}
}
})
for _, serverTest := range commonSuccessFailTestCases {
isFailed = serverTest.isFailed
for _, testStorageID := range uuidCommonTestCases {
for _, testSnapshotID := range uuidCommonTestCases {
err := client.DeleteStorageBackup(emptyCtx, testStorageID.testUUID, testSnapshotID.testUUID)
if testStorageID.isFailed || testSnapshotID.isFailed || isFailed {
assert.NotNil(t, err)
} else {
assert.Nil(t, err, "DeleteStorageBackup returned an error %v", err)
}
}
}
}
}
func TestClient_RollbackStorageBackup(t *testing.T) {
server, client, mux := setupTestClient(true)
defer server.Close()
var isFailed bool
uri := path.Join(apiStorageBase, dummyUUID, "backups", dummyUUID, "rollback")
mux.HandleFunc(uri, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(requestUUIDHeader, dummyRequestUUID)
if isFailed {
w.WriteHeader(400)
} else {
fmt.Fprintf(w, "")
}
})
for _, serverTest := range commonSuccessFailTestCases {
isFailed = serverTest.isFailed
for _, testStorageID := range uuidCommonTestCases {
for _, testSnapshotID := range uuidCommonTestCases {
err := client.RollbackStorageBackup(emptyCtx, testStorageID.testUUID, testSnapshotID.testUUID, StorageRollbackRequest{Rollback: true})
if testStorageID.isFailed || testSnapshotID.isFailed || isFailed {
assert.NotNil(t, err)
} else {
assert.Nil(t, err, "RollbackStorageBackup returned an error %v", err)
}
}
}
}
}
func getMockStorageBackup() StorageBackup {
mock := StorageBackup{Properties: StorageBackupProperties{
ObjectUUID: dummyUUID,
Name: "test",
CreateTime: dummyTime,
Capacity: 10,
}}
return mock
}
func prepareStorageBackupListHTTPGet() string {
snapshot := getMockStorageBackup()
res, _ := json.Marshal(snapshot.Properties)
return fmt.Sprintf(`{"backups" : {"%s" : %s}}`, dummyUUID, string(res))
}