Techdebt: Replace sure with regular assertions in emrcontainers, emrserverless (#6665)

This commit is contained in:
kbalk 2023-08-15 04:05:53 -04:00 committed by GitHub
parent d8d5f5905e
commit 4104865bde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 58 additions and 50 deletions

View File

@ -1,17 +1,15 @@
"""Unit tests for emrcontainers-supported APIs.""" """Unit tests for emrcontainers-supported APIs."""
import re
from datetime import datetime, timezone, timedelta from datetime import datetime, timezone, timedelta
import re
from unittest.mock import patch
from unittest import SkipTest from unittest import SkipTest
import boto3 import boto3
import pytest
import sure # noqa # pylint: disable=unused-import
from botocore.exceptions import ClientError, ParamValidationError from botocore.exceptions import ClientError, ParamValidationError
import pytest
from moto import mock_emrcontainers, settings from moto import mock_emrcontainers, settings
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
from unittest.mock import patch
from moto.emrcontainers import REGION as DEFAULT_REGION from moto.emrcontainers import REGION as DEFAULT_REGION
@ -29,16 +27,14 @@ def fixture_virtual_cluster_factory(client):
cluster_state = ["RUNNING", "TERMINATING", "TERMINATED", "ARRESTED"] cluster_state = ["RUNNING", "TERMINATING", "TERMINATED", "ARRESTED"]
cluster_list = [] cluster_list = []
for i in range(len(cluster_state)): for idx, state in enumerate(cluster_state):
with patch( with patch("moto.emrcontainers.models.VIRTUAL_CLUSTER_STATUS", state):
"moto.emrcontainers.models.VIRTUAL_CLUSTER_STATUS", cluster_state[i]
):
resp = client.create_virtual_cluster( resp = client.create_virtual_cluster(
name="test-emr-virtual-cluster", name="test-emr-virtual-cluster",
containerProvider={ containerProvider={
"type": "EKS", "type": "EKS",
"id": "test-eks-cluster", "id": "test-eks-cluster",
"info": {"eksInfo": {"namespace": f"emr-container-{i}"}}, "info": {"eksInfo": {"namespace": f"emr-container-{idx}"}},
}, },
) )
@ -53,7 +49,10 @@ def fixture_job_factory(client, virtual_cluster_factory):
default_job_driver = { default_job_driver = {
"sparkSubmitJobDriver": { "sparkSubmitJobDriver": {
"entryPoint": "s3://code/pi.py", "entryPoint": "s3://code/pi.py",
"sparkSubmitParameters": "--conf spark.executor.instances=2 --conf spark.executor.memory=2G --conf spark.driver.memory=2G --conf spark.executor.cores=4", "sparkSubmitParameters": (
"--conf spark.executor.instances=2 --conf spark.executor.memory=2G "
"--conf spark.driver.memory=2G --conf spark.executor.cores=4"
),
} }
} }
default_execution_role_arn = f"arn:aws:iam::{ACCOUNT_ID}:role/iamrole-emrcontainers" default_execution_role_arn = f"arn:aws:iam::{ACCOUNT_ID}:role/iamrole-emrcontainers"
@ -70,10 +69,10 @@ def fixture_job_factory(client, virtual_cluster_factory):
] ]
job_list = [] job_list = []
for i in range(len(job_state)): for idx, state in enumerate(job_state):
with patch("moto.emrcontainers.models.JOB_STATUS", job_state[i]): with patch("moto.emrcontainers.models.JOB_STATUS", state):
resp = client.start_job_run( resp = client.start_job_run(
name=f"test_job_{i}", name=f"test_job_{idx}",
virtualClusterId=virtual_cluster_id, virtualClusterId=virtual_cluster_id,
executionRoleArn=default_execution_role_arn, executionRoleArn=default_execution_role_arn,
releaseLabel=default_release_label, releaseLabel=default_release_label,
@ -168,7 +167,10 @@ class TestDescribeVirtualCluster:
resp = self.client.describe_virtual_cluster(id=self.virtual_cluster_ids[0]) resp = self.client.describe_virtual_cluster(id=self.virtual_cluster_ids[0])
expected_resp = { expected_resp = {
"arn": f"arn:aws:emr-containers:us-east-1:{ACCOUNT_ID}:/virtualclusters/{self.virtual_cluster_ids[0]}", "arn": (
f"arn:aws:emr-containers:us-east-1:{ACCOUNT_ID}:"
f"/virtualclusters/{self.virtual_cluster_ids[0]}"
),
"containerProvider": { "containerProvider": {
"id": "test-eks-cluster", "id": "test-eks-cluster",
"info": {"eksInfo": {"namespace": "emr-container-0"}}, "info": {"eksInfo": {"namespace": "emr-container-0"}},
@ -261,7 +263,10 @@ class TestStartJobRun:
default_job_driver = { default_job_driver = {
"sparkSubmitJobDriver": { "sparkSubmitJobDriver": {
"entryPoint": "s3://code/pi.py", "entryPoint": "s3://code/pi.py",
"sparkSubmitParameters": "--conf spark.executor.instances=2 --conf spark.executor.memory=2G --conf spark.driver.memory=2G --conf spark.executor.cores=4", "sparkSubmitParameters": (
"--conf spark.executor.instances=2 --conf spark.executor.memory=2G"
"--conf spark.driver.memory=2G --conf spark.executor.cores=4"
),
} }
} }
@ -302,9 +307,9 @@ class TestStartJobRun:
assert re.match(r"[a-z,0-9]{19}", resp["id"]) assert re.match(r"[a-z,0-9]{19}", resp["id"])
assert resp["name"] == "test_job" assert resp["name"] == "test_job"
assert ( assert resp["arn"] == (
resp["arn"] f"arn:aws:emr-containers:us-east-1:{ACCOUNT_ID}:"
== f"arn:aws:emr-containers:us-east-1:{ACCOUNT_ID}:/virtualclusters/{self.virtual_cluster_id}/jobruns/{resp['id']}" f"/virtualclusters/{self.virtual_cluster_id}/jobruns/{resp['id']}"
) )
assert resp["virtualClusterId"] == self.virtual_cluster_id assert resp["virtualClusterId"] == self.virtual_cluster_id
@ -320,9 +325,9 @@ class TestStartJobRun:
assert exc.typename == "ParamValidationError" assert exc.typename == "ParamValidationError"
assert ( assert (
"Parameter validation failed:\nInvalid length for parameter executionRoleArn, value: 6, valid min length: 20" "Parameter validation failed:\nInvalid length for parameter "
in exc.value.args "executionRoleArn, value: 6, valid min length: 20"
) ) in exc.value.args
def test_invalid_virtual_cluster_id(self): def test_invalid_virtual_cluster_id(self):
with pytest.raises(ClientError) as exc: with pytest.raises(ClientError) as exc:
@ -508,7 +513,11 @@ class TestDescribeJobRun:
) )
expected = { expected = {
"arn": f"arn:aws:emr-containers:us-east-1:{ACCOUNT_ID}:/virtualclusters/{self.virtual_cluster_id}/jobruns/{self.job_list[2]}", "arn": (
f"arn:aws:emr-containers:us-east-1:{ACCOUNT_ID}"
f":/virtualclusters/{self.virtual_cluster_id}"
f"/jobruns/{self.job_list[2]}"
),
"createdAt": ( "createdAt": (
datetime.today() datetime.today()
.replace(hour=0, minute=0, second=0, microsecond=0) .replace(hour=0, minute=0, second=0, microsecond=0)

View File

@ -1,11 +1,7 @@
"""Test the different server responses."""
import json import json
import sure # noqa # pylint: disable=unused-import
import moto.server as server import moto.server as server
"""
Test the different server responses
"""
def test_list_virtual_clusters(): def test_list_virtual_clusters():
backend = server.create_backend_app("emr-containers") backend = server.create_backend_app("emr-containers")
@ -13,4 +9,4 @@ def test_list_virtual_clusters():
res = test_client.get("/virtualclusters") res = test_client.get("/virtualclusters")
json.loads(res.data).should.have.key("virtualClusters") assert "virtualClusters" in json.loads(res.data)

View File

@ -1,17 +1,17 @@
"""Unit tests for emrserverless-supported APIs.""" """Unit tests for emrserverless-supported APIs."""
import re import re
from datetime import datetime, timezone
from contextlib import contextmanager from contextlib import contextmanager
from datetime import datetime, timezone
from unittest.mock import patch
import boto3 import boto3
import pytest
import sure # noqa # pylint: disable=unused-import
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
import pytest
from moto import mock_emrserverless, settings from moto import mock_emrserverless, settings
from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID from moto.core import DEFAULT_ACCOUNT_ID as ACCOUNT_ID
from moto.emrserverless import REGION as DEFAULT_REGION from moto.emrserverless import REGION as DEFAULT_REGION
from moto.emrserverless import RELEASE_LABEL as DEFAULT_RELEASE_LABEL from moto.emrserverless import RELEASE_LABEL as DEFAULT_RELEASE_LABEL
from unittest.mock import patch
@contextmanager @contextmanager
@ -81,9 +81,9 @@ class TestCreateApplication:
assert resp["name"] == "test-emr-serverless-application" assert resp["name"] == "test-emr-serverless-application"
assert re.match(r"[a-z,0-9]{16}", resp["applicationId"]) assert re.match(r"[a-z,0-9]{16}", resp["applicationId"])
assert ( assert resp["arn"] == (
resp["arn"] f"arn:aws:emr-containers:us-east-1:{ACCOUNT_ID}"
== f"arn:aws:emr-containers:us-east-1:{ACCOUNT_ID}:/applications/{resp['applicationId']}" f":/applications/{resp['applicationId']}"
) )
@staticmethod @staticmethod
@ -114,9 +114,9 @@ class TestCreateApplication:
err = exc.value.response["Error"] err = exc.value.response["Error"]
assert err["Code"] == "ValidationException" assert err["Code"] == "ValidationException"
assert ( assert err["Message"] == (
err["Message"] "Type 'SPARK' is not supported for release label 'emr-fake' "
== "Type 'SPARK' is not supported for release label 'emr-fake' or release label does not exist" "or release label does not exist"
) )
@ -154,9 +154,10 @@ class TestDeleteApplication:
if exc: if exc:
err = exc.value.response["Error"] err = exc.value.response["Error"]
assert err["Code"] == "ValidationException" assert err["Code"] == "ValidationException"
assert ( assert err["Message"] == (
err["Message"] f"Application {self.application_ids[index]} must be in one "
== f"Application {self.application_ids[index]} must be in one of the following statuses [CREATED, STOPPED]. Current status: {status}" "of the following statuses [CREATED, STOPPED]. Current "
f"status: {status}"
) )
else: else:
assert resp is not None assert resp is not None
@ -289,7 +290,10 @@ class TestListApplication:
expected_resp = { expected_resp = {
"id": self.application_ids[0], "id": self.application_ids[0],
"name": "test-emr-serverless-application-STARTED", "name": "test-emr-serverless-application-STARTED",
"arn": f"arn:aws:emr-containers:us-east-1:123456789012:/applications/{self.application_ids[0]}", "arn": (
f"arn:aws:emr-containers:us-east-1:123456789012"
f":/applications/{self.application_ids[0]}"
),
"releaseLabel": "emr-6.6.0", "releaseLabel": "emr-6.6.0",
"type": "Spark", "type": "Spark",
"state": "STARTED", "state": "STARTED",
@ -451,9 +455,10 @@ class TestUpdateApplication:
if exc: if exc:
err = exc.value.response["Error"] err = exc.value.response["Error"]
assert err["Code"] == "ValidationException" assert err["Code"] == "ValidationException"
assert ( assert err["Message"] == (
err["Message"] f"Application {self.application_ids[index]} must be in one "
== f"Application {self.application_ids[index]} must be in one of the following statuses [CREATED, STOPPED]. Current status: {status}" "of the following statuses [CREATED, STOPPED]. Current "
f"status: {status}"
) )
else: else:
assert resp is not None assert resp is not None

View File

@ -1,6 +1,4 @@
"""Test different server responses.""" """Test different server responses."""
import sure # noqa # pylint: disable=unused-import
import moto.server as server import moto.server as server
@ -9,5 +7,5 @@ def test_emrserverless_list():
test_client = backend.test_client() test_client = backend.test_client()
resp = test_client.get("/applications") resp = test_client.get("/applications")
resp.status_code.should.equal(200) assert resp.status_code == 200
str(resp.data).should.contain("applications") assert "applications" in str(resp.data)