github.com/pachyderm/pachyderm@v1.13.4/src/server/pkg/storage/fileset/fileset_test.go (about) 1 package fileset 2 3 // TODO: Rewrite these to account for format change. 4 //const ( 5 // max = 20 * units.MB 6 // maxTags = 10 7 // testPath = "test" 8 // scratchPath = "scratch" 9 //) 10 // 11 //type testFileOp struct { 12 // name string 13 //} 14 // 15 //type testDataOp struct { 16 // data []byte 17 // tag string 18 // hashes []string 19 //} 20 // 21 //func generateDataOps(n int) []*index.DataOp { 22 // numTags := rand.Intn(maxTags) + 1 23 // tags := []*chunk.Tag{} 24 // tagSize := n / numTags 25 // for i := 0; i < numTags-1; i++ { 26 // tags = append(tags, &chunk.Tag{ 27 // Id: strconv.Itoa(i), 28 // SizeBytes: int64(tagSize), 29 // }) 30 // } 31 // tags = append(tags, &chunk.Tag{ 32 // Id: strconv.Itoa(numTags - 1), 33 // SizeBytes: int64(n - (numTags-1)*tagSize), 34 // }) 35 // return tags 36 //} 37 // 38 //func writeFile(t *testing.T, w *Writer, f *testFile, msg string) { 39 // // Write header. 40 // hdr := &tar.Header{ 41 // Name: f.name, 42 // Size: int64(len(f.data)), 43 // } 44 // require.NoError(t, w.WriteHeader(hdr), msg) 45 // // Write content and tags. 46 // data := f.data 47 // for _, tag := range f.tags { 48 // w.Tag(tag.Id) 49 // _, err := w.Write(data[:tag.SizeBytes]) 50 // require.NoError(t, err, msg) 51 // data = data[tag.SizeBytes:] 52 // } 53 // _, err := w.Write(data) 54 // require.NoError(t, err, msg) 55 //} 56 // 57 //func writeFileSet(t *testing.T, fileSets *Storage, fileSet string, files []*testFile, msg string) { 58 // w := fileSets.newWriter(context.Background(), fileSet) 59 // for _, file := range files { 60 // writeFile(t, w, file, msg) 61 // } 62 // require.NoError(t, w.Close(), msg) 63 //} 64 // 65 //type fileReader interface { 66 // Index() *index.Index 67 // Get(w io.Writer) error 68 //} 69 // 70 //func checkFile(t *testing.T, fr fileReader, f *testFile, msg string) { 71 // // Check header. 72 // buf := &bytes.Buffer{} 73 // require.NoError(t, fr.Get(buf), msg) 74 // tr := tar.NewReader(buf) 75 // hdr, err := tr.Next() 76 // require.NoError(t, err, msg) 77 // require.Equal(t, f.name, hdr.Name, msg) 78 // require.Equal(t, int64(len(f.data)), hdr.Size, msg) 79 // // Check content. 80 // actualData := &bytes.Buffer{} 81 // _, err = io.Copy(actualData, tr) 82 // require.NoError(t, err, msg) 83 // require.Equal(t, 0, bytes.Compare(f.data, actualData.Bytes()), msg) 84 // // Check hashes. 85 // actualHashes := dataRefsToHashes(fr.Index().DataOp.DataRefs) 86 // // If no hashes are recorded then set them based on what was read. 87 // if len(f.hashes) == 0 { 88 // f.hashes = actualHashes 89 // } 90 // require.Equal(t, f.hashes, actualHashes, msg) 91 //} 92 // 93 //func dataRefsToHashes(dataRefs []*chunk.DataRef) []string { 94 // var hashes []string 95 // for _, dataRef := range dataRefs { 96 // if dataRef.Hash == "" { 97 // hashes = append(hashes, dataRef.ChunkInfo.Chunk.Hash) 98 // continue 99 // } 100 // hashes = append(hashes, dataRef.Hash) 101 // } 102 // return hashes 103 //} 104 // 105 //func TestWriteThenRead(t *testing.T) { 106 // require.NoError(t, WithLocalStorage(func(fileSets *Storage) error { 107 // msg := random.SeedRand() 108 // fileNames := index.Generate("abc") 109 // files := []*testFile{} 110 // for _, fileName := range fileNames { 111 // data := chunk.RandSeq(rand.Intn(max)) 112 // files = append(files, &testFile{ 113 // name: "/" + fileName, 114 // data: data, 115 // tags: generateTags(len(data)), 116 // }) 117 // } 118 // // Write out ten filesets where each subsequent fileset has the content of one random file changed. 119 // // Confirm that all of the content and hashes other than the changed file remain the same. 120 // fileSet := path.Join(testPath, "0") 121 // for i := 0; i < 10; i++ { 122 // // Write the files to the fileset. 123 // writeFileSet(t, fileSets, fileSet, files, msg) 124 // // Read the files from the fileset, checking against the recorded files. 125 // r := fileSets.newReader(context.Background(), fileSet) 126 // filesIter := files 127 // require.NoError(t, r.iterate(func(fr *FileReader) error { 128 // checkFile(t, fr, filesIter[0], msg) 129 // filesIter = filesIter[1:] 130 // return nil 131 // }), msg) 132 // // Change one random file 133 // data := chunk.RandSeq(rand.Intn(max)) 134 // i := rand.Intn(len(files)) 135 // files[i] = &testFile{ 136 // name: files[i].name, 137 // data: data, 138 // tags: generateTags(len(data)), 139 // } 140 // require.NoError(t, fileSets.Delete(context.Background(), fileSet), msg) 141 // } 142 // return nil 143 // })) 144 //} 145 // 146 //func TestCopy(t *testing.T) { 147 // require.NoError(t, WithLocalStorage(func(fileSets *Storage) error { 148 // msg := random.SeedRand() 149 // fileNames := index.Generate("abc") 150 // files := []*testFile{} 151 // // Write the initial fileset and count the chunks. 152 // for _, fileName := range fileNames { 153 // data := chunk.RandSeq(rand.Intn(max)) 154 // files = append(files, &testFile{ 155 // name: "/" + fileName, 156 // data: data, 157 // tags: generateTags(len(data)), 158 // }) 159 // } 160 // originalPath := path.Join(testPath, "original") 161 // writeFileSet(t, fileSets, originalPath, files, msg) 162 // var initialChunkCount int64 163 // require.NoError(t, fileSets.ChunkStorage().List(context.Background(), func(_ string) error { 164 // initialChunkCount++ 165 // return nil 166 // }), msg) 167 // // Copy intial fileset to a new copy fileset. 168 // r := fileSets.newReader(context.Background(), originalPath) 169 // copyPath := path.Join(testPath, "copy") 170 // wCopy := fileSets.newWriter(context.Background(), copyPath) 171 // require.NoError(t, r.iterate(func(fr *FileReader) error { 172 // return wCopy.CopyFile(fr) 173 // }), msg) 174 // require.NoError(t, wCopy.Close(), msg) 175 // // Compare initial fileset and copy fileset. 176 // rCopy := fileSets.newReader(context.Background(), copyPath) 177 // require.NoError(t, rCopy.iterate(func(fr *FileReader) error { 178 // checkFile(t, fr, files[0], msg) 179 // files = files[1:] 180 // return nil 181 // }), msg) 182 // // No new chunks should get created by the copy. 183 // var finalChunkCount int64 184 // require.NoError(t, fileSets.ChunkStorage().List(context.Background(), func(_ string) error { 185 // finalChunkCount++ 186 // return nil 187 // }), msg) 188 // require.Equal(t, initialChunkCount, finalChunkCount, msg) 189 // return nil 190 // })) 191 //} 192 // 193 //func TestCompaction(t *testing.T) { 194 // require.NoError(t, WithLocalStorage(func(fileSets *Storage) error { 195 // msg := random.SeedRand() 196 // numFileSets := 5 197 // // Generate filesets. 198 // files := generateFileSets(t, fileSets, numFileSets, testPath, msg) 199 // // Get the file hashes. 200 // getHashes(t, fileSets, files, msg) 201 // // Compact the files. 202 // _, err := fileSets.Compact(context.Background(), path.Join(testPath, Compacted), []string{testPath}, 0) 203 // require.NoError(t, err, msg) 204 // // Check the files. 205 // r := fileSets.NewReader(context.Background(), path.Join(testPath, Compacted)) 206 // require.NoError(t, r.iterate(func(fr *FileReader) error { 207 // checkFile(t, fr, files[0], msg) 208 // files = files[1:] 209 // return nil 210 // }), msg) 211 // return nil 212 // })) 213 //} 214 // 215 //func generateFileSets(t *testing.T, fileSets *Storage, numFileSets int, prefix, msg string) []*testFile { 216 // fileNames := index.Generate("abcd") 217 // files := []*testFile{} 218 // // Generate the files and randomly distribute them across the filesets. 219 // var ws []*Writer 220 // for i := 0; i < numFileSets; i++ { 221 // ws = append(ws, fileSets.newWriter(context.Background(), path.Join(prefix, strconv.Itoa(i)))) 222 // } 223 // for i, fileName := range fileNames { 224 // data := chunk.RandSeq(rand.Intn(max)) 225 // files = append(files, &testFile{ 226 // name: "/" + fileName, 227 // data: data, 228 // tags: generateTags(len(data)), 229 // }) 230 // // Shallow copy for slicing as data is distributed. 231 // f := *files[i] 232 // wsCopy := make([]*Writer, len(ws)) 233 // copy(wsCopy, ws) 234 // // Randomly distribute tagged data among filesets. 235 // for len(f.tags) > 0 { 236 // // Randomly select fileset to write to. 237 // i := rand.Intn(len(wsCopy)) 238 // w := wsCopy[i] 239 // wsCopy = append(wsCopy[:i], wsCopy[i+1:]...) 240 // // Write the rest of the file if this is the last fileset. 241 // if len(wsCopy) == 0 { 242 // writeFile(t, w, &f, msg) 243 // break 244 // } 245 // // Choose a random number of the tags left. 246 // numTags := rand.Intn(len(f.tags)) + 1 247 // var size int 248 // for _, tag := range f.tags[:numTags] { 249 // size += int(tag.SizeBytes) 250 // } 251 // // Create file for writing and remove data/tags from rest of the file. 252 // fWrite := f 253 // fWrite.data = fWrite.data[:size] 254 // fWrite.tags = fWrite.tags[:numTags] 255 // f.data = f.data[size:] 256 // f.tags = f.tags[numTags:] 257 // writeFile(t, w, &fWrite, msg) 258 // } 259 // } 260 // for _, w := range ws { 261 // require.NoError(t, w.Close(), msg) 262 // } 263 // return files 264 //} 265 // 266 //func getHashes(t *testing.T, fileSets *Storage, files []*testFile, msg string) { 267 // writeFileSet(t, fileSets, path.Join(scratchPath, Compacted), files, msg) 268 // r := fileSets.newReader(context.Background(), path.Join(scratchPath, Compacted)) 269 // require.NoError(t, r.iterate(func(fr *FileReader) error { 270 // files[0].hashes = dataRefsToHashes(fr.Index().DataOp.DataRefs) 271 // files = files[1:] 272 // return nil 273 // }), msg) 274 //}