Пример #1
0
func TestDeviceState(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 mock allocator
	mockAllocator := NewMockAllocator(app.db)
	app.allocator = mockAllocator

	// Create a client
	c := client.NewClientNoAuth(ts.URL)
	tests.Assert(t, c != nil)

	// Create Cluster
	cluster, err := c.ClusterCreate()
	tests.Assert(t, err == nil)

	// Create Node
	nodeReq := &api.NodeAddRequest{
		Zone:      1,
		ClusterId: cluster.Id,
	}
	nodeReq.Hostnames.Manage = sort.StringSlice{"manage.host"}
	nodeReq.Hostnames.Storage = sort.StringSlice{"storage.host"}
	node, err := c.NodeAdd(nodeReq)
	tests.Assert(t, err == nil)

	// Add device
	deviceReq := &api.DeviceAddRequest{}
	deviceReq.Name = "/dev/fake1"
	deviceReq.NodeId = node.Id

	err = c.DeviceAdd(deviceReq)
	tests.Assert(t, err == nil)

	// Get node information again
	node, err = c.NodeInfo(node.Id)
	tests.Assert(t, err == nil)

	// Get device information
	deviceId := node.DevicesInfo[0].Id
	device, err := c.DeviceInfo(deviceId)
	tests.Assert(t, err == nil)

	// Get info
	deviceInfo, err := c.DeviceInfo(device.Id)
	tests.Assert(t, err == nil)
	tests.Assert(t, deviceInfo.State == "online")

	// Check that the device is in the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 1)
	tests.Assert(t, mockAllocator.clustermap[cluster.Id][0] == device.Id)

	// Set offline
	request := []byte(`{
				"state" : "offline"
				}`)
	r, err := http.Post(ts.URL+"/devices/"+device.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusOK)

	// Check it was removed from the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 0)

	// Get Device Info
	r, err = http.Get(ts.URL + "/devices/" + device.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 api.DeviceInfoResponse
	err = utils.GetJsonFromResponse(r, &info)
	tests.Assert(t, info.Id == device.Id)
	tests.Assert(t, info.Name == device.Name)
	tests.Assert(t, info.State == "offline")
	tests.Assert(t, info.Storage.Free == device.Storage.Free)
	tests.Assert(t, info.Storage.Used == device.Storage.Used)
	tests.Assert(t, info.Storage.Total == device.Storage.Total)

	// Set online again
	request = []byte(`{
				"state" : "online"
				}`)
	r, err = http.Post(ts.URL+"/devices/"+device.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusOK)

	// Check that the device is in the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 1)
	tests.Assert(t, mockAllocator.clustermap[cluster.Id][0] == device.Id)

	// Get Device Info
	r, err = http.Get(ts.URL + "/devices/" + device.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")

	err = utils.GetJsonFromResponse(r, &info)
	tests.Assert(t, info.Id == device.Id)
	tests.Assert(t, info.Name == device.Name)
	tests.Assert(t, info.State == "online")
	tests.Assert(t, info.Storage.Free == device.Storage.Free)
	tests.Assert(t, info.Storage.Used == device.Storage.Used)
	tests.Assert(t, info.Storage.Total == device.Storage.Total)

	// Set to unknown state
	request = []byte(`{
				"state" : "blah"
				}`)
	r, err = http.Post(ts.URL+"/devices/"+device.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusBadRequest)

	// Check that the device is still in the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 1)
	tests.Assert(t, mockAllocator.clustermap[cluster.Id][0] == device.Id)

	// Make sure the state did not change
	r, err = http.Get(ts.URL + "/devices/" + device.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")

	err = utils.GetJsonFromResponse(r, &info)
	tests.Assert(t, info.Id == device.Id)
	tests.Assert(t, info.Name == device.Name)
	tests.Assert(t, info.State == "online")
	tests.Assert(t, info.Storage.Free == device.Storage.Free)
	tests.Assert(t, info.Storage.Used == device.Storage.Used)
	tests.Assert(t, info.Storage.Total == device.Storage.Total)
}
Пример #2
0
// These are the settings for the vagrant file
const (

	// The heketi server must be running on the host
	heketiUrl = "http://localhost:8080"

	// VMs
	storage0 = "192.168.10.100"
	storage1 = "192.168.10.101"
	storage2 = "192.168.10.102"
	storage3 = "192.168.10.103"
)

var (
	// Heketi client
	heketi = client.NewClientNoAuth(heketiUrl)

	// Storage systems
	storagevms = []string{
		storage0,
		storage1,
		storage2,
		storage3,
	}

	// Disks on each system
	disks = []string{
		"/dev/sdb",
		"/dev/sdc",
		"/dev/sdd",
		"/dev/sde",
Пример #3
0
func TestNodeState(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 mock allocator
	mockAllocator := NewMockAllocator(app.db)
	app.allocator = mockAllocator

	// Create a client
	c := client.NewClientNoAuth(ts.URL)
	tests.Assert(t, c != nil)

	// Create Cluster
	cluster, err := c.ClusterCreate()
	tests.Assert(t, err == nil)

	// Create Node
	nodeReq := &api.NodeAddRequest{
		Zone:      1,
		ClusterId: cluster.Id,
	}
	nodeReq.Hostnames.Manage = sort.StringSlice{"manage.host"}
	nodeReq.Hostnames.Storage = sort.StringSlice{"storage.host"}
	node, err := c.NodeAdd(nodeReq)
	tests.Assert(t, err == nil)

	// Add device
	deviceReq := &api.DeviceAddRequest{}
	deviceReq.Name = "/dev/fake1"
	deviceReq.NodeId = node.Id

	err = c.DeviceAdd(deviceReq)
	tests.Assert(t, err == nil)

	// Get node information again
	node, err = c.NodeInfo(node.Id)
	tests.Assert(t, err == nil)

	// Get device information
	deviceId := node.DevicesInfo[0].Id
	device, err := c.DeviceInfo(deviceId)
	tests.Assert(t, err == nil)

	// Get info
	deviceInfo, err := c.DeviceInfo(device.Id)
	tests.Assert(t, err == nil)
	tests.Assert(t, deviceInfo.State == "online")

	// Check that the device is in the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 1)
	tests.Assert(t, mockAllocator.clustermap[cluster.Id][0] == device.Id)

	// Set node offline
	request := []byte(`{
				"state" : "offline"
				}`)
	r, err := http.Post(ts.URL+"/nodes/"+node.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusOK)

	// Check it was removed from the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 0)

	// Get node info
	node, err = c.NodeInfo(node.Id)
	tests.Assert(t, err == nil)
	tests.Assert(t, node.State == "offline")

	// Set offline again, should succeed
	request = []byte(`{
				"state" : "offline"
				}`)
	r, err = http.Post(ts.URL+"/nodes/"+node.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusOK)

	// Check it was removed from the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 0)

	// Get node info
	node, err = c.NodeInfo(node.Id)
	tests.Assert(t, err == nil)
	tests.Assert(t, node.State == "offline")

	// Set online again
	request = []byte(`{
				"state" : "online"
				}`)
	r, err = http.Post(ts.URL+"/nodes/"+node.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusOK)

	// Check that the device is in the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 1)
	tests.Assert(t, mockAllocator.clustermap[cluster.Id][0] == device.Id)

	// Set online again, should succeed
	request = []byte(`{
				"state" : "online"
				}`)
	r, err = http.Post(ts.URL+"/nodes/"+node.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusOK)

	// Check that the device is in the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 1)
	tests.Assert(t, mockAllocator.clustermap[cluster.Id][0] == device.Id)

	// Get node info
	node, err = c.NodeInfo(node.Id)
	tests.Assert(t, err == nil)
	tests.Assert(t, node.State == "online")

	// Set unknown state
	request = []byte(`{
				"state" : "blah"
				}`)
	r, err = http.Post(ts.URL+"/nodes/"+node.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusBadRequest)

	// Check that the device is still in the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 1)
	tests.Assert(t, mockAllocator.clustermap[cluster.Id][0] == device.Id)

	// Check node is still online
	node, err = c.NodeInfo(node.Id)
	tests.Assert(t, err == nil)
	tests.Assert(t, node.State == "online")

	// Set device offline
	request = []byte(`{
				"state" : "offline"
				}`)
	r, err = http.Post(ts.URL+"/devices/"+device.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusOK)

	// Check it was removed from the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 0)

	// Set Node offline
	request = []byte(`{
				"state" : "offline"
				}`)
	r, err = http.Post(ts.URL+"/nodes/"+node.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusOK)

	// Check it was removed from the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 0)

	// Set Node online -- Device is still offline and should not be added
	request = []byte(`{
				"state" : "online"
				}`)
	r, err = http.Post(ts.URL+"/nodes/"+node.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusOK)

	// Check device is not in ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 0)

	// Now make device online
	request = []byte(`{
				"state" : "online"
				}`)
	r, err = http.Post(ts.URL+"/devices/"+device.Id+"/state",
		"application/json", bytes.NewBuffer(request))
	tests.Assert(t, err == nil)
	tests.Assert(t, r.StatusCode == http.StatusOK)

	// Now it should be back in the ring
	tests.Assert(t, len(mockAllocator.clustermap[cluster.Id]) == 1)
	tests.Assert(t, mockAllocator.clustermap[cluster.Id][0] == device.Id)

}