本文整理匯總了Golang中github.com/heketi/tests.Patch函數的典型用法代碼示例。如果您正苦於以下問題:Golang Patch函數的具體用法?Golang Patch怎麽用?Golang Patch使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Patch函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestValidateVolumeCreate
// 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)
}
示例2: TestValidateVolumeCreate
// 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)
}
示例3: TestGenerateVolfiles
// 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()
}
示例4: TestNewBrickEntry
// 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)
}
示例5: TestLogDebug
func TestLogDebug(t *testing.T) {
var testbuffer bytes.Buffer
defer tests.Patch(&stdout, &testbuffer).Restore()
l := NewLogger("[testing]", LEVEL_DEBUG)
l.Debug("Hello %v", "World")
tests.Assert(t, strings.Contains(testbuffer.String(), "[testing] DEBUG "), testbuffer.String())
tests.Assert(t, strings.Contains(testbuffer.String(), "Hello World"), testbuffer.String())
tests.Assert(t, strings.Contains(testbuffer.String(), "log_test.go"), testbuffer.String())
// [testing] DEBUG 2016/04/28 15:25:08 /src/github.com/heketi/heketi/pkg/utils/log_test.go:66: Hello World
fileinfo := strings.Split(testbuffer.String(), " ")[4]
filename := strings.Split(fileinfo, ":")[0]
// Need to check that it starts with /src/github.com
tests.Assert(t, strings.HasPrefix(filename, "/src/github.com/"))
tests.Assert(t, strings.HasSuffix(filename, "/pkg/utils/log_test.go"))
testbuffer.Reset()
l.SetLevel(LEVEL_INFO)
l.Debug("TEXT")
tests.Assert(t, testbuffer.Len() == 0)
}
示例6: TestLogError
func TestLogError(t *testing.T) {
var testbuffer bytes.Buffer
defer tests.Patch(&stderr, &testbuffer).Restore()
l := NewLogger("[testing]", LEVEL_DEBUG)
l.LogError("Hello %v", "World")
tests.Assert(t, strings.Contains(testbuffer.String(), "[testing] ERROR "), testbuffer.String())
tests.Assert(t, strings.Contains(testbuffer.String(), "Hello World"), testbuffer.String())
tests.Assert(t, strings.Contains(testbuffer.String(), "log_test.go"), testbuffer.String())
testbuffer.Reset()
testbuffer.Reset()
err := errors.New("BAD")
l.Err(err)
tests.Assert(t, strings.Contains(testbuffer.String(), "[testing] ERROR "), testbuffer.String())
tests.Assert(t, strings.Contains(testbuffer.String(), "BAD"), testbuffer.String())
tests.Assert(t, strings.Contains(testbuffer.String(), "log_test.go"), testbuffer.String())
testbuffer.Reset()
l.SetLevel(LEVEL_CRITICAL)
l.LogError("TEXT")
tests.Assert(t, testbuffer.Len() == 0)
}
示例7: TestCreateVolinfo
// TestCreateVolinfo validates createVolinfo()
func TestCreateVolinfo(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)
tests.Assert(t, e == nil && vol != nil)
// Mock failure in NewBrickEntries(), createVolume() should fail
defer heketitests.Patch(&volume.NewBrickEntriesFunc, func(bricks []string) ([]brick.Brickinfo, error) {
return nil, errBad
}).Restore()
vol, e = createVolinfo(msg)
tests.Assert(t, e == errBad)
}
示例8: TestNewVolumeEntryFromRequest
// 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)
}
示例9: TestRemoveBrickPaths
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)
}
示例10: TestValidateXattrSupport
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)
}
示例11: TestNewVolumeObject
// 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)
}
示例12: TestNewVolumeEntry
// 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)
}
示例13: TestLogLevel
func TestLogLevel(t *testing.T) {
var testbuffer bytes.Buffer
defer tests.Patch(&stdout, &testbuffer).Restore()
l := NewLogger("[testing]", LEVEL_INFO)
tests.Assert(t, LEVEL_INFO == l.level)
tests.Assert(t, LEVEL_INFO == l.Level())
l.SetLevel(LEVEL_CRITICAL)
tests.Assert(t, LEVEL_CRITICAL == l.level)
tests.Assert(t, LEVEL_CRITICAL == l.Level())
}
示例14: TestCommitVolumeCreate
// 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)
}
示例15: TestLogWarning
func TestLogWarning(t *testing.T) {
var testbuffer bytes.Buffer
defer tests.Patch(&stdout, &testbuffer).Restore()
l := NewLogger("[testing]", LEVEL_DEBUG)
l.Warning("Hello %v", "World")
tests.Assert(t, strings.Contains(testbuffer.String(), "[testing] WARNING "), testbuffer.String())
tests.Assert(t, strings.Contains(testbuffer.String(), "Hello World"), testbuffer.String())
testbuffer.Reset()
l.SetLevel(LEVEL_ERROR)
l.Warning("TEXT")
tests.Assert(t, testbuffer.Len() == 0)
}