| 
									
										
										
										
											2017-09-26 17:37:26 +01:00
										 |  |  | from __future__ import unicode_literals | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | import time | 
					
						
							|  |  |  | import datetime | 
					
						
							| 
									
										
										
										
											2017-09-26 17:37:26 +01:00
										 |  |  | import boto3 | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  | from botocore.exceptions import ClientError | 
					
						
							| 
									
										
										
										
											2017-09-26 17:37:26 +01:00
										 |  |  | import sure  # noqa | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | from moto import mock_batch, mock_iam, mock_ec2, mock_ecs, mock_logs | 
					
						
							|  |  |  | import functools | 
					
						
							|  |  |  | import nose | 
					
						
							| 
									
										
										
										
											2017-09-26 17:37:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | def expected_failure(test): | 
					
						
							|  |  |  |     @functools.wraps(test) | 
					
						
							|  |  |  |     def inner(*args, **kwargs): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             test(*args, **kwargs) | 
					
						
							|  |  |  |         except Exception as err: | 
					
						
							|  |  |  |             raise nose.SkipTest | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     return inner | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | DEFAULT_REGION = "eu-central-1" | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _get_clients(): | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     return ( | 
					
						
							|  |  |  |         boto3.client("ec2", region_name=DEFAULT_REGION), | 
					
						
							|  |  |  |         boto3.client("iam", region_name=DEFAULT_REGION), | 
					
						
							|  |  |  |         boto3.client("ecs", region_name=DEFAULT_REGION), | 
					
						
							|  |  |  |         boto3.client("logs", region_name=DEFAULT_REGION), | 
					
						
							|  |  |  |         boto3.client("batch", region_name=DEFAULT_REGION), | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _setup(ec2_client, iam_client): | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     Do prerequisite setup | 
					
						
							|  |  |  |     :return: VPC ID, Subnet ID, Security group ID, IAM Role ARN | 
					
						
							|  |  |  |     :rtype: tuple | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = ec2_client.create_vpc(CidrBlock="172.30.0.0/24") | 
					
						
							|  |  |  |     vpc_id = resp["Vpc"]["VpcId"] | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  |     resp = ec2_client.create_subnet( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         AvailabilityZone="eu-central-1a", CidrBlock="172.30.0.0/25", VpcId=vpc_id | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     subnet_id = resp["Subnet"]["SubnetId"] | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  |     resp = ec2_client.create_security_group( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         Description="test_sg_desc", GroupName="test_sg", VpcId=vpc_id | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     sg_id = resp["GroupId"] | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = iam_client.create_role( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         RoleName="TestRole", AssumeRolePolicyDocument="some_policy" | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     iam_arn = resp["Role"]["Arn"] | 
					
						
							| 
									
										
										
										
											2019-12-13 17:52:37 +00:00
										 |  |  |     iam_client.create_instance_profile(InstanceProfileName="TestRole") | 
					
						
							|  |  |  |     iam_client.add_role_to_instance_profile( | 
					
						
							|  |  |  |         InstanceProfileName="TestRole", RoleName="TestRole" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return vpc_id, subnet_id, sg_id, iam_arn | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Yes, yes it talks to all the things | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  | @mock_ecs | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  | @mock_iam | 
					
						
							| 
									
										
										
										
											2017-09-26 17:37:26 +01:00
										 |  |  | @mock_batch | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  | def test_create_managed_compute_environment(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  |     resp = batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="MANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  |         computeResources={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "type": "EC2", | 
					
						
							|  |  |  |             "minvCpus": 5, | 
					
						
							|  |  |  |             "maxvCpus": 10, | 
					
						
							|  |  |  |             "desiredvCpus": 5, | 
					
						
							|  |  |  |             "instanceTypes": ["t2.small", "t2.medium"], | 
					
						
							|  |  |  |             "imageId": "some_image_id", | 
					
						
							|  |  |  |             "subnets": [subnet_id], | 
					
						
							|  |  |  |             "securityGroupIds": [sg_id], | 
					
						
							|  |  |  |             "ec2KeyPair": "string", | 
					
						
							| 
									
										
										
										
											2019-12-13 17:52:37 +00:00
										 |  |  |             "instanceRole": iam_arn.replace("role", "instance-profile"), | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "tags": {"string": "string"}, | 
					
						
							|  |  |  |             "bidPercentage": 123, | 
					
						
							|  |  |  |             "spotIamFleetRole": "string", | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("computeEnvironmentArn") | 
					
						
							|  |  |  |     resp["computeEnvironmentName"].should.equal(compute_name) | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  |     # Given a t2.medium is 2 vcpu and t2.small is 1, therefore 2 mediums and 1 small should be created | 
					
						
							|  |  |  |     resp = ec2_client.describe_instances() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("Reservations") | 
					
						
							|  |  |  |     len(resp["Reservations"]).should.equal(3) | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Should have created 1 ECS cluster | 
					
						
							|  |  |  |     resp = ecs_client.list_clusters() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("clusterArns") | 
					
						
							|  |  |  |     len(resp["clusterArns"]).should.equal(1) | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_create_unmanaged_compute_environment(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  |     resp = batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("computeEnvironmentArn") | 
					
						
							|  |  |  |     resp["computeEnvironmentName"].should.equal(compute_name) | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Its unmanaged so no instances should be created | 
					
						
							|  |  |  |     resp = ec2_client.describe_instances() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("Reservations") | 
					
						
							|  |  |  |     len(resp["Reservations"]).should.equal(0) | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Should have created 1 ECS cluster | 
					
						
							|  |  |  |     resp = ecs_client.list_clusters() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("clusterArns") | 
					
						
							|  |  |  |     len(resp["clusterArns"]).should.equal(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-26 22:22:59 +01:00
										 |  |  | # TODO create 1000s of tests to test complex option combinations of create environment | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_describe_compute_environment(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  |     batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_compute_environments() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     len(resp["computeEnvironments"]).should.equal(1) | 
					
						
							|  |  |  |     resp["computeEnvironments"][0]["computeEnvironmentName"].should.equal(compute_name) | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Test filtering | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = batch_client.describe_compute_environments(computeEnvironments=["test1"]) | 
					
						
							|  |  |  |     len(resp["computeEnvironments"]).should.equal(0) | 
					
						
							| 
									
										
										
										
											2017-09-29 23:29:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_delete_unmanaged_compute_environment(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |     batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     batch_client.delete_compute_environment(computeEnvironment=compute_name) | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_compute_environments() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     len(resp["computeEnvironments"]).should.equal(0) | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = ecs_client.list_clusters() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     len(resp.get("clusterArns", [])).should.equal(0) | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_delete_managed_compute_environment(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |     batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="MANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |         computeResources={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "type": "EC2", | 
					
						
							|  |  |  |             "minvCpus": 5, | 
					
						
							|  |  |  |             "maxvCpus": 10, | 
					
						
							|  |  |  |             "desiredvCpus": 5, | 
					
						
							|  |  |  |             "instanceTypes": ["t2.small", "t2.medium"], | 
					
						
							|  |  |  |             "imageId": "some_image_id", | 
					
						
							|  |  |  |             "subnets": [subnet_id], | 
					
						
							|  |  |  |             "securityGroupIds": [sg_id], | 
					
						
							|  |  |  |             "ec2KeyPair": "string", | 
					
						
							| 
									
										
										
										
											2019-12-13 17:52:37 +00:00
										 |  |  |             "instanceRole": iam_arn.replace("role", "instance-profile"), | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "tags": {"string": "string"}, | 
					
						
							|  |  |  |             "bidPercentage": 123, | 
					
						
							|  |  |  |             "spotIamFleetRole": "string", | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     batch_client.delete_compute_environment(computeEnvironment=compute_name) | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_compute_environments() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     len(resp["computeEnvironments"]).should.equal(0) | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = ec2_client.describe_instances() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("Reservations") | 
					
						
							|  |  |  |     len(resp["Reservations"]).should.equal(3) | 
					
						
							|  |  |  |     for reservation in resp["Reservations"]: | 
					
						
							|  |  |  |         reservation["Instances"][0]["State"]["Name"].should.equal("terminated") | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = ecs_client.list_clusters() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     len(resp.get("clusterArns", [])).should.equal(0) | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_update_unmanaged_compute_environment_state(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |     batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     batch_client.update_compute_environment( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         computeEnvironment=compute_name, state="DISABLED" | 
					
						
							| 
									
										
										
										
											2017-10-03 22:35:30 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_compute_environments() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     len(resp["computeEnvironments"]).should.equal(1) | 
					
						
							|  |  |  |     resp["computeEnvironments"][0]["state"].should.equal("DISABLED") | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_create_job_queue(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  |     resp = batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["computeEnvironmentArn"] | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobQueueName="test_job_queue", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  |         priority=123, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}], | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("jobQueueArn") | 
					
						
							|  |  |  |     resp.should.contain("jobQueueName") | 
					
						
							|  |  |  |     queue_arn = resp["jobQueueArn"] | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_job_queues() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("jobQueues") | 
					
						
							|  |  |  |     len(resp["jobQueues"]).should.equal(1) | 
					
						
							|  |  |  |     resp["jobQueues"][0]["jobQueueArn"].should.equal(queue_arn) | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = batch_client.describe_job_queues(jobQueues=["test_invalid_queue"]) | 
					
						
							|  |  |  |     resp.should.contain("jobQueues") | 
					
						
							|  |  |  |     len(resp["jobQueues"]).should.equal(0) | 
					
						
							| 
									
										
										
										
											2017-10-03 23:28:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  |     # Create job queue which already exists | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         resp = batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             jobQueueName="test_job_queue", | 
					
						
							|  |  |  |             state="ENABLED", | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  |             priority=123, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}], | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     except ClientError as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         err.response["Error"]["Code"].should.equal("ClientException") | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create job queue with incorrect state | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         resp = batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             jobQueueName="test_job_queue2", | 
					
						
							|  |  |  |             state="JUNK", | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  |             priority=123, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}], | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     except ClientError as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         err.response["Error"]["Code"].should.equal("ClientException") | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # Create job queue with no compute env | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         resp = batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             jobQueueName="test_job_queue3", | 
					
						
							|  |  |  |             state="JUNK", | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  |             priority=123, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             computeEnvironmentOrder=[], | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     except ClientError as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         err.response["Error"]["Code"].should.equal("ClientException") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_job_queue_bad_arn(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  |     resp = batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["computeEnvironmentArn"] | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try: | 
					
						
							|  |  |  |         batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             jobQueueName="test_job_queue", | 
					
						
							|  |  |  |             state="ENABLED", | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  |             priority=123, | 
					
						
							|  |  |  |             computeEnvironmentOrder=[ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |                 {"order": 123, "computeEnvironment": arn + "LALALA"} | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2017-10-03 23:21:06 +01:00
										 |  |  |         ) | 
					
						
							|  |  |  |     except ClientError as err: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         err.response["Error"]["Code"].should.equal("ClientException") | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_update_job_queue(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  |     resp = batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["computeEnvironmentArn"] | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobQueueName="test_job_queue", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  |         priority=123, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}], | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     queue_arn = resp["jobQueueArn"] | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     batch_client.update_job_queue(jobQueue=queue_arn, priority=5) | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_job_queues() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("jobQueues") | 
					
						
							|  |  |  |     len(resp["jobQueues"]).should.equal(1) | 
					
						
							|  |  |  |     resp["jobQueues"][0]["priority"].should.equal(5) | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     batch_client.update_job_queue(jobQueue="test_job_queue", priority=5) | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_job_queues() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("jobQueues") | 
					
						
							|  |  |  |     len(resp["jobQueues"]).should.equal(1) | 
					
						
							|  |  |  |     resp["jobQueues"][0]["priority"].should.equal(5) | 
					
						
							| 
									
										
										
										
											2019-02-17 09:25:35 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_update_job_queue(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  |     resp = batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["computeEnvironmentArn"] | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobQueueName="test_job_queue", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  |         priority=123, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}], | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     queue_arn = resp["jobQueueArn"] | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     batch_client.delete_job_queue(jobQueue=queue_arn) | 
					
						
							| 
									
										
										
										
											2017-10-04 18:52:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_job_queues() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("jobQueues") | 
					
						
							|  |  |  |     len(resp["jobQueues"]).should.equal(0) | 
					
						
							| 
									
										
										
										
											2017-10-04 20:17:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_register_task_definition(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-04 20:17:29 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2017-10-04 20:17:29 +01:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 128, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-04 20:17:29 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp.should.contain("jobDefinitionArn") | 
					
						
							|  |  |  |     resp.should.contain("jobDefinitionName") | 
					
						
							|  |  |  |     resp.should.contain("revision") | 
					
						
							| 
									
										
										
										
											2017-10-04 20:17:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assert resp["jobDefinitionArn"].endswith( | 
					
						
							|  |  |  |         "{0}:{1}".format(resp["jobDefinitionName"], resp["revision"]) | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_reregister_task_definition(): | 
					
						
							|  |  |  |     # Reregistering task with the same name bumps the revision number | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp1 = batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 128, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp1.should.contain("jobDefinitionArn") | 
					
						
							|  |  |  |     resp1.should.contain("jobDefinitionName") | 
					
						
							|  |  |  |     resp1.should.contain("revision") | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     assert resp1["jobDefinitionArn"].endswith( | 
					
						
							|  |  |  |         "{0}:{1}".format(resp1["jobDefinitionName"], resp1["revision"]) | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     resp1["revision"].should.equal(1) | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp2 = batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 68, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp2["revision"].should.equal(2) | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp2["jobDefinitionArn"].should_not.equal(resp1["jobDefinitionArn"]) | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-10 14:24:00 -04:00
										 |  |  |     resp3 = batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2019-09-10 14:24:00 -04:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 42, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-09-10 14:24:00 -04:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp3["revision"].should.equal(3) | 
					
						
							| 
									
										
										
										
											2019-09-10 14:24:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp3["jobDefinitionArn"].should_not.equal(resp1["jobDefinitionArn"]) | 
					
						
							|  |  |  |     resp3["jobDefinitionArn"].should_not.equal(resp2["jobDefinitionArn"]) | 
					
						
							| 
									
										
										
										
											2019-09-10 14:24:00 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp4 = batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2019-09-10 14:24:00 -04:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 41, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-09-10 14:24:00 -04:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp4["revision"].should.equal(4) | 
					
						
							| 
									
										
										
										
											2019-09-10 14:24:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp4["jobDefinitionArn"].should_not.equal(resp1["jobDefinitionArn"]) | 
					
						
							|  |  |  |     resp4["jobDefinitionArn"].should_not.equal(resp2["jobDefinitionArn"]) | 
					
						
							|  |  |  |     resp4["jobDefinitionArn"].should_not.equal(resp3["jobDefinitionArn"]) | 
					
						
							| 
									
										
										
										
											2019-09-10 14:24:00 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_delete_task_definition(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 128, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     batch_client.deregister_job_definition(jobDefinition=resp["jobDefinitionArn"]) | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_job_definitions() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     len(resp["jobDefinitions"]).should.equal(0) | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_describe_task_definition(): | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 128, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 64, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="test1", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 64, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = batch_client.describe_job_definitions(jobDefinitionName="sleep10") | 
					
						
							|  |  |  |     len(resp["jobDefinitions"]).should.equal(2) | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_job_definitions() | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     len(resp["jobDefinitions"]).should.equal(3) | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = batch_client.describe_job_definitions(jobDefinitions=["sleep10", "test1"]) | 
					
						
							|  |  |  |     len(resp["jobDefinitions"]).should.equal(3) | 
					
						
							| 
									
										
										
										
											2017-10-05 00:00:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 05:07:29 +00:00
										 |  |  |     for job_definition in resp["jobDefinitions"]: | 
					
						
							|  |  |  |         job_definition["status"].should.equal("ACTIVE") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  | @mock_logs | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_submit_job_by_name(): | 
					
						
							|  |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							|  |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |     resp = batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["computeEnvironmentArn"] | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobQueueName="test_job_queue", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |         priority=123, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}], | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     queue_arn = resp["jobQueueArn"] | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     job_definition_name = "sleep10" | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     batch_client.register_job_definition( | 
					
						
							|  |  |  |         jobDefinitionName=job_definition_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 128, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  |     batch_client.register_job_definition( | 
					
						
							|  |  |  |         jobDefinitionName=job_definition_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 256, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |     ) | 
					
						
							|  |  |  |     resp = batch_client.register_job_definition( | 
					
						
							|  |  |  |         jobDefinitionName=job_definition_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 512, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     job_definition_arn = resp["jobDefinitionArn"] | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.submit_job( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobName="test1", jobQueue=queue_arn, jobDefinition=job_definition_name | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     job_id = resp["jobId"] | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp_jobs = batch_client.describe_jobs(jobs=[job_id]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # batch_client.terminate_job(jobId=job_id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     len(resp_jobs["jobs"]).should.equal(1) | 
					
						
							|  |  |  |     resp_jobs["jobs"][0]["jobId"].should.equal(job_id) | 
					
						
							|  |  |  |     resp_jobs["jobs"][0]["jobQueue"].should.equal(queue_arn) | 
					
						
							|  |  |  |     resp_jobs["jobs"][0]["jobDefinition"].should.equal(job_definition_arn) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-06 22:13:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | # SLOW TESTS | 
					
						
							| 
									
										
										
										
											2019-05-15 17:04:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | # @expected_failure | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | @mock_logs | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_submit_job(): | 
					
						
							|  |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							|  |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     resp = batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["computeEnvironmentArn"] | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobQueueName="test_job_queue", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |         priority=123, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}], | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     queue_arn = resp["jobQueueArn"] | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 128, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     job_def_arn = resp["jobDefinitionArn"] | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.submit_job( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobName="test1", jobQueue=queue_arn, jobDefinition=job_def_arn | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     job_id = resp["jobId"] | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     future = datetime.datetime.now() + datetime.timedelta(seconds=30) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while datetime.datetime.now() < future: | 
					
						
							|  |  |  |         resp = batch_client.describe_jobs(jobs=[job_id]) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         print( | 
					
						
							|  |  |  |             "{0}:{1} {2}".format( | 
					
						
							|  |  |  |                 resp["jobs"][0]["jobName"], | 
					
						
							|  |  |  |                 resp["jobs"][0]["jobId"], | 
					
						
							|  |  |  |                 resp["jobs"][0]["status"], | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         if resp["jobs"][0]["status"] == "FAILED": | 
					
						
							|  |  |  |             raise RuntimeError("Batch job failed") | 
					
						
							|  |  |  |         if resp["jobs"][0]["status"] == "SUCCEEDED": | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  |             break | 
					
						
							|  |  |  |         time.sleep(0.5) | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         raise RuntimeError("Batch job timed out") | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = logs_client.describe_log_streams(logGroupName="/aws/batch/job") | 
					
						
							|  |  |  |     len(resp["logStreams"]).should.equal(1) | 
					
						
							|  |  |  |     ls_name = resp["logStreams"][0]["logStreamName"] | 
					
						
							| 
									
										
										
										
											2017-10-06 01:21:29 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp = logs_client.get_log_events( | 
					
						
							|  |  |  |         logGroupName="/aws/batch/job", logStreamName=ls_name | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  |     len(resp["events"]).should.be.greater_than(5) | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @expected_failure | 
					
						
							|  |  |  | @mock_logs | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_list_jobs(): | 
					
						
							|  |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							|  |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     resp = batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["computeEnvironmentArn"] | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobQueueName="test_job_queue", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |         priority=123, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}], | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     queue_arn = resp["jobQueueArn"] | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 128, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     job_def_arn = resp["jobDefinitionArn"] | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.submit_job( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobName="test1", jobQueue=queue_arn, jobDefinition=job_def_arn | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     job_id1 = resp["jobId"] | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     resp = batch_client.submit_job( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobName="test2", jobQueue=queue_arn, jobDefinition=job_def_arn | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     job_id2 = resp["jobId"] | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     future = datetime.datetime.now() + datetime.timedelta(seconds=30) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp_finished_jobs = batch_client.list_jobs( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobQueue=queue_arn, jobStatus="SUCCEEDED" | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # Wait only as long as it takes to run the jobs | 
					
						
							|  |  |  |     while datetime.datetime.now() < future: | 
					
						
							|  |  |  |         resp = batch_client.describe_jobs(jobs=[job_id1, job_id2]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         any_failed_jobs = any([job["status"] == "FAILED" for job in resp["jobs"]]) | 
					
						
							|  |  |  |         succeeded_jobs = all([job["status"] == "SUCCEEDED" for job in resp["jobs"]]) | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if any_failed_jobs: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             raise RuntimeError("A Batch job failed") | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |         if succeeded_jobs: | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         time.sleep(0.5) | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         raise RuntimeError("Batch jobs timed out") | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp_finished_jobs2 = batch_client.list_jobs( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobQueue=queue_arn, jobStatus="SUCCEEDED" | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     len(resp_finished_jobs["jobSummaryList"]).should.equal(0) | 
					
						
							|  |  |  |     len(resp_finished_jobs2["jobSummaryList"]).should.equal(2) | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @expected_failure | 
					
						
							|  |  |  | @mock_logs | 
					
						
							|  |  |  | @mock_ec2 | 
					
						
							|  |  |  | @mock_ecs | 
					
						
							|  |  |  | @mock_iam | 
					
						
							|  |  |  | @mock_batch | 
					
						
							|  |  |  | def test_terminate_job(): | 
					
						
							|  |  |  |     ec2_client, iam_client, ecs_client, logs_client, batch_client = _get_clients() | 
					
						
							|  |  |  |     vpc_id, subnet_id, sg_id, iam_arn = _setup(ec2_client, iam_client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     compute_name = "test_compute_env" | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     resp = batch_client.create_compute_environment( | 
					
						
							|  |  |  |         computeEnvironmentName=compute_name, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         type="UNMANAGED", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							|  |  |  |         serviceRole=iam_arn, | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     arn = resp["computeEnvironmentArn"] | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.create_job_queue( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobQueueName="test_job_queue", | 
					
						
							|  |  |  |         state="ENABLED", | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |         priority=123, | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         computeEnvironmentOrder=[{"order": 123, "computeEnvironment": arn}], | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     queue_arn = resp["jobQueueArn"] | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.register_job_definition( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobDefinitionName="sleep10", | 
					
						
							|  |  |  |         type="container", | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |         containerProperties={ | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |             "image": "busybox", | 
					
						
							|  |  |  |             "vcpus": 1, | 
					
						
							|  |  |  |             "memory": 128, | 
					
						
							|  |  |  |             "command": ["sleep", "10"], | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     job_def_arn = resp["jobDefinitionArn"] | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.submit_job( | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |         jobName="test1", jobQueue=queue_arn, jobDefinition=job_def_arn | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     job_id = resp["jobId"] | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     time.sleep(2) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     batch_client.terminate_job(jobId=job_id, reason="test_terminate") | 
					
						
							| 
									
										
										
										
											2017-10-11 23:46:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     time.sleep(1) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     resp = batch_client.describe_jobs(jobs=[job_id]) | 
					
						
							| 
									
										
										
										
											2019-10-31 08:44:26 -07:00
										 |  |  |     resp["jobs"][0]["jobName"].should.equal("test1") | 
					
						
							|  |  |  |     resp["jobs"][0]["status"].should.equal("FAILED") | 
					
						
							|  |  |  |     resp["jobs"][0]["statusReason"].should.equal("test_terminate") |