2022-03-22 11:03:15 -04:00
|
|
|
package coordinator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
pb "github.com/edgelesssys/constellation/debugd/service"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWriteStream(t *testing.T) {
|
|
|
|
filename := "testfile"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
readChunkStream fakeReadChunkStream
|
|
|
|
fs afero.Fs
|
|
|
|
showProgress bool
|
2022-04-26 10:54:05 -04:00
|
|
|
wantFile []byte
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"stream works": {
|
|
|
|
readChunkStream: fakeReadChunkStream{
|
|
|
|
chunks: [][]byte{
|
|
|
|
[]byte("test"),
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.NewMemMapFs(),
|
|
|
|
wantFile: []byte("test"),
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"chunking works": {
|
|
|
|
readChunkStream: fakeReadChunkStream{
|
|
|
|
chunks: [][]byte{
|
|
|
|
[]byte("te"),
|
|
|
|
[]byte("st"),
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.NewMemMapFs(),
|
|
|
|
wantFile: []byte("test"),
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"showProgress works": {
|
|
|
|
readChunkStream: fakeReadChunkStream{
|
|
|
|
chunks: [][]byte{
|
|
|
|
[]byte("test"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
fs: afero.NewMemMapFs(),
|
|
|
|
showProgress: true,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantFile: []byte("test"),
|
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"Open fails": {
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.NewReadOnlyFs(afero.NewMemMapFs()),
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"recv fails": {
|
|
|
|
readChunkStream: fakeReadChunkStream{
|
|
|
|
recvErr: errors.New("someErr"),
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
fs: afero.NewMemMapFs(),
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
writer := NewFileStreamer(tc.fs)
|
|
|
|
err := writer.WriteStream(filename, &tc.readChunkStream, tc.showProgress)
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
|
|
|
fileContents, err := afero.ReadFile(tc.fs, filename)
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantFile, fileContents)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadStream(t *testing.T) {
|
|
|
|
correctFilename := "testfile"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
writeChunkStream stubWriteChunkStream
|
|
|
|
filename string
|
|
|
|
chunksize uint
|
|
|
|
showProgress bool
|
2022-04-26 10:54:05 -04:00
|
|
|
wantChunks [][]byte
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"stream works": {
|
|
|
|
writeChunkStream: stubWriteChunkStream{},
|
|
|
|
filename: correctFilename,
|
|
|
|
chunksize: 4,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantChunks: [][]byte{
|
2022-03-22 11:03:15 -04:00
|
|
|
[]byte("test"),
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"chunking works": {
|
|
|
|
writeChunkStream: stubWriteChunkStream{},
|
|
|
|
filename: correctFilename,
|
|
|
|
chunksize: 2,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantChunks: [][]byte{
|
2022-03-22 11:03:15 -04:00
|
|
|
[]byte("te"),
|
|
|
|
[]byte("st"),
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"chunksize of 0 detected": {
|
|
|
|
writeChunkStream: stubWriteChunkStream{},
|
|
|
|
filename: correctFilename,
|
|
|
|
chunksize: 0,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"showProgress works": {
|
|
|
|
writeChunkStream: stubWriteChunkStream{},
|
|
|
|
filename: correctFilename,
|
|
|
|
chunksize: 4,
|
|
|
|
showProgress: true,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantChunks: [][]byte{
|
2022-03-22 11:03:15 -04:00
|
|
|
[]byte("test"),
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: false,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"Open fails": {
|
|
|
|
filename: "incorrect-filename",
|
|
|
|
chunksize: 4,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"send fails": {
|
|
|
|
writeChunkStream: stubWriteChunkStream{
|
|
|
|
sendErr: errors.New("someErr"),
|
|
|
|
},
|
|
|
|
filename: correctFilename,
|
|
|
|
chunksize: 4,
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
fs := afero.NewMemMapFs()
|
|
|
|
assert.NoError(afero.WriteFile(fs, correctFilename, []byte("test"), 0o755))
|
|
|
|
reader := NewFileStreamer(fs)
|
|
|
|
err := reader.ReadStream(tc.filename, &tc.writeChunkStream, tc.chunksize, tc.showProgress)
|
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(err)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantChunks, tc.writeChunkStream.chunks)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeReadChunkStream struct {
|
|
|
|
chunks [][]byte
|
|
|
|
pos int
|
|
|
|
recvErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *fakeReadChunkStream) Recv() (*pb.Chunk, error) {
|
|
|
|
if s.recvErr != nil {
|
|
|
|
return nil, s.recvErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.pos < len(s.chunks) {
|
|
|
|
result := &pb.Chunk{Content: s.chunks[s.pos]}
|
|
|
|
s.pos++
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubWriteChunkStream struct {
|
|
|
|
chunks [][]byte
|
|
|
|
sendErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubWriteChunkStream) Send(chunk *pb.Chunk) error {
|
|
|
|
cpy := make([]byte, len(chunk.Content))
|
|
|
|
copy(cpy, chunk.Content)
|
|
|
|
s.chunks = append(s.chunks, cpy)
|
|
|
|
return s.sendErr
|
|
|
|
}
|