|
5 | 5 | from test.integration.helpers import ( |
6 | 6 | get_test_label, |
7 | 7 | send_request_when_resource_available, |
| 8 | + wait_for_condition, |
8 | 9 | ) |
| 10 | +from test.integration.models.database.helpers import get_db_engine_id |
9 | 11 | from typing import Optional, Set |
10 | 12 |
|
11 | 13 | import pytest |
12 | 14 | import requests |
13 | 15 | from requests.exceptions import ConnectionError, RequestException |
14 | 16 |
|
15 | | -from linode_api4 import PlacementGroupPolicy, PlacementGroupType |
16 | | -from linode_api4.linode_client import LinodeClient |
| 17 | +from linode_api4 import ( |
| 18 | + PlacementGroupPolicy, |
| 19 | + PlacementGroupType, |
| 20 | + PostgreSQLDatabase, |
| 21 | +) |
| 22 | +from linode_api4.linode_client import LinodeClient, MonitorClient |
17 | 23 | from linode_api4.objects import Region |
18 | 24 |
|
19 | 25 | ENV_TOKEN_NAME = "LINODE_TOKEN" |
@@ -521,3 +527,68 @@ def linode_for_vlan_tests(test_linode_client, e2e_test_firewall): |
521 | 527 | yield linode_instance |
522 | 528 |
|
523 | 529 | linode_instance.delete() |
| 530 | + |
| 531 | + |
| 532 | +@pytest.fixture(scope="session") |
| 533 | +def test_create_postgres_db(test_linode_client): |
| 534 | + client = test_linode_client |
| 535 | + label = get_test_label() + "-postgresqldb" |
| 536 | + region = "us-ord" |
| 537 | + engine_id = get_db_engine_id(client, "postgresql") |
| 538 | + dbtype = "g6-standard-1" |
| 539 | + |
| 540 | + db = client.database.postgresql_create( |
| 541 | + label=label, |
| 542 | + region=region, |
| 543 | + engine=engine_id, |
| 544 | + ltype=dbtype, |
| 545 | + cluster_size=None, |
| 546 | + ) |
| 547 | + |
| 548 | + def get_db_status(): |
| 549 | + return db.status == "active" |
| 550 | + |
| 551 | + # TAKES 15-30 MINUTES TO FULLY PROVISION DB |
| 552 | + wait_for_condition(60, 2000, get_db_status) |
| 553 | + |
| 554 | + yield db |
| 555 | + |
| 556 | + send_request_when_resource_available(300, db.delete) |
| 557 | + |
| 558 | + |
| 559 | +@pytest.fixture(scope="session") |
| 560 | +def get_monitor_token_for_db_entities(test_linode_client): |
| 561 | + client = test_linode_client |
| 562 | + |
| 563 | + dbs = client.database.postgresql_instances() |
| 564 | + |
| 565 | + if len(dbs) < 1: |
| 566 | + db_id = test_create_postgres_db.id |
| 567 | + else: |
| 568 | + db_id = dbs[0].id |
| 569 | + |
| 570 | + region = client.load(PostgreSQLDatabase, db_id).region |
| 571 | + dbs = client.database.instances() |
| 572 | + |
| 573 | + # only collect entity_ids in the same region |
| 574 | + entity_ids = [db.id for db in dbs if db.region == region] |
| 575 | + |
| 576 | + # create token for the particular service |
| 577 | + token = client.monitor.create_token( |
| 578 | + service_type="dbaas", entity_ids=entity_ids |
| 579 | + ) |
| 580 | + |
| 581 | + yield token, entity_ids |
| 582 | + |
| 583 | + |
| 584 | +@pytest.fixture(scope="session") |
| 585 | +def test_monitor_client(get_monitor_token_for_db_entities): |
| 586 | + api_ca_file = get_api_ca_file() |
| 587 | + token, entity_ids = get_monitor_token_for_db_entities |
| 588 | + |
| 589 | + client = MonitorClient( |
| 590 | + token.token, |
| 591 | + ca_path=api_ca_file, |
| 592 | + ) |
| 593 | + |
| 594 | + return client, entity_ids |
0 commit comments