@@ -22,6 +22,7 @@ import (
2222 "github.com/containerd/containerd/content/testsuite"
2323 "github.com/containerd/containerd/testutil"
2424 "github.com/opencontainers/go-digest"
25+ "github.com/stretchr/testify/require"
2526)
2627
2728type memoryLabelStore struct {
@@ -335,3 +336,42 @@ func checkWrite(ctx context.Context, t checker, cs content.Store, dgst digest.Di
335336
336337 return dgst
337338}
339+
340+ func TestWriterTruncateRecoversFromIncompleteWrite (t * testing.T ) {
341+ tmpdir , err := ioutil .TempDir ("" , "test-local-content-store-recover" )
342+ require .NoError (t , err )
343+ defer os .RemoveAll (tmpdir )
344+
345+ cs , err := NewStore (tmpdir )
346+ require .NoError (t , err )
347+
348+ ctx , cancel := context .WithCancel (context .Background ())
349+ defer cancel ()
350+
351+ ref := "ref"
352+ content := []byte ("this is the content" )
353+ total := int64 (len (content ))
354+ setupIncompleteWrite (ctx , t , cs , ref , total )
355+
356+ writer , err := cs .Writer (ctx , ref , total , "" )
357+ require .NoError (t , err )
358+
359+ require .NoError (t , writer .Truncate (0 ))
360+
361+ _ , err = writer .Write (content )
362+ require .NoError (t , err )
363+
364+ dgst := digest .FromBytes (content )
365+ err = writer .Commit (ctx , total , dgst )
366+ require .NoError (t , err )
367+ }
368+
369+ func setupIncompleteWrite (ctx context.Context , t * testing.T , cs content.Store , ref string , total int64 ) {
370+ writer , err := cs .Writer (ctx , ref , total , "" )
371+ require .NoError (t , err )
372+
373+ _ , err = writer .Write ([]byte ("bad data" ))
374+ require .NoError (t , err )
375+
376+ require .NoError (t , writer .Close ())
377+ }
0 commit comments