99 "time"
1010
1111 "github.com/google/go-cmp/cmp"
12- "github.com/percona/percona-backup-mongodb/pbm/storage/oss"
12+ "github.com/stretchr/testify/assert"
13+ "github.com/stretchr/testify/require"
1314 "github.com/testcontainers/testcontainers-go"
1415 "github.com/testcontainers/testcontainers-go/modules/mongodb"
1516 "go.mongodb.org/mongo-driver/mongo"
@@ -22,6 +23,7 @@ import (
2223 "github.com/percona/percona-backup-mongodb/pbm/storage/fs"
2324 "github.com/percona/percona-backup-mongodb/pbm/storage/gcs"
2425 "github.com/percona/percona-backup-mongodb/pbm/storage/mio"
26+ "github.com/percona/percona-backup-mongodb/pbm/storage/oss"
2527 "github.com/percona/percona-backup-mongodb/pbm/storage/s3"
2628)
2729
@@ -458,6 +460,147 @@ func TestConfig(t *testing.T) {
458460 })
459461}
460462
463+ func TestSanitizeStoragePaths (t * testing.T ) {
464+ tests := []struct {
465+ name string
466+ conf StorageConf
467+ wantBucket string
468+ wantPrefix string
469+ }{
470+ {
471+ "s3 trailing slash on bucket" ,
472+ StorageConf {Type : storage .S3 , S3 : & s3.Config {Bucket : "bcp/" , Prefix : "data" }},
473+ "bcp" , "data" ,
474+ },
475+ {
476+ "s3 leading slash on prefix" ,
477+ StorageConf {Type : storage .S3 , S3 : & s3.Config {Bucket : "bcp" , Prefix : "/data/pbm" }},
478+ "bcp" , "data/pbm" ,
479+ },
480+ {
481+ "s3 both slashes" ,
482+ StorageConf {Type : storage .S3 , S3 : & s3.Config {Bucket : "bcp/" , Prefix : "/data/" }},
483+ "bcp" , "data" ,
484+ },
485+ {
486+ "s3 multiple slashes" ,
487+ StorageConf {Type : storage .S3 , S3 : & s3.Config {Bucket : "///bcp///" , Prefix : "///data///" }},
488+ "bcp" , "data" ,
489+ },
490+ {
491+ "s3 clean values" ,
492+ StorageConf {Type : storage .S3 , S3 : & s3.Config {Bucket : "bcp" , Prefix : "data" }},
493+ "bcp" , "data" ,
494+ },
495+ {
496+ "minio trailing slash" ,
497+ StorageConf {Type : storage .Minio , Minio : & mio.Config {Bucket : "bcp/" , Prefix : "/pfx/" }},
498+ "bcp" , "pfx" ,
499+ },
500+ {
501+ "gcs leading slash on prefix" ,
502+ StorageConf {Type : storage .GCS , GCS : & gcs.Config {Bucket : "bcp/" , Prefix : "/pfx" }},
503+ "bcp" , "pfx" ,
504+ },
505+ {
506+ "oss trailing slash" ,
507+ StorageConf {Type : storage .OSS , OSS : & oss.Config {Bucket : "bcp/" , Prefix : "/pfx/" }},
508+ "bcp" , "pfx" ,
509+ },
510+ }
511+
512+ for _ , tt := range tests {
513+ t .Run (tt .name , func (t * testing.T ) {
514+ sanitizeStoragePaths (& tt .conf )
515+ switch tt .conf .Type {
516+ case storage .S3 :
517+ assert .Equal (t , tt .wantBucket , tt .conf .S3 .Bucket )
518+ assert .Equal (t , tt .wantPrefix , tt .conf .S3 .Prefix )
519+ case storage .Minio :
520+ assert .Equal (t , tt .wantBucket , tt .conf .Minio .Bucket )
521+ assert .Equal (t , tt .wantPrefix , tt .conf .Minio .Prefix )
522+ case storage .GCS :
523+ assert .Equal (t , tt .wantBucket , tt .conf .GCS .Bucket )
524+ assert .Equal (t , tt .wantPrefix , tt .conf .GCS .Prefix )
525+ case storage .OSS :
526+ assert .Equal (t , tt .wantBucket , tt .conf .OSS .Bucket )
527+ assert .Equal (t , tt .wantPrefix , tt .conf .OSS .Prefix )
528+ }
529+ })
530+ }
531+ }
532+
533+ func TestSanitizeStoragePathsAzure (t * testing.T ) {
534+ conf := StorageConf {Type : storage .Azure , Azure : & azure.Config {Container : "cnt/" , Prefix : "/pfx/" }}
535+ sanitizeStoragePaths (& conf )
536+ assert .Equal (t , "cnt" , conf .Azure .Container )
537+ assert .Equal (t , "pfx" , conf .Azure .Prefix )
538+ }
539+
540+ func TestIsStoragePathKey (t * testing.T ) {
541+ // Storage keys that should match.
542+ for _ , key := range []string {
543+ "storage.s3.bucket" ,
544+ "storage.s3.prefix" ,
545+ "storage.minio.bucket" ,
546+ "storage.minio.prefix" ,
547+ "storage.gcs.bucket" ,
548+ "storage.gcs.prefix" ,
549+ "storage.azure.container" ,
550+ "storage.azure.prefix" ,
551+ "storage.oss.bucket" ,
552+ "storage.oss.prefix" ,
553+ } {
554+ assert .True (t , isStoragePathKey (key ), "expected true for %q" , key )
555+ }
556+
557+ // Non-storage keys must not match.
558+ for _ , key := range []string {
559+ "pitr.enabled" ,
560+ "pitr.compression" ,
561+ "storage.s3.region" ,
562+ "storage.s3.debugLogLevels" ,
563+ "storage.filesystem.path" ,
564+ "bucket" ,
565+ "prefix" ,
566+ } {
567+ assert .False (t , isStoragePathKey (key ), "expected false for %q" , key )
568+ }
569+ }
570+
571+ func TestSetConfigVarTrimsSlashes (t * testing.T ) {
572+ ctx := context .Background ()
573+
574+ // Set up initial config so SetConfigVar works
575+ emptyCfg := & Config {
576+ Storage : StorageConf {Type : storage .S3 , S3 : & s3.Config {Bucket : "init" }},
577+ }
578+ err := SetConfig (ctx , connClient , emptyCfg )
579+ require .NoError (t , err )
580+
581+ tests := []struct {
582+ key string
583+ val string
584+ wantVal string
585+ }{
586+ {"storage.s3.bucket" , "bcp/" , "bcp" },
587+ {"storage.s3.prefix" , "/data/pbm/" , "data/pbm" },
588+ {"storage.s3.bucket" , "///bcp///" , "bcp" },
589+ {"storage.s3.prefix" , "clean" , "clean" },
590+ }
591+
592+ for _ , tt := range tests {
593+ t .Run (tt .key + "=" + tt .val , func (t * testing.T ) {
594+ err := SetConfigVar (ctx , connClient , tt .key , tt .val )
595+ require .NoError (t , err )
596+
597+ got , err := GetConfigVar (ctx , connClient , tt .key )
598+ require .NoError (t , err )
599+ assert .Equal (t , tt .wantVal , got )
600+ })
601+ }
602+ }
603+
461604func boolPtr (b bool ) * bool {
462605 return & b
463606}
0 commit comments