|
| 1 | +import ipaddress |
1 | 2 | import re |
2 | 3 | from test.integration.conftest import ( |
3 | 4 | get_api_ca_file, |
@@ -272,3 +273,194 @@ def test_nodebalancer_types(test_linode_client): |
272 | 273 | isinstance(region_price.monthly, (float, int)) |
273 | 274 | and region_price.monthly >= 0 |
274 | 275 | ) |
| 276 | + |
| 277 | + |
| 278 | +def test_nb_with_backend_only(test_linode_client, create_vpc_with_subnet): |
| 279 | + client = test_linode_client |
| 280 | + vpc = create_vpc_with_subnet[0].id |
| 281 | + vpc_region = create_vpc_with_subnet[0].region |
| 282 | + subnet = create_vpc_with_subnet[1].id |
| 283 | + label = get_test_label(8) |
| 284 | + |
| 285 | + nb = client.nodebalancer_create( |
| 286 | + region=vpc_region, label=label, vpcs=[{"vpc_id": vpc,"subnet_id": subnet}] |
| 287 | + ) |
| 288 | + |
| 289 | + assert isinstance(ipaddress.ip_address(nb.ipv4.address), ipaddress.IPv4Address) |
| 290 | + assert isinstance(ipaddress.ip_address(nb.ipv6), ipaddress.IPv6Address) |
| 291 | + assert nb.frontend_address_type == 'public' |
| 292 | + assert nb.frontend_vpc_subnet_id is None |
| 293 | + |
| 294 | + nb_get = NodeBalancer(client, nb.id) |
| 295 | + nb_vpcs = nb_get.vpcs() |
| 296 | + |
| 297 | + assert len(nb_vpcs) == 1 |
| 298 | + assert nb_vpcs[0].purpose == 'backend' |
| 299 | + |
| 300 | + nb_vpc = nb_get.vpc(nb_vpcs[0].id) |
| 301 | + |
| 302 | + assert nb_vpc.purpose == 'backend' |
| 303 | + |
| 304 | + # TODO: There is no API implementation yet for /backend_vpcs and /frontend_vpcs |
| 305 | + # nb_backend_vpcs = nb_get.backend_vpcs() |
| 306 | + # assert len(nb_backend_vpcs) == 1 |
| 307 | + # assert nb_backend_vpcs[0].purpose == 'backend' |
| 308 | + # |
| 309 | + # nb_frontend_vpcs = nb_get.frontend_vpcs() |
| 310 | + # assert len(nb_frontend_vpcs) == 0 |
| 311 | + |
| 312 | + nb.delete() |
| 313 | + |
| 314 | + |
| 315 | +def test_nb_with_frontend_ipv4_only_in_single_stack_vpc(test_linode_client, create_vpc_with_subnet_ipv4): |
| 316 | + client = test_linode_client |
| 317 | + vpc_region = create_vpc_with_subnet_ipv4[0].region |
| 318 | + subnet = create_vpc_with_subnet_ipv4[1].id |
| 319 | + label = get_test_label(8) |
| 320 | + ipv4_address = "10.0.0.2" # first available address |
| 321 | + |
| 322 | + nb = client.nodebalancer_create( |
| 323 | + region=vpc_region, |
| 324 | + label=label, |
| 325 | + frontend_vpcs=[{"subnet_id": subnet, "ipv4_range": "10.0.0.0/24"}], |
| 326 | + type="premium" |
| 327 | + ) |
| 328 | + assert nb.ipv4.address == ipv4_address |
| 329 | + assert nb.ipv6 is None |
| 330 | + assert nb.frontend_address_type == 'vpc' |
| 331 | + assert nb.frontend_vpc_subnet_id == subnet |
| 332 | + |
| 333 | + # TODO: There is no API implementation yet for /backend_vpcs and /frontend_vpcs |
| 334 | + # nb_frontend_vpcs = nb_get.frontend_vpcs() |
| 335 | + # assert len(nb_frontend_vpcs) == 1 |
| 336 | + # assert nb_frontend_vpcs[0].purpose == 'frontend' |
| 337 | + # |
| 338 | + # nb_backend_vpcs = nb_get.backend_vpcs() |
| 339 | + # assert len(nb_backend_vpcs) == 0 |
| 340 | + |
| 341 | + nb.delete() |
| 342 | + |
| 343 | + |
| 344 | +def test_nb_with_frontend_ipv6_only_in_single_stack_vpc(test_linode_client, create_vpc_with_subnet_ipv4): |
| 345 | + client = test_linode_client |
| 346 | + vpc_region = create_vpc_with_subnet_ipv4[0].region |
| 347 | + subnet = create_vpc_with_subnet_ipv4[1].id |
| 348 | + label = get_test_label(8) |
| 349 | + |
| 350 | + with pytest.raises(ApiError) as err: |
| 351 | + client.nodebalancer_create( |
| 352 | + region=vpc_region, |
| 353 | + label=label, |
| 354 | + frontend_vpcs=[{"subnet_id": subnet, "ipv6_range": "/62"}], |
| 355 | + type="premium" |
| 356 | + ) |
| 357 | + assert "No IPv6 subnets available in VPC" in str(err.json) |
| 358 | + |
| 359 | + |
| 360 | +def test_nb_with_frontend_and_default_type(test_linode_client, create_vpc_with_subnet): |
| 361 | + client = test_linode_client |
| 362 | + vpc_region = create_vpc_with_subnet[0].region |
| 363 | + subnet = create_vpc_with_subnet[1].id |
| 364 | + label = get_test_label(8) |
| 365 | + |
| 366 | + with pytest.raises(ApiError) as err: |
| 367 | + client.nodebalancer_create( |
| 368 | + region=vpc_region, |
| 369 | + label=label, |
| 370 | + frontend_vpcs=[{"subnet_id": subnet}], |
| 371 | + ) |
| 372 | + assert "Nodebalancer with frontend VPC IP must be premium" in str(err.json) |
| 373 | + |
| 374 | + |
| 375 | +def test_nb_with_frontend_and_premium40gb_type(test_linode_client): |
| 376 | + region = "us-iad" # premium_40gb type can be used in specific regions only |
| 377 | + client = test_linode_client |
| 378 | + |
| 379 | + vpc = client.vpcs.create( |
| 380 | + label=get_test_label(length=10), |
| 381 | + region=region, |
| 382 | + description="test description", |
| 383 | + ipv6=[{"range": "auto"}], |
| 384 | + ) |
| 385 | + |
| 386 | + subnet = vpc.subnet_create( |
| 387 | + label="test-subnet", |
| 388 | + ipv4="10.0.0.0/24", |
| 389 | + ipv6=[{"range": "auto"}], |
| 390 | + ) |
| 391 | + |
| 392 | + nb = client.nodebalancer_create( |
| 393 | + region=region, |
| 394 | + label=get_test_label(length=8), |
| 395 | + frontend_vpcs=[{"subnet_id": subnet.id}], |
| 396 | + type="premium_40gb" |
| 397 | + ) |
| 398 | + assert nb.type == 'premium_40gb' |
| 399 | + |
| 400 | + nb_get = test_linode_client.load( |
| 401 | + NodeBalancer, |
| 402 | + nb.id, |
| 403 | + ) |
| 404 | + assert nb_get.type == 'premium_40gb' |
| 405 | + |
| 406 | + nb.delete() |
| 407 | + vpc.delete() |
| 408 | + |
| 409 | + |
| 410 | +def test_nb_with_frontend_and_backend_in_different_vpcs(test_linode_client, create_vpc_with_subnet): |
| 411 | + client = test_linode_client |
| 412 | + region = create_vpc_with_subnet[0].region |
| 413 | + vpc_backend = create_vpc_with_subnet[0].id |
| 414 | + subnet_backend = create_vpc_with_subnet[1].id |
| 415 | + label = get_test_label(8) |
| 416 | + ipv4_range = "10.0.0.0/24" |
| 417 | + ipv4_address = "10.0.0.2" # first available address |
| 418 | + |
| 419 | + vpc_frontend = client.vpcs.create( |
| 420 | + label=get_test_label(length=10), |
| 421 | + region=region, |
| 422 | + description="test description", |
| 423 | + ipv6=[{"range": "auto"}], |
| 424 | + ) |
| 425 | + |
| 426 | + subnet_frontend = vpc_frontend.subnet_create( |
| 427 | + label="test-subnet", |
| 428 | + ipv4=ipv4_range, |
| 429 | + ipv6=[{"range": "auto"}], |
| 430 | + ) |
| 431 | + ipv6_range = subnet_frontend.ipv6[0].range |
| 432 | + ipv6_address = ipv6_range.split("::")[0] + ":1::1" |
| 433 | + |
| 434 | + nb = client.nodebalancer_create( |
| 435 | + region=region, |
| 436 | + label=label, |
| 437 | + vpcs=[{"vpc_id": vpc_backend,"subnet_id": subnet_backend}], |
| 438 | + frontend_vpcs=[{"subnet_id": subnet_frontend.id, "ipv4_range": ipv4_range, "ipv6_range": ipv6_range}], |
| 439 | + type="premium" |
| 440 | + ) |
| 441 | + |
| 442 | + assert nb.ipv4.address == ipv4_address |
| 443 | + assert nb.ipv6 == ipv6_address |
| 444 | + assert nb.frontend_address_type == 'vpc' |
| 445 | + assert nb.frontend_vpc_subnet_id == subnet_frontend.id |
| 446 | + |
| 447 | + nb_get = NodeBalancer(client, nb.id) |
| 448 | + nb_vpcs = nb_get.vpcs() |
| 449 | + |
| 450 | + assert len(nb_vpcs) == 2 |
| 451 | + assert nb_vpcs[0].ipv4_range == f"{ipv4_address}/32" |
| 452 | + assert nb_vpcs[0].ipv6_range == f"{ipv6_address[:-1]}/64" |
| 453 | + assert nb_vpcs[0].purpose == 'frontend' |
| 454 | + assert nb_vpcs[1].purpose == 'backend' |
| 455 | + |
| 456 | + # TODO: There is no API implementation yet for /backend_vpcs and /frontend_vpcs |
| 457 | + # nb_backend_vpcs = nb_get.backend_vpcs() |
| 458 | + # assert len(nb_backend_vpcs) == 1 |
| 459 | + # assert nb_backend_vpcs[0].purpose == 'backend' |
| 460 | + # |
| 461 | + # nb_frontend_vpcs = nb_get.frontend_vpcs() |
| 462 | + # assert len(nb_frontend_vpcs) == 1 |
| 463 | + # assert nb_frontend_vpcs[0].purpose == 'frontend' |
| 464 | + |
| 465 | + nb.delete() |
| 466 | + vpc_frontend.delete() |
0 commit comments