github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/baremetal/v1/conductors/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/baremetal/v1/conductors"
     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 TestListConductors(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  	HandleConductorListSuccessfully(t)
    17  
    18  	pages := 0
    19  	err := conductors.List(client.ServiceClient(), conductors.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    20  		pages++
    21  
    22  		actual, err := conductors.ExtractConductors(page)
    23  		if err != nil {
    24  			return false, err
    25  		}
    26  
    27  		if len(actual) != 2 {
    28  			t.Fatalf("Expected 2 conductors, got %d", len(actual))
    29  		}
    30  		th.AssertEquals(t, "compute1.localdomain", actual[0].Hostname)
    31  		th.AssertEquals(t, "compute2.localdomain", actual[1].Hostname)
    32  
    33  		return true, nil
    34  	})
    35  
    36  	th.AssertNoErr(t, err)
    37  
    38  	if pages != 1 {
    39  		t.Errorf("Expected 1 page, saw %d", pages)
    40  	}
    41  }
    42  
    43  func TestListDetailConductors(t *testing.T) {
    44  	th.SetupHTTP()
    45  	defer th.TeardownHTTP()
    46  	HandleConductorListDetailSuccessfully(t)
    47  
    48  	pages := 0
    49  	err := conductors.List(client.ServiceClient(), conductors.ListOpts{Detail: true}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
    50  		pages++
    51  
    52  		actual, err := conductors.ExtractConductors(page)
    53  		if err != nil {
    54  			return false, err
    55  		}
    56  
    57  		if len(actual) != 2 {
    58  			t.Fatalf("Expected 2 conductors, got %d", len(actual))
    59  		}
    60  		th.AssertEquals(t, "compute1.localdomain", actual[0].Hostname)
    61  		th.AssertEquals(t, false, actual[0].Alive)
    62  		th.AssertEquals(t, "compute2.localdomain", actual[1].Hostname)
    63  		th.AssertEquals(t, true, actual[1].Alive)
    64  
    65  		return true, nil
    66  	})
    67  
    68  	th.AssertNoErr(t, err)
    69  
    70  	if pages != 1 {
    71  		t.Errorf("Expected 1 page, saw %d", pages)
    72  	}
    73  }
    74  
    75  func TestListOpts(t *testing.T) {
    76  	// Detail cannot take Fields
    77  	optsDetail := conductors.ListOpts{
    78  		Fields: []string{"hostname", "alive"},
    79  		Detail: true,
    80  	}
    81  
    82  	opts := conductors.ListOpts{
    83  		Fields: []string{"hostname", "alive"},
    84  	}
    85  
    86  	_, err := optsDetail.ToConductorListQuery()
    87  	th.AssertEquals(t, err.Error(), "cannot have both fields and detail options for conductors")
    88  
    89  	// Regular ListOpts can
    90  	query, err := opts.ToConductorListQuery()
    91  	th.AssertEquals(t, "?fields=hostname%2Calive", query)
    92  	th.AssertNoErr(t, err)
    93  }
    94  
    95  func TestGetConductor(t *testing.T) {
    96  	th.SetupHTTP()
    97  	defer th.TeardownHTTP()
    98  	HandleConductorGetSuccessfully(t)
    99  
   100  	c := client.ServiceClient()
   101  	actual, err := conductors.Get(context.TODO(), c, "1234asdf").Extract()
   102  	if err != nil {
   103  		t.Fatalf("Unexpected Get error: %v", err)
   104  	}
   105  
   106  	th.CheckDeepEquals(t, ConductorFoo, *actual)
   107  }