Skip to content

Commit 5434a28

Browse files
committed
[PBM-1239] use storage.Storage interface in tests
allows to perform checks for different storage types
1 parent 28c39ff commit 5434a28

File tree

3 files changed

+29
-95
lines changed

3 files changed

+29
-95
lines changed

e2e-tests/docker/conf/minio.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ storage:
22
type: s3
33
s3:
44
endpointUrl: http://minio:9000
5+
region: us-east-1
56
bucket: bcp
67
prefix: pbme2etest
78
credentials:
Lines changed: 20 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
package sharded
22

33
import (
4+
"bytes"
45
"context"
56
"log"
6-
"net/url"
77
"os"
88
"strings"
99
"time"
1010

11-
"github.com/aws/aws-sdk-go/aws"
12-
"github.com/aws/aws-sdk-go/aws/credentials"
13-
"github.com/aws/aws-sdk-go/aws/session"
14-
awsS3 "github.com/aws/aws-sdk-go/service/s3"
15-
16-
"gopkg.in/yaml.v2"
17-
1811
"github.com/percona/percona-backup-mongodb/pbm/config"
1912
"github.com/percona/percona-backup-mongodb/pbm/defs"
2013
"github.com/percona/percona-backup-mongodb/pbm/errors"
21-
"github.com/percona/percona-backup-mongodb/pbm/storage/s3"
14+
"github.com/percona/percona-backup-mongodb/pbm/storage"
15+
"github.com/percona/percona-backup-mongodb/pbm/util"
2216
)
2317

