Update tests in kopia package (#2514)

## Description

Use the new suite.Suite wrappers in tester package. Also update suite.Run and some nolint directives.

## 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
- [x] 🤖 Test
- [ ] 💻 CI/Deployment
- [x] 🧹 Tech Debt/Cleanup

## Issue(s)

* #2373

## Test Plan

- [ ] 💪 Manual
- [x]  Unit test
- [ ] 💚 E2E
This commit is contained in:
ashmrtn 2023-02-15 18:22:43 -08:00 committed by GitHub
parent 034ee0df39
commit 476a931ccb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 146 additions and 117 deletions

View File

@ -33,11 +33,11 @@ func openKopiaRepo(t *testing.T, ctx context.Context) (*conn, error) {
// unit tests
// ---------------
type WrapperUnitSuite struct {
suite.Suite
tester.Suite
}
func TestWrapperUnitSuite(t *testing.T) {
suite.Run(t, new(WrapperUnitSuite))
suite.Run(t, &WrapperUnitSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *WrapperUnitSuite) TestCloseWithoutOpenDoesNotCrash() {
@ -55,20 +55,17 @@ func (suite *WrapperUnitSuite) TestCloseWithoutOpenDoesNotCrash() {
// integration tests that use kopia
// ---------------
type WrapperIntegrationSuite struct {
suite.Suite
tester.Suite
}
func TestWrapperIntegrationSuite(t *testing.T) {
tester.RunOnAny(
t,
tester.CorsoCITests,
tester.CorsoKopiaWrapperTests)
suite.Run(t, new(WrapperIntegrationSuite))
}
func (suite *WrapperIntegrationSuite) SetupSuite() {
tester.MustGetEnvSets(suite.T(), tester.AWSStorageCredEnvs)
suite.Run(t, &WrapperIntegrationSuite{
Suite: tester.NewIntegrationSuite(
t,
[][]string{tester.AWSStorageCredEnvs},
tester.CorsoKopiaWrapperTests,
),
})
}
func (suite *WrapperIntegrationSuite) TestRepoExistsError() {
@ -296,10 +293,12 @@ func (suite *WrapperIntegrationSuite) TestConfigDefaultsSetOnInitAndConnect() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
ctx, flush := tester.NewContext()
defer flush()
t := suite.T()
k, err := openKopiaRepo(t, ctx)
require.NoError(t, err)

View File

@ -23,11 +23,11 @@ import (
// unit tests
// ---------------
type KopiaDataCollectionUnitSuite struct {
suite.Suite
tester.Suite
}
func TestKopiaDataCollectionUnitSuite(t *testing.T) {
suite.Run(t, new(KopiaDataCollectionUnitSuite))
suite.Run(t, &KopiaDataCollectionUnitSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *KopiaDataCollectionUnitSuite) TestReturnsPath() {
@ -93,7 +93,9 @@ func (suite *KopiaDataCollectionUnitSuite) TestReturnsStreams() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
c := kopiaDataCollection{
streams: test.streams,
path: nil,

View File

@ -23,9 +23,8 @@ type fooModel struct {
Bar string
}
//revive:disable:context-as-argument
//revive:disable-next-line:context-as-argument
func getModelStore(t *testing.T, ctx context.Context) *ModelStore {
//revive:enable:context-as-argument
c, err := openKopiaRepo(t, ctx)
require.NoError(t, err)
@ -36,11 +35,11 @@ func getModelStore(t *testing.T, ctx context.Context) *ModelStore {
// unit tests
// ---------------
type ModelStoreUnitSuite struct {
suite.Suite
tester.Suite
}
func TestModelStoreUnitSuite(t *testing.T) {
suite.Run(t, new(ModelStoreUnitSuite))
suite.Run(t, &ModelStoreUnitSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *ModelStoreUnitSuite) TestCloseWithoutInitDoesNotPanic() {
@ -57,23 +56,20 @@ func (suite *ModelStoreUnitSuite) TestCloseWithoutInitDoesNotPanic() {
// integration tests that use kopia
// ---------------
type ModelStoreIntegrationSuite struct {
suite.Suite
tester.Suite
ctx context.Context
m *ModelStore
flush func()
}
func TestModelStoreIntegrationSuite(t *testing.T) {
tester.RunOnAny(
t,
tester.CorsoCITests,
tester.CorsoModelStoreTests)
suite.Run(t, new(ModelStoreIntegrationSuite))
}
func (suite *ModelStoreIntegrationSuite) SetupSuite() {
tester.MustGetEnvSets(suite.T(), tester.AWSStorageCredEnvs)
suite.Run(t, &ModelStoreIntegrationSuite{
Suite: tester.NewIntegrationSuite(
t,
[][]string{tester.AWSStorageCredEnvs},
tester.CorsoModelStoreTests,
),
})
}
func (suite *ModelStoreIntegrationSuite) SetupTest() {
@ -112,7 +108,9 @@ func (suite *ModelStoreIntegrationSuite) TestBadTagsErrors() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
foo := &fooModel{Bar: uuid.NewString()}
foo.Tags = test.tags
@ -258,7 +256,9 @@ func (suite *ModelStoreIntegrationSuite) TestPutGet() {
}
for _, test := range table {
suite.T().Run(test.s.String(), func(t *testing.T) {
suite.Run(test.s.String(), func() {
t := suite.T()
foo := &fooModel{Bar: uuid.NewString()}
// Avoid some silly test errors from comparing nil to empty map.
foo.Tags = map[string]string{}
@ -306,7 +306,9 @@ func (suite *ModelStoreIntegrationSuite) TestPutGet_PreSetID() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
foo := &fooModel{
BaseModel: model.BaseModel{ID: model.StableID(test.baseID)},
Bar: uuid.NewString(),
@ -411,7 +413,9 @@ func (suite *ModelStoreIntegrationSuite) TestPutGetOfType() {
}
for _, test := range table {
suite.T().Run(test.s.String(), func(t *testing.T) {
suite.Run(test.s.String(), func() {
t := suite.T()
foo := &fooModel{Bar: uuid.NewString()}
err := suite.m.Put(suite.ctx, test.s, foo)
@ -546,7 +550,9 @@ func (suite *ModelStoreIntegrationSuite) TestGetOfTypeWithTags() {
// Check we can properly execute our tests.
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
expected := make([]*model.BaseModel, 0, len(test.expectedModels))
for _, e := range test.expectedModels {
expected = append(expected, &e.BaseModel)
@ -585,7 +591,9 @@ func (suite *ModelStoreIntegrationSuite) TestPutUpdate() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
ctx, flush := tester.NewContext()
defer flush()
@ -660,7 +668,9 @@ func (suite *ModelStoreIntegrationSuite) TestPutUpdate_FailsNotMatchingPrev() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
ctx, flush := tester.NewContext()
defer flush()
@ -706,20 +716,17 @@ func (suite *ModelStoreIntegrationSuite) TestPutDelete_BadIDsNoop() {
// regression tests that use kopia
// ---------------
type ModelStoreRegressionSuite struct {
suite.Suite
tester.Suite
}
func TestModelStoreRegressionSuite(t *testing.T) {
tester.RunOnAny(
t,
tester.CorsoCITests,
tester.CorsoModelStoreTests)
suite.Run(t, new(ModelStoreRegressionSuite))
}
func (suite *ModelStoreRegressionSuite) SetupSuite() {
tester.MustGetEnvSets(suite.T(), tester.AWSStorageCredEnvs)
suite.Run(t, &ModelStoreRegressionSuite{
Suite: tester.NewIntegrationSuite(
t,
[][]string{tester.AWSStorageCredEnvs},
tester.CorsoModelStoreTests,
),
})
}
// TODO(ashmrtn): Make a mock of whatever controls the handle to kopia so we can
@ -784,12 +791,10 @@ func (suite *ModelStoreRegressionSuite) TestFailDuringWriteSessionHasNoVisibleEf
assert.Equal(t, foo, returned)
}
//revive:disable:context-as-argument
func openConnAndModelStore(
t *testing.T,
ctx context.Context,
ctx context.Context, //revive:disable-line:context-as-argument
) (*conn, *ModelStore) {
//revive:enable:context-as-argument
st := tester.NewPrefixedS3Storage(t)
c := NewConn(st)
@ -805,13 +810,11 @@ func openConnAndModelStore(
return c, ms
}
//revive:disable:context-as-argument
func reconnectToModelStore(
t *testing.T,
ctx context.Context,
ctx context.Context, //revive:disable-line:context-as-argument
c *conn,
) *ModelStore {
//revive:enable:context-as-argument
require.NoError(t, c.Connect(ctx))
defer func() {

View File

@ -7,14 +7,16 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/alcionai/corso/src/internal/tester"
)
type PathEncoderSuite struct {
suite.Suite
tester.Suite
}
func TestPathEncoderSuite(t *testing.T) {
suite.Run(t, new(PathEncoderSuite))
suite.Run(t, &PathEncoderSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *PathEncoderSuite) TestEncodeDecode() {
@ -66,7 +68,9 @@ func (suite *PathEncoderSuite) TestEncodeAsPathDecode() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
encoded := encodeAsPath(test.elements...)
// Sanity check, first and last character should not be '/'.

View File

@ -187,11 +187,11 @@ func (msm *mockSnapshotManager) LoadSnapshots(
}
type SnapshotFetchUnitSuite struct {
suite.Suite
tester.Suite
}
func TestSnapshotFetchUnitSuite(t *testing.T) {
suite.Run(t, new(SnapshotFetchUnitSuite))
suite.Run(t, &SnapshotFetchUnitSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *SnapshotFetchUnitSuite) TestFetchPrevSnapshots() {
@ -752,7 +752,9 @@ func (suite *SnapshotFetchUnitSuite) TestFetchPrevSnapshots() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
ctx, flush := tester.NewContext()
defer flush()
@ -874,7 +876,9 @@ func (suite *SnapshotFetchUnitSuite) TestFetchPrevSnapshots_customTags() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
ctx, flush := tester.NewContext()
defer flush()

View File

@ -93,15 +93,12 @@ func expectedTreeWithChildren(
// Currently only works for files that Corso has serialized as it expects a
// version specifier at the start of the file.
//
//revive:disable:context-as-argument
func expectFileData(
t *testing.T,
ctx context.Context,
ctx context.Context, //revive:disable-line:context-as-argument
expected []byte,
f fs.StreamingFile,
) {
//revive:enable:context-as-argument
t.Helper()
if len(expected) == 0 {
@ -132,14 +129,12 @@ func expectFileData(
assert.Equalf(t, expected, got, "data in file: %s", name)
}
//revive:disable:context-as-argument
func expectTree(
t *testing.T,
ctx context.Context,
ctx context.Context, //revive:disable-line:context-as-argument
expected *expectedNode,
got fs.Entry,
) {
//revive:enable:context-as-argument
t.Helper()
if expected == nil {
@ -199,13 +194,11 @@ func expectDirs(
assert.Subset(t, names, dirs)
}
//revive:disable:context-as-argument
func getDirEntriesForEntry(
t *testing.T,
ctx context.Context,
ctx context.Context, //revive:disable-line:context-as-argument
entry fs.Entry,
) []fs.Entry {
//revive:enable:context-as-argument
d, ok := entry.(fs.Directory)
require.True(t, ok, "entry is not a directory")
@ -238,11 +231,11 @@ func (lrr *limitedRangeReader) Read(p []byte) (int, error) {
}
type VersionReadersUnitSuite struct {
suite.Suite
tester.Suite
}
func TestVersionReadersUnitSuite(t *testing.T) {
suite.Run(t, new(VersionReadersUnitSuite))
suite.Run(t, &VersionReadersUnitSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *VersionReadersUnitSuite) TestWriteAndRead() {
@ -268,7 +261,9 @@ func (suite *VersionReadersUnitSuite) TestWriteAndRead() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
baseReader := bytes.NewReader(inputData)
reversible := &restoreStreamReader{
@ -347,13 +342,13 @@ func (suite *VersionReadersUnitSuite) TestWriteHandlesShortReads() {
}
type CorsoProgressUnitSuite struct {
suite.Suite
tester.Suite
targetFilePath path.Path
targetFileName string
}
func TestCorsoProgressUnitSuite(t *testing.T) {
suite.Run(t, new(CorsoProgressUnitSuite))
suite.Run(t, &CorsoProgressUnitSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *CorsoProgressUnitSuite) SetupSuite() {
@ -449,9 +444,11 @@ func (suite *CorsoProgressUnitSuite) TestFinishedFile() {
}
for _, cachedTest := range table {
suite.T().Run(cachedTest.name, func(outerT *testing.T) {
suite.Run(cachedTest.name, func() {
for _, test := range finishedFileTable {
outerT.Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
bd := &details.Builder{}
cp := corsoProgress{
UploadProgress: &snapshotfs.NullUploadProgress{},
@ -632,7 +629,9 @@ func (suite *CorsoProgressUnitSuite) TestFinishedFileBaseItemDoesntBuildHierarch
func (suite *CorsoProgressUnitSuite) TestFinishedHashingFile() {
for _, test := range finishedFileTable {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
bd := &details.Builder{}
cp := corsoProgress{
UploadProgress: &snapshotfs.NullUploadProgress{},
@ -654,7 +653,7 @@ func (suite *CorsoProgressUnitSuite) TestFinishedHashingFile() {
}
type HierarchyBuilderUnitSuite struct {
suite.Suite
tester.Suite
testStoragePath path.Path
testLocationPath path.Path
}
@ -671,7 +670,7 @@ func (suite *HierarchyBuilderUnitSuite) SetupSuite() {
}
func TestHierarchyBuilderUnitSuite(t *testing.T) {
suite.Run(t, new(HierarchyBuilderUnitSuite))
suite.Run(t, &HierarchyBuilderUnitSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *HierarchyBuilderUnitSuite) TestBuildDirectoryTree() {
@ -812,7 +811,9 @@ func (suite *HierarchyBuilderUnitSuite) TestBuildDirectoryTree_MixedDirectory()
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
progress := &corsoProgress{
pending: map[string]*itemDetails{},
errs: fault.New(true),
@ -916,7 +917,9 @@ func (suite *HierarchyBuilderUnitSuite) TestBuildDirectoryTree_Fails() {
ctx, flush := tester.NewContext()
defer flush()
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
_, err := inflateDirTree(ctx, nil, nil, test.layout, nil, nil)
assert.Error(t, err)
})
@ -997,7 +1000,9 @@ func (suite *HierarchyBuilderUnitSuite) TestBuildDirectoryTreeErrors() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
tester.LogTimeOfTest(t)
ctx, flush := tester.NewContext()
@ -1286,7 +1291,9 @@ func (suite *HierarchyBuilderUnitSuite) TestBuildDirectoryTreeSingleSubtree() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
tester.LogTimeOfTest(t)
ctx, flush := tester.NewContext()
@ -2061,7 +2068,9 @@ func (suite *HierarchyBuilderUnitSuite) TestBuildDirectoryTreeMultipleSubdirecto
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
tester.LogTimeOfTest(t)
ctx, flush := tester.NewContext()

View File

@ -85,15 +85,13 @@ func testForFiles(
assert.Equal(t, len(expected), count)
}
//revive:disable:context-as-argument
func checkSnapshotTags(
t *testing.T,
ctx context.Context,
ctx context.Context, //revive:disable-line:context-as-argument
rep repo.Repository,
expectedTags map[string]string,
snapshotID string,
) {
//revive:enable:context-as-argument
man, err := snapshot.LoadSnapshot(ctx, rep, manifest.ID(snapshotID))
require.NoError(t, err)
assert.Equal(t, expectedTags, man.Tags)
@ -103,7 +101,7 @@ func checkSnapshotTags(
// unit tests
// ---------------
type KopiaUnitSuite struct {
suite.Suite
tester.Suite
testPath path.Path
}
@ -124,7 +122,7 @@ func (suite *KopiaUnitSuite) SetupSuite() {
}
func TestKopiaUnitSuite(t *testing.T) {
suite.Run(t, new(KopiaUnitSuite))
suite.Run(t, &KopiaUnitSuite{Suite: tester.NewUnitSuite(t)})
}
func (suite *KopiaUnitSuite) TestCloseWithoutInitDoesNotPanic() {
@ -141,7 +139,7 @@ func (suite *KopiaUnitSuite) TestCloseWithoutInitDoesNotPanic() {
// integration tests that use kopia
// ---------------
type KopiaIntegrationSuite struct {
suite.Suite
tester.Suite
w *Wrapper
ctx context.Context
flush func()
@ -153,17 +151,16 @@ type KopiaIntegrationSuite struct {
}
func TestKopiaIntegrationSuite(t *testing.T) {
tester.RunOnAny(
t,
tester.CorsoCITests,
tester.CorsoKopiaWrapperTests)
suite.Run(t, new(KopiaIntegrationSuite))
suite.Run(t, &KopiaIntegrationSuite{
Suite: tester.NewIntegrationSuite(
t,
[][]string{tester.AWSStorageCredEnvs},
tester.CorsoKopiaWrapperTests,
),
})
}
func (suite *KopiaIntegrationSuite) SetupSuite() {
tester.MustGetEnvSets(suite.T(), tester.AWSStorageCredEnvs)
tmp, err := path.Builder{}.Append(testInboxDir).ToDataLayerExchangePathForCategory(
testTenant,
testUser,
@ -266,7 +263,9 @@ func (suite *KopiaIntegrationSuite) TestBackupCollections() {
prevSnaps := []IncrementalBase{}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
stats, deets, _, err := suite.w.BackupCollections(
suite.ctx,
prevSnaps,
@ -530,7 +529,9 @@ func (suite *KopiaIntegrationSuite) TestBackupCollectionsHandlesNoCollections()
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
ctx, flush := tester.NewContext()
defer flush()
@ -551,7 +552,7 @@ func (suite *KopiaIntegrationSuite) TestBackupCollectionsHandlesNoCollections()
}
type KopiaSimpleRepoIntegrationSuite struct {
suite.Suite
tester.Suite
w *Wrapper
ctx context.Context
snapshotID manifest.ID
@ -566,17 +567,16 @@ type KopiaSimpleRepoIntegrationSuite struct {
}
func TestKopiaSimpleRepoIntegrationSuite(t *testing.T) {
tester.RunOnAny(
t,
tester.CorsoCITests,
tester.CorsoKopiaWrapperTests)
suite.Run(t, new(KopiaSimpleRepoIntegrationSuite))
suite.Run(t, &KopiaSimpleRepoIntegrationSuite{
Suite: tester.NewIntegrationSuite(
t,
[][]string{tester.AWSStorageCredEnvs},
tester.CorsoKopiaWrapperTests,
),
})
}
func (suite *KopiaSimpleRepoIntegrationSuite) SetupSuite() {
tester.MustGetEnvSets(suite.T(), tester.AWSStorageCredEnvs)
tmp, err := path.Builder{}.Append(testInboxDir).ToDataLayerExchangePathForCategory(
testTenant,
testUser,
@ -808,7 +808,9 @@ func (suite *KopiaSimpleRepoIntegrationSuite) TestBackupExcludeItem() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
var excluded map[string]struct{}
if test.excludeItem {
excluded = map[string]struct{}{
@ -923,7 +925,9 @@ func (suite *KopiaSimpleRepoIntegrationSuite) TestRestoreMultipleItems() {
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
// May slightly overallocate as only items that are actually in our map
// are expected. The rest are errors, but best-effort says it should carry
// on even then.
@ -986,7 +990,9 @@ func (suite *KopiaSimpleRepoIntegrationSuite) TestRestoreMultipleItems_Errors()
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
c, err := suite.w.RestoreMultipleItems(
suite.ctx,
test.snapshotID,
@ -1037,7 +1043,9 @@ func (suite *KopiaSimpleRepoIntegrationSuite) TestDeleteSnapshot_BadIDs() {
},
}
for _, test := range table {
suite.T().Run(test.name, func(t *testing.T) {
suite.Run(test.name, func() {
t := suite.T()
test.expect(t, suite.w.DeleteSnapshot(suite.ctx, test.snapshotID))
})
}