File: src_test.go

package info (click to toggle)
golang-github-containers-image 5.28.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,104 kB
  • sloc: sh: 194; makefile: 73
file content (56 lines) | stat: -rw-r--r-- 1,174 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package blobcache

import (
	"bytes"
	"io"
	"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 io.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)
	}
}