github.com/m3db/m3@v1.5.0/src/m3ninx/persist/reader_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package persist 22 23 import ( 24 "fmt" 25 "testing" 26 27 "github.com/m3db/m3/src/m3ninx/index/segment/fst" 28 "github.com/m3db/m3/src/x/mmap" 29 xtest "github.com/m3db/m3/src/x/test" 30 31 "github.com/stretchr/testify/require" 32 ) 33 34 func TestReaderValidateType(t *testing.T) { 35 ctrl := xtest.NewController(t) 36 defer ctrl.Finish() 37 38 fset := NewMockIndexSegmentFileSet(ctrl) 39 fset.EXPECT().SegmentType().Return(IndexSegmentType("random")) 40 fset.EXPECT().Files().Return(nil).AnyTimes() 41 _, err := NewSegment(fset, nil) 42 require.Error(t, err) 43 } 44 45 func TestReaderValidateErrorCloses(t *testing.T) { 46 ctrl := xtest.NewController(t) 47 defer ctrl.Finish() 48 49 file := NewMockIndexSegmentFile(ctrl) 50 file.EXPECT().Close() 51 fset := NewMockIndexSegmentFileSet(ctrl) 52 fset.EXPECT().SegmentType().Return(IndexSegmentType("random")) 53 fset.EXPECT().Files().Return([]IndexSegmentFile{file}).AnyTimes() 54 _, err := NewSegment(fset, nil) 55 require.Error(t, err) 56 } 57 58 func TestReaderValidateDataSlices(t *testing.T) { 59 ctrl := xtest.NewController(t) 60 defer ctrl.Finish() 61 62 fset := NewMockIndexSegmentFileSet(ctrl) 63 fset.EXPECT().SegmentType().Return(FSTIndexSegmentType) 64 fset.EXPECT().MajorVersion().Return(fst.CurrentVersion.Major) 65 fset.EXPECT().MinorVersion().Return(1) 66 fset.EXPECT().SegmentMetadata().Return([]byte{}) 67 fset.EXPECT().Files().Return(nil).AnyTimes() 68 69 _, err := NewSegment(fset, nil) 70 require.Error(t, err) 71 } 72 73 func TestReaderValidateByteAccess(t *testing.T) { 74 ctrl := xtest.NewController(t) 75 defer ctrl.Finish() 76 77 fset := NewMockIndexSegmentFileSet(ctrl) 78 fset.EXPECT().SegmentType().Return(FSTIndexSegmentType) 79 fset.EXPECT().MajorVersion().Return(fst.CurrentVersion.Major) 80 fset.EXPECT().MinorVersion().Return(1) 81 fset.EXPECT().SegmentMetadata().Return([]byte{}) 82 83 docsDataFile := NewMockIndexSegmentFile(ctrl) 84 docsDataFile.EXPECT().SegmentFileType().Return(DocumentDataIndexSegmentFileType) 85 docsDataFile.EXPECT().Mmap().Return(mmap.Descriptor{}, fmt.Errorf("random")) 86 docsDataFile.EXPECT().Close() 87 fset.EXPECT().Files().Return([]IndexSegmentFile{docsDataFile}).AnyTimes() 88 89 _, err := NewSegment(fset, nil) 90 require.Error(t, err) 91 } 92 93 func TestReaderValidateDoesNotCloseAllOnBadByteAccess(t *testing.T) { 94 ctrl := xtest.NewController(t) 95 defer ctrl.Finish() 96 97 fset := NewMockIndexSegmentFileSet(ctrl) 98 fset.EXPECT().SegmentType().Return(FSTIndexSegmentType) 99 fset.EXPECT().MajorVersion().Return(fst.CurrentVersion.Major) 100 fset.EXPECT().MinorVersion().Return(1) 101 fset.EXPECT().SegmentMetadata().Return([]byte{}) 102 103 docsDataFile := NewMockIndexSegmentFile(ctrl) 104 docsDataFile.EXPECT().SegmentFileType().Return(DocumentDataIndexSegmentFileType) 105 docsDataFile.EXPECT().Close() 106 docsDataFile.EXPECT().Mmap().Return(mmap.Descriptor{}, nil) 107 108 docsIdxFile := NewMockIndexSegmentFile(ctrl) 109 docsIdxFile.EXPECT().SegmentFileType().Return(DocumentIndexIndexSegmentFileType) 110 docsIdxFile.EXPECT().Close() 111 docsIdxFile.EXPECT().Mmap().Return(mmap.Descriptor{}, fmt.Errorf("random")) 112 113 fset.EXPECT().Files().Return([]IndexSegmentFile{docsDataFile, docsIdxFile}).AnyTimes() 114 115 _, err := NewSegment(fset, nil) 116 require.Error(t, err) 117 } 118 119 func TestReaderValidateSegmentFileType(t *testing.T) { 120 ctrl := xtest.NewController(t) 121 defer ctrl.Finish() 122 123 fset := NewMockIndexSegmentFileSet(ctrl) 124 fset.EXPECT().SegmentType().Return(FSTIndexSegmentType) 125 fset.EXPECT().MajorVersion().Return(fst.CurrentVersion.Major) 126 fset.EXPECT().MinorVersion().Return(1) 127 fset.EXPECT().SegmentMetadata().Return([]byte{}) 128 129 docsDataFile := NewMockIndexSegmentFile(ctrl) 130 docsDataFile.EXPECT().SegmentFileType().Return(IndexSegmentFileType("rand")) 131 docsDataFile.EXPECT().Close() 132 fset.EXPECT().Files().Return([]IndexSegmentFile{docsDataFile}).AnyTimes() 133 134 _, err := NewSegment(fset, nil) 135 require.Error(t, err) 136 } 137 138 func TestReaderValidateAllByteAccess(t *testing.T) { 139 ctrl := xtest.NewController(t) 140 defer ctrl.Finish() 141 142 fset := NewMockIndexSegmentFileSet(ctrl) 143 fset.EXPECT().SegmentType().Return(FSTIndexSegmentType) 144 fset.EXPECT().MajorVersion().Return(fst.CurrentVersion.Major) 145 fset.EXPECT().MinorVersion().Return(1) 146 fset.EXPECT().SegmentMetadata().Return([]byte{}) 147 148 docsDataFile := NewMockIndexSegmentFile(ctrl) 149 docsDataFile.EXPECT().SegmentFileType().Return(DocumentDataIndexSegmentFileType) 150 docsDataFile.EXPECT().Mmap().Return(mmap.Descriptor{}, nil) 151 docsDataFile.EXPECT().Close() 152 153 docsIdxFile := NewMockIndexSegmentFile(ctrl) 154 docsIdxFile.EXPECT().SegmentFileType().Return(DocumentIndexIndexSegmentFileType) 155 docsIdxFile.EXPECT().Mmap().Return(mmap.Descriptor{}, nil) 156 docsIdxFile.EXPECT().Close() 157 158 postingsFile := NewMockIndexSegmentFile(ctrl) 159 postingsFile.EXPECT().SegmentFileType().Return(PostingsIndexSegmentFileType) 160 postingsFile.EXPECT().Mmap().Return(mmap.Descriptor{}, nil) 161 postingsFile.EXPECT().Close() 162 163 fstFieldsFile := NewMockIndexSegmentFile(ctrl) 164 fstFieldsFile.EXPECT().SegmentFileType().Return(FSTFieldsIndexSegmentFileType) 165 fstFieldsFile.EXPECT().Mmap().Return(mmap.Descriptor{}, nil) 166 fstFieldsFile.EXPECT().Close() 167 168 fstTermsFile := NewMockIndexSegmentFile(ctrl) 169 fstTermsFile.EXPECT().SegmentFileType().Return(FSTTermsIndexSegmentFileType) 170 fstTermsFile.EXPECT().Mmap().Return(mmap.Descriptor{}, nil) 171 fstTermsFile.EXPECT().Close() 172 173 fset.EXPECT().Files().Return([]IndexSegmentFile{docsDataFile, docsIdxFile, 174 postingsFile, 175 fstFieldsFile, 176 fstTermsFile}).AnyTimes() 177 178 _, err := NewSegment(fset, nil) 179 // due to empty bytes being passed 180 require.Error(t, err) 181 }