def test_we_get_an_existent_lb(self, mock_landlord, mock_lb): project = {'name': 'MyProject', 'version': 'v34'} mock_connection = Mock() mock_connection.get_all_load_balancers.return_value = [1, 2] mock_lb.connect_to_region.return_value = mock_connection mock_landlord.Tenant = StubLandlord result = loadbalancer.get_loadbalancer(project) mock_lb.connect_to_region.assert_called_with("deploy.region", aws_access_key_id="aws.id", aws_secret_access_key="aws.secret") mock_connection.get_all_load_balancers.assert_called_with(load_balancer_names=['environment-lb-MyProject']) assert result == 1
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)
def test_we_create_a_load_balancer_when_we_raised_an_exception(self, mock_dns, mock_landlord, mock_lb): project = {'name': 'MyProject', 'version': 'v34'} mock_connection = Mock() mock_connection.get_all_load_balancers.side_effect = BotoServerError('oops', 'Boom!') returned_load_balancer = Mock() returned_load_balancer.dns_name = 'dns_name' mock_connection.create_load_balancer.return_value = returned_load_balancer mock_lb.connect_to_region.return_value = mock_connection mock_landlord.Tenant = StubLandlord result = loadbalancer.get_loadbalancer(project) mock_lb.connect_to_region.assert_called_with("deploy.region", aws_access_key_id="aws.id", aws_secret_access_key="aws.secret") mock_connection.get_all_load_balancers.assert_called_with(load_balancer_names=['environment-lb-MyProject']) mock_dns.update_dns.assert_called_with('dns_name', project) health_check = returned_load_balancer.configure_health_check.call_args_list[0][0][0] assert health_check.interval == 20 assert health_check.healthy_threshold == 3 assert health_check.unhealthy_threshold == 5 assert health_check.target == 'HTTP:9000/public/version.txt' assert result == returned_load_balancer
def test_we_create_a_load_balancer_when_there_is_no_one_yet(self, mock_dns, mock_landlord, mock_lb): project = {'name': 'MyProject', 'version': 'v34'} mock_connection = Mock() mock_connection.get_all_load_balancers.return_value = None returned_load_balancer = Mock() returned_load_balancer.dns_name = 'dns_name' returned_load_balancer.instances = ['a', 'b'] mock_connection.create_load_balancer.return_value = returned_load_balancer mock_lb.connect_to_region.return_value = mock_connection mock_landlord.Tenant = StubLandlord result = loadbalancer.get_loadbalancer(project) mock_lb.connect_to_region.assert_called_with("deploy.region", aws_access_key_id="aws.id", aws_secret_access_key="aws.secret") mock_connection.get_all_load_balancers.assert_called_with(load_balancer_names=['environment-lb-MyProject']) mock_dns.update_dns.assert_called_with('dns_name', project) health_check = returned_load_balancer.configure_health_check.call_args_list[0][0][0] assert health_check.interval == 20 assert health_check.healthy_threshold == 3 assert health_check.unhealthy_threshold == 5 assert health_check.target == 'HTTP:9000/public/version.txt' assert result == returned_load_balancer self.assertEquals(['a', 'b'], returned_load_balancer.instances)