Skip to content

fix: Refactor dataproc update_cluster_test fixture system #9825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 32 additions & 33 deletions dataproc/snippets/update_cluster_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"]
REGION = "us-central1"
CLUSTER_NAME = f"py-cc-test-{str(uuid.uuid4())}"
NEW_NUM_INSTANCES = 5
NEW_NUM_INSTANCES = 3
CLUSTER = {
"project_id": PROJECT_ID,
"cluster_name": CLUSTER_NAME,
Expand All @@ -47,54 +47,53 @@
}


@pytest.fixture
@pytest.fixture(scope='module')
def cluster_client():
cluster_client = ClusterControllerClient(
client_options={"api_endpoint": "{}-dataproc.googleapis.com:443".format(REGION)}
)
return cluster_client


@pytest.fixture(autouse=True)
def setup_teardown(cluster_client):
# InvalidArgument is thrown when the subnetwork is not ready
@backoff.on_exception(backoff.expo, (InvalidArgument), max_tries=3)
def setup():
# Create the cluster.
operation = cluster_client.create_cluster(
request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER}
)
operation.result()
@backoff.on_exception(backoff.expo, (ServiceUnavailable, InvalidArgument), max_tries=5)
def setup_cluster(cluster_client):
# Create the cluster.
operation = cluster_client.create_cluster(
request={"project_id": PROJECT_ID, "region": REGION, "cluster": CLUSTER}
)
operation.result()

def teardown():
try:
operation = cluster_client.delete_cluster(
request={
"project_id": PROJECT_ID,
"region": REGION,
"cluster_name": CLUSTER_NAME,
}
)
operation.result()
except NotFound:
print("Cluster already deleted")

@backoff.on_exception(backoff.expo, ServiceUnavailable, max_tries=5)
def teardown_cluster(cluster_client):
try:
setup()
yield
finally:
teardown()
operation = cluster_client.delete_cluster(
request={
"project_id": PROJECT_ID,
"region": REGION,
"cluster_name": CLUSTER_NAME,
}
)
operation.result()
except NotFound:
print("Cluster already deleted")


@backoff.on_exception(
backoff.expo, (InternalServerError, ServiceUnavailable, Cancelled), max_tries=5
)
def test_update_cluster(capsys, cluster_client: ClusterControllerClient):
# Wrapper function for client library function
update_cluster.update_cluster(PROJECT_ID, REGION, CLUSTER_NAME, NEW_NUM_INSTANCES)
new_num_cluster = cluster_client.get_cluster(
project_id=PROJECT_ID, region=REGION, cluster_name=CLUSTER_NAME
)

try:
setup_cluster(cluster_client)
# Wrapper function for client library function
update_cluster.update_cluster(PROJECT_ID, REGION, CLUSTER_NAME, NEW_NUM_INSTANCES)
new_num_cluster = cluster_client.get_cluster(
project_id=PROJECT_ID, region=REGION, cluster_name=CLUSTER_NAME
)

finally:
teardown_cluster(cluster_client)

out, _ = capsys.readouterr()
assert CLUSTER_NAME in out
Expand Down