File: storage_linux_test.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (213 lines) | stat: -rw-r--r-- 4,789 bytes parent folder | download | duplicates (3)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package chunked

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// Mock for ImageSourceSeekable
type mockImageSource struct {
	streams chan io.ReadCloser
	errors  chan error
}

func (m *mockImageSource) GetBlobAt(chunks []ImageSourceChunk) (chan io.ReadCloser, chan error, error) {
	return m.streams, m.errors, nil
}

type mockReadCloser struct {
	io.Reader
	closed bool
}

func (m *mockReadCloser) Close() error {
	m.closed = true
	return nil
}

func mockReadCloserFromContent(content string) *mockReadCloser {
	return &mockReadCloser{Reader: bytes.NewBufferString(content), closed: false}
}

func TestGetBlobAtNormalOperation(t *testing.T) {
	errors := make(chan error, 1)
	expectedStreams := []string{"stream1", "stream2"}
	streamsObjs := []*mockReadCloser{
		mockReadCloserFromContent(expectedStreams[0]),
		mockReadCloserFromContent(expectedStreams[1]),
	}
	streams := make(chan io.ReadCloser, len(streamsObjs))

	for _, s := range streamsObjs {
		streams <- s
	}
	close(streams)
	close(errors)

	is := &mockImageSource{streams: streams, errors: errors}

	chunks := []ImageSourceChunk{
		{Offset: 0, Length: 1},
		{Offset: 1, Length: 1},
	}

	resultChan, err := getBlobAt(is, chunks...)
	require.NoError(t, err)

	i := 0
	for result := range resultChan {
		assert.NoError(t, result.err)
		buf := new(bytes.Buffer)
		_, _ = buf.ReadFrom(result.stream)
		result.stream.Close()
		assert.Equal(t, expectedStreams[i], buf.String())
		i++
	}
	assert.Len(t, expectedStreams, i)
	for _, s := range streamsObjs {
		assert.True(t, s.closed)
	}
}

func TestGetBlobAtMaxStreams(t *testing.T) {
	streams := make(chan io.ReadCloser, 5)
	errors := make(chan error)

	streamsObjs := []*mockReadCloser{}

	for i := 1; i <= 5; i++ {
		s := mockReadCloserFromContent(fmt.Sprintf("stream%d", i))
		streamsObjs = append(streamsObjs, s)
		streams <- s
	}
	close(streams)
	close(errors)

	is := &mockImageSource{streams: streams, errors: errors}

	chunks := []ImageSourceChunk{
		{Offset: 0, Length: 1},
		{Offset: 1, Length: 1},
		{Offset: 2, Length: 1},
	}

	resultChan, err := getBlobAt(is, chunks...)
	require.NoError(t, err)

	count := 0
	receivedErr := false
	for result := range resultChan {
		if result.err != nil {
			receivedErr = true
		} else {
			result.stream.Close()
			count++
		}
	}
	assert.True(t, receivedErr)
	assert.Equal(t, 3, count)
	for _, s := range streamsObjs {
		assert.True(t, s.closed)
	}
}

func TestGetBlobAtWithErrors(t *testing.T) {
	streams := make(chan io.ReadCloser)
	errorsC := make(chan error, 2)

	errorsC <- errors.New("error1")
	errorsC <- errors.New("error2")
	close(streams)
	close(errorsC)

	is := &mockImageSource{streams: streams, errors: errorsC}

	chunks := []ImageSourceChunk{
		{Offset: 0, Length: 1},
		{Offset: 1, Length: 1},
	}
	resultChan, err := getBlobAt(is, chunks...)
	require.NoError(t, err)

	expectedErrors := []string{"error1", "error2"}
	i := 0
	for result := range resultChan {
		assert.Nil(t, result.stream)
		assert.NotNil(t, result.err)
		if result.err != nil {
			assert.Equal(t, expectedErrors[i], result.err.Error())
		}
		i++
	}
	assert.Equal(t, len(expectedErrors), i)
}

func TestGetBlobAtMixedStreamsAndErrors(t *testing.T) {
	streams := make(chan io.ReadCloser, 2)
	errorsC := make(chan error, 1)

	streams <- mockReadCloserFromContent("stream1")
	streams <- mockReadCloserFromContent("stream2")
	errorsC <- errors.New("error1")
	close(streams)
	close(errorsC)

	is := &mockImageSource{streams: streams, errors: errorsC}

	chunks := []ImageSourceChunk{
		{Offset: 0, Length: 1},
		{Offset: 1, Length: 1},
	}
	resultChan, err := getBlobAt(is, chunks...)
	require.NoError(t, err)

	var receivedStreams int
	var receivedErrors int
	for result := range resultChan {
		if result.err != nil {
			receivedErrors++
		} else {
			receivedStreams++
		}
	}
	assert.Equal(t, 2, receivedStreams)
	assert.Equal(t, 1, receivedErrors)
}

func TestTypeToOsMode(t *testing.T) {
	tests := []struct {
		name      string
		input     string
		expected  os.FileMode
		expectErr bool
	}{
		{"regular file", TypeReg, 0, false},
		{"hard link", TypeLink, 0, false},
		{"symlink", TypeSymlink, os.ModeSymlink, false},
		{"directory", TypeDir, os.ModeDir, false},
		{"character device", TypeChar, os.ModeDevice | os.ModeCharDevice, false},
		{"block device", TypeBlock, os.ModeDevice, false},
		{"FIFO/pipe", TypeFifo, os.ModeNamedPipe, false},
		{"unknown type", "unknown", 0, true},
		{"empty string", "", 0, true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			mode, err := typeToOsMode(tt.input)
			if tt.expectErr {
				assert.Error(t, err)
			} else {
				require.NoError(t, err)
				assert.Equal(t, tt.expected, mode)
			}
		})
	}
}