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