github.com/gophercloud/gophercloud@v1.11.0/openstack/containerinfra/v1/nodegroups/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 6 "github.com/gophercloud/gophercloud" 7 "github.com/gophercloud/gophercloud/openstack/containerinfra/v1/nodegroups" 8 th "github.com/gophercloud/gophercloud/testhelper" 9 fake "github.com/gophercloud/gophercloud/testhelper/client" 10 ) 11 12 // TestGetNodeGroupSuccess gets a node group successfully. 13 func TestGetNodeGroupSuccess(t *testing.T) { 14 th.SetupHTTP() 15 defer th.TeardownHTTP() 16 17 handleGetNodeGroupSuccess(t) 18 19 sc := fake.ServiceClient() 20 sc.Endpoint = sc.Endpoint + "v1/" 21 22 ng, err := nodegroups.Get(sc, clusterUUID, nodeGroup1UUID).Extract() 23 th.AssertNoErr(t, err) 24 25 th.AssertDeepEquals(t, expectedNodeGroup1, *ng) 26 } 27 28 // TestGetNodeGroupNotFound tries to get a node group which does not exist. 29 func TestGetNodeGroupNotFound(t *testing.T) { 30 th.SetupHTTP() 31 defer th.TeardownHTTP() 32 33 handleGetNodeGroupNotFound(t) 34 35 sc := fake.ServiceClient() 36 sc.Endpoint = sc.Endpoint + "v1/" 37 38 _, err := nodegroups.Get(sc, clusterUUID, badNodeGroupUUID).Extract() 39 th.AssertEquals(t, true, err != nil) 40 41 _, isNotFound := err.(gophercloud.ErrDefault404) 42 th.AssertEquals(t, true, isNotFound) 43 } 44 45 // TestGetNodeGroupClusterNotFound tries to get a node group in 46 // a cluster which does not exist. 47 func TestGetNodeGroupClusterNotFound(t *testing.T) { 48 th.SetupHTTP() 49 defer th.TeardownHTTP() 50 51 handleGetNodeGroupClusterNotFound(t) 52 53 sc := fake.ServiceClient() 54 sc.Endpoint = sc.Endpoint + "v1/" 55 56 _, err := nodegroups.Get(sc, badClusterUUID, badNodeGroupUUID).Extract() 57 th.AssertEquals(t, true, err != nil) 58 59 _, isNotFound := err.(gophercloud.ErrDefault404) 60 th.AssertEquals(t, true, isNotFound) 61 } 62 63 // TestListNodeGroupsSuccess lists the node groups of a cluster successfully. 64 func TestListNodeGroupsSuccess(t *testing.T) { 65 th.SetupHTTP() 66 defer th.TeardownHTTP() 67 68 handleListNodeGroupsSuccess(t) 69 70 sc := fake.ServiceClient() 71 sc.Endpoint = sc.Endpoint + "v1/" 72 73 ngPages, err := nodegroups.List(sc, clusterUUID, nodegroups.ListOpts{}).AllPages() 74 th.AssertNoErr(t, err) 75 76 ngs, err := nodegroups.ExtractNodeGroups(ngPages) 77 th.AssertNoErr(t, err) 78 th.AssertEquals(t, 2, len(ngs)) 79 th.AssertEquals(t, nodeGroup1UUID, ngs[0].UUID) 80 th.AssertEquals(t, nodeGroup2UUID, ngs[1].UUID) 81 } 82 83 // TestListNodeGroupsLimitSuccess tests listing node groups 84 // with each returned page limited to one node group and 85 // also giving a URL to get the next page. 86 func TestListNodeGroupsLimitSuccess(t *testing.T) { 87 th.SetupHTTP() 88 defer th.TeardownHTTP() 89 90 handleListNodeGroupsLimitSuccess(t) 91 92 sc := fake.ServiceClient() 93 sc.Endpoint = sc.Endpoint + "v1/" 94 95 listOpts := nodegroups.ListOpts{Limit: 1} 96 ngPages, err := nodegroups.List(sc, clusterUUID, listOpts).AllPages() 97 th.AssertNoErr(t, err) 98 99 ngs, err := nodegroups.ExtractNodeGroups(ngPages) 100 th.AssertNoErr(t, err) 101 th.AssertEquals(t, 2, len(ngs)) 102 th.AssertEquals(t, nodeGroup1UUID, ngs[0].UUID) 103 th.AssertEquals(t, nodeGroup2UUID, ngs[1].UUID) 104 } 105 106 // TestListNodeGroupsClusterNotFound tries to list node groups 107 // of a cluster which does not exist. 108 func TestListNodeGroupsClusterNotFound(t *testing.T) { 109 th.SetupHTTP() 110 defer th.TeardownHTTP() 111 112 handleListNodeGroupsClusterNotFound(t) 113 114 sc := fake.ServiceClient() 115 sc.Endpoint = sc.Endpoint + "v1/" 116 117 _, err := nodegroups.List(sc, clusterUUID, nodegroups.ListOpts{}).AllPages() 118 th.AssertEquals(t, true, err != nil) 119 120 _, isNotFound := err.(gophercloud.ErrDefault404) 121 th.AssertEquals(t, true, isNotFound) 122 } 123 124 // TestCreateNodeGroupSuccess creates a node group successfully. 125 func TestCreateNodeGroupSuccess(t *testing.T) { 126 th.SetupHTTP() 127 defer th.TeardownHTTP() 128 129 handleCreateNodeGroupSuccess(t) 130 131 sc := fake.ServiceClient() 132 sc.Endpoint = sc.Endpoint + "v1/" 133 134 createOpts := nodegroups.CreateOpts{ 135 Name: "test-ng", 136 MergeLabels: gophercloud.Enabled, 137 } 138 139 ng, err := nodegroups.Create(sc, clusterUUID, createOpts).Extract() 140 th.AssertNoErr(t, err) 141 th.AssertDeepEquals(t, expectedCreatedNodeGroup, *ng) 142 } 143 144 // TestCreateNodeGroupDuplicate creates a node group with 145 // the same name as an existing one. 146 func TestCreateNodeGroupDuplicate(t *testing.T) { 147 th.SetupHTTP() 148 defer th.TeardownHTTP() 149 150 handleCreateNodeGroupDuplicate(t) 151 152 sc := fake.ServiceClient() 153 sc.Endpoint = sc.Endpoint + "v1/" 154 155 createOpts := nodegroups.CreateOpts{ 156 Name: "default-worker", 157 } 158 159 _, err := nodegroups.Create(sc, clusterUUID, createOpts).Extract() 160 th.AssertEquals(t, true, err != nil) 161 _, isNotAccepted := err.(gophercloud.ErrDefault409) 162 th.AssertEquals(t, true, isNotAccepted) 163 } 164 165 // TestCreateNodeGroupMaster creates a node group with 166 // role=master which is not allowed. 167 func TestCreateNodeGroupMaster(t *testing.T) { 168 th.SetupHTTP() 169 defer th.TeardownHTTP() 170 171 handleCreateNodeGroupMaster(t) 172 173 sc := fake.ServiceClient() 174 sc.Endpoint = sc.Endpoint + "v1/" 175 176 createOpts := nodegroups.CreateOpts{ 177 Name: "new-ng", 178 Role: "master", 179 } 180 181 _, err := nodegroups.Create(sc, clusterUUID, createOpts).Extract() 182 th.AssertEquals(t, true, err != nil) 183 _, isBadRequest := err.(gophercloud.ErrDefault400) 184 th.AssertEquals(t, true, isBadRequest) 185 } 186 187 // TestCreateNodeGroupBadSizes creates a node group with 188 // min_nodes greater than max_nodes. 189 func TestCreateNodeGroupBadSizes(t *testing.T) { 190 th.SetupHTTP() 191 defer th.TeardownHTTP() 192 193 handleCreateNodeGroupBadSizes(t) 194 195 sc := fake.ServiceClient() 196 sc.Endpoint = sc.Endpoint + "v1/" 197 198 maxNodes := 3 199 createOpts := nodegroups.CreateOpts{ 200 Name: "default-worker", 201 MinNodeCount: 5, 202 MaxNodeCount: &maxNodes, 203 } 204 205 _, err := nodegroups.Create(sc, clusterUUID, createOpts).Extract() 206 th.AssertEquals(t, true, err != nil) 207 _, isNotAccepted := err.(gophercloud.ErrDefault409) 208 th.AssertEquals(t, true, isNotAccepted) 209 } 210 211 // TestUpdateNodeGroupSuccess updates a node group successfully. 212 func TestUpdateNodeGroupSuccess(t *testing.T) { 213 th.SetupHTTP() 214 defer th.TeardownHTTP() 215 216 handleUpdateNodeGroupSuccess(t) 217 218 sc := fake.ServiceClient() 219 sc.Endpoint = sc.Endpoint + "v1/" 220 221 updateOpts := []nodegroups.UpdateOptsBuilder{ 222 nodegroups.UpdateOpts{ 223 Op: nodegroups.ReplaceOp, 224 Path: "/max_node_count", 225 Value: 3, 226 }, 227 } 228 229 ng, err := nodegroups.Update(sc, clusterUUID, nodeGroup2UUID, updateOpts).Extract() 230 th.AssertNoErr(t, err) 231 th.AssertDeepEquals(t, expectedUpdatedNodeGroup, *ng) 232 } 233 234 // TestUpdateNodeGroupInternal tries to update an internal 235 // property of the node group. 236 func TestUpdateNodeGroupInternal(t *testing.T) { 237 th.SetupHTTP() 238 defer th.TeardownHTTP() 239 240 handleUpdateNodeGroupInternal(t) 241 242 sc := fake.ServiceClient() 243 sc.Endpoint = sc.Endpoint + "v1/" 244 245 updateOpts := []nodegroups.UpdateOptsBuilder{ 246 nodegroups.UpdateOpts{ 247 Op: nodegroups.ReplaceOp, 248 Path: "/name", 249 Value: "newname", 250 }, 251 } 252 253 _, err := nodegroups.Update(sc, clusterUUID, nodeGroup2UUID, updateOpts).Extract() 254 th.AssertEquals(t, true, err != nil) 255 _, isBadRequest := err.(gophercloud.ErrDefault400) 256 th.AssertEquals(t, true, isBadRequest) 257 } 258 259 // TestUpdateNodeGroupBadField tries to update a 260 // field of the node group that does not exist. 261 func TestUpdateNodeGroupBadField(t *testing.T) { 262 th.SetupHTTP() 263 defer th.TeardownHTTP() 264 265 handleUpdateNodeGroupBadField(t) 266 267 sc := fake.ServiceClient() 268 sc.Endpoint = sc.Endpoint + "v1/" 269 270 updateOpts := []nodegroups.UpdateOptsBuilder{ 271 nodegroups.UpdateOpts{ 272 Op: nodegroups.ReplaceOp, 273 Path: "/bad_field", 274 Value: "abc123", 275 }, 276 } 277 278 _, err := nodegroups.Update(sc, clusterUUID, nodeGroup2UUID, updateOpts).Extract() 279 th.AssertEquals(t, true, err != nil) 280 _, isBadRequest := err.(gophercloud.ErrDefault400) 281 th.AssertEquals(t, true, isBadRequest) 282 } 283 284 // TestUpdateNodeGroupBadMin tries to set a minimum node count 285 // greater than the current node count 286 func TestUpdateNodeGroupBadMin(t *testing.T) { 287 th.SetupHTTP() 288 defer th.TeardownHTTP() 289 290 handleUpdateNodeGroupBadMin(t) 291 292 sc := fake.ServiceClient() 293 sc.Endpoint = sc.Endpoint + "v1/" 294 295 updateOpts := []nodegroups.UpdateOptsBuilder{ 296 nodegroups.UpdateOpts{ 297 Op: nodegroups.ReplaceOp, 298 Path: "/min_node_count", 299 Value: 5, 300 }, 301 } 302 303 _, err := nodegroups.Update(sc, clusterUUID, nodeGroup2UUID, updateOpts).Extract() 304 th.AssertEquals(t, true, err != nil) 305 _, isNotAccepted := err.(gophercloud.ErrDefault409) 306 th.AssertEquals(t, true, isNotAccepted) 307 } 308 309 // TestDeleteNodeGroupSuccess deletes a node group successfully. 310 func TestDeleteNodeGroupSuccess(t *testing.T) { 311 th.SetupHTTP() 312 defer th.TeardownHTTP() 313 314 handleDeleteNodeGroupSuccess(t) 315 316 sc := fake.ServiceClient() 317 sc.Endpoint = sc.Endpoint + "v1/" 318 319 err := nodegroups.Delete(sc, clusterUUID, nodeGroup2UUID).ExtractErr() 320 th.AssertNoErr(t, err) 321 } 322 323 // TestDeleteNodeGroupNotFound tries to delete a node group that does not exist. 324 func TestDeleteNodeGroupNotFound(t *testing.T) { 325 th.SetupHTTP() 326 defer th.TeardownHTTP() 327 328 handleDeleteNodeGroupNotFound(t) 329 330 sc := fake.ServiceClient() 331 sc.Endpoint = sc.Endpoint + "v1/" 332 333 err := nodegroups.Delete(sc, clusterUUID, badNodeGroupUUID).ExtractErr() 334 _, isNotFound := err.(gophercloud.ErrDefault404) 335 th.AssertEquals(t, true, isNotFound) 336 } 337 338 // TestDeleteNodeGroupClusterNotFound tries to delete a node group in a cluster that does not exist. 339 func TestDeleteNodeGroupClusterNotFound(t *testing.T) { 340 th.SetupHTTP() 341 defer th.TeardownHTTP() 342 343 handleDeleteNodeGroupClusterNotFound(t) 344 345 sc := fake.ServiceClient() 346 sc.Endpoint = sc.Endpoint + "v1/" 347 348 err := nodegroups.Delete(sc, badClusterUUID, badNodeGroupUUID).ExtractErr() 349 _, isNotFound := err.(gophercloud.ErrDefault404) 350 th.AssertEquals(t, true, isNotFound) 351 } 352 353 // TestDeleteNodeGroupDefault tries to delete a protected default node group. 354 func TestDeleteNodeGroupDefault(t *testing.T) { 355 th.SetupHTTP() 356 defer th.TeardownHTTP() 357 358 handleDeleteNodeGroupDefault(t) 359 360 sc := fake.ServiceClient() 361 sc.Endpoint = sc.Endpoint + "v1/" 362 363 err := nodegroups.Delete(sc, clusterUUID, nodeGroup2UUID).ExtractErr() 364 _, isBadRequest := err.(gophercloud.ErrDefault400) 365 th.AssertEquals(t, true, isBadRequest) 366 }