forked from stackrox/stackrox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexternal_backup_test.go
142 lines (120 loc) · 3.93 KB
/
external_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//go:build externalbackups
package tests
import (
"context"
"os"
"testing"
"time"
googleStorage "cloud.google.com/go/storage"
"github.com/pkg/errors"
v1 "github.com/stackrox/rox/generated/api/v1"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/retry"
"github.com/stackrox/rox/pkg/testutils/centralgrpc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
const (
testGCSBucket = "stackrox-ci-gcs-db-upload-test"
)
func countNumBackups(t *testing.T, client *googleStorage.Client, prefix string) int {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
it := client.Bucket(testGCSBucket).Objects(ctx, &googleStorage.Query{
Prefix: prefix,
})
numBackups := 0
var err error
for _, err = it.Next(); err == nil; _, err = it.Next() {
numBackups++
}
require.Equal(t, iterator.Done, err)
return numBackups
}
func verifyNumBackups(t *testing.T, numBackups int, numExpected int) {
assert.Equal(t, numExpected, numBackups)
}
func TestGCSExternalBackup(t *testing.T) {
serviceAccount := os.Getenv("GOOGLE_GCS_BACKUP_SERVICE_ACCOUNT")
require.NotEmpty(t, serviceAccount)
prefix := os.Getenv("BUILD_ID")
require.NotEmpty(t, prefix)
client, err := googleStorage.NewClient(context.Background(), option.WithCredentialsJSON([]byte(serviceAccount)))
require.NoError(t, err)
conn := centralgrpc.GRPCConnectionToCentral(t)
service := v1.NewExternalBackupServiceClient(conn)
externalBackup := &storage.ExternalBackup{
Name: "GCS backup",
Type: "gcs",
BackupsToKeep: 2,
Schedule: &storage.Schedule{
IntervalType: storage.Schedule_DAILY,
Hour: 3,
Minute: 0,
},
Config: &storage.ExternalBackup_Gcs{
Gcs: &storage.GCSConfig{
Bucket: testGCSBucket,
ServiceAccount: os.Getenv("GOOGLE_GCS_BACKUP_SERVICE_ACCOUNT"),
ObjectPrefix: prefix,
},
},
}
// We could be in a situation where central isn't quite ready from the
// previous tests. This will retry a few times until it is if that is the case.
// If this first one doesn't work, then the rest are doomed so no need to wrap those
// in retries.
err = retry.WithRetry(func() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
_, err := service.TestExternalBackup(ctx, externalBackup)
cancel()
return err
},
retry.Tries(10),
retry.BetweenAttempts(func(_ int) {
time.Sleep(10 * time.Second)
}),
retry.OnFailedAttempts(func(err error) {
log.Error(err.Error())
}),
)
assert.NoError(t, err)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
backup, err := service.PostExternalBackup(ctx, externalBackup)
assert.NoError(t, err)
cancel()
verifyNumBackups(t, countNumBackups(t, client, prefix), 0)
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Minute)
_, err = service.TriggerExternalBackup(ctx, &v1.ResourceByID{Id: backup.GetId()})
assert.NoError(t, err)
cancel()
verifyNumBackups(t, countNumBackups(t, client, prefix), 1)
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Minute)
_, err = service.TriggerExternalBackup(ctx, &v1.ResourceByID{Id: backup.GetId()})
assert.NoError(t, err)
cancel()
verifyNumBackups(t, countNumBackups(t, client, prefix), 2)
ctx, cancel = context.WithTimeout(context.Background(), 2*time.Minute)
_, err = service.TriggerExternalBackup(ctx, &v1.ResourceByID{Id: backup.GetId()})
assert.NoError(t, err)
cancel()
// Should have pruned the first one
err = retry.WithRetry(func() error {
numBackups := countNumBackups(t, client, prefix)
if numBackups != 2 {
return errors.Errorf("Backup is not pruned: got %d", numBackups)
}
return nil
},
retry.Tries(10),
retry.BetweenAttempts(func(_ int) {
time.Sleep(1 * time.Second)
}),
retry.OnFailedAttempts(func(err error) {
log.Error(err.Error())
}),
)
require.NoError(t, err)
}