Create a struct that handles: * initialization from existing kopia config info * in-memory updates to config info * detecting which config info structs from kopia need updated * returning kopia config info structs Overall, this allows us to isolate the logic for calculating the new retention configuration info in kopia Viewing by commit may help. First commit just splits up existing code, moving it into either conn.go (will be used later) or retention/opts.go. Subsequent commits switch to using a struct, add tests, and fixup existing logic --- #### Does this PR need a docs update or release note? - [ ] ✅ Yes, it's included - [ ] 🕐 Yes, but in a later PR - [x] ⛔ No #### Type of change - [ ] 🌻 Feature - [ ] 🐛 Bugfix - [ ] 🗺️ Documentation - [ ] 🤖 Supportability/Tests - [ ] 💻 CI/Deployment - [x] 🧹 Tech Debt/Cleanup #### Test Plan - [ ] 💪 Manual - [x] ⚡ Unit test - [ ] 💚 E2E
205 lines
5.2 KiB
Go
205 lines
5.2 KiB
Go
package retention_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/alcionai/clues"
|
|
"github.com/kopia/kopia/repo/blob"
|
|
"github.com/kopia/kopia/repo/format"
|
|
"github.com/kopia/kopia/repo/maintenance"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/alcionai/corso/src/internal/common/ptr"
|
|
"github.com/alcionai/corso/src/internal/kopia/retention"
|
|
"github.com/alcionai/corso/src/internal/tester"
|
|
"github.com/alcionai/corso/src/pkg/control/repository"
|
|
)
|
|
|
|
type OptsUnitSuite struct {
|
|
tester.Suite
|
|
}
|
|
|
|
func TestOptsUnitSuite(t *testing.T) {
|
|
suite.Run(t, &OptsUnitSuite{Suite: tester.NewUnitSuite(t)})
|
|
}
|
|
|
|
func (suite *OptsUnitSuite) TestOptsFromConfigs() {
|
|
var (
|
|
t = suite.T()
|
|
|
|
mode = blob.Governance
|
|
duration = time.Hour * 48
|
|
extend = true
|
|
|
|
blobCfgInput = format.BlobStorageConfiguration{
|
|
RetentionMode: mode,
|
|
RetentionPeriod: duration,
|
|
}
|
|
paramsInput = maintenance.Params{ExtendObjectLocks: extend}
|
|
)
|
|
|
|
ctx, flush := tester.NewContext(t)
|
|
defer flush()
|
|
|
|
opts := retention.OptsFromConfigs(blobCfgInput, paramsInput)
|
|
|
|
assert.False(t, opts.BlobChanged(), "BlobChanged")
|
|
assert.False(t, opts.ParamsChanged(), "ParamsChanged")
|
|
|
|
blobCfg, params, err := opts.AsConfigs(ctx)
|
|
require.NoError(t, err, "AsConfigs: %v", clues.ToCore(err))
|
|
assert.Equal(t, blobCfgInput, blobCfg)
|
|
assert.Equal(t, paramsInput, params)
|
|
}
|
|
|
|
func (suite *OptsUnitSuite) TestSet() {
|
|
var (
|
|
kopiaMode = blob.Governance
|
|
mode = repository.GovernanceRetention
|
|
duration = time.Hour * 48
|
|
)
|
|
|
|
table := []struct {
|
|
name string
|
|
inputBlob format.BlobStorageConfiguration
|
|
inputParams maintenance.Params
|
|
ctrlOpts repository.Retention
|
|
setErr require.ErrorAssertionFunc
|
|
expectMode blob.RetentionMode
|
|
expectDuration time.Duration
|
|
expectExtend bool
|
|
expectBlobChanged bool
|
|
expectParamsChanged bool
|
|
}{
|
|
{
|
|
name: "All Nils",
|
|
setErr: require.NoError,
|
|
},
|
|
{
|
|
name: "All Off",
|
|
ctrlOpts: repository.Retention{
|
|
Mode: ptr.To(repository.NoRetention),
|
|
Duration: ptr.To(time.Duration(0)),
|
|
Extend: ptr.To(false),
|
|
},
|
|
setErr: require.NoError,
|
|
},
|
|
{
|
|
name: "UnknownRetention",
|
|
ctrlOpts: repository.Retention{
|
|
Mode: ptr.To(repository.UnknownRetention),
|
|
Duration: ptr.To(duration),
|
|
},
|
|
setErr: require.Error,
|
|
},
|
|
{
|
|
name: "Invalid Retention Mode",
|
|
ctrlOpts: repository.Retention{
|
|
Mode: ptr.To(repository.RetentionMode(-1)),
|
|
Duration: ptr.To(duration),
|
|
},
|
|
setErr: require.Error,
|
|
},
|
|
{
|
|
name: "Valid Set All",
|
|
ctrlOpts: repository.Retention{
|
|
Mode: ptr.To(mode),
|
|
Duration: ptr.To(duration),
|
|
Extend: ptr.To(true),
|
|
},
|
|
setErr: require.NoError,
|
|
expectMode: kopiaMode,
|
|
expectDuration: duration,
|
|
expectExtend: true,
|
|
expectBlobChanged: true,
|
|
expectParamsChanged: true,
|
|
},
|
|
{
|
|
name: "Valid Set BlobConfig",
|
|
ctrlOpts: repository.Retention{
|
|
Mode: ptr.To(mode),
|
|
Duration: ptr.To(duration),
|
|
},
|
|
setErr: require.NoError,
|
|
expectMode: kopiaMode,
|
|
expectDuration: duration,
|
|
expectBlobChanged: true,
|
|
},
|
|
{
|
|
name: "Valid Set Params",
|
|
ctrlOpts: repository.Retention{
|
|
Extend: ptr.To(true),
|
|
},
|
|
setErr: require.NoError,
|
|
expectExtend: true,
|
|
expectParamsChanged: true,
|
|
},
|
|
{
|
|
name: "Partial BlobConfig Change",
|
|
inputBlob: format.BlobStorageConfiguration{
|
|
RetentionMode: kopiaMode,
|
|
RetentionPeriod: duration,
|
|
},
|
|
ctrlOpts: repository.Retention{
|
|
Duration: ptr.To(duration + time.Hour),
|
|
},
|
|
setErr: require.NoError,
|
|
expectMode: kopiaMode,
|
|
expectDuration: duration + time.Hour,
|
|
expectBlobChanged: true,
|
|
},
|
|
{
|
|
name: "No BlobConfig Change",
|
|
inputBlob: format.BlobStorageConfiguration{
|
|
RetentionMode: kopiaMode,
|
|
RetentionPeriod: duration,
|
|
},
|
|
ctrlOpts: repository.Retention{
|
|
Mode: ptr.To(mode),
|
|
Duration: ptr.To(duration),
|
|
},
|
|
setErr: require.NoError,
|
|
expectMode: kopiaMode,
|
|
expectDuration: duration,
|
|
},
|
|
{
|
|
name: "No Params Change",
|
|
inputParams: maintenance.Params{ExtendObjectLocks: true},
|
|
ctrlOpts: repository.Retention{
|
|
Extend: ptr.To(true),
|
|
},
|
|
setErr: require.NoError,
|
|
expectExtend: true,
|
|
},
|
|
}
|
|
|
|
for _, test := range table {
|
|
suite.Run(test.name, func() {
|
|
t := suite.T()
|
|
|
|
ctx, flush := tester.NewContext(t)
|
|
defer flush()
|
|
|
|
opts := retention.OptsFromConfigs(test.inputBlob, test.inputParams)
|
|
err := opts.Set(test.ctrlOpts)
|
|
test.setErr(t, err, "setting params: %v", clues.ToCore(err))
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
blobCfg, params, err := opts.AsConfigs(ctx)
|
|
require.NoError(t, err, "getting configs: %v", clues.ToCore(err))
|
|
|
|
assert.Equal(t, test.expectMode, blobCfg.RetentionMode, "mode")
|
|
assert.Equal(t, test.expectDuration, blobCfg.RetentionPeriod, "duration")
|
|
assert.Equal(t, test.expectExtend, params.ExtendObjectLocks, "extend locks")
|
|
assert.Equal(t, test.expectBlobChanged, opts.BlobChanged(), "blob changed")
|
|
assert.Equal(t, test.expectParamsChanged, opts.ParamsChanged(), "params changed")
|
|
})
|
|
}
|
|
}
|