|
36 | 36 | PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] |
37 | 37 | REGION = "us-central1" |
38 | 38 | CLUSTER_NAME = f"py-cc-test-{str(uuid.uuid4())}" |
39 | | -NEW_NUM_INSTANCES = 5 |
| 39 | +NEW_NUM_INSTANCES = 3 |
40 | 40 | CLUSTER = { |
41 | 41 | "project_id": PROJECT_ID, |
42 | 42 | "cluster_name": CLUSTER_NAME, |
|
47 | 47 | } |
48 | 48 |
|
49 | 49 |
|
50 | | -@pytest.fixture |
| 50 | +@pytest.fixture(scope='module') |
51 | 51 | def cluster_client(): |
52 | 52 | cluster_client = ClusterControllerClient( |
53 | 53 | client_options={"api_endpoint": "{}-dataproc.googleapis.com:443".format(REGION)} |
54 | 54 | ) |
55 | 55 | return cluster_client |
56 | 56 |
|
57 | 57 |
|
58 | | -@pytest.fixture(autouse=True) |
59 | | -def setup_teardown(cluster_client): |
60 | | - # InvalidArgument is thrown when the subnetwork is not ready |
61 | | - @backoff.on_exception(backoff.expo, (InvalidArgument), max_tries=3) |
62 | | - def setup(): |
63 | | - # Create the cluster. |
64 | | - operation = cluster_client.create_cluster( |
65 | | - request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER} |
66 | | - ) |
67 | | - operation.result() |
| 58 | +@backoff.on_exception(backoff.expo, (ServiceUnavailable, InvalidArgument), max_tries=5) |
| 59 | +def setup_cluster(cluster_client): |
| 60 | + # Create the cluster. |
| 61 | + operation = cluster_client.create_cluster( |
| 62 | + request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER} |
| 63 | + ) |
| 64 | + operation.result() |
68 | 65 |
|
69 | | - def teardown(): |
70 | | - try: |
71 | | - operation = cluster_client.delete_cluster( |
72 | | - request={ |
73 | | - "project_id": PROJECT_ID, |
74 | | - "region": REGION, |
75 | | - "cluster_name": CLUSTER_NAME, |
76 | | - } |
77 | | - ) |
78 | | - operation.result() |
79 | | - except NotFound: |
80 | | - print("Cluster already deleted") |
81 | 66 |
|
| 67 | +@backoff.on_exception(backoff.expo, ServiceUnavailable, max_tries=5) |
| 68 | +def teardown_cluster(cluster_client): |
82 | 69 | try: |
83 | | - setup() |
84 | | - yield |
85 | | - finally: |
86 | | - teardown() |
| 70 | + operation = cluster_client.delete_cluster( |
| 71 | + request={ |
| 72 | + "project_id": PROJECT_ID, |
| 73 | + "region": REGION, |
| 74 | + "cluster_name": CLUSTER_NAME, |
| 75 | + } |
| 76 | + ) |
| 77 | + operation.result() |
| 78 | + except NotFound: |
| 79 | + print("Cluster already deleted") |
87 | 80 |
|
88 | 81 |
|
89 | 82 | @backoff.on_exception( |
90 | 83 | backoff.expo, (InternalServerError, ServiceUnavailable, Cancelled), max_tries=5 |
91 | 84 | ) |
92 | 85 | def test_update_cluster(capsys, cluster_client: ClusterControllerClient): |
93 | | - # Wrapper function for client library function |
94 | | - update_cluster.update_cluster(PROJECT_ID, REGION, CLUSTER_NAME, NEW_NUM_INSTANCES) |
95 | | - new_num_cluster = cluster_client.get_cluster( |
96 | | - project_id=PROJECT_ID, region=REGION, cluster_name=CLUSTER_NAME |
97 | | - ) |
| 86 | + |
| 87 | + try: |
| 88 | + setup_cluster(cluster_client) |
| 89 | + # Wrapper function for client library function |
| 90 | + update_cluster.update_cluster(PROJECT_ID, REGION, CLUSTER_NAME, NEW_NUM_INSTANCES) |
| 91 | + new_num_cluster = cluster_client.get_cluster( |
| 92 | + project_id=PROJECT_ID, region=REGION, cluster_name=CLUSTER_NAME |
| 93 | + ) |
| 94 | + |
| 95 | + finally: |
| 96 | + teardown_cluster(cluster_client) |
98 | 97 |
|
99 | 98 | out, _ = capsys.readouterr() |
100 | 99 | assert CLUSTER_NAME in out |
|
0 commit comments