Skip to content

Commit eed3b1c

Browse files
authored
Merge pull request containerd#2076 from stevvooe/content-testsuite-unique-seed
content/testsuite: ensure unique content per test
2 parents 5f89502 + 3295bca commit eed3b1c

File tree

4 files changed

+35
-23
lines changed

4 files changed

+35
-23
lines changed

content/local/store_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ func (mls *memoryLabelStore) Update(d digest.Digest, update map[string]string) (
7272
}
7373

7474
func TestContent(t *testing.T) {
75-
testsuite.ContentSuite(t, "fs", func(ctx context.Context, root string) (content.Store, func() error, error) {
75+
testsuite.ContentSuite(t, "fs", func(ctx context.Context, root string) (context.Context, content.Store, func() error, error) {
7676
cs, err := NewLabeledStore(root, newMemoryLabelStore())
7777
if err != nil {
78-
return nil, nil, err
78+
return nil, nil, nil, err
7979
}
80-
return cs, func() error {
80+
return ctx, cs, func() error {
8181
return nil
8282
}, nil
8383
})

content/testsuite/testsuite.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"math/rand"
1010
"os"
1111
"runtime"
12+
"sync/atomic"
1213
"testing"
1314
"time"
1415

@@ -21,7 +22,7 @@ import (
2122
)
2223

2324
// ContentSuite runs a test suite on the content store given a factory function.
24-
func ContentSuite(t *testing.T, name string, storeFn func(ctx context.Context, root string) (content.Store, func() error, error)) {
25+
func ContentSuite(t *testing.T, name string, storeFn func(ctx context.Context, root string) (context.Context, content.Store, func() error, error)) {
2526
t.Run("Writer", makeTest(t, name, storeFn, checkContentStoreWriter))
2627
t.Run("UploadStatus", makeTest(t, name, storeFn, checkUploadStatus))
2728
t.Run("Resume", makeTest(t, name, storeFn, checkResumeWriter))
@@ -33,7 +34,7 @@ func ContentSuite(t *testing.T, name string, storeFn func(ctx context.Context, r
3334
t.Run("Labels", makeTest(t, name, storeFn, checkLabels))
3435
}
3536

36-
func makeTest(t *testing.T, name string, storeFn func(ctx context.Context, root string) (content.Store, func() error, error), fn func(ctx context.Context, t *testing.T, cs content.Store)) func(t *testing.T) {
37+
func makeTest(t *testing.T, name string, storeFn func(ctx context.Context, root string) (context.Context, content.Store, func() error, error), fn func(ctx context.Context, t *testing.T, cs content.Store)) func(t *testing.T) {
3738
return func(t *testing.T) {
3839
ctx := namespaces.WithNamespace(context.Background(), name)
3940

@@ -43,7 +44,7 @@ func makeTest(t *testing.T, name string, storeFn func(ctx context.Context, root
4344
}
4445
defer os.RemoveAll(tmpDir)
4546

46-
cs, cleanup, err := storeFn(ctx, tmpDir)
47+
ctx, cs, cleanup, err := storeFn(ctx, tmpDir)
4748
if err != nil {
4849
t.Fatal(err)
4950
}
@@ -63,28 +64,28 @@ var labels = map[string]string{
6364
}
6465

6566
func checkContentStoreWriter(ctx context.Context, t *testing.T, cs content.Store) {
66-
c1, d1 := createContent(256, 1)
67+
c1, d1 := createContent(256)
6768
w1, err := cs.Writer(ctx, "c1", 0, "")
6869
if err != nil {
6970
t.Fatal(err)
7071
}
7172
defer w1.Close()
7273

73-
c2, d2 := createContent(256, 2)
74+
c2, d2 := createContent(256)
7475
w2, err := cs.Writer(ctx, "c2", int64(len(c2)), "")
7576
if err != nil {
7677
t.Fatal(err)
7778
}
7879
defer w2.Close()
7980

80-
c3, d3 := createContent(256, 3)
81+
c3, d3 := createContent(256)
8182
w3, err := cs.Writer(ctx, "c3", 0, d3)
8283
if err != nil {
8384
t.Fatal(err)
8485
}
8586
defer w3.Close()
8687

87-
c4, d4 := createContent(256, 4)
88+
c4, d4 := createContent(256)
8889
w4, err := cs.Writer(ctx, "c4", int64(len(c4)), d4)
8990
if err != nil {
9091
t.Fatal(err)
@@ -163,7 +164,7 @@ func checkResumeWriter(ctx context.Context, t *testing.T, cs content.Store) {
163164

164165
var (
165166
ref = "cb"
166-
cb, dgst = createContent(256, 10)
167+
cb, dgst = createContent(256)
167168
first, second = cb[:128], cb[128:]
168169
)
169170

@@ -223,7 +224,7 @@ func checkResumeWriter(ctx context.Context, t *testing.T, cs content.Store) {
223224
}
224225

225226
func checkUploadStatus(ctx context.Context, t *testing.T, cs content.Store) {
226-
c1, d1 := createContent(256, 17)
227+
c1, d1 := createContent(256)
227228

228229
preStart := time.Now()
229230
w1, err := cs.Writer(ctx, "c1", 256, d1)
@@ -292,7 +293,7 @@ func checkUploadStatus(ctx context.Context, t *testing.T, cs content.Store) {
292293
}
293294

294295
func checkLabels(ctx context.Context, t *testing.T, cs content.Store) {
295-
c1, d1 := createContent(256, 19)
296+
c1, d1 := createContent(256)
296297

297298
w1, err := cs.Writer(ctx, "c1", 256, d1)
298299
if err != nil {
@@ -365,7 +366,7 @@ func checkResume(rf func(context.Context, content.Writer, []byte, int64, int64,
365366

366367
for i, size := range sizes {
367368
for j, tp := range truncations {
368-
b, d := createContent(size, int64(i*len(truncations)+j))
369+
b, d := createContent(size)
369370
limit := int64(float64(size) * tp)
370371
ref := fmt.Sprintf("ref-%d-%d", i, j)
371372

@@ -548,7 +549,14 @@ func checkInfo(ctx context.Context, cs content.Store, d digest.Digest, expected
548549
return nil
549550
}
550551

551-
func createContent(size, seed int64) ([]byte, digest.Digest) {
552+
var contentSeed int64
553+
554+
func createContent(size int64) ([]byte, digest.Digest) {
555+
// each time we call this, we want to get a different seed, but it should
556+
// be related to the intitialization order and fairly consistent between
557+
// test runs. An atomic integer works just good enough for this.
558+
seed := atomic.AddInt64(&contentSeed, 1)
559+
552560
b, err := ioutil.ReadAll(io.LimitReader(rand.New(rand.NewSource(seed)), size))
553561
if err != nil {
554562
panic(err)

content_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ import (
1010
"github.com/pkg/errors"
1111
)
1212

13-
func newContentStore(ctx context.Context, root string) (content.Store, func() error, error) {
13+
func newContentStore(ctx context.Context, root string) (context.Context, content.Store, func() error, error) {
1414
client, err := New(address)
1515
if err != nil {
16-
return nil, nil, err
16+
return nil, nil, nil, err
17+
}
18+
ctx, releaselease, err := client.WithLease(ctx)
19+
if err != nil {
20+
return nil, nil, nil, err
1721
}
18-
1922
cs := client.ContentStore()
2023

21-
return cs, func() error {
24+
return ctx, cs, func() error {
2225
statuses, err := cs.ListStatuses(ctx)
2326
if err != nil {
2427
return err
@@ -28,6 +31,7 @@ func newContentStore(ctx context.Context, root string) (content.Store, func() er
2831
return errors.Wrapf(err, "failed to abort %s", st.Ref)
2932
}
3033
}
34+
releaselease()
3135
return cs.Walk(ctx, func(info content.Info) error {
3236
if err := cs.Delete(ctx, info.Digest); err != nil {
3337
if errdefs.IsNotFound(err) {

metadata/content_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ import (
1717
"github.com/pkg/errors"
1818
)
1919

20-
func createContentStore(ctx context.Context, root string) (content.Store, func() error, error) {
20+
func createContentStore(ctx context.Context, root string) (context.Context, content.Store, func() error, error) {
2121
// TODO: Use mocked or in-memory store
2222
cs, err := local.NewStore(root)
2323
if err != nil {
24-
return nil, nil, err
24+
return nil, nil, nil, err
2525
}
2626

2727
db, err := bolt.Open(filepath.Join(root, "metadata.db"), 0660, nil)
2828
if err != nil {
29-
return nil, nil, err
29+
return nil, nil, nil, err
3030
}
3131

32-
return NewDB(db, cs, nil).ContentStore(), func() error {
32+
return ctx, NewDB(db, cs, nil).ContentStore(), func() error {
3333
return db.Close()
3434
}, nil
3535
}

0 commit comments

Comments
 (0)