-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forward all of private.ImageSource by blobCacheSource
Also add support for reading blob chunks from a (complete, not partial) cached blob. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
- Loading branch information
Showing
3 changed files
with
146 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package blobcache | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/containers/image/v5/internal/private" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func readNextStream(streams chan io.ReadCloser, errs chan error) ([]byte, error) { | ||
select { | ||
case r := <-streams: | ||
if r == nil { | ||
return nil, nil | ||
} | ||
defer r.Close() | ||
return ioutil.ReadAll(r) | ||
case err := <-errs: | ||
return nil, err | ||
} | ||
} | ||
|
||
// readSeekerNopCloser adds a no-op Close() method to a readSeeker | ||
type readSeekerNopCloser struct { | ||
io.ReadSeeker | ||
} | ||
|
||
func (c *readSeekerNopCloser) Close() error { | ||
return nil | ||
} | ||
|
||
func TestStreamChunksFromFile(t *testing.T) { | ||
file := &readSeekerNopCloser{bytes.NewReader([]byte("123456789"))} | ||
streams := make(chan io.ReadCloser) | ||
errs := make(chan error) | ||
chunks := []private.ImageSourceChunk{ | ||
{Offset: 1, Length: 2}, | ||
{Offset: 4, Length: 1}, | ||
} | ||
go streamChunksFromFile(streams, errs, file, chunks) | ||
|
||
for _, c := range []struct { | ||
expectedData []byte | ||
expectedError error | ||
}{ | ||
{[]byte("23"), nil}, | ||
{[]byte("5"), nil}, | ||
{[]byte(nil), nil}, | ||
} { | ||
data, err := readNextStream(streams, errs) | ||
assert.Equal(t, c.expectedData, data) | ||
assert.Equal(t, c.expectedError, err) | ||
} | ||
} |