// TestNewVolumeEntryFromRequest tests whether the volume is created with a // valid request func TestNewVolumeEntryFromRequest(t *testing.T) { var err error defer heketitests.Patch(&utils.PathMax, 4096).Restore() defer heketitests.Patch(&utils.Setxattr, tests.MockSetxattr).Restore() defer heketitests.Patch(&utils.Getxattr, tests.MockGetxattr).Restore() defer heketitests.Patch(&utils.Removexattr, tests.MockRemovexattr).Restore() defer heketitests.Patch(&getVolumesFunc, mockGetVolumes).Restore() defer heketitests.Patch(&peer.GetPeerIDByAddrF, peer.GetPeerIDByAddrMockGood).Restore() //peer.GetPeerIDByAddrF = peer.GetPeerIDByAddrMockGood //defer func() { peer.GetPeerIDByAddrF = peer.GetPeerIDByAddr }() req := new(VolCreateRequest) req.Name = "vol1" req.Bricks = getSampleBricks("/tmp/b1", "/tmp/b2") req.Force = true v, e := NewVolumeEntry(req) tests.Assert(t, e == nil) tests.Assert(t, v.Name == "vol1") tests.Assert(t, v.Transport == "tcp") tests.Assert(t, v.ReplicaCount == 1) tests.Assert(t, len(v.ID) != 0) v.Bricks, err = NewBrickEntriesFunc(req.Bricks) tests.Assert(t, err == nil) tests.Assert(t, v.Bricks != nil) tests.Assert(t, len(v.Bricks) != 0) _, err = ValidateBrickEntriesFunc(v.Bricks, v.ID, true) tests.Assert(t, err == nil) defer heketitests.Patch(&validateBrickPathStatsFunc, tests.MockValidateBrickPathStats).Restore() _, err = ValidateBrickEntriesFunc(v.Bricks, v.ID, false) tests.Assert(t, err == nil) }
func TestValidateXattrSupport(t *testing.T) { defer heketitests.Patch(&Setxattr, tests.MockSetxattr).Restore() defer heketitests.Patch(&Getxattr, tests.MockGetxattr).Restore() defer heketitests.Patch(&Removexattr, tests.MockRemovexattr).Restore() tests.Assert(t, ValidateXattrSupport("/tmp/b1", "localhost", uuid.NewRandom(), true) == nil) // Some negative tests var xattr_err error baderror := errors.New("Bad") xattr_err = baderror // Now check what happens when setxattr fails defer heketitests.Patch(&Setxattr, func(path string, attr string, data []byte, flags int) (err error) { return xattr_err }).Restore() tests.Assert(t, ValidateXattrSupport("/tmp/b1", "localhost", uuid.NewRandom(), true) == baderror) // Now check what happens when getxattr fails defer heketitests.Patch(&Getxattr, func(path string, attr string, dest []byte) (sz int, err error) { return 0, xattr_err }).Restore() tests.Assert(t, ValidateXattrSupport("/tmp/b1", "localhost", uuid.NewRandom(), true) == baderror) // Now check what happens when removexattr fails defer heketitests.Patch(&Removexattr, func(path string, attr string) (err error) { return xattr_err }).Restore() tests.Assert(t, ValidateXattrSupport("/tmp/b1", "localhost", uuid.NewRandom(), true) == baderror) }
// TestGenerateVolfiles validates generateVolfiles func TestGenerateVolfiles(t *testing.T) { defer heketitests.Patch(&peer.GetPeerIDByAddrF, peer.GetPeerIDByAddrMockGood).Restore() msg := new(volume.VolCreateRequest) msg.Name = "vol" msg.Bricks = []string{"127.0.0.1:/tmp/b1", "127.0.0.1:/tmp/b2"} vol, e := createVolinfo(msg) c := transaction.NewMockCtx() c.Set("volinfo", vol) defer heketitests.Patch(&volgen.GenerateVolfileFunc, func(vinfo *volume.Volinfo) error { return nil }).Restore() defer heketitests.Patch(&volume.AddOrUpdateVolumeFunc, func(vinfo *volume.Volinfo) error { return nil }).Restore() e = generateVolfiles(c) tests.Assert(t, e == nil) // Mock volgen failure defer heketitests.Patch(&volgen.GenerateVolfileFunc, func(vinfo *volume.Volinfo) error { return errBad }).Restore() e = generateVolfiles(c) tests.Assert(t, e == errBad) defer heketitests.Patch(&volgen.GenerateVolfileFunc, func(vinfo *volume.Volinfo) error { return nil }).Restore() }
// TestValidateVolumeCreate validates validateVolumeCreate() func TestValidateVolumeCreate(t *testing.T) { msg := new(volume.VolCreateRequest) msg.Name = "vol" msg.Bricks = []string{"127.0.0.1:/tmp/b1", "127.0.0.1:/tmp/b2"} vol, e := createVolume(msg) defer heketitests.Patch(&volume.ExistsFunc, func(name string) bool { return false }).Restore() defer heketitests.Patch(&volume.ValidateBrickEntriesFunc, func(bricks []volume.Brickinfo, volID uuid.UUID, force bool) (int, error) { return 0, nil }).Restore() _, e = validateVolumeCreate(msg, vol) tests.Assert(t, e == nil) // Mock volume exists failure defer heketitests.Patch(&volume.ExistsFunc, func(name string) bool { return true }).Restore() _, e = validateVolumeCreate(msg, vol) tests.Assert(t, e == gderrors.ErrVolExists) // Mock validateBrickEntries failure defer heketitests.Patch(&volume.ExistsFunc, func(name string) bool { return false }).Restore() defer heketitests.Patch(&volume.ValidateBrickEntriesFunc, func(bricks []volume.Brickinfo, volID uuid.UUID, force bool) (int, error) { return 0, errBad }).Restore() _, e = validateVolumeCreate(msg, vol) tests.Assert(t, e == errBad) }
// TestValidateVolumeCreate validates validateVolumeCreate() func TestValidateVolumeCreate(t *testing.T) { msg := new(volume.VolCreateRequest) msg.Name = "vol" msg.Bricks = []string{"127.0.0.1:/tmp/b1", "127.0.0.1:/tmp/b2"} c := transaction.NewMockCtx() c.Set("req", msg) defer heketitests.Patch(&volume.ValidateBrickEntriesFunc, func(bricks []brick.Brickinfo, volID uuid.UUID, force bool) (int, error) { return 0, nil }).Restore() defer heketitests.Patch(&peer.GetPeerIDByAddrF, peer.GetPeerIDByAddrMockGood).Restore() vol, e := createVolinfo(msg) tests.Assert(t, e == nil) c.Set("volinfo", vol) e = validateVolumeCreate(c) tests.Assert(t, e == nil) // Mock validateBrickEntries failure defer heketitests.Patch(&volume.ValidateBrickEntriesFunc, func(bricks []brick.Brickinfo, volID uuid.UUID, force bool) (int, error) { return 0, errBad }).Restore() e = validateVolumeCreate(c) tests.Assert(t, e == errBad) }
func TestValidateBrickSubDirLength(t *testing.T) { brick := "/tmp/" for i := 0; i <= PosixPathMax; i++ { brick = brick + "a" } tests.Assert(t, ValidateBrickSubDirLength(brick) != nil) tests.Assert(t, ValidateBrickSubDirLength("/tmp/brick1") == nil) }
func TestValidateBrickPathLength(t *testing.T) { var brick string for i := 0; i <= unix.PathMax; i++ { brick = brick + "a" } tests.Assert(t, ValidateBrickPathLength(brick) != nil) tests.Assert(t, ValidateBrickPathLength("/brick/b1") == nil) }
// TestNewVolumeEntry tests whether the volinfo object is successfully created func TestNewVolumeObject(t *testing.T) { v := NewVolinfoFunc() tests.Assert(t, v.Options != nil) tests.Assert(t, len(v.ID) == 0) // Negative test defer heketitests.Patch(&NewVolinfoFunc, func() (vol *Volinfo) { return nil }).Restore() v1 := NewVolinfoFunc() tests.Assert(t, v1 == nil) }
func TestNewVolumeEntryFromRequest(t *testing.T) { req := VolCreateRequest{} req.Name = "vol1" v := NewVolumeEntry(req) tests.Assert(t, v.Name == "vol1") tests.Assert(t, v.Transport == "tcp") tests.Assert(t, v.ReplicaCount == 1) tests.Assert(t, len(v.ID) != 0) tests.Assert(t, len(v.Bricks) == 0) }
// TestNewVolumeEntry validates NewVolumeEntry() func TestNewVolumeEntry(t *testing.T) { req := new(VolCreateRequest) v, e := NewVolumeEntry(req) tests.Assert(t, e == nil) tests.Assert(t, v != nil) // Negative test - mock out NewVolInfo() defer heketitests.Patch(&NewVolinfoFunc, func() (vol *Volinfo) { return nil }).Restore() _, e = NewVolumeEntry(req) tests.Assert(t, e == errors.ErrVolCreateFail) }
// TestCreateVolume validates createVolume() func TestCreateVolume(t *testing.T) { msg := new(volume.VolCreateRequest) msg.Name = "vol" msg.Bricks = []string{"127.0.0.1:/tmp/b1", "127.0.0.1:/tmp/b2"} vol, e := createVolume(msg) tests.Assert(t, e == nil && vol != nil) // Mock failure in NewBrickEntries(), createVolume() should fail defer heketitests.Patch(&volume.NewBrickEntriesFunc, func(bricks []string) ([]volume.Brickinfo, error) { return nil, errBad }).Restore() vol, e = createVolume(msg) tests.Assert(t, e == errBad) }
func TestNewVolumeEntryFromEmptyRequest(t *testing.T) { req := VolCreateRequest{} v := NewVolumeEntry(req) tests.Assert(t, len(v.Name) == 0) }
func TestNewVolumeEntryFromRequestRedundancy(t *testing.T) { req := VolCreateRequest{} req.RedundancyCount = 2 v := NewVolumeEntry(req) tests.Assert(t, v.RedundancyCount == 2) }
func TestNewVolumeEntryFromRequestTransport(t *testing.T) { req := VolCreateRequest{} req.Transport = "rdma" v := NewVolumeEntry(req) tests.Assert(t, v.Transport == "rdma") }
func TestNewVolumeEntryFromRequestDisperse(t *testing.T) { req := VolCreateRequest{} req.DisperseCount = 2 v := NewVolumeEntry(req) tests.Assert(t, v.DisperseCount == 2) }
// TestNewVolumeEntryFromRequestTransport validates whether the volume create is // successful with given transport type func TestNewVolumeEntryFromRequestTransport(t *testing.T) { req := new(VolCreateRequest) req.Name = "vol1" req.Transport = "rdma" req.Force = true req.Bricks = getSampleBricks("/tmp/b1", "/tmp/b2") v, _ := NewVolumeEntry(req) tests.Assert(t, v.Transport == "rdma") }
func TestNewVolumeEntryFromRequestReplica(t *testing.T) { req := VolCreateRequest{} req.Name = "vol1" req.ReplicaCount = 3 v := NewVolumeEntry(req) tests.Assert(t, v.ReplicaCount == 3) }
func TestNewVolumeEntryFromRequestBricks(t *testing.T) { req := VolCreateRequest{} req.Bricks = []string{"abc", "def"} v := NewVolumeEntry(req) for _, brick := range req.Bricks { tests.Assert(t, find(v.Bricks, brick)) } }
// TestNewVolumeEntryFromRequestDisperse validates whether the volume create is // successful with given disperse count func TestNewVolumeEntryFromRequestDisperse(t *testing.T) { req := new(VolCreateRequest) req.Name = "vol1" req.Force = true req.Bricks = getSampleBricks("/tmp/b1", "/tmp/b2") req.DisperseCount = 2 v, _ := NewVolumeEntry(req) tests.Assert(t, v.DisperseCount == 2) }
// TestNewVolumeEntryFromRequestReplica validates whether the volume create is // successful with given replica information func TestNewVolumeEntryFromRequestReplica(t *testing.T) { req := new(VolCreateRequest) req.Name = "vol1" req.Bricks = getSampleBricks("/tmp/b1", "/tmp/b2") req.Force = true req.ReplicaCount = 3 v, _ := NewVolumeEntry(req) tests.Assert(t, v.ReplicaCount == 3) }
func TestRemoveBrickPaths(t *testing.T) { defer heketitests.Patch(&peer.GetPeerIDByAddrF, peer.GetPeerIDByAddrMockGood).Restore() req := new(VolCreateRequest) req.Name = "vol1" req.Bricks = getSampleBricks("/tmp/b1", "/tmp/b2") v, e := NewVolumeEntry(req) v.Bricks, e = NewBrickEntriesFunc(req.Bricks) e = RemoveBrickPaths(v.Bricks) tests.Assert(t, e == nil) }
// TestNewVolumeEntryFromRequestRedundancy validates whether the volume create // is successful with given redundancy count func TestNewVolumeEntryFromRequestRedundancy(t *testing.T) { req := new(VolCreateRequest) req.Name = "vol1" req.Force = true req.Bricks = getSampleBricks("/tmp/b1", "/tmp/b2") req.RedundancyCount = 2 //TODO : This test needs improvement as redundancy count is tightly //coupled with disperse count, ideally this should fail v, _ := NewVolumeEntry(req) tests.Assert(t, v.RedundancyCount == 2) }
// TestNewBrickEntry validates NewBrickEntries () func TestNewBrickEntry(t *testing.T) { defer heketitests.Patch(&peer.GetPeerIDByAddrF, peer.GetPeerIDByAddrMockGood).Restore() bricks := getSampleBricks("/tmp/b1", "/tmp/b2") brickPaths := []string{"/tmp/b1", "/tmp/b2"} host, _ := os.Hostname() b, err := NewBrickEntriesFunc(bricks) tests.Assert(t, err == nil) tests.Assert(t, b != nil) for _, brick := range b { tests.Assert(t, find(brickPaths, brick.Path)) tests.Assert(t, host == brick.Hostname) } // Some negative tests mockBricks := []string{"/tmp/b1", "/tmp/b2"} //with out IPs _, err = NewBrickEntriesFunc(mockBricks) tests.Assert(t, err != nil) //Now mock filepath.Abs() defer heketitests.Patch(&absFilePath, func(path string) (string, error) { return "", errors.ErrBrickPathConvertFail }).Restore() _, err = NewBrickEntriesFunc(bricks) tests.Assert(t, err == errors.ErrBrickPathConvertFail) }
//TestUnmarshalVolCreateRequest validates the JSON request of volume //create request func TestUnmarshalVolCreateRequest(t *testing.T) { msg := new(volume.VolCreateRequest) tests.Assert(t, msg != nil) // Request with invalid JSON format r, _ := http.NewRequest("POST", "/v1/volumes/", bytes.NewBuffer([]byte(`{"invalid_format"}`))) _, e := unmarshalVolCreateRequest(msg, r) tests.Assert(t, e == gderrors.ErrJSONParsingFailed) // Request with empty volume name r, _ = http.NewRequest("POST", "/v1/volumes/", bytes.NewBuffer([]byte(`{}`))) _, e = unmarshalVolCreateRequest(msg, r) tests.Assert(t, e == gderrors.ErrEmptyVolName) // Request with empty bricks r, _ = http.NewRequest("POST", "/v1/volumes/", bytes.NewBuffer([]byte(`{"name" : "vol"}`))) _, e = unmarshalVolCreateRequest(msg, r) tests.Assert(t, msg.Name == "vol") tests.Assert(t, e == gderrors.ErrEmptyBrickList) // Request with volume name & bricks r, _ = http.NewRequest("POST", "/v1/volumes/", bytes.NewBuffer([]byte(`{"name" : "vol", "bricks":["127.0.0.1:/tmp/b1"]}`))) _, e = unmarshalVolCreateRequest(msg, r) tests.Assert(t, e == nil) }
// TestCommitVolumeCreate validates commitVolumeCreate() func TestCommitVolumeCreate(t *testing.T) { msg := new(volume.VolCreateRequest) msg.Name = "vol" msg.Bricks = []string{"127.0.0.1:/tmp/b1", "127.0.0.1:/tmp/b2"} vol, e := createVolume(msg) defer heketitests.Patch(&volgen.GenerateVolfileFunc, func(vinfo *volume.Volinfo) error { return nil }).Restore() defer heketitests.Patch(&volume.AddOrUpdateVolumeFunc, func(vinfo *volume.Volinfo) error { return nil }).Restore() _, e = commitVolumeCreate(vol) tests.Assert(t, e == nil) // Mock volgen failure defer heketitests.Patch(&volgen.GenerateVolfileFunc, func(vinfo *volume.Volinfo) error { return errBad }).Restore() _, e = commitVolumeCreate(vol) tests.Assert(t, e == errBad) defer heketitests.Patch(&volgen.GenerateVolfileFunc, func(vinfo *volume.Volinfo) error { return nil }).Restore() // Mock store failure defer heketitests.Patch(&volume.AddOrUpdateVolumeFunc, func(vinfo *volume.Volinfo) error { return errBad }).Restore() _, e = commitVolumeCreate(vol) tests.Assert(t, e == errBad) }
func TestParseHostAndBrickPath(t *testing.T) { hostname := "abc" brick := "/brick" brickPath := "abc:/brick" var h, b string var e error h, b, e = ParseHostAndBrickPath(brickPath) tests.Assert(t, e == nil) tests.Assert(t, h == hostname) tests.Assert(t, b == brick) h, b, e = ParseHostAndBrickPath("invalid brick") tests.Assert(t, e != nil) tests.Assert(t, len(h) == 0) tests.Assert(t, len(b) == 0) h, b, e = ParseHostAndBrickPath("a:b:c") tests.Assert(t, e == nil) tests.Assert(t, h == "a:b") tests.Assert(t, b == "c") }
func TestValidateBrickPathStats(t *testing.T) { tests.Assert(t, ValidateBrickPathStats("/bricks/b1", "host", false) != nil) tests.Assert(t, ValidateBrickPathStats("/bricks/b1", "host", true) == nil) tests.Assert(t, ValidateBrickPathStats("/tmp", "host", false) != nil) //TODO : In build system /tmp is considered as root, hence passing //force = true tests.Assert(t, ValidateBrickPathStats("/tmp/bricks/b1", "host", true) == nil) cmd := exec.Command("touch", "/tmp/bricks/b1/b2") err := cmd.Run() tests.Assert(t, err == nil) tests.Assert(t, ValidateBrickPathStats("/tmp/bricks/b1/b2", "host", false) != nil) }
func TestIsLocalAddress(t *testing.T) { host, _ := os.Hostname() var local bool var e error local, e = IsLocalAddress(host) tests.Assert(t, e == nil) tests.Assert(t, local == true) local, e = IsLocalAddress("invalid ip") tests.Assert(t, local == false) tests.Assert(t, e != nil) local, e = IsLocalAddress("127.0.0.1") tests.Assert(t, local == true) tests.Assert(t, e == nil) local, e = IsLocalAddress("122.122.122.122.122") tests.Assert(t, local == false) tests.Assert(t, e != nil) }
func TestStartETCDWithInvalidExecName(t *testing.T) { // Mock the executable name such that it fails defer heketitests.Patch(&ExecName, "abc").Restore() _, err := StartETCD() tests.Assert(t, err != nil) }
func TestStartETCD(t *testing.T) { etcdCtx, err := StartETCD() tests.Assert(t, err == nil) etcdCtx.Kill() }