-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
92 lines (85 loc) · 3.23 KB
/
main_test.go
File metadata and controls
92 lines (85 loc) · 3.23 KB
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
package main
import (
"sort"
"testing"
"time"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
fake "k8s.io/client-go/dynamic/fake"
)
func newUnstructuredBackup(name, namespace, creationTimestamp, actionName, schedule, status, backupLocation string) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "cr.kanister.io/v1alpha1",
"kind": "ActionSet",
"metadata": map[string]interface{}{
"namespace": namespace,
"name": name,
"creationTimestamp": creationTimestamp,
},
"spec": map[string]interface{}{
"actions": []interface{}{
map[string]interface{}{
"name": actionName,
"options": map[string]interface{}{
"backup-schedule": schedule,
},
},
},
},
"status": map[string]interface{}{
"state": status,
"actions": []interface{}{
map[string]interface{}{
"artifacts": map[string]interface{}{
"cloudObject": map[string]interface{}{
"keyValue": map[string]interface{}{
"backupLocation": backupLocation,
},
},
},
},
},
},
},
}
}
func TestGetBackups(t *testing.T) {
defaultTime, _ := time.Parse(time.RFC3339, "2022-01-01T02:03:04.52Z")
expectedBackups := []backup{
{name: "backup-foo", schedule: "weekly", status: "complete", time: defaultTime, backupLocation: "pg_backups/renku/renku-postgresql/2022-01-01T02:03:04.52Z/backup.sql.gz"},
{name: "backup-bar", schedule: "daily", status: "complete", time: defaultTime, backupLocation: "pg_backups/renku/renku-postgresql/2022-01-01T02:03:04.52Z/backup.sql.gz"},
}
sort.Slice(expectedBackups, func(i, j int) bool { return expectedBackups[i].name < expectedBackups[j].name })
gvr := schema.GroupVersionResource{
Group: "cr.kanister.io",
Version: "v1alpha1",
Resource: "actionsets",
}
scheme := runtime.NewScheme()
client := fake.NewSimpleDynamicClientWithCustomListKinds(scheme,
map[schema.GroupVersionResource]string{
{Group: "cr.kanister.io", Version: "v1alpha1", Resource: "actionsets"}: "ActionSetsList",
},
newUnstructuredBackup("backup-foo", "kanister", "2022-01-01T02:03:04.52Z", "backup", "weekly", "complete", "pg_backups/renku/renku-postgresql/2022-01-01T02:03:04.52Z/backup.sql.gz"),
newUnstructuredBackup("backup-bar", "kanister", "2022-01-01T02:03:04.52Z", "backup", "daily", "complete", "pg_backups/renku/renku-postgresql/2022-01-01T02:03:04.52Z/backup.sql.gz"),
newUnstructuredBackup("backup-baz", "kanister", "2022-01-01T02:03:04.52Z", "not-a-backup", "daily", "complete", "pg_backups/renku/renku-postgresql/2022-01-01T02:03:04.52Z/backup.sql.gz"),
)
var backupConfig backupconfig
backupConfig.KanisterNamespace = "kanister"
backupConfig.Name = "daily"
backups, err := getBackups(client, gvr, backupConfig)
if err != nil {
t.Fatalf("unexpected error from getBackups: %v", err)
}
if len(backups) < 1 {
t.Fatal("Empty backups")
}
sort.Slice(backups, func(i, j int) bool { return expectedBackups[i].name < expectedBackups[j].name })
for i, backup := range backups {
if backup != expectedBackups[i] {
t.Fatal("Returned backup different from the expected one.")
}
}
}