github.com/gophercloud/gophercloud@v1.11.0/openstack/compute/v2/extensions/hypervisors/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 6 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/hypervisors" 7 "github.com/gophercloud/gophercloud/pagination" 8 "github.com/gophercloud/gophercloud/testhelper" 9 "github.com/gophercloud/gophercloud/testhelper/client" 10 ) 11 12 func TestListHypervisorsPre253(t *testing.T) { 13 testhelper.SetupHTTP() 14 defer testhelper.TeardownHTTP() 15 HandleHypervisorListPre253Successfully(t) 16 17 pages := 0 18 err := hypervisors.List(client.ServiceClient(), 19 hypervisors.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 20 pages++ 21 22 actual, err := hypervisors.ExtractHypervisors(page) 23 if err != nil { 24 return false, err 25 } 26 27 if len(actual) != 2 { 28 t.Fatalf("Expected 2 hypervisors, got %d", len(actual)) 29 } 30 testhelper.CheckDeepEquals(t, HypervisorFakePre253, actual[0]) 31 testhelper.CheckDeepEquals(t, HypervisorFakePre253, actual[1]) 32 33 return true, nil 34 }) 35 36 testhelper.AssertNoErr(t, err) 37 38 if pages != 1 { 39 t.Errorf("Expected 1 page, saw %d", pages) 40 } 41 } 42 43 func TestListAllHypervisorsPre253(t *testing.T) { 44 testhelper.SetupHTTP() 45 defer testhelper.TeardownHTTP() 46 HandleHypervisorListPre253Successfully(t) 47 48 allPages, err := hypervisors.List(client.ServiceClient(), hypervisors.ListOpts{}).AllPages() 49 testhelper.AssertNoErr(t, err) 50 actual, err := hypervisors.ExtractHypervisors(allPages) 51 testhelper.AssertNoErr(t, err) 52 testhelper.CheckDeepEquals(t, HypervisorFakePre253, actual[0]) 53 testhelper.CheckDeepEquals(t, HypervisorFakePre253, actual[1]) 54 } 55 56 func TestListHypervisors(t *testing.T) { 57 testhelper.SetupHTTP() 58 defer testhelper.TeardownHTTP() 59 HandleHypervisorListSuccessfully(t) 60 61 pages := 0 62 err := hypervisors.List(client.ServiceClient(), 63 hypervisors.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 64 pages++ 65 66 actual, err := hypervisors.ExtractHypervisors(page) 67 if err != nil { 68 return false, err 69 } 70 71 if len(actual) != 2 { 72 t.Fatalf("Expected 2 hypervisors, got %d", len(actual)) 73 } 74 testhelper.CheckDeepEquals(t, HypervisorFake, actual[0]) 75 testhelper.CheckDeepEquals(t, HypervisorFake, actual[1]) 76 77 return true, nil 78 }) 79 80 testhelper.AssertNoErr(t, err) 81 82 if pages != 1 { 83 t.Errorf("Expected 1 page, saw %d", pages) 84 } 85 } 86 87 func TestListAllHypervisors(t *testing.T) { 88 testhelper.SetupHTTP() 89 defer testhelper.TeardownHTTP() 90 HandleHypervisorListSuccessfully(t) 91 92 allPages, err := hypervisors.List(client.ServiceClient(), hypervisors.ListOpts{}).AllPages() 93 testhelper.AssertNoErr(t, err) 94 actual, err := hypervisors.ExtractHypervisors(allPages) 95 testhelper.AssertNoErr(t, err) 96 testhelper.CheckDeepEquals(t, HypervisorFake, actual[0]) 97 testhelper.CheckDeepEquals(t, HypervisorFake, actual[1]) 98 } 99 100 func TestListAllHypervisorsWithParameters(t *testing.T) { 101 testhelper.SetupHTTP() 102 defer testhelper.TeardownHTTP() 103 HandleHypervisorListWithParametersSuccessfully(t) 104 105 with_servers := true 106 allPages, err := hypervisors.List(client.ServiceClient(), hypervisors.ListOpts{WithServers: &with_servers}).AllPages() 107 testhelper.AssertNoErr(t, err) 108 actual, err := hypervisors.ExtractHypervisors(allPages) 109 testhelper.AssertNoErr(t, err) 110 testhelper.CheckDeepEquals(t, HypervisorFakeWithParameters, actual[0]) 111 testhelper.CheckDeepEquals(t, HypervisorFakeWithParameters, actual[1]) 112 } 113 114 func TestHypervisorsStatistics(t *testing.T) { 115 testhelper.SetupHTTP() 116 defer testhelper.TeardownHTTP() 117 HandleHypervisorsStatisticsSuccessfully(t) 118 119 expected := HypervisorsStatisticsExpected 120 121 actual, err := hypervisors.GetStatistics(client.ServiceClient()).Extract() 122 testhelper.AssertNoErr(t, err) 123 testhelper.CheckDeepEquals(t, &expected, actual) 124 } 125 126 func TestGetHypervisor(t *testing.T) { 127 testhelper.SetupHTTP() 128 defer testhelper.TeardownHTTP() 129 HandleHypervisorGetSuccessfully(t) 130 131 expected := HypervisorFake 132 133 actual, err := hypervisors.Get(client.ServiceClient(), expected.ID).Extract() 134 testhelper.AssertNoErr(t, err) 135 testhelper.CheckDeepEquals(t, &expected, actual) 136 } 137 138 func TestGetHypervisorEmptyCPUInfo(t *testing.T) { 139 testhelper.SetupHTTP() 140 defer testhelper.TeardownHTTP() 141 HandleHypervisorGetEmptyCPUInfoSuccessfully(t) 142 143 expected := HypervisorEmptyCPUInfo 144 145 actual, err := hypervisors.Get(client.ServiceClient(), expected.ID).Extract() 146 testhelper.AssertNoErr(t, err) 147 testhelper.CheckDeepEquals(t, &expected, actual) 148 } 149 150 func TestHypervisorsUptime(t *testing.T) { 151 testhelper.SetupHTTP() 152 defer testhelper.TeardownHTTP() 153 HandleHypervisorUptimeSuccessfully(t) 154 155 expected := HypervisorUptimeExpected 156 157 actual, err := hypervisors.GetUptime(client.ServiceClient(), HypervisorFake.ID).Extract() 158 testhelper.AssertNoErr(t, err) 159 testhelper.CheckDeepEquals(t, &expected, actual) 160 }