本文整理匯總了Golang中github.com/heketi/heketi/tests.Patch函數的典型用法代碼示例。如果您正苦於以下問題:Golang Patch函數的具體用法?Golang Patch怎麽用?Golang Patch使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Patch函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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)
}
示例2: TestDeviceAddBadRequests
func TestDeviceAddBadRequests(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// ClusterCreate JSON Request
request := []byte(`{
bad json
}`)
// Post bad JSON
r, err := http.Post(ts.URL+"/devices", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == 422)
// Make a request with no devices
request = []byte(`{
"node" : "123",
"devices" : []
}`)
// Post bad JSON
r, err = http.Post(ts.URL+"/devices", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusBadRequest)
// Make a request with unknown node
request = []byte(`{
"node" : "123",
"devices" : [
{
"name" : "/dev/fake",
"weight" : 20
}
]
}`)
// Post bad JSON
r, err = http.Post(ts.URL+"/devices", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusNotFound)
}
示例3: TestNewNodeEntryNewInfoResponse
func TestNewNodeEntryNewInfoResponse(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
// Create a node
req := &NodeAddRequest{
ClusterId: "123",
Hostnames: HostAddresses{
Manage: []string{"manage"},
Storage: []string{"storage"},
},
Zone: 99,
}
n := NewNodeEntryFromRequest(req)
// Save element in database
err := app.db.Update(func(tx *bolt.Tx) error {
return n.Save(tx)
})
tests.Assert(t, err == nil)
var info *NodeInfoResponse
err = app.db.View(func(tx *bolt.Tx) error {
node, err := NewNodeEntryFromId(tx, n.Info.Id)
if err != nil {
return err
}
info, err = node.NewInfoReponse(tx)
if err != nil {
return err
}
return nil
})
tests.Assert(t, err == nil)
tests.Assert(t, info.ClusterId == n.Info.ClusterId)
tests.Assert(t, info.Id == n.Info.Id)
tests.Assert(t, info.Zone == n.Info.Zone)
tests.Assert(t, len(info.Hostnames.Manage) == 1)
tests.Assert(t, len(info.Hostnames.Storage) == 1)
tests.Assert(t, reflect.DeepEqual(info.Hostnames.Manage, n.Info.Hostnames.Manage))
tests.Assert(t, reflect.DeepEqual(info.Hostnames.Storage, n.Info.Hostnames.Storage))
}
示例4: TestNewDeviceEntryNewInfoResponse
func TestNewDeviceEntryNewInfoResponse(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
// Create a device
req := &Device{
Name: "dev",
Weight: 123,
}
nodeid := "abc"
d := NewDeviceEntryFromRequest(req, nodeid)
d.Info.Storage.Free = 10
d.Info.Storage.Total = 100
d.Info.Storage.Used = 1000
d.BrickAdd("abc")
d.BrickAdd("def")
// Save element in database
err := app.db.Update(func(tx *bolt.Tx) error {
return d.Save(tx)
})
tests.Assert(t, err == nil)
var info *DeviceInfoResponse
err = app.db.View(func(tx *bolt.Tx) error {
device, err := NewDeviceEntryFromId(tx, d.Info.Id)
if err != nil {
return err
}
info, err = device.NewInfoResponse(tx)
if err != nil {
return err
}
return nil
})
tests.Assert(t, err == nil)
tests.Assert(t, info.Id == d.Info.Id)
tests.Assert(t, info.Name == d.Info.Name)
tests.Assert(t, info.Weight == d.Info.Weight)
tests.Assert(t, reflect.DeepEqual(info.Storage, d.Info.Storage))
tests.Assert(t, len(info.Bricks) == 0)
}
示例5: 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())
}
示例6: TestClusterCreate
func TestClusterCreate(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// ClusterCreate JSON Request
request := []byte(`{
}`)
// Post nothing
r, err := http.Post(ts.URL+"/clusters", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusCreated)
// Read JSON
var msg ClusterInfoResponse
err = utils.GetJsonFromResponse(r, &msg)
tests.Assert(t, err == nil)
// Test JSON
tests.Assert(t, len(msg.Nodes) == 0)
tests.Assert(t, len(msg.Volumes) == 0)
// Check that the data on the database is recorded correctly
var entry ClusterEntry
err = app.db.View(func(tx *bolt.Tx) error {
return entry.Unmarshal(
tx.Bucket([]byte(BOLTDB_BUCKET_CLUSTER)).
Get([]byte(msg.Id)))
})
tests.Assert(t, err == nil)
// Make sure they entries are euqal
tests.Assert(t, entry.Info.Id == msg.Id)
tests.Assert(t, len(entry.Info.Volumes) == 0)
tests.Assert(t, len(entry.Info.Nodes) == 0)
}
示例7: TestDeviceDeleteErrors
func TestDeviceDeleteErrors(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Create a device to save in the db
device := NewDeviceEntry()
device.Info.Id = "abc"
device.Info.Name = "/dev/fake1"
device.Info.Weight = 101
device.NodeId = "def"
device.StorageSet(10000)
device.StorageAllocate(1000)
// Save device in the db
err := app.db.Update(func(tx *bolt.Tx) error {
return device.Save(tx)
})
tests.Assert(t, err == nil)
// Delete unknown id
req, err := http.NewRequest("DELETE", ts.URL+"/devices/123", nil)
tests.Assert(t, err == nil)
r, err := http.DefaultClient.Do(req)
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusNotFound)
// Delete device without a node there.. that's probably a really
// bad situation
req, err = http.NewRequest("DELETE", ts.URL+"/devices/"+device.Info.Id, nil)
tests.Assert(t, err == nil)
r, err = http.DefaultClient.Do(req)
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusInternalServerError)
}
示例8: TestDeviceInfo
func TestDeviceInfo(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Create a device to save in the db
device := NewDeviceEntry()
device.Info.Id = "abc"
device.Info.Name = "/dev/fake1"
device.Info.Weight = 101
device.NodeId = "def"
device.StorageSet(10000)
device.StorageAllocate(1000)
// Save device in the db
err := app.db.Update(func(tx *bolt.Tx) error {
return device.Save(tx)
})
tests.Assert(t, err == nil)
// Get device information
r, err := http.Get(ts.URL + "/devices/" + device.Info.Id)
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
var info DeviceInfoResponse
err = utils.GetJsonFromResponse(r, &info)
tests.Assert(t, info.Id == device.Info.Id)
tests.Assert(t, info.Name == device.Info.Name)
tests.Assert(t, info.Weight == device.Info.Weight)
tests.Assert(t, info.Storage.Free == device.Info.Storage.Free)
tests.Assert(t, info.Storage.Used == device.Info.Storage.Used)
tests.Assert(t, info.Storage.Total == device.Info.Storage.Total)
}
示例9: 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)
}
示例10: TestNodeInfo
func TestNodeInfo(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Create a node to save in the db
node := NewNodeEntry()
node.Info.Id = "abc"
node.Info.ClusterId = "123"
node.Info.Hostnames.Manage = sort.StringSlice{"manage.system"}
node.Info.Hostnames.Storage = sort.StringSlice{"storage.system"}
node.Info.Zone = 10
// Save node in the db
err := app.db.Update(func(tx *bolt.Tx) error {
return node.Save(tx)
})
tests.Assert(t, err == nil)
// Get unknown node id
r, err := http.Get(ts.URL + "/nodes/" + node.Info.Id)
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
var info NodeInfoResponse
err = utils.GetJsonFromResponse(r, &info)
tests.Assert(t, info.Id == node.Info.Id)
tests.Assert(t, info.Hostnames.Manage[0] == node.Info.Hostnames.Manage[0])
tests.Assert(t, len(info.Hostnames.Manage) == len(node.Info.Hostnames.Manage))
tests.Assert(t, info.Hostnames.Storage[0] == node.Info.Hostnames.Storage[0])
tests.Assert(t, len(info.Hostnames.Storage) == len(node.Info.Hostnames.Storage))
tests.Assert(t, info.Zone == node.Info.Zone)
}
示例11: TestNodeDeleteErrors
func TestNodeDeleteErrors(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Create a node to save in the db
node := NewNodeEntry()
node.Info.Id = "abc"
node.Info.ClusterId = "123"
node.Info.Hostnames.Manage = sort.StringSlice{"manage.system"}
node.Info.Hostnames.Storage = sort.StringSlice{"storage.system"}
node.Info.Zone = 10
// Save node in the db
err := app.db.Update(func(tx *bolt.Tx) error {
return node.Save(tx)
})
tests.Assert(t, err == nil)
// Delete unknown id
req, err := http.NewRequest("DELETE", ts.URL+"/nodes/123", nil)
tests.Assert(t, err == nil)
r, err := http.DefaultClient.Do(req)
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusNotFound)
// Delete node without a cluster there.. that's probably a really
// bad situation
req, err = http.NewRequest("DELETE", ts.URL+"/nodes/"+node.Info.Id, nil)
tests.Assert(t, err == nil)
r, err = http.DefaultClient.Do(req)
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusInternalServerError)
}
示例12: TestNewNodeEntryFromId
func TestNewNodeEntryFromId(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
// Create a node
req := &NodeAddRequest{
ClusterId: "123",
Hostnames: HostAddresses{
Manage: []string{"manage"},
Storage: []string{"storage"},
},
Zone: 99,
}
n := NewNodeEntryFromRequest(req)
n.DeviceAdd("abc")
n.DeviceAdd("def")
// Save element in database
err := app.db.Update(func(tx *bolt.Tx) error {
return n.Save(tx)
})
tests.Assert(t, err == nil)
var node *NodeEntry
err = app.db.View(func(tx *bolt.Tx) error {
var err error
node, err = NewNodeEntryFromId(tx, n.Info.Id)
if err != nil {
return err
}
return nil
})
tests.Assert(t, err == nil)
tests.Assert(t, reflect.DeepEqual(node, n))
}
示例13: TestNewClusterEntryNewInfoResponse
func TestNewClusterEntryNewInfoResponse(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
// Create a cluster
c := NewClusterEntryFromRequest()
c.NodeAdd("node_abc")
c.NodeAdd("node_def")
c.VolumeAdd("vol_abc")
// Save element in database
err := app.db.Update(func(tx *bolt.Tx) error {
return c.Save(tx)
})
tests.Assert(t, err == nil)
var info *ClusterInfoResponse
err = app.db.View(func(tx *bolt.Tx) error {
cluster, err := NewClusterEntryFromId(tx, c.Info.Id)
if err != nil {
return err
}
info, err = cluster.NewClusterInfoResponse(tx)
if err != nil {
return err
}
return nil
})
tests.Assert(t, err == nil)
tests.Assert(t, info.Id == c.Info.Id)
tests.Assert(t, reflect.DeepEqual(info.Nodes, c.Info.Nodes))
tests.Assert(t, reflect.DeepEqual(info.Volumes, c.Info.Volumes))
}
示例14: TestNewNodeEntryFromIdNotFound
func TestNewNodeEntryFromIdNotFound(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
// Test for ID not found
err := app.db.View(func(tx *bolt.Tx) error {
_, err := NewNodeEntryFromId(tx, "123")
return err
})
tests.Assert(t, err == ErrNotFound)
}
示例15: TestNewClusterEntryFromId
func TestNewClusterEntryFromId(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Patch dbfilename so that it is restored at the end of the tests
defer tests.Patch(&dbfilename, tmpfile).Restore()
// Create the app
app := NewApp()
defer app.Close()
// Create a cluster
c := NewClusterEntryFromRequest()
c.NodeAdd("node_abc")
c.NodeAdd("node_def")
c.VolumeAdd("vol_abc")
// Save element in database
err := app.db.Update(func(tx *bolt.Tx) error {
return c.Save(tx)
})
tests.Assert(t, err == nil)
var cluster *ClusterEntry
err = app.db.View(func(tx *bolt.Tx) error {
var err error
cluster, err = NewClusterEntryFromId(tx, c.Info.Id)
if err != nil {
return err
}
return nil
})
tests.Assert(t, err == nil)
tests.Assert(t, cluster.Info.Id == c.Info.Id)
tests.Assert(t, len(c.Info.Nodes) == 2)
tests.Assert(t, len(c.Info.Volumes) == 1)
tests.Assert(t, utils.SortedStringHas(c.Info.Nodes, "node_abc"))
tests.Assert(t, utils.SortedStringHas(c.Info.Nodes, "node_def"))
tests.Assert(t, utils.SortedStringHas(c.Info.Volumes, "vol_abc"))
}