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_can_get_all_instances_with_region(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.ip_address = '192.1.11.1' instance1.state = 'running' instance1.launch_time = datetime.date.today().isoformat() instance1.tags = {'Name': 'STAGE-Instance-1', 'Project': 'Instance', 'Version': 'v43', 'StopTime' : '8', 'StartTime': '6'} instance2 = Mock() instance2.id = 'i-542211' instance2.state = 'stopped' instance2.ip_address = None instance2.launch_time = datetime.date.today().isoformat() instance2.tags = {'Name': 'STAGE-Instance-2', 'Project': 'Instance', 'Version': 'v43', 'StopTime' : '9', 'StartTime': '7'} mock_connection.get_only_instances.return_value = [instance1, instance2] instances = ec2.get_all_instances('myRegion') mock_ec2.connect_to_region.assert_called_with('myRegion', 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]['stopTime'], '8') self.assertEquals(instances[0]['startTime'], '6') 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[1]['name'], 'Instance-2') self.assertEquals(instances[1]['version'], 'v43') self.assertEquals(instances[1]['startTime'], '7') self.assertEquals(instances[1]['stopTime'], '9') self.assertEquals(instances[1]['project'], 'Instance') self.assertEquals(instances[1]['date'], datetime.date.today().isoformat()) self.assertEquals(instances[1]['ip'], 'None') self.assertEquals(instances[1]['status'], 'stopped')
def get_instances(): return ec2.get_all_instances()