How to use Fatalf method of log Package

Best Syzkaller code snippet using log.Fatalf

swarmfs_test.go

Source:swarmfs_test.go Github

copy

Full Screen

...55 filePath := filepath.Dir(actualPath)56 //create directory57 err := os.MkdirAll(filePath, 0777)58 if err != nil {59 t.Fatalf("Error creating directory '%v' : %v", filePath, err)60 }61 //create file62 fd, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(finfo.perm))63 if err1 != nil {64 t.Fatalf("Error creating file %v: %v", actualPath, err1)65 }66 //write content to file67 _, err = fd.Write(finfo.contents)68 if err != nil {69 t.Fatalf("Error writing to file '%v' : %v", filePath, err)70 }71 /*72 Note @holisticode: It's not clear why the Chown command was added to the test suite.73 Some files are initialized with different permissions in the individual test,74 resulting in errors on Chown which were not checked.75 After adding the checks tests would fail.76 What's then the reason to have this check in the first place?77 Disabling for now78 err = fd.Chown(finfo.uid, finfo.gid)79 if err != nil {80 t.Fatalf("Error chown file '%v' : %v", filePath, err)81 }82 */83 err = fd.Chmod(os.FileMode(finfo.perm))84 if err != nil {85 t.Fatalf("Error chmod file '%v' : %v", filePath, err)86 }87 err = fd.Sync()88 if err != nil {89 t.Fatalf("Error sync file '%v' : %v", filePath, err)90 }91 err = fd.Close()92 if err != nil {93 t.Fatalf("Error closing file '%v' : %v", filePath, err)94 }95 }96 //upload directory to swarm and return hash97 bzzhash, err := Upload(uploadDir, "", api, toEncrypt)98 if err != nil {99 t.Fatalf("Error uploading directory %v: %vm encryption: %v", uploadDir, err, toEncrypt)100 }101 return bzzhash102}103//mount a swarm hash as a directory on files system via FUSE104func mountDir(t *testing.T, api *api.API, files map[string]fileInfo, bzzHash string, mountDir string) *SwarmFS {105 swarmfs := NewSwarmFS(api)106 _, err := swarmfs.Mount(bzzHash, mountDir)107 if isFUSEUnsupportedError(err) {108 t.Skip("FUSE not supported:", err)109 } else if err != nil {110 t.Fatalf("Error mounting hash %v: %v", bzzHash, err)111 }112 //check directory is mounted113 found := false114 mi := swarmfs.Listmounts()115 for _, minfo := range mi {116 minfo.lock.RLock()117 if minfo.MountPoint == mountDir {118 if minfo.StartManifest != bzzHash ||119 minfo.LatestManifest != bzzHash ||120 minfo.fuseConnection == nil {121 minfo.lock.RUnlock()122 t.Fatalf("Error mounting: exp(%s): act(%s)", bzzHash, minfo.StartManifest)123 }124 found = true125 }126 minfo.lock.RUnlock()127 }128 // Test listMounts129 if !found {130 t.Fatalf("Error getting mounts information for %v: %v", mountDir, err)131 }132 // Check if file and their attributes are as expected133 compareGeneratedFileWithFileInMount(t, files, mountDir)134 return swarmfs135}136// Check if file and their attributes are as expected137func compareGeneratedFileWithFileInMount(t *testing.T, files map[string]fileInfo, mountDir string) {138 err := filepath.Walk(mountDir, func(path string, f os.FileInfo, err error) error {139 if f.IsDir() {140 return nil141 }142 fname := path[len(mountDir)+1:]143 if _, ok := files[fname]; !ok {144 t.Fatalf(" file %v present in mount dir and is not expected", fname)145 }146 return nil147 })148 if err != nil {149 t.Fatalf("Error walking dir %v", mountDir)150 }151 for fname, finfo := range files {152 destinationFile := filepath.Join(mountDir, fname)153 dfinfo, err := os.Stat(destinationFile)154 if err != nil {155 t.Fatalf("Destination file %v missing in mount: %v", fname, err)156 }157 if int64(len(finfo.contents)) != dfinfo.Size() {158 t.Fatalf("file %v Size mismatch source (%v) vs destination(%v)", fname, int64(len(finfo.contents)), dfinfo.Size())159 }160 if dfinfo.Mode().Perm().String() != "-rwx------" {161 t.Fatalf("file %v Permission mismatch source (-rwx------) vs destination(%v)", fname, dfinfo.Mode().Perm())162 }163 fileContents, err := ioutil.ReadFile(filepath.Join(mountDir, fname))164 if err != nil {165 t.Fatalf("Could not readfile %v : %v", fname, err)166 }167 if !bytes.Equal(fileContents, finfo.contents) {168 t.Fatalf("File %v contents mismatch: %v , %v", fname, fileContents, finfo.contents)169 }170 // TODO: check uid and gid171 }172}173//check mounted file with provided content174func checkFile(t *testing.T, testMountDir, fname string, contents []byte) {175 destinationFile := filepath.Join(testMountDir, fname)176 dfinfo, err1 := os.Stat(destinationFile)177 if err1 != nil {178 t.Fatalf("Could not stat file %v", destinationFile)179 }180 if dfinfo.Size() != int64(len(contents)) {181 t.Fatalf("Mismatch in size actual(%v) vs expected(%v)", dfinfo.Size(), int64(len(contents)))182 }183 fd, err2 := os.OpenFile(destinationFile, os.O_RDONLY, os.FileMode(0665))184 if err2 != nil {185 t.Fatalf("Could not open file %v", destinationFile)186 }187 newcontent := make([]byte, len(contents))188 _, err := fd.Read(newcontent)189 if err != nil {190 t.Fatalf("Could not read from file %v", err)191 }192 err = fd.Close()193 if err != nil {194 t.Fatalf("Could not close file %v", err)195 }196 if !bytes.Equal(contents, newcontent) {197 t.Fatalf("File content mismatch expected (%v): received (%v) ", contents, newcontent)198 }199}200func isDirEmpty(name string) bool {201 f, err := os.Open(name)202 if err != nil {203 return false204 }205 defer f.Close()206 _, err = f.Readdirnames(1)207 return err == io.EOF208}209type testAPI struct {210 api *api.API211}212type testData struct {213 testDir string214 testUploadDir string215 testMountDir string216 bzzHash string217 files map[string]fileInfo218 toEncrypt bool219 swarmfs *SwarmFS220}221//create the root dir of a test222func (ta *testAPI) initSubtest(name string) (*testData, error) {223 var err error224 d := &testData{}225 d.testDir, err = ioutil.TempDir(os.TempDir(), name)226 if err != nil {227 return nil, fmt.Errorf("Couldn't create test dir: %v", err)228 }229 return d, nil230}231//upload data and mount directory232func (ta *testAPI) uploadAndMount(dat *testData, t *testing.T) (*testData, error) {233 //create upload dir234 err := os.MkdirAll(dat.testUploadDir, 0777)235 if err != nil {236 return nil, fmt.Errorf("Couldn't create upload dir: %v", err)237 }238 //create mount dir239 err = os.MkdirAll(dat.testMountDir, 0777)240 if err != nil {241 return nil, fmt.Errorf("Couldn't create mount dir: %v", err)242 }243 //upload the file244 dat.bzzHash = createTestFilesAndUploadToSwarm(t, ta.api, dat.files, dat.testUploadDir, dat.toEncrypt)245 log.Debug("Created test files and uploaded to Swarm")246 //mount the directory247 dat.swarmfs = mountDir(t, ta.api, dat.files, dat.bzzHash, dat.testMountDir)248 log.Debug("Mounted swarm fs")249 return dat, nil250}251//add a directory to the test directory tree252func addDir(root string, name string) (string, error) {253 d := filepath.Join(root, name)254 err := os.MkdirAll(d, 0777)255 if err != nil {256 return "", fmt.Errorf("Couldn't create dir inside test dir: %v", err)257 }258 return d, nil259}260func (ta *testAPI) mountListAndUnmountEncrypted(t *testing.T) {261 log.Debug("Starting mountListAndUnmountEncrypted test")262 ta.mountListAndUnmount(t, true)263 log.Debug("Test mountListAndUnmountEncrypted terminated")264}265func (ta *testAPI) mountListAndUnmountNonEncrypted(t *testing.T) {266 log.Debug("Starting mountListAndUnmountNonEncrypted test")267 ta.mountListAndUnmount(t, false)268 log.Debug("Test mountListAndUnmountNonEncrypted terminated")269}270//mount a directory unmount and check the directory is empty afterwards271func (ta *testAPI) mountListAndUnmount(t *testing.T, toEncrypt bool) {272 dat, err := ta.initSubtest("mountListAndUnmount")273 if err != nil {274 t.Fatalf("Couldn't initialize subtest dirs: %v", err)275 }276 defer os.RemoveAll(dat.testDir)277 dat.toEncrypt = toEncrypt278 dat.testUploadDir = filepath.Join(dat.testDir, "testUploadDir")279 dat.testMountDir = filepath.Join(dat.testDir, "testMountDir")280 dat.files = make(map[string]fileInfo)281 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}282 dat.files["2.txt"] = fileInfo{0711, 333, 444, testutil.RandomBytes(2, 10)}283 dat.files["3.txt"] = fileInfo{0622, 333, 444, testutil.RandomBytes(3, 100)}284 dat.files["4.txt"] = fileInfo{0533, 333, 444, testutil.RandomBytes(4, 1024)}285 dat.files["5.txt"] = fileInfo{0544, 333, 444, testutil.RandomBytes(5, 10)}286 dat.files["6.txt"] = fileInfo{0555, 333, 444, testutil.RandomBytes(6, 10)}287 dat.files["7.txt"] = fileInfo{0666, 333, 444, testutil.RandomBytes(7, 10)}288 dat.files["8.txt"] = fileInfo{0777, 333, 333, testutil.RandomBytes(8, 10)}289 dat.files["11.txt"] = fileInfo{0777, 333, 444, testutil.RandomBytes(9, 10)}290 dat.files["111.txt"] = fileInfo{0777, 333, 444, testutil.RandomBytes(10, 10)}291 dat.files["two/2.txt"] = fileInfo{0777, 333, 444, testutil.RandomBytes(11, 10)}292 dat.files["two/2/2.txt"] = fileInfo{0777, 333, 444, testutil.RandomBytes(12, 10)}293 dat.files["two/2./2.txt"] = fileInfo{0777, 444, 444, testutil.RandomBytes(13, 10)}294 dat.files["twice/2.txt"] = fileInfo{0777, 444, 333, testutil.RandomBytes(14, 200)}295 dat.files["one/two/three/four/five/six/seven/eight/nine/10.txt"] = fileInfo{0777, 333, 444, testutil.RandomBytes(15, 10240)}296 dat.files["one/two/three/four/five/six/six"] = fileInfo{0777, 333, 444, testutil.RandomBytes(16, 10)}297 dat, err = ta.uploadAndMount(dat, t)298 if err != nil {299 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)300 }301 defer dat.swarmfs.Stop()302 // Check unmount303 _, err = dat.swarmfs.Unmount(dat.testMountDir)304 if err != nil {305 t.Fatalf("could not unmount %v", dat.bzzHash)306 }307 log.Debug("Unmount successful")308 if !isDirEmpty(dat.testMountDir) {309 t.Fatalf("unmount didnt work for %v", dat.testMountDir)310 }311 log.Debug("subtest terminated")312}313func (ta *testAPI) maxMountsEncrypted(t *testing.T) {314 log.Debug("Starting maxMountsEncrypted test")315 ta.runMaxMounts(t, true)316 log.Debug("Test maxMountsEncrypted terminated")317}318func (ta *testAPI) maxMountsNonEncrypted(t *testing.T) {319 log.Debug("Starting maxMountsNonEncrypted test")320 ta.runMaxMounts(t, false)321 log.Debug("Test maxMountsNonEncrypted terminated")322}323//mount several different directories until the maximum has been reached324func (ta *testAPI) runMaxMounts(t *testing.T, toEncrypt bool) {325 dat, err := ta.initSubtest("runMaxMounts")326 if err != nil {327 t.Fatalf("Couldn't initialize subtest dirs: %v", err)328 }329 defer os.RemoveAll(dat.testDir)330 dat.toEncrypt = toEncrypt331 dat.testUploadDir = filepath.Join(dat.testDir, "max-upload1")332 dat.testMountDir = filepath.Join(dat.testDir, "max-mount1")333 dat.files = make(map[string]fileInfo)334 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}335 dat, err = ta.uploadAndMount(dat, t)336 if err != nil {337 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)338 }339 defer dat.swarmfs.Stop()340 dat.testUploadDir = filepath.Join(dat.testDir, "max-upload2")341 dat.testMountDir = filepath.Join(dat.testDir, "max-mount2")342 dat.files["2.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}343 dat, err = ta.uploadAndMount(dat, t)344 if err != nil {345 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)346 }347 dat.testUploadDir = filepath.Join(dat.testDir, "max-upload3")348 dat.testMountDir = filepath.Join(dat.testDir, "max-mount3")349 dat.files["3.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}350 dat, err = ta.uploadAndMount(dat, t)351 if err != nil {352 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)353 }354 dat.testUploadDir = filepath.Join(dat.testDir, "max-upload4")355 dat.testMountDir = filepath.Join(dat.testDir, "max-mount4")356 dat.files["4.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}357 dat, err = ta.uploadAndMount(dat, t)358 if err != nil {359 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)360 }361 dat.testUploadDir = filepath.Join(dat.testDir, "max-upload5")362 dat.testMountDir = filepath.Join(dat.testDir, "max-mount5")363 dat.files["5.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}364 dat, err = ta.uploadAndMount(dat, t)365 if err != nil {366 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)367 }368 //now try an additional mount, should fail due to max mounts reached369 testUploadDir6 := filepath.Join(dat.testDir, "max-upload6")370 err = os.MkdirAll(testUploadDir6, 0777)371 if err != nil {372 t.Fatalf("Couldn't create upload dir 6: %v", err)373 }374 dat.files["6.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}375 testMountDir6 := filepath.Join(dat.testDir, "max-mount6")376 err = os.MkdirAll(testMountDir6, 0777)377 if err != nil {378 t.Fatalf("Couldn't create mount dir 5: %v", err)379 }380 bzzHash6 := createTestFilesAndUploadToSwarm(t, ta.api, dat.files, testUploadDir6, toEncrypt)381 log.Debug("Created test files and uploaded to swarm with uploadDir6")382 _, err = dat.swarmfs.Mount(bzzHash6, testMountDir6)383 if err == nil {384 t.Fatalf("Expected this mount to fail due to exceeding max number of allowed mounts, but succeeded. %v", bzzHash6)385 }386 log.Debug("Maximum mount reached, additional mount failed. Correct.")387}388func (ta *testAPI) remountEncrypted(t *testing.T) {389 log.Debug("Starting remountEncrypted test")390 ta.remount(t, true)391 log.Debug("Test remountEncrypted terminated")392}393func (ta *testAPI) remountNonEncrypted(t *testing.T) {394 log.Debug("Starting remountNonEncrypted test")395 ta.remount(t, false)396 log.Debug("Test remountNonEncrypted terminated")397}398//test remounting same hash second time and different hash in already mounted point399func (ta *testAPI) remount(t *testing.T, toEncrypt bool) {400 dat, err := ta.initSubtest("remount")401 if err != nil {402 t.Fatalf("Couldn't initialize subtest dirs: %v", err)403 }404 defer os.RemoveAll(dat.testDir)405 dat.toEncrypt = toEncrypt406 dat.testUploadDir = filepath.Join(dat.testDir, "remount-upload1")407 dat.testMountDir = filepath.Join(dat.testDir, "remount-mount1")408 dat.files = make(map[string]fileInfo)409 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}410 dat, err = ta.uploadAndMount(dat, t)411 if err != nil {412 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)413 }414 defer dat.swarmfs.Stop()415 // try mounting the same hash second time416 testMountDir2, err2 := addDir(dat.testDir, "remount-mount2")417 if err2 != nil {418 t.Fatalf("Error creating second mount dir: %v", err2)419 }420 _, err2 = dat.swarmfs.Mount(dat.bzzHash, testMountDir2)421 if err2 != nil {422 t.Fatalf("Error mounting hash second time on different dir %v", dat.bzzHash)423 }424 // mount a different hash in already mounted point425 dat.files["2.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}426 testUploadDir2, err3 := addDir(dat.testDir, "remount-upload2")427 if err3 != nil {428 t.Fatalf("Error creating second upload dir: %v", err3)429 }430 bzzHash2 := createTestFilesAndUploadToSwarm(t, ta.api, dat.files, testUploadDir2, toEncrypt)431 _, err = swarmfs.Mount(bzzHash2, dat.testMountDir)432 if err == nil {433 t.Fatalf("Error mounting hash %v", bzzHash2)434 }435 log.Debug("Mount on existing mount point failed. Correct.")436 // mount nonexistent hash437 failDir, err3 := addDir(dat.testDir, "remount-fail")438 if err3 != nil {439 t.Fatalf("Error creating remount dir: %v", bzzHash2)440 }441 failHash := "0xfea11223344"442 _, err = swarmfs.Mount(failHash, failDir)443 if err == nil {444 t.Fatalf("Expected this mount to fail due to non existing hash. But succeeded %v", failHash)445 }446 log.Debug("Nonexistent hash hasn't been mounted. Correct.")447}448func (ta *testAPI) unmountEncrypted(t *testing.T) {449 log.Debug("Starting unmountEncrypted test")450 ta.unmount(t, true)451 log.Debug("Test unmountEncrypted terminated")452}453func (ta *testAPI) unmountNonEncrypted(t *testing.T) {454 log.Debug("Starting unmountNonEncrypted test")455 ta.unmount(t, false)456 log.Debug("Test unmountNonEncrypted terminated")457}458//mount then unmount and check that it has been unmounted459func (ta *testAPI) unmount(t *testing.T, toEncrypt bool) {460 dat, err := ta.initSubtest("unmount")461 if err != nil {462 t.Fatalf("Couldn't initialize subtest dirs: %v", err)463 }464 defer os.RemoveAll(dat.testDir)465 dat.toEncrypt = toEncrypt466 dat.testUploadDir = filepath.Join(dat.testDir, "ex-upload1")467 dat.testMountDir = filepath.Join(dat.testDir, "ex-mount1")468 dat.files = make(map[string]fileInfo)469 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}470 dat, err = ta.uploadAndMount(dat, t)471 if err != nil {472 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)473 }474 defer dat.swarmfs.Stop()475 _, err = dat.swarmfs.Unmount(dat.testMountDir)476 if err != nil {477 t.Fatalf("could not unmount %v", dat.bzzHash)478 }479 log.Debug("Unmounted Dir")480 mi := swarmfs.Listmounts()481 log.Debug("Going to list mounts")482 for _, minfo := range mi {483 log.Debug("Mount point in list: ", "point", minfo.MountPoint)484 if minfo.MountPoint == dat.testMountDir {485 t.Fatalf("mount state not cleaned up in unmount case %v", dat.testMountDir)486 }487 }488 log.Debug("subtest terminated")489}490func (ta *testAPI) unmountWhenResourceBusyEncrypted(t *testing.T) {491 log.Debug("Starting unmountWhenResourceBusyEncrypted test")492 ta.unmountWhenResourceBusy(t, true)493 log.Debug("Test unmountWhenResourceBusyEncrypted terminated")494}495func (ta *testAPI) unmountWhenResourceBusyNonEncrypted(t *testing.T) {496 log.Debug("Starting unmountWhenResourceBusyNonEncrypted test")497 ta.unmountWhenResourceBusy(t, false)498 log.Debug("Test unmountWhenResourceBusyNonEncrypted terminated")499}500//unmount while a resource is busy; should fail501func (ta *testAPI) unmountWhenResourceBusy(t *testing.T, toEncrypt bool) {502 dat, err := ta.initSubtest("unmountWhenResourceBusy")503 if err != nil {504 t.Fatalf("Couldn't initialize subtest dirs: %v", err)505 }506 defer os.RemoveAll(dat.testDir)507 dat.toEncrypt = toEncrypt508 dat.testUploadDir = filepath.Join(dat.testDir, "ex-upload1")509 dat.testMountDir = filepath.Join(dat.testDir, "ex-mount1")510 dat.files = make(map[string]fileInfo)511 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}512 dat, err = ta.uploadAndMount(dat, t)513 if err != nil {514 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)515 }516 defer dat.swarmfs.Stop()517 //create a file in the mounted directory, then try to unmount - should fail518 actualPath := filepath.Join(dat.testMountDir, "2.txt")519 //d, err := os.OpenFile(actualPath, os.O_RDWR, os.FileMode(0700))520 d, err := os.Create(actualPath)521 if err != nil {522 t.Fatalf("Couldn't create new file: %v", err)523 }524 //we need to manually close the file before mount for this test525 //but let's defer too in case of errors526 defer d.Close()527 _, err = d.Write(testutil.RandomBytes(1, 10))528 if err != nil {529 t.Fatalf("Couldn't write to file: %v", err)530 }531 log.Debug("Bytes written")532 _, err = dat.swarmfs.Unmount(dat.testMountDir)533 if err == nil {534 t.Fatalf("Expected mount to fail due to resource busy, but it succeeded...")535 }536 //free resources537 err = d.Close()538 if err != nil {539 t.Fatalf("Couldn't close file! %v", dat.bzzHash)540 }541 log.Debug("File closed")542 //now unmount after explicitly closed file543 _, err = dat.swarmfs.Unmount(dat.testMountDir)544 if err != nil {545 t.Fatalf("Expected mount to succeed after freeing resource, but it failed: %v", err)546 }547 //check if the dir is still mounted548 mi := dat.swarmfs.Listmounts()549 log.Debug("Going to list mounts")550 for _, minfo := range mi {551 log.Debug("Mount point in list: ", "point", minfo.MountPoint)552 if minfo.MountPoint == dat.testMountDir {553 t.Fatalf("mount state not cleaned up in unmount case %v", dat.testMountDir)554 }555 }556 log.Debug("subtest terminated")557}558func (ta *testAPI) seekInMultiChunkFileEncrypted(t *testing.T) {559 log.Debug("Starting seekInMultiChunkFileEncrypted test")560 ta.seekInMultiChunkFile(t, true)561 log.Debug("Test seekInMultiChunkFileEncrypted terminated")562}563func (ta *testAPI) seekInMultiChunkFileNonEncrypted(t *testing.T) {564 log.Debug("Starting seekInMultiChunkFileNonEncrypted test")565 ta.seekInMultiChunkFile(t, false)566 log.Debug("Test seekInMultiChunkFileNonEncrypted terminated")567}568//open a file in a mounted dir and go to a certain position569func (ta *testAPI) seekInMultiChunkFile(t *testing.T, toEncrypt bool) {570 dat, err := ta.initSubtest("seekInMultiChunkFile")571 if err != nil {572 t.Fatalf("Couldn't initialize subtest dirs: %v", err)573 }574 defer os.RemoveAll(dat.testDir)575 dat.toEncrypt = toEncrypt576 dat.testUploadDir = filepath.Join(dat.testDir, "seek-upload1")577 dat.testMountDir = filepath.Join(dat.testDir, "seek-mount")578 dat.files = make(map[string]fileInfo)579 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10240)}580 dat, err = ta.uploadAndMount(dat, t)581 if err != nil {582 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)583 }584 defer dat.swarmfs.Stop()585 // Open the file in the mounted dir and seek the second chunk586 actualPath := filepath.Join(dat.testMountDir, "1.txt")587 d, err := os.OpenFile(actualPath, os.O_RDONLY, os.FileMode(0700))588 if err != nil {589 t.Fatalf("Couldn't open file: %v", err)590 }591 log.Debug("Opened file")592 defer func() {593 err := d.Close()594 if err != nil {595 t.Fatalf("Error closing file! %v", err)596 }597 }()598 _, err = d.Seek(5000, 0)599 if err != nil {600 t.Fatalf("Error seeking in file: %v", err)601 }602 contents := make([]byte, 1024)603 _, err = d.Read(contents)604 if err != nil {605 t.Fatalf("Error reading file: %v", err)606 }607 log.Debug("Read contents")608 finfo := dat.files["1.txt"]609 if !bytes.Equal(finfo.contents[:6024][5000:], contents) {610 t.Fatalf("File seek contents mismatch")611 }612 log.Debug("subtest terminated")613}614func (ta *testAPI) createNewFileEncrypted(t *testing.T) {615 log.Debug("Starting createNewFileEncrypted test")616 ta.createNewFile(t, true)617 log.Debug("Test createNewFileEncrypted terminated")618}619func (ta *testAPI) createNewFileNonEncrypted(t *testing.T) {620 log.Debug("Starting createNewFileNonEncrypted test")621 ta.createNewFile(t, false)622 log.Debug("Test createNewFileNonEncrypted terminated")623}624//create a new file in a mounted swarm directory,625//unmount the fuse dir and then remount to see if new file is still there626func (ta *testAPI) createNewFile(t *testing.T, toEncrypt bool) {627 dat, err := ta.initSubtest("createNewFile")628 if err != nil {629 t.Fatalf("Couldn't initialize subtest dirs: %v", err)630 }631 defer os.RemoveAll(dat.testDir)632 dat.toEncrypt = toEncrypt633 dat.testUploadDir = filepath.Join(dat.testDir, "create-upload1")634 dat.testMountDir = filepath.Join(dat.testDir, "create-mount")635 dat.files = make(map[string]fileInfo)636 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}637 dat.files["five.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(2, 10)}638 dat.files["six.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(3, 10)}639 dat, err = ta.uploadAndMount(dat, t)640 if err != nil {641 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)642 }643 defer dat.swarmfs.Stop()644 // Create a new file in the root dir and check645 actualPath := filepath.Join(dat.testMountDir, "2.txt")646 d, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(0665))647 if err1 != nil {648 t.Fatalf("Could not open file %s : %v", actualPath, err1)649 }650 defer d.Close()651 log.Debug("Opened file")652 contents := testutil.RandomBytes(1, 11)653 log.Debug("content read")654 _, err = d.Write(contents)655 if err != nil {656 t.Fatalf("Couldn't write contents: %v", err)657 }658 log.Debug("content written")659 err = d.Close()660 if err != nil {661 t.Fatalf("Couldn't close file: %v", err)662 }663 log.Debug("file closed")664 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir)665 if err2 != nil {666 t.Fatalf("Could not unmount %v", err2)667 }668 log.Debug("Directory unmounted")669 testMountDir2, err3 := addDir(dat.testDir, "create-mount2")670 if err3 != nil {671 t.Fatalf("Error creating mount dir2: %v", err3)672 }673 // mount again and see if things are okay674 dat.files["2.txt"] = fileInfo{0700, 333, 444, contents}675 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, testMountDir2)676 log.Debug("Directory mounted again")677 checkFile(t, testMountDir2, "2.txt", contents)678 _, err2 = dat.swarmfs.Unmount(testMountDir2)679 if err2 != nil {680 t.Fatalf("Could not unmount %v", err2)681 }682 log.Debug("subtest terminated")683}684func (ta *testAPI) createNewFileInsideDirectoryEncrypted(t *testing.T) {685 log.Debug("Starting createNewFileInsideDirectoryEncrypted test")686 ta.createNewFileInsideDirectory(t, true)687 log.Debug("Test createNewFileInsideDirectoryEncrypted terminated")688}689func (ta *testAPI) createNewFileInsideDirectoryNonEncrypted(t *testing.T) {690 log.Debug("Starting createNewFileInsideDirectoryNonEncrypted test")691 ta.createNewFileInsideDirectory(t, false)692 log.Debug("Test createNewFileInsideDirectoryNonEncrypted terminated")693}694//create a new file inside a directory inside the mount695func (ta *testAPI) createNewFileInsideDirectory(t *testing.T, toEncrypt bool) {696 dat, err := ta.initSubtest("createNewFileInsideDirectory")697 if err != nil {698 t.Fatalf("Couldn't initialize subtest dirs: %v", err)699 }700 defer os.RemoveAll(dat.testDir)701 dat.toEncrypt = toEncrypt702 dat.testUploadDir = filepath.Join(dat.testDir, "createinsidedir-upload")703 dat.testMountDir = filepath.Join(dat.testDir, "createinsidedir-mount")704 dat.files = make(map[string]fileInfo)705 dat.files["one/1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}706 dat, err = ta.uploadAndMount(dat, t)707 if err != nil {708 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)709 }710 defer dat.swarmfs.Stop()711 // Create a new file inside a existing dir and check712 dirToCreate := filepath.Join(dat.testMountDir, "one")713 actualPath := filepath.Join(dirToCreate, "2.txt")714 d, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(0665))715 if err1 != nil {716 t.Fatalf("Could not create file %s : %v", actualPath, err1)717 }718 defer d.Close()719 log.Debug("File opened")720 contents := testutil.RandomBytes(1, 11)721 log.Debug("Content read")722 _, err = d.Write(contents)723 if err != nil {724 t.Fatalf("Error writing random bytes into file %v", err)725 }726 log.Debug("Content written")727 err = d.Close()728 if err != nil {729 t.Fatalf("Error closing file %v", err)730 }731 log.Debug("File closed")732 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir)733 if err2 != nil {734 t.Fatalf("Could not unmount %v", err2)735 }736 log.Debug("Directory unmounted")737 testMountDir2, err3 := addDir(dat.testDir, "createinsidedir-mount2")738 if err3 != nil {739 t.Fatalf("Error creating mount dir2: %v", err3)740 }741 // mount again and see if things are okay742 dat.files["one/2.txt"] = fileInfo{0700, 333, 444, contents}743 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, testMountDir2)744 log.Debug("Directory mounted again")745 checkFile(t, testMountDir2, "one/2.txt", contents)746 _, err = dat.swarmfs.Unmount(testMountDir2)747 if err != nil {748 t.Fatalf("could not unmount %v", dat.bzzHash)749 }750 log.Debug("subtest terminated")751}752func (ta *testAPI) createNewFileInsideNewDirectoryEncrypted(t *testing.T) {753 log.Debug("Starting createNewFileInsideNewDirectoryEncrypted test")754 ta.createNewFileInsideNewDirectory(t, true)755 log.Debug("Test createNewFileInsideNewDirectoryEncrypted terminated")756}757func (ta *testAPI) createNewFileInsideNewDirectoryNonEncrypted(t *testing.T) {758 log.Debug("Starting createNewFileInsideNewDirectoryNonEncrypted test")759 ta.createNewFileInsideNewDirectory(t, false)760 log.Debug("Test createNewFileInsideNewDirectoryNonEncrypted terminated")761}762//create a new directory in mount and a new file763func (ta *testAPI) createNewFileInsideNewDirectory(t *testing.T, toEncrypt bool) {764 dat, err := ta.initSubtest("createNewFileInsideNewDirectory")765 if err != nil {766 t.Fatalf("Couldn't initialize subtest dirs: %v", err)767 }768 defer os.RemoveAll(dat.testDir)769 dat.toEncrypt = toEncrypt770 dat.testUploadDir = filepath.Join(dat.testDir, "createinsidenewdir-upload")771 dat.testMountDir = filepath.Join(dat.testDir, "createinsidenewdir-mount")772 dat.files = make(map[string]fileInfo)773 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}774 dat, err = ta.uploadAndMount(dat, t)775 if err != nil {776 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)777 }778 defer dat.swarmfs.Stop()779 // Create a new file inside a existing dir and check780 dirToCreate, err2 := addDir(dat.testMountDir, "one")781 if err2 != nil {782 t.Fatalf("Error creating mount dir2: %v", err2)783 }784 actualPath := filepath.Join(dirToCreate, "2.txt")785 d, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(0665))786 if err1 != nil {787 t.Fatalf("Could not create file %s : %v", actualPath, err1)788 }789 defer d.Close()790 log.Debug("File opened")791 contents := testutil.RandomBytes(1, 11)792 log.Debug("content read")793 _, err = d.Write(contents)794 if err != nil {795 t.Fatalf("Error writing to file: %v", err)796 }797 log.Debug("content written")798 err = d.Close()799 if err != nil {800 t.Fatalf("Error closing file: %v", err)801 }802 log.Debug("File closed")803 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir)804 if err2 != nil {805 t.Fatalf("Could not unmount %v", err2)806 }807 log.Debug("Directory unmounted")808 // mount again and see if things are okay809 dat.files["one/2.txt"] = fileInfo{0700, 333, 444, contents}810 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, dat.testMountDir)811 log.Debug("Directory mounted again")812 checkFile(t, dat.testMountDir, "one/2.txt", contents)813 _, err2 = dat.swarmfs.Unmount(dat.testMountDir)814 if err2 != nil {815 t.Fatalf("Could not unmount %v", err2)816 }817 log.Debug("subtest terminated")818}819func (ta *testAPI) removeExistingFileEncrypted(t *testing.T) {820 log.Debug("Starting removeExistingFileEncrypted test")821 ta.removeExistingFile(t, true)822 log.Debug("Test removeExistingFileEncrypted terminated")823}824func (ta *testAPI) removeExistingFileNonEncrypted(t *testing.T) {825 log.Debug("Starting removeExistingFileNonEncrypted test")826 ta.removeExistingFile(t, false)827 log.Debug("Test removeExistingFileNonEncrypted terminated")828}829//remove existing file in mount830func (ta *testAPI) removeExistingFile(t *testing.T, toEncrypt bool) {831 dat, err := ta.initSubtest("removeExistingFile")832 if err != nil {833 t.Fatalf("Couldn't initialize subtest dirs: %v", err)834 }835 defer os.RemoveAll(dat.testDir)836 dat.toEncrypt = toEncrypt837 dat.testUploadDir = filepath.Join(dat.testDir, "remove-upload")838 dat.testMountDir = filepath.Join(dat.testDir, "remove-mount")839 dat.files = make(map[string]fileInfo)840 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}841 dat.files["five.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(2, 10)}842 dat.files["six.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(3, 10)}843 dat, err = ta.uploadAndMount(dat, t)844 if err != nil {845 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)846 }847 defer dat.swarmfs.Stop()848 // Remove a file in the root dir and check849 actualPath := filepath.Join(dat.testMountDir, "five.txt")850 err = os.Remove(actualPath)851 if err != nil {852 t.Fatalf("Error removing file! %v", err)853 }854 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir)855 if err2 != nil {856 t.Fatalf("Could not unmount %v", err2)857 }858 log.Debug("Directory unmounted")859 // mount again and see if things are okay860 delete(dat.files, "five.txt")861 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, dat.testMountDir)862 _, err = os.Stat(actualPath)863 if err == nil {864 t.Fatal("Expected file to not be present in re-mount after removal, but it is there")865 }866 _, err2 = dat.swarmfs.Unmount(dat.testMountDir)867 if err2 != nil {868 t.Fatalf("Could not unmount %v", err2)869 }870 log.Debug("subtest terminated")871}872func (ta *testAPI) removeExistingFileInsideDirEncrypted(t *testing.T) {873 log.Debug("Starting removeExistingFileInsideDirEncrypted test")874 ta.removeExistingFileInsideDir(t, true)875 log.Debug("Test removeExistingFileInsideDirEncrypted terminated")876}877func (ta *testAPI) removeExistingFileInsideDirNonEncrypted(t *testing.T) {878 log.Debug("Starting removeExistingFileInsideDirNonEncrypted test")879 ta.removeExistingFileInsideDir(t, false)880 log.Debug("Test removeExistingFileInsideDirNonEncrypted terminated")881}882//remove a file inside a directory inside a mount883func (ta *testAPI) removeExistingFileInsideDir(t *testing.T, toEncrypt bool) {884 dat, err := ta.initSubtest("removeExistingFileInsideDir")885 if err != nil {886 t.Fatalf("Couldn't initialize subtest dirs: %v", err)887 }888 defer os.RemoveAll(dat.testDir)889 dat.toEncrypt = toEncrypt890 dat.testUploadDir = filepath.Join(dat.testDir, "remove-upload")891 dat.testMountDir = filepath.Join(dat.testDir, "remove-mount")892 dat.files = make(map[string]fileInfo)893 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}894 dat.files["one/five.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(2, 10)}895 dat.files["one/six.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(3, 10)}896 dat, err = ta.uploadAndMount(dat, t)897 if err != nil {898 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)899 }900 defer dat.swarmfs.Stop()901 // Remove a file in the root dir and check902 actualPath := filepath.Join(dat.testMountDir, "one")903 actualPath = filepath.Join(actualPath, "five.txt")904 err = os.Remove(actualPath)905 if err != nil {906 t.Fatalf("Error removing file! %v", err)907 }908 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir)909 if err2 != nil {910 t.Fatalf("Could not unmount %v", err2)911 }912 log.Debug("Directory unmounted")913 // mount again and see if things are okay914 delete(dat.files, "one/five.txt")915 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, dat.testMountDir)916 _, err = os.Stat(actualPath)917 if err == nil {918 t.Fatal("Expected file to not be present in re-mount after removal, but it is there")919 }920 okPath := filepath.Join(dat.testMountDir, "one")921 okPath = filepath.Join(okPath, "six.txt")922 _, err = os.Stat(okPath)923 if err != nil {924 t.Fatal("Expected file to be present in re-mount after removal, but it is not there")925 }926 _, err2 = dat.swarmfs.Unmount(dat.testMountDir)927 if err2 != nil {928 t.Fatalf("Could not unmount %v", err2)929 }930 log.Debug("subtest terminated")931}932func (ta *testAPI) removeNewlyAddedFileEncrypted(t *testing.T) {933 log.Debug("Starting removeNewlyAddedFileEncrypted test")934 ta.removeNewlyAddedFile(t, true)935 log.Debug("Test removeNewlyAddedFileEncrypted terminated")936}937func (ta *testAPI) removeNewlyAddedFileNonEncrypted(t *testing.T) {938 log.Debug("Starting removeNewlyAddedFileNonEncrypted test")939 ta.removeNewlyAddedFile(t, false)940 log.Debug("Test removeNewlyAddedFileNonEncrypted terminated")941}942//add a file in mount and then remove it; on remount file should not be there943func (ta *testAPI) removeNewlyAddedFile(t *testing.T, toEncrypt bool) {944 dat, err := ta.initSubtest("removeNewlyAddedFile")945 if err != nil {946 t.Fatalf("Couldn't initialize subtest dirs: %v", err)947 }948 defer os.RemoveAll(dat.testDir)949 dat.toEncrypt = toEncrypt950 dat.testUploadDir = filepath.Join(dat.testDir, "removenew-upload")951 dat.testMountDir = filepath.Join(dat.testDir, "removenew-mount")952 dat.files = make(map[string]fileInfo)953 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}954 dat.files["five.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(2, 10)}955 dat.files["six.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(3, 10)}956 dat, err = ta.uploadAndMount(dat, t)957 if err != nil {958 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)959 }960 defer dat.swarmfs.Stop()961 // Add a a new file and remove it962 dirToCreate := filepath.Join(dat.testMountDir, "one")963 err = os.MkdirAll(dirToCreate, os.FileMode(0665))964 if err != nil {965 t.Fatalf("Error creating dir in mounted dir: %v", err)966 }967 actualPath := filepath.Join(dirToCreate, "2.txt")968 d, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(0665))969 if err1 != nil {970 t.Fatalf("Could not create file %s : %v", actualPath, err1)971 }972 defer d.Close()973 log.Debug("file opened")974 contents := testutil.RandomBytes(1, 11)975 log.Debug("content read")976 _, err = d.Write(contents)977 if err != nil {978 t.Fatalf("Error writing random bytes to file: %v", err)979 }980 log.Debug("content written")981 err = d.Close()982 if err != nil {983 t.Fatalf("Error closing file: %v", err)984 }985 log.Debug("file closed")986 checkFile(t, dat.testMountDir, "one/2.txt", contents)987 log.Debug("file checked")988 err = os.Remove(actualPath)989 if err != nil {990 t.Fatalf("Error removing file: %v", err)991 }992 log.Debug("file removed")993 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir)994 if err2 != nil {995 t.Fatalf("Could not unmount %v", err2)996 }997 log.Debug("Directory unmounted")998 testMountDir2, err3 := addDir(dat.testDir, "removenew-mount2")999 if err3 != nil {1000 t.Fatalf("Error creating mount dir2: %v", err3)1001 }1002 // mount again and see if things are okay1003 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, testMountDir2)1004 log.Debug("Directory mounted again")1005 if dat.bzzHash != mi.LatestManifest {1006 t.Fatalf("same contents different hash orig(%v): new(%v)", dat.bzzHash, mi.LatestManifest)1007 }1008 _, err2 = dat.swarmfs.Unmount(testMountDir2)1009 if err2 != nil {1010 t.Fatalf("Could not unmount %v", err2)1011 }1012 log.Debug("subtest terminated")1013}1014func (ta *testAPI) addNewFileAndModifyContentsEncrypted(t *testing.T) {1015 log.Debug("Starting addNewFileAndModifyContentsEncrypted test")1016 ta.addNewFileAndModifyContents(t, true)1017 log.Debug("Test addNewFileAndModifyContentsEncrypted terminated")1018}1019func (ta *testAPI) addNewFileAndModifyContentsNonEncrypted(t *testing.T) {1020 log.Debug("Starting addNewFileAndModifyContentsNonEncrypted test")1021 ta.addNewFileAndModifyContents(t, false)1022 log.Debug("Test addNewFileAndModifyContentsNonEncrypted terminated")1023}1024//add a new file and modify content; remount and check the modified file is intact1025func (ta *testAPI) addNewFileAndModifyContents(t *testing.T, toEncrypt bool) {1026 dat, err := ta.initSubtest("addNewFileAndModifyContents")1027 if err != nil {1028 t.Fatalf("Couldn't initialize subtest dirs: %v", err)1029 }1030 defer os.RemoveAll(dat.testDir)1031 dat.toEncrypt = toEncrypt1032 dat.testUploadDir = filepath.Join(dat.testDir, "modifyfile-upload")1033 dat.testMountDir = filepath.Join(dat.testDir, "modifyfile-mount")1034 dat.files = make(map[string]fileInfo)1035 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}1036 dat.files["five.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(2, 10)}1037 dat.files["six.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(3, 10)}1038 dat, err = ta.uploadAndMount(dat, t)1039 if err != nil {1040 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)1041 }1042 defer dat.swarmfs.Stop()1043 // Create a new file in the root dir1044 actualPath := filepath.Join(dat.testMountDir, "2.txt")1045 d, err1 := os.OpenFile(actualPath, os.O_RDWR|os.O_CREATE, os.FileMode(0665))1046 if err1 != nil {1047 t.Fatalf("Could not create file %s : %v", actualPath, err1)1048 }1049 defer d.Close()1050 //write some random data into the file1051 log.Debug("file opened")1052 line1 := []byte("Line 1")1053 _, err = rand.Read(line1)1054 if err != nil {1055 t.Fatalf("Error writing random bytes to byte array: %v", err)1056 }1057 log.Debug("line read")1058 _, err = d.Write(line1)1059 if err != nil {1060 t.Fatalf("Error writing random bytes to file: %v", err)1061 }1062 log.Debug("line written")1063 err = d.Close()1064 if err != nil {1065 t.Fatalf("Error closing file: %v", err)1066 }1067 log.Debug("file closed")1068 //unmount the hash on the mounted dir1069 mi1, err2 := dat.swarmfs.Unmount(dat.testMountDir)1070 if err2 != nil {1071 t.Fatalf("Could not unmount %v", err2)1072 }1073 log.Debug("Directory unmounted")1074 //mount on a different dir to see if modified file is correct1075 testMountDir2, err3 := addDir(dat.testDir, "modifyfile-mount2")1076 if err3 != nil {1077 t.Fatalf("Error creating mount dir2: %v", err3)1078 }1079 dat.files["2.txt"] = fileInfo{0700, 333, 444, line1}1080 _ = mountDir(t, ta.api, dat.files, mi1.LatestManifest, testMountDir2)1081 log.Debug("Directory mounted again")1082 checkFile(t, testMountDir2, "2.txt", line1)1083 log.Debug("file checked")1084 //unmount second dir1085 mi2, err4 := dat.swarmfs.Unmount(testMountDir2)1086 if err4 != nil {1087 t.Fatalf("Could not unmount %v", err4)1088 }1089 log.Debug("Directory unmounted again")1090 //mount again on original dir and modify the file1091 //let's clean up the mounted dir first: remove...1092 err = os.RemoveAll(dat.testMountDir)1093 if err != nil {1094 t.Fatalf("Error cleaning up mount dir: %v", err)1095 }1096 //...and re-create1097 err = os.MkdirAll(dat.testMountDir, 0777)1098 if err != nil {1099 t.Fatalf("Error re-creating mount dir: %v", err)1100 }1101 //now remount1102 _ = mountDir(t, ta.api, dat.files, mi2.LatestManifest, dat.testMountDir)1103 log.Debug("Directory mounted yet again")1104 //open the file....1105 fd, err5 := os.OpenFile(actualPath, os.O_RDWR|os.O_APPEND, os.FileMode(0665))1106 if err5 != nil {1107 t.Fatalf("Could not create file %s : %v", actualPath, err5)1108 }1109 defer fd.Close()1110 log.Debug("file opened")1111 //...and modify something1112 line2 := []byte("Line 2")1113 _, err = rand.Read(line2)1114 if err != nil {1115 t.Fatalf("Error modifying random bytes to byte array: %v", err)1116 }1117 log.Debug("line read")1118 _, err = fd.Seek(int64(len(line1)), 0)1119 if err != nil {1120 t.Fatalf("Error seeking position for modification: %v", err)1121 }1122 _, err = fd.Write(line2)1123 if err != nil {1124 t.Fatalf("Error modifying file: %v", err)1125 }1126 log.Debug("line written")1127 err = fd.Close()1128 if err != nil {1129 t.Fatalf("Error closing modified file; %v", err)1130 }1131 log.Debug("file closed")1132 //unmount the modified directory1133 mi3, err6 := dat.swarmfs.Unmount(dat.testMountDir)1134 if err6 != nil {1135 t.Fatalf("Could not unmount %v", err6)1136 }1137 log.Debug("Directory unmounted yet again")1138 //now remount on a different dir and check that the modified file is ok1139 testMountDir4, err7 := addDir(dat.testDir, "modifyfile-mount4")1140 if err7 != nil {1141 t.Fatalf("Could not unmount %v", err7)1142 }1143 b := [][]byte{line1, line2}1144 line1and2 := bytes.Join(b, []byte(""))1145 dat.files["2.txt"] = fileInfo{0700, 333, 444, line1and2}1146 _ = mountDir(t, ta.api, dat.files, mi3.LatestManifest, testMountDir4)1147 log.Debug("Directory mounted final time")1148 checkFile(t, testMountDir4, "2.txt", line1and2)1149 _, err = dat.swarmfs.Unmount(testMountDir4)1150 if err != nil {1151 t.Fatalf("Could not unmount %v", err)1152 }1153 log.Debug("subtest terminated")1154}1155func (ta *testAPI) removeEmptyDirEncrypted(t *testing.T) {1156 log.Debug("Starting removeEmptyDirEncrypted test")1157 ta.removeEmptyDir(t, true)1158 log.Debug("Test removeEmptyDirEncrypted terminated")1159}1160func (ta *testAPI) removeEmptyDirNonEncrypted(t *testing.T) {1161 log.Debug("Starting removeEmptyDirNonEncrypted test")1162 ta.removeEmptyDir(t, false)1163 log.Debug("Test removeEmptyDirNonEncrypted terminated")1164}1165//remove an empty dir inside mount1166func (ta *testAPI) removeEmptyDir(t *testing.T, toEncrypt bool) {1167 dat, err := ta.initSubtest("removeEmptyDir")1168 if err != nil {1169 t.Fatalf("Couldn't initialize subtest dirs: %v", err)1170 }1171 defer os.RemoveAll(dat.testDir)1172 dat.toEncrypt = toEncrypt1173 dat.testUploadDir = filepath.Join(dat.testDir, "rmdir-upload")1174 dat.testMountDir = filepath.Join(dat.testDir, "rmdir-mount")1175 dat.files = make(map[string]fileInfo)1176 dat.files["1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}1177 dat.files["five.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(2, 10)}1178 dat.files["six.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(3, 10)}1179 dat, err = ta.uploadAndMount(dat, t)1180 if err != nil {1181 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)1182 }1183 defer dat.swarmfs.Stop()1184 _, err2 := addDir(dat.testMountDir, "newdir")1185 if err2 != nil {1186 t.Fatalf("Could not unmount %v", err2)1187 }1188 mi, err := dat.swarmfs.Unmount(dat.testMountDir)1189 if err != nil {1190 t.Fatalf("Could not unmount %v", err)1191 }1192 log.Debug("Directory unmounted")1193 //by just adding an empty dir, the hash doesn't change; test this1194 if dat.bzzHash != mi.LatestManifest {1195 t.Fatalf("same contents different hash orig(%v): new(%v)", dat.bzzHash, mi.LatestManifest)1196 }1197 log.Debug("subtest terminated")1198}1199func (ta *testAPI) removeDirWhichHasFilesEncrypted(t *testing.T) {1200 log.Debug("Starting removeDirWhichHasFilesEncrypted test")1201 ta.removeDirWhichHasFiles(t, true)1202 log.Debug("Test removeDirWhichHasFilesEncrypted terminated")1203}1204func (ta *testAPI) removeDirWhichHasFilesNonEncrypted(t *testing.T) {1205 log.Debug("Starting removeDirWhichHasFilesNonEncrypted test")1206 ta.removeDirWhichHasFiles(t, false)1207 log.Debug("Test removeDirWhichHasFilesNonEncrypted terminated")1208}1209//remove a directory with a file; check on remount file isn't there1210func (ta *testAPI) removeDirWhichHasFiles(t *testing.T, toEncrypt bool) {1211 dat, err := ta.initSubtest("removeDirWhichHasFiles")1212 if err != nil {1213 t.Fatalf("Couldn't initialize subtest dirs: %v", err)1214 }1215 defer os.RemoveAll(dat.testDir)1216 dat.toEncrypt = toEncrypt1217 dat.testUploadDir = filepath.Join(dat.testDir, "rmdir-upload")1218 dat.testMountDir = filepath.Join(dat.testDir, "rmdir-mount")1219 dat.files = make(map[string]fileInfo)1220 dat.files["one/1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}1221 dat.files["two/five.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(2, 10)}1222 dat.files["two/six.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(3, 10)}1223 dat, err = ta.uploadAndMount(dat, t)1224 if err != nil {1225 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)1226 }1227 defer dat.swarmfs.Stop()1228 //delete a directory inside the mounted dir with all its files1229 dirPath := filepath.Join(dat.testMountDir, "two")1230 err = os.RemoveAll(dirPath)1231 if err != nil {1232 t.Fatalf("Error removing directory in mounted dir: %v", err)1233 }1234 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir)1235 if err2 != nil {1236 t.Fatalf("Could not unmount %v ", err2)1237 }1238 log.Debug("Directory unmounted")1239 //we deleted files in the OS, so let's delete them also in the files map1240 delete(dat.files, "two/five.txt")1241 delete(dat.files, "two/six.txt")1242 // mount again and see if deleted files have been deleted indeed1243 testMountDir2, err3 := addDir(dat.testDir, "remount-mount2")1244 if err3 != nil {1245 t.Fatalf("Could not unmount %v", err3)1246 }1247 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, testMountDir2)1248 log.Debug("Directory mounted")1249 actualPath := filepath.Join(dirPath, "five.txt")1250 _, err = os.Stat(actualPath)1251 if err == nil {1252 t.Fatal("Expected file to not be present in re-mount after removal, but it is there")1253 }1254 _, err = os.Stat(dirPath)1255 if err == nil {1256 t.Fatal("Expected file to not be present in re-mount after removal, but it is there")1257 }1258 _, err = dat.swarmfs.Unmount(testMountDir2)1259 if err != nil {1260 t.Fatalf("Could not unmount %v", err)1261 }1262 log.Debug("subtest terminated")1263}1264func (ta *testAPI) removeDirWhichHasSubDirsEncrypted(t *testing.T) {1265 log.Debug("Starting removeDirWhichHasSubDirsEncrypted test")1266 ta.removeDirWhichHasSubDirs(t, true)1267 log.Debug("Test removeDirWhichHasSubDirsEncrypted terminated")1268}1269func (ta *testAPI) removeDirWhichHasSubDirsNonEncrypted(t *testing.T) {1270 log.Debug("Starting removeDirWhichHasSubDirsNonEncrypted test")1271 ta.removeDirWhichHasSubDirs(t, false)1272 log.Debug("Test removeDirWhichHasSubDirsNonEncrypted terminated")1273}1274//remove a directory with subdirectories inside mount; on remount check they are not there1275func (ta *testAPI) removeDirWhichHasSubDirs(t *testing.T, toEncrypt bool) {1276 dat, err := ta.initSubtest("removeDirWhichHasSubDirs")1277 if err != nil {1278 t.Fatalf("Couldn't initialize subtest dirs: %v", err)1279 }1280 defer os.RemoveAll(dat.testDir)1281 dat.toEncrypt = toEncrypt1282 dat.testUploadDir = filepath.Join(dat.testDir, "rmsubdir-upload")1283 dat.testMountDir = filepath.Join(dat.testDir, "rmsubdir-mount")1284 dat.files = make(map[string]fileInfo)1285 dat.files["one/1.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(1, 10)}1286 dat.files["two/three/2.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(2, 10)}1287 dat.files["two/three/3.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(3, 10)}1288 dat.files["two/four/5.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(4, 10)}1289 dat.files["two/four/6.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(5, 10)}1290 dat.files["two/four/six/7.txt"] = fileInfo{0700, 333, 444, testutil.RandomBytes(6, 10)}1291 dat, err = ta.uploadAndMount(dat, t)1292 if err != nil {1293 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)1294 }1295 defer dat.swarmfs.Stop()1296 dirPath := filepath.Join(dat.testMountDir, "two")1297 err = os.RemoveAll(dirPath)1298 if err != nil {1299 t.Fatalf("Error removing directory in mounted dir: %v", err)1300 }1301 //delete a directory inside the mounted dir with all its files1302 mi, err2 := dat.swarmfs.Unmount(dat.testMountDir)1303 if err2 != nil {1304 t.Fatalf("Could not unmount %v ", err2)1305 }1306 log.Debug("Directory unmounted")1307 //we deleted files in the OS, so let's delete them also in the files map1308 delete(dat.files, "two/three/2.txt")1309 delete(dat.files, "two/three/3.txt")1310 delete(dat.files, "two/four/5.txt")1311 delete(dat.files, "two/four/6.txt")1312 delete(dat.files, "two/four/six/7.txt")1313 // mount again and see if things are okay1314 testMountDir2, err3 := addDir(dat.testDir, "remount-mount2")1315 if err3 != nil {1316 t.Fatalf("Could not unmount %v", err3)1317 }1318 _ = mountDir(t, ta.api, dat.files, mi.LatestManifest, testMountDir2)1319 log.Debug("Directory mounted again")1320 actualPath := filepath.Join(dirPath, "three")1321 actualPath = filepath.Join(actualPath, "2.txt")1322 _, err = os.Stat(actualPath)1323 if err == nil {1324 t.Fatal("Expected file to not be present in re-mount after removal, but it is there")1325 }1326 actualPath = filepath.Join(dirPath, "four")1327 _, err = os.Stat(actualPath)1328 if err == nil {1329 t.Fatal("Expected file to not be present in re-mount after removal, but it is there")1330 }1331 _, err = os.Stat(dirPath)1332 if err == nil {1333 t.Fatal("Expected file to not be present in re-mount after removal, but it is there")1334 }1335 _, err = dat.swarmfs.Unmount(testMountDir2)1336 if err != nil {1337 t.Fatalf("Could not unmount %v", err)1338 }1339 log.Debug("subtest terminated")1340}1341func (ta *testAPI) appendFileContentsToEndEncrypted(t *testing.T) {1342 log.Debug("Starting appendFileContentsToEndEncrypted test")1343 ta.appendFileContentsToEnd(t, true)1344 log.Debug("Test appendFileContentsToEndEncrypted terminated")1345}1346func (ta *testAPI) appendFileContentsToEndNonEncrypted(t *testing.T) {1347 log.Debug("Starting appendFileContentsToEndNonEncrypted test")1348 ta.appendFileContentsToEnd(t, false)1349 log.Debug("Test appendFileContentsToEndNonEncrypted terminated")1350}1351//append contents to the end of a file; remount and check it's intact1352func (ta *testAPI) appendFileContentsToEnd(t *testing.T, toEncrypt bool) {1353 dat, err := ta.initSubtest("appendFileContentsToEnd")1354 if err != nil {1355 t.Fatalf("Couldn't initialize subtest dirs: %v", err)1356 }1357 defer os.RemoveAll(dat.testDir)1358 dat.toEncrypt = toEncrypt1359 dat.testUploadDir = filepath.Join(dat.testDir, "appendlargefile-upload")1360 dat.testMountDir = filepath.Join(dat.testDir, "appendlargefile-mount")1361 dat.files = make(map[string]fileInfo)1362 line1 := testutil.RandomBytes(1, 10)1363 dat.files["1.txt"] = fileInfo{0700, 333, 444, line1}1364 dat, err = ta.uploadAndMount(dat, t)1365 if err != nil {1366 t.Fatalf("Error during upload of files to swarm / mount of swarm dir: %v", err)1367 }1368 defer dat.swarmfs.Stop()1369 actualPath := filepath.Join(dat.testMountDir, "1.txt")1370 fd, err4 := os.OpenFile(actualPath, os.O_RDWR|os.O_APPEND, os.FileMode(0665))1371 if err4 != nil {1372 t.Fatalf("Could not create file %s : %v", actualPath, err4)1373 }1374 defer fd.Close()1375 log.Debug("file opened")1376 line2 := testutil.RandomBytes(1, 5)1377 log.Debug("line read")1378 _, err = fd.Seek(int64(len(line1)), 0)1379 if err != nil {1380 t.Fatalf("Error searching for position to append: %v", err)1381 }1382 _, err = fd.Write(line2)1383 if err != nil {1384 t.Fatalf("Error appending: %v", err)1385 }1386 log.Debug("line written")1387 err = fd.Close()1388 if err != nil {1389 t.Fatalf("Error closing file: %v", err)1390 }1391 log.Debug("file closed")1392 mi1, err5 := dat.swarmfs.Unmount(dat.testMountDir)1393 if err5 != nil {1394 t.Fatalf("Could not unmount %v ", err5)1395 }1396 log.Debug("Directory unmounted")1397 // mount again and see if appended file is correct1398 b := [][]byte{line1, line2}1399 line1and2 := bytes.Join(b, []byte(""))1400 dat.files["1.txt"] = fileInfo{0700, 333, 444, line1and2}1401 testMountDir2, err6 := addDir(dat.testDir, "remount-mount2")1402 if err6 != nil {1403 t.Fatalf("Could not unmount %v", err6)1404 }1405 _ = mountDir(t, ta.api, dat.files, mi1.LatestManifest, testMountDir2)1406 log.Debug("Directory mounted")1407 checkFile(t, testMountDir2, "1.txt", line1and2)1408 _, err = dat.swarmfs.Unmount(testMountDir2)1409 if err != nil {1410 t.Fatalf("Could not unmount %v", err)1411 }1412 log.Debug("subtest terminated")1413}1414//run all the tests1415func TestFUSE(t *testing.T) {1416 t.Skip("disable fuse tests until they are stable")1417 //create a data directory for swarm1418 datadir, err := ioutil.TempDir("", "fuse")1419 if err != nil {1420 t.Fatalf("unable to create temp dir: %v", err)1421 }1422 defer os.RemoveAll(datadir)1423 fileStore, err := storage.NewLocalFileStore(datadir, make([]byte, 32))1424 if err != nil {1425 t.Fatal(err)1426 }1427 ta := &testAPI{api: api.NewAPI(fileStore, nil, nil, nil)}1428 //run a short suite of tests1429 //approx time: 28s1430 t.Run("mountListAndUnmountEncrypted", ta.mountListAndUnmountEncrypted)1431 t.Run("remountEncrypted", ta.remountEncrypted)1432 t.Run("unmountWhenResourceBusyNonEncrypted", ta.unmountWhenResourceBusyNonEncrypted)1433 t.Run("removeExistingFileEncrypted", ta.removeExistingFileEncrypted)1434 t.Run("addNewFileAndModifyContentsNonEncrypted", ta.addNewFileAndModifyContentsNonEncrypted)...

