Michal Kobus | f611358 | 2019-09-09 15:58:21 +0200 | [diff] [blame^] | 1 | package testing |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "gerrit.mcp.mirantis.net/debian/gophercloud.git" |
| 7 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/baremetal/v1/nodes" |
| 8 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination" |
| 9 | th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper" |
| 10 | "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper/client" |
| 11 | ) |
| 12 | |
| 13 | func TestListDetailNodes(t *testing.T) { |
| 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
| 16 | HandleNodeListDetailSuccessfully(t) |
| 17 | |
| 18 | pages := 0 |
| 19 | err := nodes.ListDetail(client.ServiceClient(), nodes.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 20 | pages++ |
| 21 | |
| 22 | actual, err := nodes.ExtractNodes(page) |
| 23 | if err != nil { |
| 24 | return false, err |
| 25 | } |
| 26 | |
| 27 | if len(actual) != 3 { |
| 28 | t.Fatalf("Expected 3 nodes, got %d", len(actual)) |
| 29 | } |
| 30 | th.CheckDeepEquals(t, NodeFoo, actual[0]) |
| 31 | th.CheckDeepEquals(t, NodeBar, actual[1]) |
| 32 | th.CheckDeepEquals(t, NodeBaz, actual[2]) |
| 33 | |
| 34 | return true, nil |
| 35 | }) |
| 36 | |
| 37 | th.AssertNoErr(t, err) |
| 38 | |
| 39 | if pages != 1 { |
| 40 | t.Errorf("Expected 1 page, saw %d", pages) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestListNodes(t *testing.T) { |
| 45 | th.SetupHTTP() |
| 46 | defer th.TeardownHTTP() |
| 47 | HandleNodeListSuccessfully(t) |
| 48 | |
| 49 | pages := 0 |
| 50 | err := nodes.List(client.ServiceClient(), nodes.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 51 | pages++ |
| 52 | |
| 53 | actual, err := nodes.ExtractNodes(page) |
| 54 | if err != nil { |
| 55 | return false, err |
| 56 | } |
| 57 | |
| 58 | if len(actual) != 3 { |
| 59 | t.Fatalf("Expected 3 nodes, got %d", len(actual)) |
| 60 | } |
| 61 | th.AssertEquals(t, "foo", actual[0].Name) |
| 62 | th.AssertEquals(t, "bar", actual[1].Name) |
| 63 | th.AssertEquals(t, "baz", actual[2].Name) |
| 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 | opts := nodes.ListOpts{ |
| 78 | Fields: []string{"name", "uuid"}, |
| 79 | } |
| 80 | |
| 81 | _, err := opts.ToNodeListDetailQuery() |
| 82 | th.AssertEquals(t, err.Error(), "fields is not a valid option when getting a detailed listing of nodes") |
| 83 | |
| 84 | // Regular ListOpts can |
| 85 | query, err := opts.ToNodeListQuery() |
| 86 | th.AssertEquals(t, query, "?fields=name&fields=uuid") |
| 87 | th.AssertNoErr(t, err) |
| 88 | } |
| 89 | |
| 90 | func TestCreateNode(t *testing.T) { |
| 91 | th.SetupHTTP() |
| 92 | defer th.TeardownHTTP() |
| 93 | HandleNodeCreationSuccessfully(t, SingleNodeBody) |
| 94 | |
| 95 | actual, err := nodes.Create(client.ServiceClient(), nodes.CreateOpts{ |
| 96 | Name: "foo", |
| 97 | Driver: "ipmi", |
| 98 | BootInterface: "pxe", |
| 99 | DriverInfo: map[string]interface{}{ |
| 100 | "ipmi_port": "6230", |
| 101 | "ipmi_username": "admin", |
| 102 | "deploy_kernel": "http://172.22.0.1/images/tinyipa-stable-rocky.vmlinuz", |
| 103 | "ipmi_address": "192.168.122.1", |
| 104 | "deploy_ramdisk": "http://172.22.0.1/images/tinyipa-stable-rocky.gz", |
| 105 | "ipmi_password": "admin", |
| 106 | }, |
| 107 | }).Extract() |
| 108 | th.AssertNoErr(t, err) |
| 109 | |
| 110 | th.CheckDeepEquals(t, NodeFoo, *actual) |
| 111 | } |
| 112 | |
| 113 | func TestDeleteNode(t *testing.T) { |
| 114 | th.SetupHTTP() |
| 115 | defer th.TeardownHTTP() |
| 116 | HandleNodeDeletionSuccessfully(t) |
| 117 | |
| 118 | res := nodes.Delete(client.ServiceClient(), "asdfasdfasdf") |
| 119 | th.AssertNoErr(t, res.Err) |
| 120 | } |
| 121 | |
| 122 | func TestGetNode(t *testing.T) { |
| 123 | th.SetupHTTP() |
| 124 | defer th.TeardownHTTP() |
| 125 | HandleNodeGetSuccessfully(t) |
| 126 | |
| 127 | c := client.ServiceClient() |
| 128 | actual, err := nodes.Get(c, "1234asdf").Extract() |
| 129 | if err != nil { |
| 130 | t.Fatalf("Unexpected Get error: %v", err) |
| 131 | } |
| 132 | |
| 133 | th.CheckDeepEquals(t, NodeFoo, *actual) |
| 134 | } |
| 135 | |
| 136 | func TestUpdateNode(t *testing.T) { |
| 137 | th.SetupHTTP() |
| 138 | defer th.TeardownHTTP() |
| 139 | HandleNodeUpdateSuccessfully(t, SingleNodeBody) |
| 140 | |
| 141 | c := client.ServiceClient() |
| 142 | actual, err := nodes.Update(c, "1234asdf", nodes.UpdateOpts{ |
| 143 | nodes.UpdateOperation{ |
| 144 | Op: nodes.ReplaceOp, |
| 145 | Path: "/properties", |
| 146 | Value: map[string]interface{}{ |
| 147 | "root_gb": 25, |
| 148 | }, |
| 149 | }, |
| 150 | }).Extract() |
| 151 | if err != nil { |
| 152 | t.Fatalf("Unexpected Update error: %v", err) |
| 153 | } |
| 154 | |
| 155 | th.CheckDeepEquals(t, NodeFoo, *actual) |
| 156 | } |
| 157 | |
| 158 | func TestUpdateRequiredOp(t *testing.T) { |
| 159 | c := client.ServiceClient() |
| 160 | _, err := nodes.Update(c, "1234asdf", nodes.UpdateOpts{ |
| 161 | nodes.UpdateOperation{ |
| 162 | Path: "/driver", |
| 163 | Value: "new-driver", |
| 164 | }, |
| 165 | }).Extract() |
| 166 | |
| 167 | if _, ok := err.(gophercloud.ErrMissingInput); !ok { |
| 168 | t.Fatal("ErrMissingInput was expected to occur") |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | func TestUpdateRequiredPath(t *testing.T) { |
| 174 | c := client.ServiceClient() |
| 175 | _, err := nodes.Update(c, "1234asdf", nodes.UpdateOpts{ |
| 176 | nodes.UpdateOperation{ |
| 177 | Op: nodes.ReplaceOp, |
| 178 | Value: "new-driver", |
| 179 | }, |
| 180 | }).Extract() |
| 181 | |
| 182 | if _, ok := err.(gophercloud.ErrMissingInput); !ok { |
| 183 | t.Fatal("ErrMissingInput was expected to occur") |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | func TestValidateNode(t *testing.T) { |
| 188 | th.SetupHTTP() |
| 189 | defer th.TeardownHTTP() |
| 190 | HandleNodeValidateSuccessfully(t) |
| 191 | |
| 192 | c := client.ServiceClient() |
| 193 | actual, err := nodes.Validate(c, "1234asdf").Extract() |
| 194 | th.AssertNoErr(t, err) |
| 195 | th.CheckDeepEquals(t, NodeFooValidation, *actual) |
| 196 | } |
| 197 | |
| 198 | func TestInjectNMI(t *testing.T) { |
| 199 | th.SetupHTTP() |
| 200 | defer th.TeardownHTTP() |
| 201 | HandleInjectNMISuccessfully(t) |
| 202 | |
| 203 | c := client.ServiceClient() |
| 204 | err := nodes.InjectNMI(c, "1234asdf").ExtractErr() |
| 205 | th.AssertNoErr(t, err) |
| 206 | } |
| 207 | |
| 208 | func TestSetBootDevice(t *testing.T) { |
| 209 | th.SetupHTTP() |
| 210 | defer th.TeardownHTTP() |
| 211 | HandleSetBootDeviceSuccessfully(t) |
| 212 | |
| 213 | c := client.ServiceClient() |
| 214 | err := nodes.SetBootDevice(c, "1234asdf", nodes.BootDeviceOpts{ |
| 215 | BootDevice: "pxe", |
| 216 | Persistent: false, |
| 217 | }).ExtractErr() |
| 218 | th.AssertNoErr(t, err) |
| 219 | } |
| 220 | |
| 221 | func TestGetBootDevice(t *testing.T) { |
| 222 | th.SetupHTTP() |
| 223 | defer th.TeardownHTTP() |
| 224 | HandleGetBootDeviceSuccessfully(t) |
| 225 | |
| 226 | c := client.ServiceClient() |
| 227 | bootDevice, err := nodes.GetBootDevice(c, "1234asdf").Extract() |
| 228 | th.AssertNoErr(t, err) |
| 229 | th.CheckDeepEquals(t, NodeBootDevice, *bootDevice) |
| 230 | } |
| 231 | |
| 232 | func TestGetSupportedBootDevices(t *testing.T) { |
| 233 | th.SetupHTTP() |
| 234 | defer th.TeardownHTTP() |
| 235 | HandleGetSupportedBootDeviceSuccessfully(t) |
| 236 | |
| 237 | c := client.ServiceClient() |
| 238 | bootDevices, err := nodes.GetSupportedBootDevices(c, "1234asdf").Extract() |
| 239 | th.AssertNoErr(t, err) |
| 240 | th.CheckDeepEquals(t, NodeSupportedBootDevice, bootDevices) |
| 241 | } |
| 242 | |
| 243 | func TestNodeChangeProvisionStateActive(t *testing.T) { |
| 244 | th.SetupHTTP() |
| 245 | defer th.TeardownHTTP() |
| 246 | HandleNodeChangeProvisionStateActive(t) |
| 247 | |
| 248 | c := client.ServiceClient() |
| 249 | err := nodes.ChangeProvisionState(c, "1234asdf", nodes.ProvisionStateOpts{ |
| 250 | Target: nodes.TargetActive, |
| 251 | ConfigDrive: "http://127.0.0.1/images/test-node-config-drive.iso.gz", |
| 252 | }).ExtractErr() |
| 253 | |
| 254 | th.AssertNoErr(t, err) |
| 255 | } |
| 256 | |
| 257 | func TestHandleNodeChangeProvisionStateConfigDrive(t *testing.T) { |
| 258 | th.SetupHTTP() |
| 259 | defer th.TeardownHTTP() |
| 260 | |
| 261 | HandleNodeChangeProvisionStateConfigDrive(t) |
| 262 | |
| 263 | c := client.ServiceClient() |
| 264 | |
| 265 | err := nodes.ChangeProvisionState(c, "1234asdf", nodes.ProvisionStateOpts{ |
| 266 | Target: nodes.TargetActive, |
| 267 | ConfigDrive: ConfigDriveMap, |
| 268 | }).ExtractErr() |
| 269 | |
| 270 | th.AssertNoErr(t, err) |
| 271 | } |
| 272 | |
| 273 | func TestNodeChangeProvisionStateClean(t *testing.T) { |
| 274 | th.SetupHTTP() |
| 275 | defer th.TeardownHTTP() |
| 276 | HandleNodeChangeProvisionStateClean(t) |
| 277 | |
| 278 | c := client.ServiceClient() |
| 279 | err := nodes.ChangeProvisionState(c, "1234asdf", nodes.ProvisionStateOpts{ |
| 280 | Target: nodes.TargetClean, |
| 281 | CleanSteps: []nodes.CleanStep{ |
| 282 | { |
| 283 | Interface: "deploy", |
| 284 | Step: "upgrade_firmware", |
| 285 | Args: map[string]interface{}{ |
| 286 | "force": "True", |
| 287 | }, |
| 288 | }, |
| 289 | }, |
| 290 | }).ExtractErr() |
| 291 | |
| 292 | th.AssertNoErr(t, err) |
| 293 | } |
| 294 | |
| 295 | func TestNodeChangeProvisionStateCleanWithConflict(t *testing.T) { |
| 296 | th.SetupHTTP() |
| 297 | defer th.TeardownHTTP() |
| 298 | HandleNodeChangeProvisionStateCleanWithConflict(t) |
| 299 | |
| 300 | c := client.ServiceClient() |
| 301 | err := nodes.ChangeProvisionState(c, "1234asdf", nodes.ProvisionStateOpts{ |
| 302 | Target: nodes.TargetClean, |
| 303 | CleanSteps: []nodes.CleanStep{ |
| 304 | { |
| 305 | Interface: "deploy", |
| 306 | Step: "upgrade_firmware", |
| 307 | Args: map[string]interface{}{ |
| 308 | "force": "True", |
| 309 | }, |
| 310 | }, |
| 311 | }, |
| 312 | }).ExtractErr() |
| 313 | |
| 314 | if _, ok := err.(gophercloud.ErrDefault409); !ok { |
| 315 | t.Fatal("ErrDefault409 was expected to occur") |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | func TestCleanStepRequiresInterface(t *testing.T) { |
| 320 | c := client.ServiceClient() |
| 321 | err := nodes.ChangeProvisionState(c, "1234asdf", nodes.ProvisionStateOpts{ |
| 322 | Target: nodes.TargetClean, |
| 323 | CleanSteps: []nodes.CleanStep{ |
| 324 | { |
| 325 | Step: "upgrade_firmware", |
| 326 | Args: map[string]interface{}{ |
| 327 | "force": "True", |
| 328 | }, |
| 329 | }, |
| 330 | }, |
| 331 | }).ExtractErr() |
| 332 | |
| 333 | if _, ok := err.(gophercloud.ErrMissingInput); !ok { |
| 334 | t.Fatal("ErrMissingInput was expected to occur") |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | func TestCleanStepRequiresStep(t *testing.T) { |
| 339 | c := client.ServiceClient() |
| 340 | err := nodes.ChangeProvisionState(c, "1234asdf", nodes.ProvisionStateOpts{ |
| 341 | Target: nodes.TargetClean, |
| 342 | CleanSteps: []nodes.CleanStep{ |
| 343 | { |
| 344 | Interface: "deploy", |
| 345 | Args: map[string]interface{}{ |
| 346 | "force": "True", |
| 347 | }, |
| 348 | }, |
| 349 | }, |
| 350 | }).ExtractErr() |
| 351 | |
| 352 | if _, ok := err.(gophercloud.ErrMissingInput); !ok { |
| 353 | t.Fatal("ErrMissingInput was expected to occur") |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | func TestChangePowerState(t *testing.T) { |
| 358 | th.SetupHTTP() |
| 359 | defer th.TeardownHTTP() |
| 360 | HandleChangePowerStateSuccessfully(t) |
| 361 | |
| 362 | opts := nodes.PowerStateOpts{ |
| 363 | Target: nodes.PowerOn, |
| 364 | Timeout: 100, |
| 365 | } |
| 366 | |
| 367 | c := client.ServiceClient() |
| 368 | err := nodes.ChangePowerState(c, "1234asdf", opts).ExtractErr() |
| 369 | th.AssertNoErr(t, err) |
| 370 | } |
| 371 | |
| 372 | func TestChangePowerStateWithConflict(t *testing.T) { |
| 373 | th.SetupHTTP() |
| 374 | defer th.TeardownHTTP() |
| 375 | HandleChangePowerStateWithConflict(t) |
| 376 | |
| 377 | opts := nodes.PowerStateOpts{ |
| 378 | Target: nodes.PowerOn, |
| 379 | Timeout: 100, |
| 380 | } |
| 381 | |
| 382 | c := client.ServiceClient() |
| 383 | err := nodes.ChangePowerState(c, "1234asdf", opts).ExtractErr() |
| 384 | if _, ok := err.(gophercloud.ErrDefault409); !ok { |
| 385 | t.Fatal("ErrDefault409 was expected to occur") |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | func TestSetRAIDConfig(t *testing.T) { |
| 390 | th.SetupHTTP() |
| 391 | defer th.TeardownHTTP() |
| 392 | HandleSetRAIDConfig(t) |
| 393 | |
| 394 | sizeGB := 100 |
| 395 | isRootVolume := true |
| 396 | |
| 397 | config := nodes.RAIDConfigOpts{ |
| 398 | LogicalDisks: []nodes.LogicalDisk{ |
| 399 | { |
| 400 | SizeGB: &sizeGB, |
| 401 | IsRootVolume: &isRootVolume, |
| 402 | RAIDLevel: nodes.RAID1, |
| 403 | }, |
| 404 | }, |
| 405 | } |
| 406 | |
| 407 | c := client.ServiceClient() |
| 408 | err := nodes.SetRAIDConfig(c, "1234asdf", config).ExtractErr() |
| 409 | th.AssertNoErr(t, err) |
| 410 | } |
| 411 | |
| 412 | // Without specifying a size, we need to send a string: "MAX" |
| 413 | func TestSetRAIDConfigMaxSize(t *testing.T) { |
| 414 | th.SetupHTTP() |
| 415 | defer th.TeardownHTTP() |
| 416 | HandleSetRAIDConfigMaxSize(t) |
| 417 | |
| 418 | isRootVolume := true |
| 419 | |
| 420 | config := nodes.RAIDConfigOpts{ |
| 421 | LogicalDisks: []nodes.LogicalDisk{ |
| 422 | { |
| 423 | IsRootVolume: &isRootVolume, |
| 424 | RAIDLevel: nodes.RAID1, |
| 425 | }, |
| 426 | }, |
| 427 | } |
| 428 | |
| 429 | c := client.ServiceClient() |
| 430 | err := nodes.SetRAIDConfig(c, "1234asdf", config).ExtractErr() |
| 431 | th.AssertNoErr(t, err) |
| 432 | } |