Skip to content

Commit

Permalink
test(general): prefer require (kopia#4183)
Browse files Browse the repository at this point in the history
No functional changes to the tests
  • Loading branch information
julio-lopez authored Oct 18, 2024
1 parent 5edc81b commit d7a26b3
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 108 deletions.
65 changes: 21 additions & 44 deletions internal/repotesting/repotesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,11 @@ func (e *Environment) setup(tb testing.TB, version format.Version, opts ...Optio
e.Password = DefaultPasswordForTesting
}

if err := repo.Initialize(ctx, st, opt, e.Password); err != nil {
tb.Fatalf("err: %v", err)
}
err := repo.Initialize(ctx, st, opt, e.Password)
require.NoError(tb, err)

if err := repo.Connect(ctx, e.ConfigFile(), st, e.Password, nil); err != nil {
tb.Fatalf("can't connect: %v", err)
}
err = repo.Connect(ctx, e.ConfigFile(), st, e.Password, nil)
require.NoError(tb, err, "can't connect")

e.connected = true

Expand All @@ -118,15 +116,12 @@ func (e *Environment) setup(tb testing.TB, version format.Version, opts ...Optio
defer cancel()

rep, err := repo.Open(ctx2, e.ConfigFile(), e.Password, openOpt)

require.NoError(tb, err)

e.Repository = rep

_, e.RepositoryWriter, err = rep.(repo.DirectRepository).NewDirectWriter(ctx, repo.WriteSessionOptions{Purpose: "test"})
if err != nil {
tb.Fatal(err)
}
require.NoError(tb, err)

tb.Cleanup(func() {
e.RepositoryWriter.Close(ctx)
Expand All @@ -140,20 +135,17 @@ func (e *Environment) setup(tb testing.TB, version format.Version, opts ...Optio
func (e *Environment) Close(ctx context.Context, tb testing.TB) {
tb.Helper()

if err := e.RepositoryWriter.Close(ctx); err != nil {
tb.Fatalf("unable to close: %v", err)
}
err := e.RepositoryWriter.Close(ctx)
require.NoError(tb, err, "unable to close")

if e.connected {
if err := repo.Disconnect(ctx, e.ConfigFile()); err != nil {
tb.Errorf("error disconnecting: %v", err)
}
err := repo.Disconnect(ctx, e.ConfigFile())
require.NoError(tb, err, "error disconnecting")
}

if err := os.Remove(e.configDir); err != nil {
// should be empty, assuming Disconnect was successful
tb.Errorf("error removing config directory: %v", err)
}
err = os.Remove(e.configDir)
// should be empty, assuming Disconnect was successful
require.NoError(tb, err, "error removing config directory")
}

// ConfigFile returns the name of the config file.
Expand All @@ -168,25 +160,19 @@ func (e *Environment) MustReopen(tb testing.TB, openOpts ...func(*repo.Options))
ctx := testlogging.Context(tb)

err := e.RepositoryWriter.Close(ctx)
if err != nil {
tb.Fatalf("close error: %v", err)
}
require.NoError(tb, err, "close error")

// ensure context passed to Open() is not used for cancellation signal.
ctx2, cancel := context.WithCancel(ctx)
defer cancel()

rep, err := repo.Open(ctx2, e.ConfigFile(), e.Password, repoOptions(openOpts))
if err != nil {
tb.Fatalf("err: %v", err)
}
require.NoError(tb, err)

tb.Cleanup(func() { rep.Close(ctx) })

_, e.RepositoryWriter, err = rep.(repo.DirectRepository).NewDirectWriter(ctx, repo.WriteSessionOptions{Purpose: "test"})
if err != nil {
tb.Fatalf("err: %v", err)
}
require.NoError(tb, err)
}

// MustOpenAnother opens another repository backed by the same storage location.
Expand All @@ -196,18 +182,14 @@ func (e *Environment) MustOpenAnother(tb testing.TB, openOpts ...func(*repo.Opti
ctx := testlogging.Context(tb)

rep2, err := repo.Open(ctx, e.ConfigFile(), e.Password, repoOptions(openOpts))
if err != nil {
tb.Fatalf("err: %v", err)
}
require.NoError(tb, err)

tb.Cleanup(func() {
rep2.Close(ctx)
})

_, w, err := rep2.NewWriter(ctx, repo.WriteSessionOptions{Purpose: "test"})
if err != nil {
tb.Fatal(err)
}
require.NoError(tb, err)

return w
}
Expand All @@ -226,14 +208,11 @@ func (e *Environment) MustConnectOpenAnother(tb testing.TB, openOpts ...func(*re
},
}

if err := repo.Connect(ctx, config, e.st, e.Password, connOpts); err != nil {
tb.Fatal("can't connect:", err)
}
err := repo.Connect(ctx, config, e.st, e.Password, connOpts)
require.NoError(tb, err, "can't connect")

rep, err := repo.Open(ctx, e.ConfigFile(), e.Password, repoOptions(openOpts))
if err != nil {
tb.Fatal("can't open:", err)
}
require.NoError(tb, err, "can't open")

return rep
}
Expand All @@ -249,9 +228,7 @@ func (e *Environment) VerifyBlobCount(tb testing.TB, want int) {
return nil
})

if got != want {
tb.Errorf("got unexpected number of BLOBs: %v, wanted %v", got, want)
}
require.Equal(tb, want, got, "got unexpected number of BLOBs")
}

// LocalPathSourceInfo is a convenience method that returns SourceInfo for the local user and path.
Expand Down
52 changes: 20 additions & 32 deletions repo/maintenance/blob_gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

"github.com/kopia/kopia/internal/clock"
Expand Down Expand Up @@ -55,11 +54,9 @@ func (s *formatSpecificTestSuite) TestDeleteUnreferencedBlobs(t *testing.T) {
env.RepositoryWriter.Flush(ctx)

blobsBefore, err := blob.ListAllBlobs(ctx, env.RepositoryWriter.BlobStorage(), "")
require.NoError(t, err)

if got, want := len(blobsBefore), 4; got != want {
t.Fatalf("unexpected number of blobs after writing: %v", blobsBefore)
}
require.NoError(t, err)
require.Len(t, blobsBefore, 4, "unexpected number of blobs after writing")

// add some more unreferenced blobs
const (
Expand All @@ -73,9 +70,8 @@ func (s *formatSpecificTestSuite) TestDeleteUnreferencedBlobs(t *testing.T) {
verifyBlobExists(t, env.RepositoryWriter.BlobStorage(), extraBlobID2)

// new blobs not will be deleted because of minimum age requirement
if _, err = maintenance.DeleteUnreferencedBlobs(ctx, env.RepositoryWriter, maintenance.DeleteUnreferencedBlobsOptions{}, maintenance.SafetyFull); err != nil {
t.Fatal(err)
}
_, err = maintenance.DeleteUnreferencedBlobs(ctx, env.RepositoryWriter, maintenance.DeleteUnreferencedBlobsOptions{}, maintenance.SafetyFull)
require.NoError(t, err)

verifyBlobExists(t, env.RepositoryWriter.BlobStorage(), extraBlobID1)
verifyBlobExists(t, env.RepositoryWriter.BlobStorage(), extraBlobID2)
Expand All @@ -87,9 +83,8 @@ func (s *formatSpecificTestSuite) TestDeleteUnreferencedBlobs(t *testing.T) {
}

// new blobs will be deleted
if _, err = maintenance.DeleteUnreferencedBlobs(ctx, env.RepositoryWriter, maintenance.DeleteUnreferencedBlobsOptions{}, maintenance.SafetyNone); err != nil {
t.Fatal(err)
}
_, err = maintenance.DeleteUnreferencedBlobs(ctx, env.RepositoryWriter, maintenance.DeleteUnreferencedBlobsOptions{}, maintenance.SafetyNone)
require.NoError(t, err)

verifyBlobNotFound(t, env.RepositoryWriter.BlobStorage(), extraBlobID1)
verifyBlobNotFound(t, env.RepositoryWriter.BlobStorage(), extraBlobID2)
Expand All @@ -112,9 +107,8 @@ func (s *formatSpecificTestSuite) TestDeleteUnreferencedBlobs(t *testing.T) {
CheckpointTime: ta.NowFunc()(),
})

if _, err = maintenance.DeleteUnreferencedBlobs(ctx, env.RepositoryWriter, maintenance.DeleteUnreferencedBlobsOptions{}, safetyFastDeleteLongSessionExpiration); err != nil {
t.Fatal(err)
}
_, err = maintenance.DeleteUnreferencedBlobs(ctx, env.RepositoryWriter, maintenance.DeleteUnreferencedBlobsOptions{}, safetyFastDeleteLongSessionExpiration)
require.NoError(t, err)

verifyBlobExists(t, env.RepositoryWriter.BlobStorage(), extraBlobIDWithSession1)
verifyBlobExists(t, env.RepositoryWriter.BlobStorage(), extraBlobIDWithSession2)
Expand All @@ -125,9 +119,8 @@ func (s *formatSpecificTestSuite) TestDeleteUnreferencedBlobs(t *testing.T) {
// now finish session 2
env.RepositoryWriter.BlobStorage().DeleteBlob(ctx, session2Marker)

if _, err = maintenance.DeleteUnreferencedBlobs(ctx, env.RepositoryWriter, maintenance.DeleteUnreferencedBlobsOptions{}, safetyFastDeleteLongSessionExpiration); err != nil {
t.Fatal(err)
}
_, err = maintenance.DeleteUnreferencedBlobs(ctx, env.RepositoryWriter, maintenance.DeleteUnreferencedBlobsOptions{}, safetyFastDeleteLongSessionExpiration)
require.NoError(t, err)

verifyBlobExists(t, env.RepositoryWriter.BlobStorage(), extraBlobIDWithSession1)
verifyBlobExists(t, env.RepositoryWriter.BlobStorage(), extraBlobIDWithSession2)
Expand All @@ -138,9 +131,8 @@ func (s *formatSpecificTestSuite) TestDeleteUnreferencedBlobs(t *testing.T) {
// now move time into the future making session 1 timed out
ta.Advance(7 * 24 * time.Hour)

if _, err = maintenance.DeleteUnreferencedBlobs(ctx, env.RepositoryWriter, maintenance.DeleteUnreferencedBlobsOptions{}, maintenance.SafetyFull); err != nil {
t.Fatal(err)
}
_, err = maintenance.DeleteUnreferencedBlobs(ctx, env.RepositoryWriter, maintenance.DeleteUnreferencedBlobsOptions{}, maintenance.SafetyFull)
require.NoError(t, err)

verifyBlobNotFound(t, env.RepositoryWriter.BlobStorage(), extraBlobIDWithSession1)
verifyBlobNotFound(t, env.RepositoryWriter.BlobStorage(), extraBlobIDWithSession2)
Expand All @@ -153,33 +145,29 @@ func (s *formatSpecificTestSuite) TestDeleteUnreferencedBlobs(t *testing.T) {
blobsAfter, err := blob.ListAllBlobs(ctx, env.RepositoryWriter.BlobStorage(), "")
require.NoError(t, err)

if diff := cmp.Diff(blobsBefore, blobsAfter); diff != "" {
t.Fatalf("unexpected diff: %v", diff)
}
diff := cmp.Diff(blobsBefore, blobsAfter)
require.Empty(t, diff, "unexpected blobs")
}

func verifyBlobExists(t *testing.T, st blob.Storage, blobID blob.ID) {
t.Helper()

if _, err := st.GetMetadata(testlogging.Context(t), blobID); err != nil {
t.Fatalf("expected blob %v to exist, got %v", blobID, err)
}
_, err := st.GetMetadata(testlogging.Context(t), blobID)
require.NoError(t, err)
}

func verifyBlobNotFound(t *testing.T, st blob.Storage, blobID blob.ID) {
t.Helper()

if _, err := st.GetMetadata(testlogging.Context(t), blobID); !errors.Is(err, blob.ErrBlobNotFound) {
t.Fatalf("expected blob %v to be not found, got %v", blobID, err)
}
_, err := st.GetMetadata(testlogging.Context(t), blobID)
require.ErrorIsf(t, err, blob.ErrBlobNotFound, "expected blob %v to be not found", blobID)
}

func mustPutDummyBlob(t *testing.T, st blob.Storage, blobID blob.ID) {
t.Helper()

if err := st.PutBlob(testlogging.Context(t), blobID, gather.FromSlice([]byte{1, 2, 3}), blob.PutOptions{}); err != nil {
t.Fatal(err)
}
err := st.PutBlob(testlogging.Context(t), blobID, gather.FromSlice([]byte{1, 2, 3}), blob.PutOptions{})
require.NoError(t, err)
}

func mustPutDummySessionBlob(t *testing.T, st blob.Storage, sessionIDSuffix blob.ID, si *content.SessionInfo) blob.ID {
Expand Down
12 changes: 4 additions & 8 deletions repo/maintenance/blob_retain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ func (s *formatSpecificTestSuite) TestExtendBlobRetentionTime(t *testing.T) {
env.RepositoryWriter.Flush(ctx)

blobsBefore, err := blob.ListAllBlobs(ctx, env.RepositoryWriter.BlobStorage(), "")
require.NoError(t, err)

if got, want := len(blobsBefore), 4; got != want {
t.Fatalf("unexpected number of blobs after writing: %v", blobsBefore)
}
require.NoError(t, err)
require.Len(t, blobsBefore, 4, "unexpected number of blobs after writing")

lastBlobIdx := len(blobsBefore) - 1
st := env.RootStorage().(blobtesting.RetentionStorage)
Expand Down Expand Up @@ -106,11 +104,9 @@ func (s *formatSpecificTestSuite) TestExtendBlobRetentionTimeDisabled(t *testing
env.RepositoryWriter.Flush(ctx)

blobsBefore, err := blob.ListAllBlobs(ctx, env.RepositoryWriter.BlobStorage(), "")
require.NoError(t, err)

if got, want := len(blobsBefore), 4; got != want {
t.Fatalf("unexpected number of blobs after writing: %v", blobsBefore)
}
require.NoError(t, err)
require.Len(t, blobsBefore, 4, "unexpected number of blobs after writing")

// Need to continue using TouchBlob because the environment only supports the
// locking map if no retention time is given.
Expand Down
5 changes: 2 additions & 3 deletions repo/maintenance/maintenance_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ func TestFindSafeDropTime(t *testing.T) {
}

for _, tc := range cases {
if got, want := findSafeDropTime(tc.runs, SafetyFull), tc.wantTime; !got.Equal(want) {
t.Errorf("invalid safe drop time for %v: %v, want %v", tc.runs, got, want)
}
got := findSafeDropTime(tc.runs, SafetyFull)
require.Equalf(t, tc.wantTime, got, "invalid safe drop time for %v", tc.runs)
}
}
29 changes: 8 additions & 21 deletions repo/maintenance/maintenance_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"
"time"

"github.com/kylelemons/godebug/pretty"
"github.com/stretchr/testify/require"

"github.com/kopia/kopia/internal/clock"
Expand All @@ -15,19 +14,11 @@ import (

func (s *formatSpecificTestSuite) TestMaintenanceSchedule(t *testing.T) {
ctx, env := repotesting.NewEnvironment(t, s.formatVersion)

sch, err := maintenance.GetSchedule(ctx, env.RepositoryWriter)
if err != nil {
t.Fatalf("err: %v", err)
}

if !sch.NextFullMaintenanceTime.IsZero() {
t.Errorf("unexpected NextFullMaintenanceTime: %v", sch.NextFullMaintenanceTime)
}

if !sch.NextQuickMaintenanceTime.IsZero() {
t.Errorf("unexpected NextQuickMaintenanceTime: %v", sch.NextQuickMaintenanceTime)
}
require.NoError(t, err)
require.True(t, sch.NextFullMaintenanceTime.IsZero(), "unexpected NextFullMaintenanceTime")
require.True(t, sch.NextQuickMaintenanceTime.IsZero(), "unexpected NextQuickMaintenanceTime")

sch.NextFullMaintenanceTime = clock.Now()
sch.NextQuickMaintenanceTime = clock.Now()
Expand All @@ -37,18 +28,14 @@ func (s *formatSpecificTestSuite) TestMaintenanceSchedule(t *testing.T) {
Success: true,
})

if err = maintenance.SetSchedule(ctx, env.RepositoryWriter, sch); err != nil {
t.Fatalf("unable to set schedule: %v", err)
}
err = maintenance.SetSchedule(ctx, env.RepositoryWriter, sch)
require.NoError(t, err, "unable to set schedule")

s2, err := maintenance.GetSchedule(ctx, env.RepositoryWriter)
if err != nil {
t.Fatalf("unable to get schedule: %v", err)
}
require.NoError(t, err, "unable to get schedule")

if got, want := toJSON(s2), toJSON(sch); got != want {
t.Errorf("invalid schedule (-want,+got) %v", pretty.Compare(want, got))
}
got, want := toJSON(s2), toJSON(sch)
require.Equal(t, want, got, "unexpected schedule")
}

func TestTimeToAttemptNextMaintenance(t *testing.T) {
Expand Down

0 comments on commit d7a26b3

Please sign in to comment.