func TestLink(t *testing.T) { ino := maggiefs.NewInode(0, maggiefs.FTYPE_REG, 0755, uint32(os.Getuid()), uint32(os.Getgid())) id, err := testCluster.Names.AddInode(ino) if err != nil { panic(err) } ino.Inodeid = id ino2 := maggiefs.NewInode(0, maggiefs.FTYPE_REG, 0755, uint32(os.Getuid()), uint32(os.Getgid())) id, err = testCluster.Names.AddInode(ino) if err != nil { panic(err) } ino2.Inodeid = id // test normal link fmt.Println("linking") err = testCluster.Names.Link(ino.Inodeid, ino2.Inodeid, "name", true) if err != nil { panic(err) } ino, err = testCluster.Names.GetInode(ino.Inodeid) if err != nil { panic(err) } if ino.Children["name"].Inodeid != ino2.Inodeid { t.Fatalf("Didn't link properly!") } // test an unforced attempt to overwrite // test overwriting forced }
func TestWriteRead2(t *testing.T) { fmt.Println("testWriteRead2") fmt.Println("Adding node to cluster") ino := maggiefs.NewInode(0, maggiefs.FTYPE_REG, 0755, uint32(os.Getuid()), uint32(os.Getgid())) id, err := testCluster.Names.AddInode(ino) if err != nil { t.Fatal(err) } ino.Inodeid = id writer, err := client.NewInodeWriter(ino.Inodeid, testCluster.Leases, testCluster.Names, testCluster.Datas) if err != nil { t.Fatal(err) } // we wnat to write a bunch of bytes the way the fs does it, in chunks of 65536 bytes := make([]byte, 65536) n, err := rand.Read(bytes) if n < len(bytes) { t.Fatal(fmt.Errorf("Only returned %d bytes in call to rand.Read", n)) } if err != nil { t.Fatal(err) } for i := 0; i < 5000; i++ { n, err := writer.WriteAt(bytes, uint64(65536*i), uint32(len(bytes))) if err != nil { t.Fatal(err) } if n < uint32(len(bytes)) { t.Fatal(fmt.Sprintf("Only wrote %d bytes out of %d", n, len(bytes))) } } // then do that many reads across the file to confirm it's ok readBytes := make([]byte, 65536) reader, err := client.NewReader(ino.Inodeid, testCluster.Names, testCluster.Datas) if err != nil { t.Fatal(fmt.Sprintf("Error opening reader %s", err.Error())) } for i := 0; i < 5000; i++ { n, err := reader.ReadAt(readBytes, uint64(65536*i), 0, uint32(len(readBytes))) if err != nil { t.Fatal(err) } if n < uint32(len(readBytes)) { t.Fatal(fmt.Sprintf("Only read %d bytes out of %d", n, len(readBytes))) } for idx := 0; idx < 65536; idx++ { if readBytes[idx] != bytes[idx] { fmt.Printf("Bytes at beginning: %x : %x\n", readBytes[:5], bytes[:5]) //fmt.Printf("Bytes near offest: %x : %x\n",readBytes[idx-5:idx+5],bytes[idx-5:idx+5]) t.Fatal(fmt.Sprintf("Bytes not equal at offset %d, iteration %d : %x != %x", idx, i, readBytes[idx], bytes[idx])) } } } fmt.Println("Done TestReadWrite") }
func TestUnlink(t *testing.T) { ino := maggiefs.NewInode(0, maggiefs.FTYPE_REG, 0755, uint32(os.Getuid()), uint32(os.Getgid())) id, err := testCluster.Names.AddInode(ino) if err != nil { panic(err) } ino.Inodeid = id ino2 := maggiefs.NewInode(0, maggiefs.FTYPE_REG, 0755, uint32(os.Getuid()), uint32(os.Getgid())) id, err = testCluster.Names.AddInode(ino) if err != nil { panic(err) } ino2.Inodeid = id // test normal link fmt.Println("linking") err = testCluster.Names.Link(ino.Inodeid, ino2.Inodeid, "name", true) if err != nil { panic(err) } ino, err = testCluster.Names.GetInode(ino.Inodeid) if err != nil { panic(err) } if ino.Children["name"].Inodeid != ino2.Inodeid { t.Fatalf("Didn't link properly!") } err = testCluster.Names.Unlink(ino.Inodeid, "name") if err != nil { panic(err) } ino, err = testCluster.Names.GetInode(ino.Inodeid) if err != nil { panic(err) } _, ok := ino.Children["name"] if ok { t.Fatalf("Didn't unlink properly!") } }
// formats a new filesystem in the given data dir func Format(dataDir string, rootUid, rootGid uint32) error { // wipe out previous err := os.RemoveAll(dataDir) if err != nil { return err } // create err = os.Mkdir(dataDir, 0755) if err != nil { return fmt.Errorf("issue creating namenode home dir: %s\n", err.Error()) } err = os.Mkdir(dataDir+"/"+dir_inodb, 0755) if err != nil { return fmt.Errorf("issue creating inodb parent dir: %s\n", err.Error()) } err = os.Mkdir(dataDir+"/"+dir_counters, 0755) if err != nil { return fmt.Errorf("issue creating counters parent dir: %s\n", err.Error()) } opts := levigo.NewOptions() defer opts.Close() opts.SetCreateIfMissing(true) // create inodb db, err := levigo.Open(dataDir+"/"+dir_inodb, opts) if err != nil { return err } // add root node ino := maggiefs.NewInode(1, maggiefs.FTYPE_DIR, 0755, rootUid, rootGid) binSize := ino.BinSize() inoBytes := make([]byte, binSize) ino.ToBytes(inoBytes) rootNodeId := make([]byte, 8) binary.LittleEndian.PutUint64(rootNodeId, 1) db.Put(WriteOpts, rootNodeId, inoBytes) db.Close() db, err = levigo.Open(dataDir+"/"+dir_counters, opts) if err != nil { return err } // put 1 for inode counter so other nodes are higher key := []byte(COUNTER_INODE) val := make([]byte, 8) binary.LittleEndian.PutUint64(val, 1) err = db.Put(WriteOpts, key, val) if err != nil { return err } db.Close() return nil }
func TestAddInode(t *testing.T) { ino := maggiefs.NewInode(0, maggiefs.FTYPE_REG, 0755, uint32(os.Getuid()), uint32(os.Getgid())) id, err := testCluster.Names.AddInode(ino) if err != nil { panic(err) } ino.Inodeid = id ino2, err := testCluster.Names.GetInode(id) if !ino.Equals(ino2) { t.Fatal(fmt.Errorf("Error, inodes not equal : %+v : %+v\n", *ino, *ino2)) } }
func TestAddBlock(t *testing.T) { fmt.Println("Adding node to cluster") ino := maggiefs.NewInode(0, maggiefs.FTYPE_REG, 0755, uint32(os.Getuid()), uint32(os.Getgid())) id, err := testCluster.Names.AddInode(ino) if err != nil { t.Fatal(err) } ino.Inodeid = id newBlock, err := testCluster.Names.AddBlock(ino.Inodeid, 1024) if err != nil { t.Fatal(err) } fmt.Printf("got block back %+v\n", newBlock) ino, err = testCluster.Names.GetInode(ino.Inodeid) if newBlock.Id != ino.Blocks[0].Id || ino.Blocks[0].EndPos != 1023 { // 1023 end pos for 1024 length because we're 0 indexed t.Fatal(fmt.Errorf("Wrong end length for block %+v", ino.Blocks[0])) } // check that block made it to each datanode fstat, err := testCluster.Names.StatFs() fmt.Printf("got fstat %+v\n", fstat) for _, dnInfo := range fstat.DnStat { for _, volStat := range dnInfo.Volumes { volId := volStat.VolId for _, blockVolId := range newBlock.Volumes { if blockVolId == volId { fmt.Printf("looking for vol %d on dn %d\n", volId, dnInfo.DnId) // dnIDs start at 1 so decrement blocks, err := testCluster.DataNodes[dnInfo.DnId-1].BlockReport(volId) if err != nil { t.Fatal(err.Error()) } fmt.Printf("Blocks for vol %d : %+v\n", volId, blocks) var found = false for _, blk := range blocks { if blk.Id == newBlock.Id { found = true } } if !found { t.Fatalf("Didn't find block %d on volume %d! Blocks on volume: \n %+v \n", newBlock.Id, volId, blocks) } } } } } }
func TestShortRead(t *testing.T) { fmt.Println("Adding node to cluster") ino := maggiefs.NewInode(0, maggiefs.FTYPE_REG, 0755, uint32(os.Getuid()), uint32(os.Getgid())) id, err := testCluster.Names.AddInode(ino) if err != nil { t.Fatal(err) } ino.Inodeid = id writer, err := client.NewInodeWriter(ino.Inodeid, testCluster.Leases, testCluster.Names, testCluster.Datas) if err != nil { t.Fatal(err) } // 5 bytes bytes := make([]byte, 5) _, err = rand.Read(bytes) if err != nil { t.Fatal(err) } fmt.Println("Writing some bytes") n, err := writer.WriteAt(bytes, 0, uint32(len(bytes))) if err != nil { t.Fatal(err) } if n < uint32(len(bytes)) { t.Fatal(fmt.Sprintf("Only wrote %d bytes out of %d", n, len(bytes))) } readBytes := make([]byte, 4096) r, err := client.NewReader(ino.Inodeid, testCluster.Names, testCluster.Datas) if err != nil { t.Fatal(fmt.Sprintf("Error opening reader %s", err.Error())) } n, err = r.ReadAt(readBytes, 0, 0, 5) fmt.Printf("Read %d bytes\n", n) if err != nil { t.Fatal(err) } for idx := 0; idx < 5; idx++ { if readBytes[idx] != bytes[idx] { t.Fatal(fmt.Sprintf("Bytes not equal at offset %d : %x != %x", idx, readBytes[idx], bytes[idx])) } } fmt.Println("Done TestShortRead") }
func TestWriteRead(t *testing.T) { fmt.Println("testWriteRead") fmt.Println("Adding node to cluster") ino := maggiefs.NewInode(0, maggiefs.FTYPE_REG, 0755, uint32(os.Getuid()), uint32(os.Getgid())) id, err := testCluster.Names.AddInode(ino) if err != nil { t.Fatal(err) } ino.Inodeid = id writer, err := client.NewInodeWriter(ino.Inodeid, testCluster.Leases, testCluster.Names, testCluster.Datas) if err != nil { t.Fatal(err) } // 200 MB to make us 2 blocks bytes := make([]byte, 1024*1024*200) _, err = rand.Read(bytes) if err != nil { t.Fatal(err) } fmt.Printf("first 5 before write %x\n", bytes[:5]) fmt.Println("Writing some bytes") n, err := writer.WriteAt(bytes, 0, uint32(len(bytes))) if err != nil { t.Fatal(err) } if n < uint32(len(bytes)) { t.Fatal(fmt.Sprintf("Only wrote %d bytes out of %d", n, len(bytes))) } readBytes := make([]byte, 1024*1024*200) r, err := client.NewReader(ino.Inodeid, testCluster.Names, testCluster.Datas) if err != nil { t.Fatal(fmt.Sprintf("Error opening reader %s", err.Error())) } _, err = r.ReadAt(readBytes, 0, 0, 1024*1024*200) if err != nil { t.Fatal(err) } for idx, b := range readBytes { if b != bytes[idx] { t.Fatal(fmt.Sprintf("Bytes not equal at offset %d : %x != %x", idx, b, bytes[idx])) } } }
func TestGetInodeJson(t *testing.T) { ino := maggiefs.NewInode(0, maggiefs.FTYPE_REG, 0755, uint32(os.Getuid()), uint32(os.Getgid())) id, err := testCluster.Names.AddInode(ino) if err != nil { panic(err) } ino.Inodeid = id ino2, err := testCluster.Names.GetInode(id) if !ino.Equals(ino2) { t.Fatal(fmt.Errorf("Error, inodes not equal : %+v : %+v\n", *ino, *ino2)) } inoJsonAddr := fmt.Sprintf("http://%s/inode?inodeid=%d", testCluster.NameServer.HttpAddr(), ino.Inodeid) fmt.Printf("Getting ino json from %s\n", inoJsonAddr) inoJson, err := http.Get(inoJsonAddr) if err != nil { panic(err) } fmt.Printf("Got json %s\n", inoJson) }