def setUp(self): self.dynamo = MagicMock(spec=DynamoDBConnection) self.dynamo.describe_table.return_value = { 'Table': { 'TableStatus': 'ACTIVE', 'ProvisionedThroughput': { 'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1 } } } self.tables = DynamoDbTables(self.dynamo, backoff=0.001)
def add_revision(environment, regions, service_name, label, stream_in): files = files_from_tar(stream_in) units = get_units(files) service_revision = FlotillaServiceRevision(label, units=units) for region in regions: kms = boto.kms.connect_to_region(region) dynamo = boto.dynamodb2.connect_to_region(region) tables = DynamoDbTables(dynamo, environment=environment) tables.setup(['revisions', 'services', 'units']) db = FlotillaClientDynamo(None, None, tables.revisions, tables.services, tables.units, None, kms) db.add_revision(service_name, service_revision)
def add_revision(environment, regions, service_name, label, env_vars, highlander, stream_in): # Extract services and files from input: files = files_from_tar(stream_in) services, environments = get_services_environments(files, environment, env_vars) # Extract metadata from environments: service_updates = extract_service_updates(environments.values()) env_regions = extract_regions(environments.values()) regions = set(regions) | env_regions # Build a ServiceRevision with services+enviroment that are left: units = get_units(services, environments) service_revision = FlotillaServiceRevision(label, units=units) # Add revision and perform updates in each region: dynamo_cache = {} for region in regions: kms = boto.kms.connect_to_region(region) dynamo = boto.dynamodb2.connect_to_region(region) tables = DynamoDbTables(dynamo, environment=environment) dynamo_cache[region] = tables tables.setup(['revisions', 'services', 'units']) db = FlotillaClientDynamo(None, None, tables.revisions, tables.services, tables.units, None, kms) db.add_revision(service_name, service_revision) if service_updates: db.configure_service(service_name, service_updates) if highlander > 0: wait_for_deployment(dynamo_cache, regions, service_name, service_revision.revision_hash, highlander)
def test_setup_environment(self): self.tables = DynamoDbTables(self.dynamo, environment='test') self.tables.setup(['revisions']) self.assertEqual(self.tables.revisions.table_name, 'flotilla-test-revisions')
class TestDynamoDbTables(unittest.TestCase): def setUp(self): self.dynamo = MagicMock(spec=DynamoDBConnection) self.dynamo.describe_table.return_value = { 'Table': { 'TableStatus': 'ACTIVE', 'ProvisionedThroughput': { 'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1 } } } self.tables = DynamoDbTables(self.dynamo, backoff=0.001) def test_setup_existing(self): self.tables.setup(['revisions']) self.assertNotEqual(self.tables.revisions, None) self.dynamo.create_table.assert_not_called() def test_setup_create(self): self.dynamo.describe_table.side_effect = [ BotoServerError(400, 'Not Found', '<Code>ResourceNotFoundException</Code>'), self.dynamo.describe_table.return_value ] self.tables.setup(['revisions']) self.assertNotEqual(self.tables.revisions, None) self.dynamo.create_table.assert_called_with( table_name='flotilla-revisions', attribute_definitions=ANY, key_schema=ANY, provisioned_throughput=ANY ) def test_setup_create_wait(self): self.dynamo.describe_table.side_effect = [ BotoServerError(400, 'Not Found', '<Code>ResourceNotFoundException</Code>'), { 'Table': { 'TableStatus': 'CREATING', 'ProvisionedThroughput': { 'ReadCapacityUnits': 1, 'WriteCapacityUnits': 1 } } }, self.dynamo.describe_table.return_value ] self.tables.setup(['revisions']) self.assertEqual(self.dynamo.describe_table.call_count, 3) def test_setup_create_error(self): self.dynamo.describe_table.side_effect = [ BotoServerError(400, 'Kaboom') ] self.assertRaises(Exception, self.tables.setup, ['revisions']) def test_setup_unknown_table(self): self.tables.setup(['meow']) self.dynamo.describe_table.assert_not_called() self.dynamo.create_table.assert_not_called() def test_setup_environment(self): self.tables = DynamoDbTables(self.dynamo, environment='test') self.tables.setup(['revisions']) self.assertEqual(self.tables.revisions.table_name, 'flotilla-test-revisions')