github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas_v2/pools/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 6 fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common" 7 "github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/pools" 8 "github.com/huaweicloud/golangsdk/pagination" 9 th "github.com/huaweicloud/golangsdk/testhelper" 10 ) 11 12 func TestListPools(t *testing.T) { 13 th.SetupHTTP() 14 defer th.TeardownHTTP() 15 HandlePoolListSuccessfully(t) 16 17 pages := 0 18 err := pools.List(fake.ServiceClient(), pools.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 19 pages++ 20 21 actual, err := pools.ExtractPools(page) 22 if err != nil { 23 return false, err 24 } 25 26 if len(actual) != 2 { 27 t.Fatalf("Expected 2 pools, got %d", len(actual)) 28 } 29 th.CheckDeepEquals(t, PoolWeb, actual[0]) 30 th.CheckDeepEquals(t, PoolDb, 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 TestListAllPools(t *testing.T) { 43 th.SetupHTTP() 44 defer th.TeardownHTTP() 45 HandlePoolListSuccessfully(t) 46 47 allPages, err := pools.List(fake.ServiceClient(), pools.ListOpts{}).AllPages() 48 th.AssertNoErr(t, err) 49 actual, err := pools.ExtractPools(allPages) 50 th.AssertNoErr(t, err) 51 th.CheckDeepEquals(t, PoolWeb, actual[0]) 52 th.CheckDeepEquals(t, PoolDb, actual[1]) 53 } 54 55 func TestCreatePool(t *testing.T) { 56 th.SetupHTTP() 57 defer th.TeardownHTTP() 58 HandlePoolCreationSuccessfully(t, SinglePoolBody) 59 60 actual, err := pools.Create(fake.ServiceClient(), pools.CreateOpts{ 61 LBMethod: pools.LBMethodRoundRobin, 62 Protocol: "HTTP", 63 Name: "Example pool", 64 TenantID: "2ffc6e22aae24e4795f87155d24c896f", 65 LoadbalancerID: "79e05663-7f03-45d2-a092-8b94062f22ab", 66 }).Extract() 67 th.AssertNoErr(t, err) 68 69 th.CheckDeepEquals(t, PoolDb, *actual) 70 } 71 72 func TestGetPool(t *testing.T) { 73 th.SetupHTTP() 74 defer th.TeardownHTTP() 75 HandlePoolGetSuccessfully(t) 76 77 client := fake.ServiceClient() 78 actual, err := pools.Get(client, "c3741b06-df4d-4715-b142-276b6bce75ab").Extract() 79 if err != nil { 80 t.Fatalf("Unexpected Get error: %v", err) 81 } 82 83 th.CheckDeepEquals(t, PoolDb, *actual) 84 } 85 86 func TestDeletePool(t *testing.T) { 87 th.SetupHTTP() 88 defer th.TeardownHTTP() 89 HandlePoolDeletionSuccessfully(t) 90 91 res := pools.Delete(fake.ServiceClient(), "c3741b06-df4d-4715-b142-276b6bce75ab") 92 th.AssertNoErr(t, res.Err) 93 } 94 95 func TestUpdatePool(t *testing.T) { 96 th.SetupHTTP() 97 defer th.TeardownHTTP() 98 HandlePoolUpdateSuccessfully(t) 99 100 client := fake.ServiceClient() 101 actual, err := pools.Update(client, "c3741b06-df4d-4715-b142-276b6bce75ab", pools.UpdateOpts{ 102 Name: "NewPoolName", 103 LBMethod: pools.LBMethodLeastConnections, 104 }).Extract() 105 if err != nil { 106 t.Fatalf("Unexpected Update error: %v", err) 107 } 108 109 th.CheckDeepEquals(t, PoolUpdated, *actual) 110 } 111 112 func TestRequiredPoolCreateOpts(t *testing.T) { 113 res := pools.Create(fake.ServiceClient(), pools.CreateOpts{}) 114 if res.Err == nil { 115 t.Fatalf("Expected error, got none") 116 } 117 res = pools.Create(fake.ServiceClient(), pools.CreateOpts{ 118 LBMethod: pools.LBMethod("invalid"), 119 Protocol: pools.ProtocolHTTPS, 120 LoadbalancerID: "69055154-f603-4a28-8951-7cc2d9e54a9a", 121 }) 122 if res.Err == nil { 123 t.Fatalf("Expected error, but got none") 124 } 125 126 res = pools.Create(fake.ServiceClient(), pools.CreateOpts{ 127 LBMethod: pools.LBMethodRoundRobin, 128 Protocol: pools.Protocol("invalid"), 129 LoadbalancerID: "69055154-f603-4a28-8951-7cc2d9e54a9a", 130 }) 131 if res.Err == nil { 132 t.Fatalf("Expected error, but got none") 133 } 134 135 res = pools.Create(fake.ServiceClient(), pools.CreateOpts{ 136 LBMethod: pools.LBMethodRoundRobin, 137 Protocol: pools.ProtocolHTTPS, 138 }) 139 if res.Err == nil { 140 t.Fatalf("Expected error, but got none") 141 } 142 } 143 144 func TestListMembers(t *testing.T) { 145 th.SetupHTTP() 146 defer th.TeardownHTTP() 147 HandleMemberListSuccessfully(t) 148 149 pages := 0 150 err := pools.ListMembers(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", pools.ListMembersOpts{}).EachPage(func(page pagination.Page) (bool, error) { 151 pages++ 152 153 actual, err := pools.ExtractMembers(page) 154 if err != nil { 155 return false, err 156 } 157 158 if len(actual) != 2 { 159 t.Fatalf("Expected 2 members, got %d", len(actual)) 160 } 161 th.CheckDeepEquals(t, MemberWeb, actual[0]) 162 th.CheckDeepEquals(t, MemberDb, actual[1]) 163 164 return true, nil 165 }) 166 167 th.AssertNoErr(t, err) 168 169 if pages != 1 { 170 t.Errorf("Expected 1 page, saw %d", pages) 171 } 172 } 173 174 func TestListAllMembers(t *testing.T) { 175 th.SetupHTTP() 176 defer th.TeardownHTTP() 177 HandleMemberListSuccessfully(t) 178 179 allPages, err := pools.ListMembers(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", pools.ListMembersOpts{}).AllPages() 180 th.AssertNoErr(t, err) 181 actual, err := pools.ExtractMembers(allPages) 182 th.AssertNoErr(t, err) 183 th.CheckDeepEquals(t, MemberWeb, actual[0]) 184 th.CheckDeepEquals(t, MemberDb, actual[1]) 185 } 186 187 func TestCreateMember(t *testing.T) { 188 th.SetupHTTP() 189 defer th.TeardownHTTP() 190 HandleMemberCreationSuccessfully(t, SingleMemberBody) 191 192 actual, err := pools.CreateMember(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", pools.CreateMemberOpts{ 193 Name: "db", 194 SubnetID: "1981f108-3c48-48d2-b908-30f7d28532c9", 195 TenantID: "2ffc6e22aae24e4795f87155d24c896f", 196 Address: "10.0.2.11", 197 ProtocolPort: 80, 198 Weight: 10, 199 }).Extract() 200 th.AssertNoErr(t, err) 201 202 th.CheckDeepEquals(t, MemberDb, *actual) 203 } 204 205 func TestRequiredMemberCreateOpts(t *testing.T) { 206 res := pools.CreateMember(fake.ServiceClient(), "", pools.CreateMemberOpts{}) 207 if res.Err == nil { 208 t.Fatalf("Expected error, got none") 209 } 210 res = pools.CreateMember(fake.ServiceClient(), "", pools.CreateMemberOpts{Address: "1.2.3.4", ProtocolPort: 80}) 211 if res.Err == nil { 212 t.Fatalf("Expected error, but got none") 213 } 214 res = pools.CreateMember(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", pools.CreateMemberOpts{ProtocolPort: 80}) 215 if res.Err == nil { 216 t.Fatalf("Expected error, but got none") 217 } 218 res = pools.CreateMember(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", pools.CreateMemberOpts{Address: "1.2.3.4"}) 219 if res.Err == nil { 220 t.Fatalf("Expected error, but got none") 221 } 222 } 223 224 func TestGetMember(t *testing.T) { 225 th.SetupHTTP() 226 defer th.TeardownHTTP() 227 HandleMemberGetSuccessfully(t) 228 229 client := fake.ServiceClient() 230 actual, err := pools.GetMember(client, "332abe93-f488-41ba-870b-2ac66be7f853", "2a280670-c202-4b0b-a562-34077415aabf").Extract() 231 if err != nil { 232 t.Fatalf("Unexpected Get error: %v", err) 233 } 234 235 th.CheckDeepEquals(t, MemberDb, *actual) 236 } 237 238 func TestDeleteMember(t *testing.T) { 239 th.SetupHTTP() 240 defer th.TeardownHTTP() 241 HandleMemberDeletionSuccessfully(t) 242 243 res := pools.DeleteMember(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", "2a280670-c202-4b0b-a562-34077415aabf") 244 th.AssertNoErr(t, res.Err) 245 } 246 247 func TestUpdateMember(t *testing.T) { 248 th.SetupHTTP() 249 defer th.TeardownHTTP() 250 HandleMemberUpdateSuccessfully(t) 251 252 client := fake.ServiceClient() 253 actual, err := pools.UpdateMember(client, "332abe93-f488-41ba-870b-2ac66be7f853", "2a280670-c202-4b0b-a562-34077415aabf", pools.UpdateMemberOpts{ 254 Name: "newMemberName", 255 Weight: 4, 256 }).Extract() 257 if err != nil { 258 t.Fatalf("Unexpected Update error: %v", err) 259 } 260 261 th.CheckDeepEquals(t, MemberUpdated, *actual) 262 }