2418
func (c *Cluster) BackupCancellation(storage string) {
@@ -55,62 +49,39 @@ func (c *Cluster) BackupCancellation(storage string) {
5549

5650
func checkNoBackupFiles(backupName, conf string) {
5751
log.Println("check no artifacts left for backup", backupName)
58-
buf, err := os.ReadFile(conf)
59-
if err != nil {
60-
log.Fatalln("Error: unable to read config file:", err)
61-
}
6252

63-
var cfg config.Config
64-
err = yaml.UnmarshalStrict(buf, &cfg)
53+
files, err := listAllFiles(conf)
6554
if err != nil {
66-
log.Fatalln("Error: unmarshal yaml:", err)
55+
log.Fatalln("ERROR: list files:", err)
6756
}
6857

69-
stg := cfg.Storage
70-
71-
endopintURL := awsurl
72-
if stg.S3.EndpointURL != "" {
73-
eu, err := url.Parse(stg.S3.EndpointURL)
74-
if err != nil {
75-
log.Fatalln("Error: parse EndpointURL:", err)
58+
for _, file := range files {
59+
if strings.Contains(file.Name, backupName) {
60+
log.Fatalln("ERROR: failed to delete lefover", file.Name)
7661
}
77-
endopintURL = eu.Host
7862
}
63+
}
7964

80-
ss, err := newS3Client(endopintURL, stg.S3.Region, &stg.S3.Credentials)
65+
func listAllFiles(confFilepath string) ([]storage.FileInfo, error) {
66+
buf, err := os.ReadFile(confFilepath)
8167
if err != nil {
82-
log.Fatalf("create S3 client: %v", err)
68+
return nil, errors.Wrap(err, "read config file")
8369
}
8470

85-
res, err := ss.ListObjectsV2(&awsS3.ListObjectsV2Input{
86-
Bucket: &stg.S3.Bucket,
87-
Prefix: &stg.S3.Prefix,
88-
})
71+
cfg, err := config.Parse(bytes.NewBuffer(buf))
8972
if err != nil {
90-
log.Fatalf("list files on S3: %v", err)
73+
return nil, errors.Wrap(err, "parse config")
9174
}
9275

93-
for _, object := range res.Contents {
94-
s := object.String()
95-
if strings.Contains(s, backupName) {
96-
log.Fatalln("Error: failed to delete lefover", object.Key)
97-
}
76+
stg, err := util.StorageFromConfig(&cfg.Storage, nil)
77+
if err != nil {
78+
return nil, errors.Wrap(err, "storage from config")
9879
}
99-
}
10080

101-
func newS3Client(uri, region string, creds *s3.Credentials) (*awsS3.S3, error) {
102-
sess, err := session.NewSession(&aws.Config{
103-
Region: &region,
104-
Endpoint: &uri,
105-
Credentials: credentials.NewStaticCredentials(
106-
creds.AccessKeyID,
107-
creds.SecretAccessKey,
108-
creds.SessionToken,
109-
),
110-
})
81+
files, err := stg.List("", "")
11182
if err != nil {
112-
return nil, errors.Wrap(err, "create AWS session")
83+
return nil, errors.Wrap(err, "list files")
11384
}
11485

115-
return awsS3.New(sess), nil
86+
return files, nil
11687
}

e2e-tests/pkg/tests/sharded/test_delete_backup.go

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,10 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"net/url"
87
"os"
98
"strings"
109
"time"
1110

12-
awsS3 "github.com/aws/aws-sdk-go/service/s3"
13-
"gopkg.in/yaml.v2"
14-
15-
"github.com/percona/percona-backup-mongodb/pbm/config"
1611
"github.com/percona/percona-backup-mongodb/pbm/ctrl"
1712
"github.com/percona/percona-backup-mongodb/pbm/defs"
1813
"github.com/percona/percona-backup-mongodb/pbm/lock"
@@ -165,66 +160,33 @@ func (c *Cluster) BackupDelete(storage string) {
165160
checkData()
166161
}
167162

168-
const awsurl = "s3.amazonaws.com"
169-
170163
// checkArtefacts checks if all backups artifacts removed
171164
// except for the shouldStay
172165
func checkArtefacts(conf string, shouldStay map[string]struct{}) {
173166
log.Println("check all artifacts deleted excepts backup's", shouldStay)
174-
buf, err := os.ReadFile(conf)
175-
if err != nil {
176-
log.Fatalln("ERROR: unable to read config file:", err)
177-
}
178167

179-
var cfg config.Config
180-
err = yaml.UnmarshalStrict(buf, &cfg)
168+
files, err := listAllFiles(conf)
181169
if err != nil {
182-
log.Fatalln("ERROR: unmarshal yaml:", err)
170+
log.Fatalln("ERROR: list files:", err)
183171
}
184172

185-
stg := cfg.Storage
186-
187-
if stg.Type == "azure" || stg.Type == "filesystem" {
188-
return
189-
}
190-
191-
endopintURL := awsurl
192-
if stg.S3.EndpointURL != "" {
193-
eu, err := url.Parse(stg.S3.EndpointURL)
194-
if err != nil {
195-
log.Fatalln("ERROR: parse EndpointURL:", err)
173+
for _, file := range files {
174+
if strings.Contains(file.Name, defs.StorInitFile) {
175+
continue
196176
}
197-
endopintURL = eu.Host
198-
}
199-
200-
ss, err := newS3Client(endopintURL, stg.S3.Region, &stg.S3.Credentials)
201-
if err != nil {
202-
log.Fatalf("create S3 client: %v", err)
203-
}
204-
205-
res, err := ss.ListObjectsV2(&awsS3.ListObjectsV2Input{
206-
Bucket: &stg.S3.Bucket,
207-
Prefix: &stg.S3.Prefix,
208-
})
209-
if err != nil {
210-
log.Fatalf("list files on S3: %v", err)
211-
}
212-
213-
for _, object := range res.Contents {
214-
objectKey := *object.Key
215-
if strings.Contains(objectKey, defs.StorInitFile) || strings.Contains(objectKey, "/pbmPitr/") {
177+
if strings.Contains(file.Name, defs.PITRfsPrefix) {
216178
continue
217179
}
218180

219181
var ok bool
220182
for b := range shouldStay {
221-
if strings.Contains(objectKey, b) {
183+
if strings.Contains(file.Name, b) {
222184
ok = true
223185
break
224186
}
225187
}
226188
if !ok {
227-
log.Fatalln("ERROR: failed to delete lefover", objectKey)
189+
log.Fatalln("ERROR: failed to delete lefover", file.Name)
228190
}
229191
}
230192
}

0 commit comments

Comments
 (0)