def test_we_can_get_all_instances(self, mock_landlord, mock_ec2): persistence.save('Instance1', 'v43') persistence.save('Instance2', 'v43') mock_landlord.Tenant = StubLandlord mock_connection = Mock() mock_ec2.connect_to_region.return_value = mock_connection instance1 = Mock() instance1.id = 'i-938372' instance1.dns_name = '192.1.11.1.dnsname' instance1.ip_address = '192.1.11.1' instance1.state = 'running' instance1.tags = {'Name': 'STAGE-Instance-1', 'Project': 'Instance', 'Version': 'v43', 'AutoStopped': 'True'} instance1.launch_time = datetime.date.today().isoformat() instance1.image_id = 'ami-192812' instance2 = Mock() instance2.id = 'i-542211' instance2.dns_name = '192.5.5.5.dnsname' instance2.ip_address = '192.5.5.5' instance2.state = 'stopped' instance2.tags = {'Name': 'Instance2', 'Project': 'Instance', 'Version': 'v43'} instance2.launch_time = datetime.date.today().isoformat() instance2.image_id = 'ami-237829' mock_connection.get_only_instances.return_value = [instance1, instance2] instances = ec2.get_all_instances() mock_ec2.connect_to_region.assert_called_with('eu-west-1', aws_access_key_id='aws.id', aws_secret_access_key='aws.secret') mock_connection.get_only_instances.assert_called_with(filters={"tag:Deployer": "igor"}) self.assertEquals(len(instances), 2) self.assertEquals(instances[0]['name'], 'Instance-1') self.assertEquals(instances[0]['version'], 'v43') self.assertEquals(instances[0]['project'], 'Instance') self.assertEquals(instances[0]['date'], datetime.date.today().isoformat()) self.assertEquals(instances[0]['ip'], '192.1.11.1') self.assertEquals(instances[0]['status'], 'running') self.assertEquals(instances[0]['autoStopped'], 'True') self.assertEquals(instances[1]['name'], 'Instance2') self.assertEquals(instances[1]['version'], 'v43') self.assertEquals(instances[1]['project'], 'Instance') self.assertEquals(instances[1]['date'], datetime.date.today().isoformat()) self.assertEquals(instances[1]['ip'], '192.5.5.5') self.assertEquals(instances[1]['status'], 'stopped') self.assertEquals(instances[1]['ami'], 'ami-237829') self.assertEquals(instances[1]['autoStopped'], 'NA')
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)