Skip to content

Commit

Permalink
add more maintenance interval unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed Feb 21, 2025
1 parent 3fb8c72 commit c36122e
Showing 1 changed file with 78 additions and 2 deletions.
80 changes: 78 additions & 2 deletions pkg/repository/udmrepo/kopialib/lib_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/kopia/kopia/repo"
"github.com/kopia/kopia/repo/maintenance"
"github.com/kopia/kopia/repo/manifest"
"github.com/kopia/kopia/repo/object"
"github.com/pkg/errors"
Expand Down Expand Up @@ -272,7 +273,11 @@ func TestWriteInitParameters(t *testing.T) {
repoOpen func(context.Context, string, string, *repo.Options) (repo.Repository, error)
newRepoWriterError error
replaceManifestError error
expectedErr string
// expected replacemanifest params to received by maintenance.SetParams, and therefore writeInitParameters
expectedReplaceManifestsParams *maintenance.Params
// allows for asserting only certain fields are set as expected
assertReplaceManifestsParams func(*maintenance.Params, *maintenance.Params) bool
expectedErr string
}{
{
name: "repo open fail, repo not exist",
Expand Down Expand Up @@ -323,6 +328,65 @@ func TestWriteInitParameters(t *testing.T) {
replaceManifestError: errors.New("fake-replace-manifest-error"),
expectedErr: "error to init write repo parameters: error to set maintenance params: put manifest: fake-replace-manifest-error",
},
{
name: "repo with maintenance interval has expected params",
repoOptions: udmrepo.RepoOptions{
ConfigFilePath: "/tmp",
StorageOptions: map[string]string{
udmrepo.StoreOptionKeyFullMaintenanceInterval: string(udmrepo.FastGC),
},
},
repoOpen: func(context.Context, string, string, *repo.Options) (repo.Repository, error) {
return directRpo, nil
},
returnRepo: new(repomocks.DirectRepository),
returnRepoWriter: new(repomocks.DirectRepositoryWriter),
expectedReplaceManifestsParams: &maintenance.Params{
FullCycle: maintenance.CycleParams{
Interval: udmrepo.FastGCInterval,
},
},
assertReplaceManifestsParams: func(expected, actual *maintenance.Params) bool {
return assert.Equal(t, expected.FullCycle.Interval, actual.FullCycle.Interval)
},
},
{
name: "repo with empty maintenance interval has expected params",
repoOptions: udmrepo.RepoOptions{
ConfigFilePath: "/tmp",
StorageOptions: map[string]string{
udmrepo.StoreOptionKeyFullMaintenanceInterval: string(""),
},
},
repoOpen: func(context.Context, string, string, *repo.Options) (repo.Repository, error) {
return directRpo, nil
},
returnRepo: new(repomocks.DirectRepository),
returnRepoWriter: new(repomocks.DirectRepositoryWriter),
expectedReplaceManifestsParams: &maintenance.Params{
FullCycle: maintenance.CycleParams{
Interval: udmrepo.NormalGCInterval,
},
},
assertReplaceManifestsParams: func(expected, actual *maintenance.Params) bool {
return assert.Equal(t, expected.FullCycle.Interval, actual.FullCycle.Interval)
},
},
{
name: "repo with invalid maintenance interval has expected errors",
repoOptions: udmrepo.RepoOptions{
ConfigFilePath: "/tmp",
StorageOptions: map[string]string{
udmrepo.StoreOptionKeyFullMaintenanceInterval: string("foo"),
},
},
repoOpen: func(context.Context, string, string, *repo.Options) (repo.Repository, error) {
return directRpo, nil
},
returnRepo: new(repomocks.DirectRepository),
returnRepoWriter: new(repomocks.DirectRepositoryWriter),
expectedErr: "error to init write repo parameters: invalid full maintenance interval option foo",
},
}

for _, tc := range testCases {
Expand All @@ -346,7 +410,13 @@ func TestWriteInitParameters(t *testing.T) {

if tc.returnRepoWriter != nil {
tc.returnRepoWriter.On("Close", mock.Anything).Return(nil)
tc.returnRepoWriter.On("ReplaceManifests", mock.Anything, mock.Anything, mock.Anything).Return(manifest.ID(""), tc.replaceManifestError)
if tc.replaceManifestError != nil {
tc.returnRepoWriter.On("ReplaceManifests", mock.Anything, mock.Anything, mock.Anything).Return(manifest.ID(""), tc.replaceManifestError)
}
if tc.expectedReplaceManifestsParams != nil {
tc.returnRepoWriter.On("ReplaceManifests", mock.AnythingOfType("context.backgroundCtx"), mock.AnythingOfType("map[string]string"), mock.AnythingOfType("*maintenance.Params")).Return(manifest.ID(""), nil)
tc.returnRepoWriter.On("Flush", mock.Anything).Return(nil)
}
}

err := writeInitParameters(ctx, tc.repoOptions, logger)
Expand All @@ -356,6 +426,12 @@ func TestWriteInitParameters(t *testing.T) {
} else {
assert.EqualError(t, err, tc.expectedErr)
}
if tc.expectedReplaceManifestsParams != nil {
actualReplaceManifestsParams, converted := tc.returnRepoWriter.Calls[0].Arguments.Get(2).(*maintenance.Params)
assert.True(t, converted)
tc.assertReplaceManifestsParams(tc.expectedReplaceManifestsParams, actualReplaceManifestsParams)
}
// tc.returnRepoWriter.AssertExpectations(t)
})
}
}
Expand Down

0 comments on commit c36122e

Please sign in to comment.