diff --git a/pbm/config/config_test.go b/pbm/config/config_test.go index 4cc6167bf..2fd23a386 100644 --- a/pbm/config/config_test.go +++ b/pbm/config/config_test.go @@ -5,6 +5,7 @@ import ( "github.com/google/go-cmp/cmp" + "github.com/percona/percona-backup-mongodb/pbm/storage" "github.com/percona/percona-backup-mongodb/pbm/storage/azure" "github.com/percona/percona-backup-mongodb/pbm/storage/fs" "github.com/percona/percona-backup-mongodb/pbm/storage/gcs" @@ -207,6 +208,18 @@ func TestIsSameStorage(t *testing.T) { }) } +func TestCastError(t *testing.T) { + t.Run("S3", func(t *testing.T) { + cfg := StorageConf{Type: storage.S3} + + err := cfg.Cast() + if err == nil { + t.Errorf("Cast did not raise an error") + } + + }) +} + func boolPtr(b bool) *bool { return &b } diff --git a/pbm/storage/fs/fs.go b/pbm/storage/fs/fs.go index 1b8c4e9b0..e24ed5a19 100644 --- a/pbm/storage/fs/fs.go +++ b/pbm/storage/fs/fs.go @@ -64,6 +64,9 @@ func (cfg *Config) IsSameStorage(other *Config) bool { } func (cfg *Config) Cast() error { + if cfg == nil { + return errors.New("missing filesystem configuration with filesystem storage type") + } if cfg.Path == "" { return errors.New("path can't be empty") } diff --git a/pbm/storage/s3/s3.go b/pbm/storage/s3/s3.go index 0bcd461c1..c32f307d1 100644 --- a/pbm/storage/s3/s3.go +++ b/pbm/storage/s3/s3.go @@ -44,7 +44,7 @@ const ( //nolint:lll type Config struct { - Region string `bson:"region" json:"region" yaml:"region"` + Region string `bson:"region,omitempty" json:"region,omitempty" yaml:"region,omitempty"` EndpointURL string `bson:"endpointUrl,omitempty" json:"endpointUrl" yaml:"endpointUrl,omitempty"` EndpointURLMap map[string]string `bson:"endpointUrlMap,omitempty" json:"endpointUrlMap,omitempty" yaml:"endpointUrlMap,omitempty"` ForcePathStyle *bool `bson:"forcePathStyle,omitempty" json:"forcePathStyle,omitempty" yaml:"forcePathStyle,omitempty"` @@ -200,6 +200,9 @@ func (cfg *Config) IsSameStorage(other *Config) bool { } func (cfg *Config) Cast() error { + if cfg == nil { + return errors.New("missing S3 configuration with S3 storage type") + } if cfg.Region == "" { cfg.Region = defaultS3Region } diff --git a/pbm/storage/s3/s3_test.go b/pbm/storage/s3/s3_test.go index 67e274b40..ffd5edf18 100644 --- a/pbm/storage/s3/s3_test.go +++ b/pbm/storage/s3/s3_test.go @@ -162,6 +162,17 @@ func TestConfig(t *testing.T) { t.Error("expected not to be equal when updating credentials") } }) + + t.Run("Cast succeeds", func(t *testing.T) { + if opts.Region != "" { + t.Error("Start value is not ''") + } + opts.Cast() + + if opts.Region != "us-east-1" { + t.Error("Default value should be set on Cast") + } + }) } func TestRetryer(t *testing.T) {