def test_we_attach_instances_to_the_load_balancer(self, mock_landlord, mock_lb): load_balancer = Mock() mock_connection = Mock() mock_landlord.Tenant = StubLandlord mock_lb.connect_to_region.return_value = mock_connection load_balancer.name = "MyLoadBalancer" instances = ['a', 'b'] loadbalancer.attach(load_balancer, instances) mock_lb.connect_to_region.assert_called_with("deploy.region", aws_access_key_id="aws.id", aws_secret_access_key="aws.secret") mock_connection.register_instances.assert_called_with("MyLoadBalancer", instances)
def test_we_dont_call_register_when_the_instaces_are_empty(self, mock_landlord, mock_lb): load_balancer = Mock() mock_connection = Mock() mock_landlord.Tenant = StubLandlord mock_lb.connect_to_region.return_value = mock_connection load_balancer.name = "MyLoadBalancer" instances = [] loadbalancer.attach(load_balancer, instances) assert not mock_lb.connect_to_region.called assert not mock_connection.register_instances.called
def deploy(job): job.set_status(JobStatus.running) project_attributes = job.get_project() new_instances = ec2.create_and_start_instances(project_attributes) if ec2.is_running(new_instances, project_attributes): if project_attributes.get('environment_tier') == EnvironmentTier.WORKER.value: old_instances = ec2.get_instances(filters={'tag:Project': project_attributes['name']}) old_instance_ids = list(set([instance.id for instance in old_instances]) - set(new_instances)) else: load_balancer = loadbalancer.get_loadbalancer(project_attributes) old_instance_ids = [instance.id for instance in load_balancer.instances] loadbalancer.attach(load_balancer, new_instances) loadbalancer.dettach(load_balancer, old_instance_ids) ec2.terminate(old_instance_ids) job.set_status(JobStatus.done) else: ec2.terminate(new_instances) job.set_status(JobStatus.failed)