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