1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
// +build acceptance compute hypervisors
package v2
import (
"fmt"
"testing"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/acceptance/clients"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/hypervisors"
th "github.com/gophercloud/gophercloud/testhelper"
)
func TestHypervisorsList(t *testing.T) {
clients.RequireAdmin(t)
client, err := clients.NewComputeV2Client()
th.AssertNoErr(t, err)
allPages, err := hypervisors.List(client).AllPages()
th.AssertNoErr(t, err)
allHypervisors, err := hypervisors.ExtractHypervisors(allPages)
th.AssertNoErr(t, err)
for _, h := range allHypervisors {
tools.PrintResource(t, h)
}
}
func TestHypervisorsGet(t *testing.T) {
clients.RequireAdmin(t)
client, err := clients.NewComputeV2Client()
th.AssertNoErr(t, err)
hypervisorID, err := getHypervisorID(t, client)
th.AssertNoErr(t, err)
hypervisor, err := hypervisors.Get(client, hypervisorID).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, hypervisor)
th.AssertEquals(t, hypervisorID, hypervisor.ID)
}
func TestHypervisorsGetStatistics(t *testing.T) {
clients.RequireAdmin(t)
client, err := clients.NewComputeV2Client()
th.AssertNoErr(t, err)
hypervisorsStats, err := hypervisors.GetStatistics(client).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, hypervisorsStats)
if hypervisorsStats.Count == 0 {
t.Fatalf("Unable to get hypervisor stats")
}
}
func TestHypervisorsGetUptime(t *testing.T) {
clients.RequireAdmin(t)
client, err := clients.NewComputeV2Client()
th.AssertNoErr(t, err)
hypervisorID, err := getHypervisorID(t, client)
th.AssertNoErr(t, err)
hypervisor, err := hypervisors.GetUptime(client, hypervisorID).Extract()
th.AssertNoErr(t, err)
tools.PrintResource(t, hypervisor)
th.AssertEquals(t, hypervisorID, hypervisor.ID)
}
func getHypervisorID(t *testing.T, client *gophercloud.ServiceClient) (int, error) {
allPages, err := hypervisors.List(client).AllPages()
th.AssertNoErr(t, err)
allHypervisors, err := hypervisors.ExtractHypervisors(allPages)
th.AssertNoErr(t, err)
if len(allHypervisors) > 0 {
return allHypervisors[0].ID, nil
}
return 0, fmt.Errorf("Unable to get hypervisor ID")
}
|