def test_delete_compute_environment(self, boto_session): session = MagicMock(['client']) client = MagicMock([ 'describe_job_definitions', 'deregister_job_definition', 'describe_job_queues', 'update_job_queue', 'delete_job_queue', 'describe_compute_environments', 'update_compute_environment', 'delete_compute_environment' ]) session.client.return_value = client boto_session.return_value = session batch = Batch({'lambda': {'name': 'fname'}}) batch.client.client.describe_job_definitions.return_value = {} batch.client.client.describe_job_queues.side_effect = [{ 'jobQueues': [{ 'state': 'ENABLED', 'status': 'VALID' }] }, { 'jobQueues': [{ 'state': 'DISABLED', 'status': 'VALID' }] }, { 'jobQueues': [] }] batch.client.client.describe_compute_environments.side_effect = [{ 'computeEnvironments': [{ 'state': 'ENABLED', 'status': 'VALID' }] }, { 'computeEnvironments': [{ 'state': 'DISABLED', 'status': 'VALID' }] }, { 'computeEnvironments': [] }] batch.delete_compute_environment() res = {'jobQueue': 'fname', 'state': 'DISABLED'} self.assertEqual( batch.client.client.update_job_queue.call_args_list[0][1], res) res = {'jobQueue': 'fname'} self.assertEqual( batch.client.client.delete_job_queue.call_args_list[0][1], res) res = {'computeEnvironment': 'fname', 'state': 'DISABLED'} self.assertEqual( batch.client.client.update_compute_environment.call_args_list[0] [1], res) res = {'computeEnvironment': 'fname'} self.assertEqual( batch.client.client.delete_compute_environment.call_args_list[0] [1], res)
def get_aws_logs(self) -> str: """Returns Cloudwatch logs for an specific lambda function and batch job (if any).""" aws_logs = self._get_lambda_logs() batch_logs = "" if self.resources_info.get('cloudwatch').get('request_id', False): batch_jobs = Batch(self.resources_info).get_jobs_with_request_id() batch_logs = self._get_batch_job_log(batch_jobs["jobs"]) return aws_logs + batch_logs if batch_logs else aws_logs
def _delete_batch_resources(self, resources_info: Dict) -> None: batch = Batch(resources_info) if batch.exist_compute_environments(): batch.delete_compute_environment()
def _create_batch_environment(self, resources_info: Dict) -> None: mode = resources_info.get('lambda').get('execution_mode') if mode in ("batch", "lambda-batch"): Batch(resources_info).create_batch_environment()
def batch(self): batch = Batch(self.aws_properties, self.scar_properties.supervisor_version) return batch
def test_create_batch_environment(self, load_tmp_config_file, get_supervisor_binary_url, boto_session): session = MagicMock(['client']) client = MagicMock([ 'register_job_definition', 'describe_launch_templates', 'create_launch_template', 'create_compute_environment', 'describe_compute_environments', 'create_job_queue' ]) session.client.return_value = client boto_session.return_value = session load_tmp_config_file.return_value = {} get_supervisor_binary_url.return_value = "https://some.es" batch = Batch({ 'lambda': { 'name': 'fname', 'supervisor': { 'version': '1.4.2' }, 'container': { 'image': 'some/image:tag', 'environment': { 'Variables': {} } } }, 'batch': { 'service_role': 'srole', 'memory': 1024, 'vcpus': 1, 'type': 'MANAGED', 'state': 'ENABLED', 'enable_gpu': True, 'compute_resources': { 'instance_role': 'irole', 'type': 'EC2', 'min_v_cpus': 0, 'max_v_cpus': 2, 'desired_v_cpus': 1, 'instance_types': ['m1.small'], 'subnets': [], 'security_group_ids': [], 'launch_template_name': 'temp_name' }, 'environment': { 'Variables': {} }, 'multi_node_parallel': { 'enabled': False } }, 'iam': { 'account_id': 'id', 'role': 'role' } }) batch.client.client.register_job_definition.return_value = {} batch.client.client.describe_launch_templates.return_value = {} batch.client.client.create_launch_template.return_value = { 'LaunchTemplate': { 'LatestVersionNumber': '1' } } batch.client.client.create_compute_environment.return_value = {} batch.client.client.describe_compute_environments.return_value = { 'computeEnvironments': [{ 'state': 'ENABLED', 'status': 'VALID' }] } batch.client.client.create_job_queue.return_value = {} batch.create_batch_environment() func_config = b"container:\n" func_config += b" environment:\n" func_config += b" Variables: {}\n" func_config += b" image: some/image:tag\n" func_config += b"name: fname\n" func_config += b"storage_providers: {}\n" func_config += b"supervisor:\n" func_config += b" version: 1.4.2\n" func_config_64 = base64.b64encode(func_config).decode() res = { 'jobDefinitionName': 'fname', 'containerProperties': { 'image': 'some/image:tag', 'memory': 1024, 'vcpus': 1, 'command': [ '/bin/sh', '-c', 'echo $EVENT | /opt/faas-supervisor/bin/supervisor' ], 'volumes': [{ 'host': { 'sourcePath': '/opt/faas-supervisor/bin' }, 'name': 'supervisor-bin' }], 'environment': [{ 'name': 'AWS_LAMBDA_FUNCTION_NAME', 'value': 'fname' }, { 'name': 'SCRIPT', 'value': '' }, { 'name': 'FUNCTION_CONFIG', 'value': func_config_64 }], 'mountPoints': [{ 'containerPath': '/opt/faas-supervisor/bin', 'sourceVolume': 'supervisor-bin' }], 'resourceRequirements': [{ 'value': '1', 'type': 'GPU' }] }, 'type': 'container' } self.assertEqual( batch.client.client.register_job_definition.call_args_list[0][1], res) self.assertEqual( batch.client.client.create_launch_template.call_args_list[0][1] ['LaunchTemplateName'], 'temp_name') self.assertEqual( batch.client.client.create_launch_template.call_args_list[0][1] ['VersionDescription'], '1.4.2') res = { 'computeEnvironmentName': 'fname', 'serviceRole': 'srole', 'type': 'MANAGED', 'state': 'ENABLED', 'computeResources': { 'type': 'EC2', 'minvCpus': 0, 'maxvCpus': 2, 'desiredvCpus': 1, 'instanceTypes': ['m1.small'], 'subnets': [], 'securityGroupIds': [], 'instanceRole': 'irole', 'launchTemplate': { 'launchTemplateName': 'temp_name', 'version': '1' } } } self.assertEqual( batch.client.client.create_compute_environment.call_args_list[0] [1], res)
def test_init(self): batch = Batch({'lambda': {'name': 'fname'}}) self.assertEqual(type(batch.client.client).__name__, "Batch")