Full Screen

Full Screen

bolt_store_test.go

Source:bolt_store_test.go Github

copy

Full Screen

...10)11func testBoltStore(t testing.TB) *BoltStore {12 fh, err := ioutil.TempFile("", "bolt")13 if err != nil {14 t.Fatalf("err: %s", err)15 }16 os.Remove(fh.Name())17 // Successfully creates and returns a store18 store, err := NewBoltStore(fh.Name())19 if err != nil {20 t.Fatalf("err: %s", err)21 }22 return store23}24func testRaftLog(idx uint64, data string) *raft.Log {25 return &raft.Log{26 Data: []byte(data),27 Index: idx,28 }29}30func TestBoltStore_Implements(t *testing.T) {31 var store interface{} = &BoltStore{}32 if _, ok := store.(raft.StableStore); !ok {33 t.Fatalf("BoltStore does not implement raft.StableStore")34 }35 if _, ok := store.(raft.LogStore); !ok {36 t.Fatalf("BoltStore does not implement raft.LogStore")37 }38}39func TestNewBoltStore(t *testing.T) {40 fh, err := ioutil.TempFile("", "bolt")41 if err != nil {42 t.Fatalf("err: %s", err)43 }44 os.Remove(fh.Name())45 defer os.Remove(fh.Name())46 // Successfully creates and returns a store47 store, err := NewBoltStore(fh.Name())48 if err != nil {49 t.Fatalf("err: %s", err)50 }51 // Ensure the file was created52 if store.path != fh.Name() {53 t.Fatalf("unexpected file path %q", store.path)54 }55 if _, err := os.Stat(fh.Name()); err != nil {56 t.Fatalf("err: %s", err)57 }58 // Close the store so we can open again59 if err := store.Close(); err != nil {60 t.Fatalf("err: %s", err)61 }62 // Ensure our tables were created63 db, err := bolt.Open(fh.Name(), dbFileMode, nil)64 if err != nil {65 t.Fatalf("err: %s", err)66 }67 tx, err := db.Begin(true)68 if err != nil {69 t.Fatalf("err: %s", err)70 }71 if _, err := tx.CreateBucket([]byte(dbLogs)); err != bolt.ErrBucketExists {72 t.Fatalf("bad: %v", err)73 }74 if _, err := tx.CreateBucket([]byte(dbConf)); err != bolt.ErrBucketExists {75 t.Fatalf("bad: %v", err)76 }77}78func TestBoltStore_FirstIndex(t *testing.T) {79 store := testBoltStore(t)80 defer store.Close()81 defer os.Remove(store.path)82 // Should get 0 index on empty log83 idx, err := store.FirstIndex()84 if err != nil {85 t.Fatalf("err: %s", err)86 }87 if idx != 0 {88 t.Fatalf("bad: %v", idx)89 }90 // Set a mock raft log91 logs := []*raft.Log{92 testRaftLog(1, "log1"),93 testRaftLog(2, "log2"),94 testRaftLog(3, "log3"),95 }96 if err := store.StoreLogs(logs); err != nil {97 t.Fatalf("bad: %s", err)98 }99 // Fetch the first Raft index100 idx, err = store.FirstIndex()101 if err != nil {102 t.Fatalf("err: %s", err)103 }104 if idx != 1 {105 t.Fatalf("bad: %d", idx)106 }107}108func TestBoltStore_LastIndex(t *testing.T) {109 store := testBoltStore(t)110 defer store.Close()111 defer os.Remove(store.path)112 // Should get 0 index on empty log113 idx, err := store.LastIndex()114 if err != nil {115 t.Fatalf("err: %s", err)116 }117 if idx != 0 {118 t.Fatalf("bad: %v", idx)119 }120 // Set a mock raft log121 logs := []*raft.Log{122 testRaftLog(1, "log1"),123 testRaftLog(2, "log2"),124 testRaftLog(3, "log3"),125 }126 if err := store.StoreLogs(logs); err != nil {127 t.Fatalf("bad: %s", err)128 }129 // Fetch the last Raft index130 idx, err = store.LastIndex()131 if err != nil {132 t.Fatalf("err: %s", err)133 }134 if idx != 3 {135 t.Fatalf("bad: %d", idx)136 }137}138func TestBoltStore_GetLog(t *testing.T) {139 store := testBoltStore(t)140 defer store.Close()141 defer os.Remove(store.path)142 log := new(raft.Log)143 // Should return an error on non-existent log144 if err := store.GetLog(1, log); err != raft.ErrLogNotFound {145 t.Fatalf("expected raft log not found error, got: %v", err)146 }147 // Set a mock raft log148 logs := []*raft.Log{149 testRaftLog(1, "log1"),150 testRaftLog(2, "log2"),151 testRaftLog(3, "log3"),152 }153 if err := store.StoreLogs(logs); err != nil {154 t.Fatalf("bad: %s", err)155 }156 // Should return the proper log157 if err := store.GetLog(2, log); err != nil {158 t.Fatalf("err: %s", err)159 }160 if !reflect.DeepEqual(log, logs[1]) {161 t.Fatalf("bad: %#v", log)162 }163}164func TestBoltStore_SetLog(t *testing.T) {165 store := testBoltStore(t)166 defer store.Close()167 defer os.Remove(store.path)168 // Create the log169 log := &raft.Log{170 Data: []byte("log1"),171 Index: 1,172 }173 // Attempt to store the log174 if err := store.StoreLog(log); err != nil {175 t.Fatalf("err: %s", err)176 }177 // Retrieve the log again178 result := new(raft.Log)179 if err := store.GetLog(1, result); err != nil {180 t.Fatalf("err: %s", err)181 }182 // Ensure the log comes back the same183 if !reflect.DeepEqual(log, result) {184 t.Fatalf("bad: %v", result)185 }186}187func TestBoltStore_SetLogs(t *testing.T) {188 store := testBoltStore(t)189 defer store.Close()190 defer os.Remove(store.path)191 // Create a set of logs192 logs := []*raft.Log{193 testRaftLog(1, "log1"),194 testRaftLog(2, "log2"),195 }196 // Attempt to store the logs197 if err := store.StoreLogs(logs); err != nil {198 t.Fatalf("err: %s", err)199 }200 // Ensure we stored them all201 result1, result2 := new(raft.Log), new(raft.Log)202 if err := store.GetLog(1, result1); err != nil {203 t.Fatalf("err: %s", err)204 }205 if !reflect.DeepEqual(logs[0], result1) {206 t.Fatalf("bad: %#v", result1)207 }208 if err := store.GetLog(2, result2); err != nil {209 t.Fatalf("err: %s", err)210 }211 if !reflect.DeepEqual(logs[1], result2) {212 t.Fatalf("bad: %#v", result2)213 }214}215func TestBoltStore_DeleteRange(t *testing.T) {216 store := testBoltStore(t)217 defer store.Close()218 defer os.Remove(store.path)219 // Create a set of logs220 log1 := testRaftLog(1, "log1")221 log2 := testRaftLog(2, "log2")222 log3 := testRaftLog(3, "log3")223 logs := []*raft.Log{log1, log2, log3}224 // Attempt to store the logs225 if err := store.StoreLogs(logs); err != nil {226 t.Fatalf("err: %s", err)227 }228 // Attempt to delete a range of logs229 if err := store.DeleteRange(1, 2); err != nil {230 t.Fatalf("err: %s", err)231 }232 // Ensure the logs were deleted233 if err := store.GetLog(1, new(raft.Log)); err != raft.ErrLogNotFound {234 t.Fatalf("should have deleted log1")235 }236 if err := store.GetLog(2, new(raft.Log)); err != raft.ErrLogNotFound {237 t.Fatalf("should have deleted log2")238 }239}240func TestBoltStore_Set_Get(t *testing.T) {241 store := testBoltStore(t)242 defer store.Close()243 defer os.Remove(store.path)244 // Returns error on non-existent key245 if _, err := store.Get([]byte("bad")); err != ErrKeyNotFound {246 t.Fatalf("expected not found error, got: %q", err)247 }248 k, v := []byte("hello"), []byte("world")249 // Try to set a k/v pair250 if err := store.Set(k, v); err != nil {251 t.Fatalf("err: %s", err)252 }253 // Try to read it back254 val, err := store.Get(k)255 if err != nil {256 t.Fatalf("err: %s", err)257 }258 if !bytes.Equal(val, v) {259 t.Fatalf("bad: %v", val)260 }261}262func TestBoltStore_SetUint64_GetUint64(t *testing.T) {263 store := testBoltStore(t)264 defer store.Close()265 defer os.Remove(store.path)266 // Returns error on non-existent key267 if _, err := store.GetUint64([]byte("bad")); err != ErrKeyNotFound {268 t.Fatalf("expected not found error, got: %q", err)269 }270 k, v := []byte("abc"), uint64(123)271 // Attempt to set the k/v pair272 if err := store.SetUint64(k, v); err != nil {273 t.Fatalf("err: %s", err)274 }275 // Read back the value276 val, err := store.GetUint64(k)277 if err != nil {278 t.Fatalf("err: %s", err)279 }280 if val != v {281 t.Fatalf("bad: %v", val)282 }283}...

Full Screen

Full Screen

recordio.go

Source:recordio.go Github

copy

Full Screen

...17}18func simpleRead(path string) {19 reader, err := rProto.NewProtoReaderWithPath(path)20 if err != nil {21 log.Fatalf("error: %v", err)22 }23 err = reader.Open()24 if err != nil {25 log.Fatalf("error: %v", err)26 }27 for {28 record := &proto.HelloWorld{}29 _, err := reader.ReadNext(record)30 // io.EOF signals that no records are left to be read31 if errors.Is(err, io.EOF) {32 break33 }34 if err != nil {35 log.Fatalf("error: %v", err)36 }37 log.Printf("%s", record.GetMessage())38 }39 err = reader.Close()40 if err != nil {41 log.Fatalf("error: %v", err)42 }43}44func simpleWrite(path string) {45 writer, err := rProto.NewWriter(rProto.Path(path), rProto.CompressionType(recordio.CompressionTypeSnappy))46 if err != nil {47 log.Fatalf("error: %v", err)48 }49 err = writer.Open()50 if err != nil {51 log.Fatalf("error: %v", err)52 }53 record := &proto.HelloWorld{Message: "Hello World"}54 recordOffset, err := writer.Write(record)55 if err != nil {56 log.Fatalf("error: %v", err)57 }58 log.Printf("wrote a record at offset of %d bytes", recordOffset)59 err = writer.Close()60 if err != nil {61 log.Fatalf("error: %v", err)62 }63}64func simpleReadAtOffset(path string) {65 reader, err := rProto.NewMMapProtoReaderWithPath(path)66 if err != nil {67 log.Fatalf("error: %v", err)68 }69 err = reader.Open()70 if err != nil {71 log.Fatalf("error: %v", err)72 }73 record := &proto.HelloWorld{}74 _, err = reader.ReadNextAt(record, 8)75 if err != nil {76 log.Fatalf("error: %v", err)77 }78 log.Printf("Reading message at offset 8: %s", record.GetMessage())79 err = reader.Close()80 if err != nil {81 log.Fatalf("error: %v", err)82 }83}...

Full Screen

Full Screen

Fatalf

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 file, err := os.Create("test.log")4 if err != nil {5 log.Fatalf("failed to create file: %s", err)6 }7 defer file.Close()8 log.SetOutput(file)9 log.Println("This is a test log entry")10}11import (12func main() {13 file, err := os.Create("test.log")14 if err != nil {15 log.Fatalf("failed to create file: %s", err)16 }17 defer file.Close()18 log.SetOutput(file)19 log.Println("This is a test log entry")20}21import (22func main() {23 file, err := os.Create("test.log")24 if err != nil {25 log.Fatalf("failed to create file: %s", err)26 }27 defer file.Close()28 log.SetOutput(file)29 log.SetPrefix("TRACE: ")30 log.SetFlags(log.Ldate | log.Lmicroseconds | log.Llongfile)31 log.Println("This is a test log entry")32}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful