github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/aggregates/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 6 "github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/aggregates" 7 "github.com/huaweicloud/golangsdk/pagination" 8 th "github.com/huaweicloud/golangsdk/testhelper" 9 "github.com/huaweicloud/golangsdk/testhelper/client" 10 ) 11 12 func TestListAggregates(t *testing.T) { 13 th.SetupHTTP() 14 defer th.TeardownHTTP() 15 HandleListSuccessfully(t) 16 17 pages := 0 18 err := aggregates.List(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { 19 pages++ 20 21 actual, err := aggregates.ExtractAggregates(page) 22 if err != nil { 23 return false, err 24 } 25 26 if len(actual) != 2 { 27 t.Fatalf("Expected 2 aggregates, got %d", len(actual)) 28 } 29 th.CheckDeepEquals(t, FirstFakeAggregate, actual[0]) 30 th.CheckDeepEquals(t, SecondFakeAggregate, actual[1]) 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 TestCreateAggregates(t *testing.T) { 43 th.SetupHTTP() 44 defer th.TeardownHTTP() 45 HandleCreateSuccessfully(t) 46 47 expected := CreatedAggregate 48 49 opts := aggregates.CreateOpts{ 50 Name: "name", 51 AvailabilityZone: "london", 52 } 53 54 actual, err := aggregates.Create(client.ServiceClient(), opts).Extract() 55 th.AssertNoErr(t, err) 56 57 th.AssertDeepEquals(t, &expected, actual) 58 } 59 60 func TestDeleteAggregates(t *testing.T) { 61 th.SetupHTTP() 62 defer th.TeardownHTTP() 63 HandleDeleteSuccessfully(t) 64 65 err := aggregates.Delete(client.ServiceClient(), AggregateIDtoDelete).ExtractErr() 66 th.AssertNoErr(t, err) 67 } 68 69 func TestGetAggregates(t *testing.T) { 70 th.SetupHTTP() 71 defer th.TeardownHTTP() 72 HandleGetSuccessfully(t) 73 74 expected := SecondFakeAggregate 75 76 actual, err := aggregates.Get(client.ServiceClient(), AggregateIDtoGet).Extract() 77 th.AssertNoErr(t, err) 78 79 th.AssertDeepEquals(t, &expected, actual) 80 } 81 82 func TestUpdateAggregate(t *testing.T) { 83 th.SetupHTTP() 84 defer th.TeardownHTTP() 85 HandleUpdateSuccessfully(t) 86 87 expected := UpdatedAggregate 88 89 opts := aggregates.UpdateOpts{ 90 Name: "test-aggregates2", 91 AvailabilityZone: "nova2", 92 } 93 94 actual, err := aggregates.Update(client.ServiceClient(), expected.ID, opts).Extract() 95 th.AssertNoErr(t, err) 96 97 th.AssertDeepEquals(t, &expected, actual) 98 } 99 100 func TestAddHostAggregate(t *testing.T) { 101 th.SetupHTTP() 102 defer th.TeardownHTTP() 103 HandleAddHostSuccessfully(t) 104 105 expected := AggregateWithAddedHost 106 107 opts := aggregates.AddHostOpts{ 108 Host: "cmp1", 109 } 110 111 actual, err := aggregates.AddHost(client.ServiceClient(), expected.ID, opts).Extract() 112 th.AssertNoErr(t, err) 113 114 th.AssertDeepEquals(t, &expected, actual) 115 } 116 117 func TestRemoveHostAggregate(t *testing.T) { 118 th.SetupHTTP() 119 defer th.TeardownHTTP() 120 HandleRemoveHostSuccessfully(t) 121 122 expected := AggregateWithRemovedHost 123 124 opts := aggregates.RemoveHostOpts{ 125 Host: "cmp1", 126 } 127 128 actual, err := aggregates.RemoveHost(client.ServiceClient(), expected.ID, opts).Extract() 129 th.AssertNoErr(t, err) 130 131 th.AssertDeepEquals(t, &expected, actual) 132 } 133 134 func TestSetMetadataAggregate(t *testing.T) { 135 th.SetupHTTP() 136 defer th.TeardownHTTP() 137 HandleSetMetadataSuccessfully(t) 138 139 expected := AggregateWithUpdatedMetadata 140 141 opts := aggregates.SetMetadataOpts{ 142 Metadata: map[string]interface{}{"key": "value"}, 143 } 144 145 actual, err := aggregates.SetMetadata(client.ServiceClient(), expected.ID, opts).Extract() 146 th.AssertNoErr(t, err) 147 148 th.AssertDeepEquals(t, &expected, actual) 149 }