本文整理匯總了Golang中github.com/heketi/heketi/utils.GetJsonFromResponse函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetJsonFromResponse函數的具體用法?Golang GetJsonFromResponse怎麽用?Golang GetJsonFromResponse使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GetJsonFromResponse函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestNodeAddHandler
func TestNodeAddHandler(t *testing.T) {
var msg requests.NodeInfoResp
// Instead of coding our own JSON here,
// create the JSON message as a string to test the handler
request := []byte(`{
"name" : "test_name",
"zone" : 12345
}`)
n := NewNodeServer(mock.NewMockPlugin())
ts := httptest.NewServer(http.HandlerFunc(n.NodeAddHandler))
defer ts.Close()
// Request
r, err := http.Post(ts.URL, "application/json", bytes.NewBuffer(request))
tests.Assert(t, r.StatusCode == http.StatusCreated)
tests.Assert(t, err == nil)
// Check body
err = utils.GetJsonFromResponse(r, &msg)
tests.Assert(t, err == nil)
tests.Assert(t, msg.Name == "test_name")
tests.Assert(t, msg.Zone == 12345)
}
示例2: ClusterList
func (c *Client) ClusterList() (*glusterfs.ClusterListResponse, error) {
// Create request
req, err := http.NewRequest("GET", c.host+"/clusters", nil)
if err != nil {
return nil, err
}
// Set token
err = c.setToken(req)
if err != nil {
return nil, err
}
// Get info
r, err := c.do(req)
if err != nil {
return nil, err
}
if r.StatusCode != http.StatusOK {
return nil, utils.GetErrorFromResponse(r)
}
// Read JSON response
var clusters glusterfs.ClusterListResponse
err = utils.GetJsonFromResponse(r, &clusters)
if err != nil {
return nil, err
}
return &clusters, nil
}
示例3: TestVolumeListEmpty
func TestVolumeListEmpty(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Get volumes, there should be none
r, err := http.Get(ts.URL + "/volumes")
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, err == nil)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
// Read response
var msg VolumeListResponse
err = utils.GetJsonFromResponse(r, &msg)
tests.Assert(t, err == nil)
tests.Assert(t, len(msg.Volumes) == 0)
}
示例4: DeviceInfo
func (c *Client) DeviceInfo(id string) (*glusterfs.DeviceInfoResponse, error) {
// Create request
req, err := http.NewRequest("GET", c.host+"/devices/"+id, nil)
if err != nil {
return nil, err
}
// Set token
err = c.setToken(req)
if err != nil {
return nil, err
}
// Get info
r, err := c.do(req)
if err != nil {
return nil, err
}
if r.StatusCode != http.StatusOK {
return nil, utils.GetErrorFromResponse(r)
}
// Read JSON response
var device glusterfs.DeviceInfoResponse
err = utils.GetJsonFromResponse(r, &device)
r.Body.Close()
if err != nil {
return nil, err
}
return &device, nil
}
示例5: ClusterCreate
func (c *Client) ClusterCreate() (*glusterfs.ClusterInfoResponse, error) {
// Create a request
req, err := http.NewRequest("POST", c.host+"/clusters", bytes.NewBuffer([]byte(`{}`)))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
// Set token
err = c.setToken(req)
if err != nil {
return nil, err
}
// Send request
r, err := c.do(req)
if err != nil {
return nil, err
}
if r.StatusCode != http.StatusCreated {
return nil, utils.GetErrorFromResponse(r)
}
// Read JSON response
var cluster glusterfs.ClusterInfoResponse
err = utils.GetJsonFromResponse(r, &cluster)
r.Body.Close()
if err != nil {
return nil, err
}
return &cluster, nil
}
示例6: TestVolumeList
func TestVolumeList(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Create some volumes
numvolumes := 1000
err := app.db.Update(func(tx *bolt.Tx) error {
for i := 0; i < numvolumes; i++ {
v := createSampleVolumeEntry(100)
err := v.Save(tx)
if err != nil {
return err
}
}
return nil
})
tests.Assert(t, err == nil)
// Get volumes, there should be none
r, err := http.Get(ts.URL + "/volumes")
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, err == nil)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
// Read response
var msg VolumeListResponse
err = utils.GetJsonFromResponse(r, &msg)
tests.Assert(t, err == nil)
tests.Assert(t, len(msg.Volumes) == numvolumes)
// Check that all the volumes are in the database
err = app.db.View(func(tx *bolt.Tx) error {
for _, id := range msg.Volumes {
_, err := NewVolumeEntryFromId(tx, id)
if err != nil {
return err
}
}
return nil
})
tests.Assert(t, err == nil)
}
示例7: TestVolumeInfo
func TestVolumeInfo(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
defer app.Close()
router := mux.NewRouter()
app.SetRoutes(router)
// Setup the server
ts := httptest.NewServer(router)
defer ts.Close()
// Setup database
err := setupSampleDbWithTopology(app,
1, // clusters
10, // nodes_per_cluster
10, // devices_per_node,
5*TB, // disksize)
)
tests.Assert(t, err == nil)
// Create a volume
req := &VolumeCreateRequest{}
req.Size = 100
req.Durability.Type = DURABILITY_STRING_EC
v := NewVolumeEntryFromRequest(req)
tests.Assert(t, v != nil)
err = v.Create(app.db, app.executor, app.allocator)
tests.Assert(t, err == nil)
// Now that we have some data in the database, we can
// make a request for the clutser list
r, err := http.Get(ts.URL + "/volumes/" + v.Info.Id)
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, err == nil)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
// Read response
var msg VolumeInfoResponse
err = utils.GetJsonFromResponse(r, &msg)
tests.Assert(t, err == nil)
tests.Assert(t, msg.Id == v.Info.Id)
tests.Assert(t, msg.Cluster == v.Info.Cluster)
tests.Assert(t, msg.Name == v.Info.Name)
tests.Assert(t, msg.Size == v.Info.Size)
tests.Assert(t, reflect.DeepEqual(msg.Durability, v.Info.Durability))
tests.Assert(t, reflect.DeepEqual(msg.Snapshot, v.Info.Snapshot))
for _, brick := range msg.Bricks {
tests.Assert(t, utils.SortedStringHas(v.Bricks, brick.Id))
}
}
示例8: VolumeExpand
func (c *Client) VolumeExpand(id string, request *glusterfs.VolumeExpandRequest) (
*glusterfs.VolumeInfoResponse, error) {
// Marshal request to JSON
buffer, err := json.Marshal(request)
if err != nil {
return nil, err
}
// Create a request
req, err := http.NewRequest("POST",
c.host+"/volumes/"+id+"/expand",
bytes.NewBuffer(buffer))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
// Set token
err = c.setToken(req)
if err != nil {
return nil, err
}
// Send request
r, err := c.do(req)
if err != nil {
return nil, err
}
if r.StatusCode != http.StatusAccepted {
return nil, utils.GetErrorFromResponse(r)
}
// Wait for response
r, err = c.waitForResponseWithTimer(r, time.Second)
if err != nil {
return nil, err
}
if r.StatusCode != http.StatusOK {
return nil, utils.GetErrorFromResponse(r)
}
// Read JSON response
var volume glusterfs.VolumeInfoResponse
err = utils.GetJsonFromResponse(r, &volume)
r.Body.Close()
if err != nil {
return nil, err
}
return &volume, nil
}
示例9: 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)
}
示例10: 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)
}
示例11: 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)
}
示例12: TestNodeListHandlerEmpty
func TestNodeListHandlerEmpty(t *testing.T) {
var msg requests.NodeListResponse
n := NewNodeServer(mock.NewMockPlugin())
ts := httptest.NewServer(http.HandlerFunc(n.NodeListHandler))
defer ts.Close()
// Request
r, err := http.Get(ts.URL)
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, err == nil)
// Check body
err = utils.GetJsonFromResponse(r, &msg)
tests.Assert(t, err == nil)
tests.Assert(t, len(msg.Nodes) == 0)
}
示例13: TestClusterInfo
func TestClusterInfo(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 new ClusterInfo
entry := NewClusterEntry()
entry.Info.Id = "123"
for _, node := range []string{"a1", "a2", "a3"} {
entry.NodeAdd(node)
}
for _, vol := range []string{"b1", "b2", "b3"} {
entry.VolumeAdd(vol)
}
// Save the info in the database
err := app.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(BOLTDB_BUCKET_CLUSTER))
if b == nil {
return errors.New("Unable to open bucket")
}
buffer, err := entry.Marshal()
if err != nil {
return err
}
err = b.Put([]byte(entry.Info.Id), buffer)
if err != nil {
return err
}
return nil
})
tests.Assert(t, err == nil)
// Now that we have some data in the database, we can
// make a request for the clutser list
r, err := http.Get(ts.URL + "/clusters/" + "123")
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, err == nil)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
// Read response
var msg ClusterInfoResponse
err = utils.GetJsonFromResponse(r, &msg)
tests.Assert(t, err == nil)
// Check values are equal
tests.Assert(t, entry.Info.Id == msg.Id)
tests.Assert(t, entry.Info.Volumes[0] == msg.Volumes[0])
tests.Assert(t, entry.Info.Volumes[1] == msg.Volumes[1])
tests.Assert(t, entry.Info.Volumes[2] == msg.Volumes[2])
tests.Assert(t, entry.Info.Nodes[0] == msg.Nodes[0])
tests.Assert(t, entry.Info.Nodes[1] == msg.Nodes[1])
tests.Assert(t, entry.Info.Nodes[2] == msg.Nodes[2])
}
示例14: TestNodeAddDelete
func TestNodeAddDelete(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)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
// Read cluster information
var clusterinfo ClusterInfoResponse
err = utils.GetJsonFromResponse(r, &clusterinfo)
tests.Assert(t, err == nil)
// Create node on this cluster
request = []byte(fmt.Sprintf(`{
"cluster" : "%v",
"hostnames" : {
"storage" : [ "storage.hostname.com" ],
"manage" : [ "manage.hostname.com" ]
},
"zone" : 1
}`, clusterinfo.Id))
// Post nothing
r, err = http.Post(ts.URL+"/nodes", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusAccepted)
location, err := r.Location()
tests.Assert(t, err == nil)
// Query queue until finished
var node NodeInfoResponse
for {
r, err = http.Get(location.String())
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusOK)
if r.ContentLength <= 0 {
time.Sleep(time.Millisecond * 10)
continue
} else {
// Should have node information here
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
err = utils.GetJsonFromResponse(r, &node)
tests.Assert(t, err == nil)
break
}
}
tests.Assert(t, len(node.Id) > 0)
tests.Assert(t, len(node.Hostnames.Manage) == 1)
tests.Assert(t, len(node.Hostnames.Storage) == 1)
tests.Assert(t, node.Hostnames.Manage[0] == "manage.hostname.com")
tests.Assert(t, node.Hostnames.Storage[0] == "storage.hostname.com")
tests.Assert(t, node.Zone == 1)
tests.Assert(t, node.ClusterId == clusterinfo.Id)
tests.Assert(t, len(node.DevicesInfo) == 0)
// Check Cluster has node
r, err = http.Get(ts.URL + "/clusters/" + clusterinfo.Id)
tests.Assert(t, r.StatusCode == http.StatusOK)
tests.Assert(t, err == nil)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
err = utils.GetJsonFromResponse(r, &clusterinfo)
tests.Assert(t, len(clusterinfo.Nodes) == 1)
tests.Assert(t, clusterinfo.Nodes[0] == node.Id)
// Check the data is in the database correctly
var entry *NodeEntry
err = app.db.View(func(tx *bolt.Tx) error {
entry, err = NewNodeEntryFromId(tx, node.Id)
return err
})
tests.Assert(t, err == nil)
tests.Assert(t, entry != nil)
tests.Assert(t, entry.Info.Id == node.Id)
tests.Assert(t, len(entry.Info.Hostnames.Manage) == 1)
tests.Assert(t, len(entry.Info.Hostnames.Storage) == 1)
tests.Assert(t, entry.Info.Hostnames.Manage[0] == node.Hostnames.Manage[0])
tests.Assert(t, entry.Info.Hostnames.Storage[0] == node.Hostnames.Storage[0])
tests.Assert(t, len(entry.Devices) == 0)
//.........這裏部分代碼省略.........
示例15: TestPeerProbe
func TestPeerProbe(t *testing.T) {
tmpfile := tests.Tempfile()
defer os.Remove(tmpfile)
// Create the app
app := NewTestApp(tmpfile)
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)
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
// Read cluster information
var clusterinfo ClusterInfoResponse
err = utils.GetJsonFromResponse(r, &clusterinfo)
tests.Assert(t, err == nil)
// Override mock to check if the peer function was called
probe_called := false
app.xo.MockPeerProbe = func(exec_host, newnode string) error {
probe_called = true
return nil
}
// Create node on this cluster
request = []byte(`{
"cluster" : "` + clusterinfo.Id + `",
"hostnames" : {
"storage" : [ "storage0.hostname.com" ],
"manage" : [ "manage0.hostname.com" ]
},
"zone" : 1
}`)
// Create node
r, err = http.Post(ts.URL+"/nodes", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusAccepted)
location, err := r.Location()
tests.Assert(t, err == nil)
// Query queue until finished
for {
r, err = http.Get(location.String())
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusOK)
if r.ContentLength <= 0 {
time.Sleep(time.Millisecond * 10)
continue
} else {
// Should have node information here
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
tests.Assert(t, err == nil)
break
}
}
tests.Assert(t, probe_called == false)
// Now add another and check that probe was called
request = []byte(`{
"cluster" : "` + clusterinfo.Id + `",
"hostnames" : {
"storage" : [ "storage1.hostname.com" ],
"manage" : [ "manage1.hostname.com" ]
},
"zone" : 1
}`)
// Create node
r, err = http.Post(ts.URL+"/nodes", "application/json", bytes.NewBuffer(request))
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusAccepted)
location, err = r.Location()
tests.Assert(t, err == nil)
// Query queue until finished
for {
r, err = http.Get(location.String())
tests.Assert(t, err == nil)
tests.Assert(t, r.StatusCode == http.StatusOK)
if r.ContentLength <= 0 {
time.Sleep(time.Millisecond * 10)
continue
} else {
// Should have node information here
tests.Assert(t, r.Header.Get("Content-Type") == "application/json; charset=UTF-8")
tests.Assert(t, err == nil)
break
//.........這裏部分代碼省略.........