moto/tests/test_batch/test_batch_job_queue.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

213 lines
6.4 KiB
Python
Raw Normal View History

from uuid import uuid4
import boto3
2021-08-04 12:40:10 +00:00
import pytest
from botocore.exceptions import ClientError
2024-01-07 12:03:33 +00:00
from moto import mock_aws
from . import _get_clients, _setup
2021-08-04 12:40:10 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-04 12:40:10 +00:00
def test_create_job_queue():
2021-10-18 19:44:29 +00:00
ec2_client, iam_client, _, _, batch_client = _get_clients()
_, _, _, iam_arn = _setup(ec2_client, iam_client)
2021-08-04 12:40:10 +00:00
2021-10-05 17:11:07 +00:00
compute_name = str(uuid4())
2021-08-04 12:40:10 +00:00
resp = batch_client.create_compute_environment(
computeEnvironmentName=compute_name,
type="UNMANAGED",
state="ENABLED",
serviceRole=iam_arn,
)
arn = resp["computeEnvironmentArn"]
2021-10-05 17:11:07 +00:00
jq_name = str(uuid4())[0:6]
2021-08-04 12:40:10 +00:00
resp = batch_client.create_job_queue(
2021-10-05 17:11:07 +00:00
jobQueueName=jq_name,
2021-08-04 12:40:10 +00:00
state="ENABLED",
priority=123,
computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}],
schedulingPolicyArn="policy_arn",
2021-08-04 12:40:10 +00:00
)
assert "jobQueueArn" in resp
assert "jobQueueName" in resp
2021-08-04 12:40:10 +00:00
queue_arn = resp["jobQueueArn"]
2021-10-05 17:11:07 +00:00
all_queues = batch_client.describe_job_queues()["jobQueues"]
our_queues = [q for q in all_queues if q["jobQueueName"] == jq_name]
assert len(our_queues) == 1
assert our_queues[0]["jobQueueArn"] == queue_arn
assert our_queues[0]["schedulingPolicyArn"] == "policy_arn"
2021-08-04 12:40:10 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-04 12:40:10 +00:00
def test_describe_job_queue_unknown_value():
batch_client = boto3.client("batch", "us-east-1")
2021-08-04 12:40:10 +00:00
resp = batch_client.describe_job_queues(jobQueues=["test_invalid_queue"])
assert len(resp["jobQueues"]) == 0
2021-08-04 12:40:10 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-04 12:40:10 +00:00
def test_create_job_queue_twice():
2021-10-18 19:44:29 +00:00
ec2_client, iam_client, _, _, batch_client = _get_clients()
_, _, _, iam_arn = _setup(ec2_client, iam_client)
2021-08-04 12:40:10 +00:00
2021-10-05 17:11:07 +00:00
compute_name = str(uuid4())
2021-08-04 12:40:10 +00:00
resp = batch_client.create_compute_environment(
computeEnvironmentName=compute_name,
type="UNMANAGED",
state="ENABLED",
serviceRole=iam_arn,
)
compute_env_arn = resp["computeEnvironmentArn"]
2021-10-05 17:11:07 +00:00
jq_name = str(uuid4())[0:6]
2021-08-04 12:40:10 +00:00
batch_client.create_job_queue(
2021-10-05 17:11:07 +00:00
jobQueueName=jq_name,
2021-08-04 12:40:10 +00:00
state="ENABLED",
priority=123,
computeEnvironmentOrder=[{"order": 123, "computeEnvironment": compute_env_arn}],
)
with pytest.raises(ClientError) as ex:
batch_client.create_job_queue(
2021-10-05 17:11:07 +00:00
jobQueueName=jq_name,
2021-08-04 12:40:10 +00:00
state="ENABLED",
priority=123,
computeEnvironmentOrder=[
{"order": 123, "computeEnvironment": compute_env_arn}
],
)
err = ex.value.response["Error"]
assert err["Code"] == "ClientException"
assert err["Message"] == f"Job queue {jq_name} already exists"
2021-08-04 12:40:10 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-04 12:40:10 +00:00
def test_create_job_queue_incorrect_state():
2021-10-18 19:44:29 +00:00
_, _, _, _, batch_client = _get_clients()
2021-08-04 12:40:10 +00:00
with pytest.raises(ClientError) as ex:
batch_client.create_job_queue(
2021-10-05 17:11:07 +00:00
jobQueueName=str(uuid4()),
2021-08-04 12:40:10 +00:00
state="JUNK",
priority=123,
computeEnvironmentOrder=[],
)
err = ex.value.response["Error"]
assert err["Code"] == "ClientException"
assert err["Message"] == "state JUNK must be one of ENABLED | DISABLED"
2021-08-04 12:40:10 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-04 12:40:10 +00:00
def test_create_job_queue_without_compute_environment():
2021-10-18 19:44:29 +00:00
_, _, _, _, batch_client = _get_clients()
2021-08-04 12:40:10 +00:00
with pytest.raises(ClientError) as ex:
batch_client.create_job_queue(
2021-10-05 17:11:07 +00:00
jobQueueName=str(uuid4()),
2021-08-04 12:40:10 +00:00
state="ENABLED",
priority=123,
computeEnvironmentOrder=[],
)
err = ex.value.response["Error"]
assert err["Code"] == "ClientException"
assert err["Message"] == "At least 1 compute environment must be provided"
2021-08-04 12:40:10 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-04 12:40:10 +00:00
def test_job_queue_bad_arn():
2021-10-18 19:44:29 +00:00
ec2_client, iam_client, _, _, batch_client = _get_clients()
_, _, _, iam_arn = _setup(ec2_client, iam_client)
2021-08-04 12:40:10 +00:00
2021-10-05 17:11:07 +00:00
compute_name = str(uuid4())
2021-08-04 12:40:10 +00:00
resp = batch_client.create_compute_environment(
computeEnvironmentName=compute_name,
type="UNMANAGED",
state="ENABLED",
serviceRole=iam_arn,
)
arn = resp["computeEnvironmentArn"]
with pytest.raises(ClientError) as ex:
batch_client.create_job_queue(
2021-10-05 17:11:07 +00:00
jobQueueName=str(uuid4()),
2021-08-04 12:40:10 +00:00
state="ENABLED",
priority=123,
computeEnvironmentOrder=[
{"order": 123, "computeEnvironment": arn + "LALALA"}
],
)
err = ex.value.response["Error"]
assert err["Code"] == "ClientException"
assert err["Message"] == "computeEnvironmentOrder is malformed"
2021-08-04 12:40:10 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-04 12:40:10 +00:00
def test_update_job_queue():
2021-10-18 19:44:29 +00:00
ec2_client, iam_client, _, _, batch_client = _get_clients()
_, _, _, iam_arn = _setup(ec2_client, iam_client)
2021-08-04 12:40:10 +00:00
2021-10-05 17:11:07 +00:00
compute_name = str(uuid4())
2021-08-04 12:40:10 +00:00
resp = batch_client.create_compute_environment(
computeEnvironmentName=compute_name,
type="UNMANAGED",
state="ENABLED",
serviceRole=iam_arn,
)
arn = resp["computeEnvironmentArn"]
2021-10-05 17:11:07 +00:00
jq_name = str(uuid4())
2021-08-04 12:40:10 +00:00
resp = batch_client.create_job_queue(
2021-10-05 17:11:07 +00:00
jobQueueName=jq_name,
2021-08-04 12:40:10 +00:00
state="ENABLED",
priority=123,
computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}],
)
queue_arn = resp["jobQueueArn"]
batch_client.update_job_queue(jobQueue=queue_arn, priority=5)
2021-10-05 17:11:07 +00:00
all_queues = batch_client.describe_job_queues()["jobQueues"]
our_queues = [q for q in all_queues if q["jobQueueName"] == jq_name]
assert our_queues[0]["priority"] == 5
2021-08-04 12:40:10 +00:00
2021-10-05 17:11:07 +00:00
batch_client.update_job_queue(jobQueue=jq_name, priority=15)
2021-08-04 12:40:10 +00:00
2021-10-05 17:11:07 +00:00
all_queues = batch_client.describe_job_queues()["jobQueues"]
our_queues = [q for q in all_queues if q["jobQueueName"] == jq_name]
assert len(our_queues) == 1
assert our_queues[0]["priority"] == 15
2021-08-04 12:40:10 +00:00
2024-01-07 12:03:33 +00:00
@mock_aws
2021-08-04 12:40:10 +00:00
def test_delete_job_queue():
2021-10-18 19:44:29 +00:00
ec2_client, iam_client, _, _, batch_client = _get_clients()
_, _, _, iam_arn = _setup(ec2_client, iam_client)
2021-08-04 12:40:10 +00:00
2021-10-05 17:11:07 +00:00
compute_name = str(uuid4())
2021-08-04 12:40:10 +00:00
resp = batch_client.create_compute_environment(
computeEnvironmentName=compute_name,
type="UNMANAGED",
state="ENABLED",
serviceRole=iam_arn,
)
arn = resp["computeEnvironmentArn"]
2021-10-05 17:11:07 +00:00
jq_name = str(uuid4())
2021-08-04 12:40:10 +00:00
resp = batch_client.create_job_queue(
2021-10-05 17:11:07 +00:00
jobQueueName=jq_name,
2021-08-04 12:40:10 +00:00
state="ENABLED",
priority=123,
computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}],
)
queue_arn = resp["jobQueueArn"]
batch_client.delete_job_queue(jobQueue=queue_arn)
2021-10-05 17:11:07 +00:00
all_queues = batch_client.describe_job_queues()["jobQueues"]
assert jq_name not in [q["jobQueueName"] for q in all_queues]