-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathbackup_test.go
More file actions
181 lines (154 loc) · 4.79 KB
/
backup_test.go
File metadata and controls
181 lines (154 loc) · 4.79 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//go:build test_e2e
package tests
import (
"archive/zip"
"context"
"fmt"
"io"
"math/rand"
"net/http"
"os"
"path"
"path/filepath"
"testing"
"time"
"github.com/stackrox/rox/pkg/backup"
"github.com/stackrox/rox/pkg/migrations"
"github.com/stackrox/rox/pkg/testutils/centralgrpc"
"github.com/stackrox/rox/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v3"
)
// Grab the backup DB and open it, ensuring that there are values for deployments
func TestBackup(t *testing.T) {
if os.Getenv("ORCHESTRATOR_FLAVOR") == "openshift" {
t.Skip("temporarily skipped on OCP. TODO(ROX-25171)")
}
deploymentName := fmt.Sprintf("test-backup-%d", rand.Intn(10000))
setupDeploymentInNamespace(t, "quay.io/rhacs-eng/qa-multi-arch:nginx-1.21.1", deploymentName, "default")
defer teardownDeploymentWithoutCheck(t, deploymentName, "default")
waitForDeploymentInCentral(t, deploymentName)
for _, includeCerts := range []bool{false, true} {
t.Run(fmt.Sprintf("includeCerts=%t", includeCerts), func(t *testing.T) {
doTestBackup(t, includeCerts, false)
})
}
// Make a run with certs only
doTestBackup(t, false, true)
}
func doTestBackup(t *testing.T, includeCerts bool, certsOnly bool) {
tmpZipDir := t.TempDir()
zipFilePath := filepath.Join(tmpZipDir, "backup.zip")
out, err := os.Create(zipFilePath)
require.NoError(t, err)
client := centralgrpc.HTTPClientForCentral(t)
// Backup could be long depend on the size of current database.
// Allow up to 3 minutes.
backupTimeout := 3 * time.Minute
client.Timeout = backupTimeout
endpoint := "/db/backup"
if includeCerts {
endpoint = "/api/extensions/backup"
}
if certsOnly {
endpoint = "/api/extensions/certs/backup"
}
ctx, cancel := context.WithTimeout(context.Background(), backupTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
require.NoError(t, err)
resp, err := client.Do(req)
require.NoError(t, err)
defer utils.IgnoreError(resp.Body.Close)
_, err = io.Copy(out, resp.Body)
require.NoError(t, err)
defer utils.IgnoreError(out.Close)
zipFile, err := zip.OpenReader(zipFilePath)
require.NoError(t, err)
defer utils.IgnoreError(zipFile.Close)
if !certsOnly {
checkZipForPostgres(t, zipFile)
checkZipForPassword(t, zipFile, includeCerts)
checkZipForCerts(t, zipFile, includeCerts)
checkZipForVersion(t, zipFile)
} else {
checkZipForOnlyCerts(t, zipFile)
}
}
func checkZipForVersion(t *testing.T, zipFile *zip.ReadCloser) {
versionFileEntry := getFileWithName(zipFile, backup.MigrationVersion)
require.NotNil(t, versionFileEntry)
reader, err := versionFileEntry.Open()
require.NoError(t, err)
bytes, err := io.ReadAll(reader)
require.NoError(t, err)
version := &migrations.MigrationVersion{}
err = yaml.Unmarshal(bytes, version)
require.NoError(t, err)
assert.Equal(t, version.MainVersion, version.MainVersion)
assert.Equal(t, migrations.CurrentDBVersionSeqNum(), version.SeqNum)
}
func checkZipForCerts(t *testing.T, zipFile *zip.ReadCloser, includeCerts bool) {
files := getFilesInDir(zipFile, "keys")
if !includeCerts {
require.Empty(t, files)
return
}
require.NotEmpty(t, files)
require.Equal(t, len(files), 3)
for _, f := range files {
info := f.FileInfo()
require.NotZero(t, info.Size())
require.Equal(t, filepath.Ext(f.Name), ".pem")
}
}
func checkZipForPostgres(t *testing.T, zipFile *zip.ReadCloser) {
// Open the dump file holding the Postgres backup.
postgresFileEntry := getFileWithName(zipFile, "postgres.dump")
require.NotNil(t, postgresFileEntry)
_, err := postgresFileEntry.Open()
require.NoError(t, err)
}
func checkZipForPassword(t *testing.T, zipFile *zip.ReadCloser, includeCerts bool) {
files := getFilesInDir(zipFile, backup.DatabaseBaseFolder)
if !includeCerts {
require.Empty(t, files)
return
}
require.NotEmpty(t, files)
require.Equal(t, len(files), 1)
for _, f := range files {
info := f.FileInfo()
require.NotZero(t, info.Size())
require.Equal(t, f.FileInfo().Name(), backup.DatabasePassword)
}
}
func checkZipForOnlyCerts(t *testing.T, zipFile *zip.ReadCloser) {
checkZipForCerts(t, zipFile, true)
dbFiles := getFilesInDir(zipFile, backup.DatabaseBaseFolder)
require.Empty(t, dbFiles)
versionFileEntry := getFileWithName(zipFile, backup.MigrationVersion)
require.Nil(t, versionFileEntry)
postgresFileEntry := getFileWithName(zipFile, "postgres.dump")
require.Nil(t, postgresFileEntry)
}
func getFileWithName(zipFile *zip.ReadCloser, name string) *zip.File {
var ret *zip.File
for _, f := range zipFile.File {
if f.Name == name {
ret = f
break
}
}
return ret
}
func getFilesInDir(zipFile *zip.ReadCloser, dir string) []*zip.File {
var files []*zip.File
for _, f := range zipFile.File {
if path.Dir(f.Name) == dir {
files = append(files, f)
}
}
return files
}