github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/hypervisors/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/hypervisors"
     7  	"github.com/huaweicloud/golangsdk/pagination"
     8  	"github.com/huaweicloud/golangsdk/testhelper"
     9  	"github.com/huaweicloud/golangsdk/testhelper/client"
    10  )
    11  
    12  func TestListHypervisors(t *testing.T) {
    13  	testhelper.SetupHTTP()
    14  	defer testhelper.TeardownHTTP()
    15  	HandleHypervisorListSuccessfully(t)
    16  
    17  	pages := 0
    18  	err := hypervisors.List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
    19  		pages++
    20  
    21  		actual, err := hypervisors.ExtractHypervisors(page)
    22  		if err != nil {
    23  			return false, err
    24  		}
    25  
    26  		if len(actual) != 2 {
    27  			t.Fatalf("Expected 2 hypervisors, got %d", len(actual))
    28  		}
    29  		testhelper.CheckDeepEquals(t, HypervisorFake, actual[0])
    30  		testhelper.CheckDeepEquals(t, HypervisorFake, actual[1])
    31  
    32  		return true, nil
    33  	})
    34  
    35  	testhelper.AssertNoErr(t, err)
    36  
    37  	if pages != 1 {
    38  		t.Errorf("Expected 1 page, saw %d", pages)
    39  	}
    40  }
    41  
    42  func TestListAllHypervisors(t *testing.T) {
    43  	testhelper.SetupHTTP()
    44  	defer testhelper.TeardownHTTP()
    45  	HandleHypervisorListSuccessfully(t)
    46  
    47  	allPages, err := hypervisors.List(client.ServiceClient()).AllPages()
    48  	testhelper.AssertNoErr(t, err)
    49  	actual, err := hypervisors.ExtractHypervisors(allPages)
    50  	testhelper.AssertNoErr(t, err)
    51  	testhelper.CheckDeepEquals(t, HypervisorFake, actual[0])
    52  	testhelper.CheckDeepEquals(t, HypervisorFake, actual[1])
    53  }
    54  
    55  func TestHypervisorsStatistics(t *testing.T) {
    56  	testhelper.SetupHTTP()
    57  	defer testhelper.TeardownHTTP()
    58  	HandleHypervisorsStatisticsSuccessfully(t)
    59  
    60  	expected := HypervisorsStatisticsExpected
    61  
    62  	actual, err := hypervisors.GetStatistics(client.ServiceClient()).Extract()
    63  	testhelper.AssertNoErr(t, err)
    64  	testhelper.CheckDeepEquals(t, &expected, actual)
    65  }
    66  
    67  func TestGetHypervisor(t *testing.T) {
    68  	testhelper.SetupHTTP()
    69  	defer testhelper.TeardownHTTP()
    70  	HandleHypervisorGetSuccessfully(t)
    71  
    72  	expected := HypervisorFake
    73  
    74  	actual, err := hypervisors.Get(client.ServiceClient(), expected.ID).Extract()
    75  	testhelper.AssertNoErr(t, err)
    76  	testhelper.CheckDeepEquals(t, &expected, actual)
    77  }
    78  
    79  func TestHypervisorsUptime(t *testing.T) {
    80  	testhelper.SetupHTTP()
    81  	defer testhelper.TeardownHTTP()
    82  	HandleHypervisorUptimeSuccessfully(t)
    83  
    84  	expected := HypervisorUptimeExpected
    85  
    86  	actual, err := hypervisors.GetUptime(client.ServiceClient(), HypervisorFake.ID).Extract()
    87  	testhelper.AssertNoErr(t, err)
    88  	testhelper.CheckDeepEquals(t, &expected, actual)
    89  }