forked from microsoft/hdfs-mount
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileHandleWriter_test.go
158 lines (132 loc) · 6.45 KB
/
FileHandleWriter_test.go
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
package main
import (
"bazil.org/fuse"
"errors"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"io"
"os"
"testing"
)
func TestWriteFile(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockClock := &MockClock{}
hdfsAccessor := NewMockHdfsAccessor(mockCtrl)
fileName := "/testWriteFile_1"
fs, _ := NewFileSystem(hdfsAccessor, "/tmp/x", []string{"*"}, false, false, NewDefaultRetryPolicy(mockClock), mockClock)
hdfswriter := NewMockHdfsWriter(mockCtrl)
hdfsAccessor.EXPECT().Remove(fileName).Return(nil)
hdfsAccessor.EXPECT().CreateFile(fileName, os.FileMode(0757)).Return(hdfswriter, nil)
hdfswriter.EXPECT().Close().Return(nil)
hdfsAccessor.EXPECT().Remove(fileName).Return(nil)
root, _ := fs.Root()
_, h, _ := root.(*Dir).Create(nil, &fuse.CreateRequest{Name: fileName, Mode: os.FileMode(0757)}, &fuse.CreateResponse{})
// Test for newfilehandlewriter
hdfsAccessor.EXPECT().CreateFile(fileName, os.FileMode(0757)).Return(hdfswriter, nil)
hdfswriter.EXPECT().Close().Return(nil)
writeHandle, err := NewFileHandleWriter(h.(*FileHandle), true)
assert.Nil(t, err)
// Test for normal write
hdfsAccessor.EXPECT().StatFs().Return(FsInfo{capacity: uint64(100), used: uint64(20), remaining: uint64(80)}, nil)
err = writeHandle.Write(h.(*FileHandle), nil, &fuse.WriteRequest{Data: []byte("hello world"), Offset: int64(11)}, &fuse.WriteResponse{})
assert.Nil(t, err)
assert.Equal(t, writeHandle.BytesWritten, uint64(11))
hdfsAccessor.EXPECT().Remove("/testWriteFile_1").Return(nil)
hdfsAccessor.EXPECT().CreateFile(fileName, os.FileMode(0757)).Return(hdfswriter, nil)
hdfswriter.EXPECT().Close().Return(nil)
binaryData := make([]byte, 65536, 65536)
nr, _ := writeHandle.stagingFile.Read(binaryData)
binaryData = binaryData[:nr]
hdfswriter.EXPECT().Write(binaryData).Return(11, nil)
err = writeHandle.Flush()
assert.Nil(t, err)
// Test for closing file
err = writeHandle.Close()
assert.Nil(t, err)
// Test for writing file larger than available size
hdfsAccessor.EXPECT().StatFs().Return(FsInfo{capacity: uint64(100), used: uint64(95), remaining: uint64(5)}, nil)
err = writeHandle.Write(h.(*FileHandle), nil, &fuse.WriteRequest{Data: []byte("hello world"), Offset: int64(11)}, &fuse.WriteResponse{})
assert.Equal(t, errors.New("Too large file"), err)
}
func TestFaultTolerantWriteFile(t *testing.T) {
mockCtrl := gomock.NewController(t)
mockClock := &MockClock{}
hdfsAccessor := NewMockHdfsAccessor(mockCtrl)
fileName := "/testWriteFile_1"
fs, _ := NewFileSystem(hdfsAccessor, "/tmp/x", []string{"*"}, false, false, NewDefaultRetryPolicy(mockClock), mockClock)
hdfswriter := NewMockHdfsWriter(mockCtrl)
hdfsAccessor.EXPECT().Remove(fileName).Return(nil)
hdfsAccessor.EXPECT().CreateFile(fileName, os.FileMode(0757)).Return(hdfswriter, nil)
hdfswriter.EXPECT().Close().Return(nil)
hdfsAccessor.EXPECT().Remove(fileName).Return(nil)
root, _ := fs.Root()
_, h, _ := root.(*Dir).Create(nil, &fuse.CreateRequest{Name: fileName, Mode: os.FileMode(0757)}, &fuse.CreateResponse{})
// Test for newfilehandlewriter
hdfsAccessor.EXPECT().CreateFile(fileName, os.FileMode(0757)).Return(hdfswriter, nil)
hdfswriter.EXPECT().Close().Return(nil)
writeHandle, err := NewFileHandleWriter(h.(*FileHandle), true)
assert.Nil(t, err)
// Test for normal write
hdfsAccessor.EXPECT().StatFs().Return(FsInfo{capacity: uint64(100), used: uint64(20), remaining: uint64(80)}, nil)
err = writeHandle.Write(h.(*FileHandle), nil, &fuse.WriteRequest{Data: []byte("hello world"), Offset: int64(11)}, &fuse.WriteResponse{})
assert.Nil(t, err)
assert.Equal(t, writeHandle.BytesWritten, uint64(11))
hdfsAccessor.EXPECT().Remove("/testWriteFile_1").Return(nil)
hdfsAccessor.EXPECT().CreateFile(fileName, os.FileMode(0757)).Return(hdfswriter, nil)
// hdfswriter.EXPECT().Close().Return(nil)
binaryData := make([]byte, 65536, 65536)
nr, _ := writeHandle.stagingFile.Read(binaryData)
binaryData = binaryData[:nr]
// Mock the EOF error to test the fault tolerant write/flush
hdfswriter.EXPECT().Write(binaryData).Return(0, io.EOF)
hdfswriter.EXPECT().Close().Return(nil)
err = writeHandle.FlushAttempt()
assert.Equal(t, io.EOF, err)
// The connection would be closed
hdfsAccessor.EXPECT().Close().Return(nil)
// New connection being established
newhdfswriter := NewMockHdfsWriter(mockCtrl)
hdfsAccessor.EXPECT().StatFs().Return(FsInfo{capacity: uint64(100), used: uint64(20), remaining: uint64(80)}, nil)
hdfsAccessor.EXPECT().Remove(fileName).Return(nil)
hdfsAccessor.EXPECT().CreateFile(fileName, os.FileMode(0757)).Return(newhdfswriter, nil)
newbinaryData := make([]byte, 65536, 65536)
newnr, _ := writeHandle.stagingFile.Read(binaryData)
newbinaryData = newbinaryData[:newnr]
newhdfswriter.EXPECT().Write(binaryData).Return(11, nil)
newhdfswriter.EXPECT().Close().Return(nil)
hdfsAccessor.EXPECT().Remove(fileName).Return(nil)
err = writeHandle.Flush()
assert.Nil(t, err)
// Test for closing file
err = writeHandle.Close()
assert.Nil(t, err)
}
func TestFlushFile(t *testing.T) {
t.Skip("Cannot mock hdfsreader for overwiting file")
mockCtrl := gomock.NewController(t)
mockClock := &MockClock{}
hdfsAccessor := NewMockHdfsAccessor(mockCtrl)
fileName := "/testWriteFile_2"
fs, _ := NewFileSystem(hdfsAccessor, "/tmp/x", []string{"*"}, false, false, NewDefaultRetryPolicy(mockClock), mockClock)
hdfswriter := NewMockHdfsWriter(mockCtrl)
hdfsAccessor.EXPECT().Remove(fileName).Return(nil)
hdfsAccessor.EXPECT().CreateFile(fileName, os.FileMode(0757)).Return(hdfswriter, nil)
hdfswriter.EXPECT().Close().Return(nil)
root, _ := fs.Root()
_, h, _ := root.(*Dir).Create(nil, &fuse.CreateRequest{Name: fileName, Mode: os.FileMode(0757)}, &fuse.CreateResponse{})
// Test for newfilehandlewriter with existing file
hdfsAccessor.EXPECT().CreateFile(fileName, os.FileMode(0757)).Return(hdfswriter, nil)
hdfswriter.EXPECT().Close().Return(nil)
hdfsAccessor.EXPECT().StatFs().Return(FsInfo{capacity: uint64(100), used: uint64(20), remaining: uint64(80)}, nil)
hdfsAccessor.EXPECT().Stat("/testWriteFile_2").Return(Attrs{Name: "testWriteFile_2"}, nil)
// BUG: cannot mock the returned hdfsreader here
hdfsAccessor.EXPECT().OpenRead("/testWriteFile_2").Return(nil, nil)
writeHandle, err := NewFileHandleWriter(h.(*FileHandle), false)
assert.Nil(t, err)
// Test for flush
_ = writeHandle.Write(h.(*FileHandle), nil, &fuse.WriteRequest{Data: []byte("hello world"), Offset: int64(11)}, &fuse.WriteResponse{})
err = writeHandle.Flush()
assert.Nil(t, err)
err = writeHandle.Close()
assert.Nil(t, err)
}