def durable_vpc_table(): from historical.vpc.models import DurableVPCModel mock_dynamodb2().start() yield DurableVPCModel.create_table(read_capacity_units=1, write_capacity_units=1, wait=True) mock_dynamodb2().stop()
def test_durable_table(durable_vpc_table): """Tests for the Durable PynamoDB model.""" from historical.vpc.models import DurableVPCModel # we are explicit about our eventTimes because as RANGE_KEY it will need to be unique. vpc = VPC.copy() vpc.pop("eventSource") vpc['eventTime'] = datetime(2017, 5, 11, 23, 30) DurableVPCModel(**vpc).save() items = list(DurableVPCModel.query('arn:aws:ec2:us-east-1:123456789012:vpc/vpc-123343')) assert len(items) == 1 assert not getattr(items[0], 'ttl', None) vpc['eventTime'] = datetime(2017, 5, 12, 23, 30) DurableVPCModel(**vpc).save() items = list(DurableVPCModel.query('arn:aws:ec2:us-east-1:123456789012:vpc/vpc-123343')) assert len(items) == 2
def test_durable_table(durable_vpc_table): from historical.vpc.models import DurableVPCModel # we are explicit about our eventTimes because as RANGE_KEY it will need to be unique. VPC['eventTime'] = datetime(2017, 5, 11, 23, 30) DurableVPCModel(**VPC).save() items = list( DurableVPCModel.query( 'arn:aws:ec2:us-east-1:123456789012:vpc/vpc-123343')) assert len(items) == 1 assert not getattr(items[0], 'ttl', None) VPC['eventTime'] = datetime(2017, 5, 12, 23, 30) DurableVPCModel(**VPC).save() items = list( DurableVPCModel.query( 'arn:aws:ec2:us-east-1:123456789012:vpc/vpc-123343')) assert len(items) == 2
def test_differ(current_vpc_table, durable_vpc_table, mock_lambda_environment): from historical.vpc.models import DurableVPCModel from historical.vpc.differ import handler from historical.models import TTL_EXPIRY ttl = int(time.time() + TTL_EXPIRY) new_vpc = VPC.copy() new_vpc.pop("eventSource") new_vpc['eventTime'] = datetime( year=2017, month=5, day=12, hour=10, minute=30, second=0).isoformat() + 'Z' new_vpc['ttl'] = ttl data = json.dumps(DynamoDBRecordFactory(dynamodb=DynamoDBDataFactory( NewImage=new_vpc, Keys={'arn': new_vpc['arn']}), eventName='INSERT'), default=serialize) data = RecordsFactory(records=[ SQSDataFactory( body=json.dumps(SnsDataFactory(Message=data), default=serialize)) ]) data = json.loads(json.dumps(data, default=serialize)) handler(data, None) assert DurableVPCModel.count() == 1 # ensure no new record for the same data duplicate_vpc = VPC.copy() duplicate_vpc.pop("eventSource") duplicate_vpc['eventTime'] = datetime( year=2017, month=5, day=12, hour=11, minute=30, second=0).isoformat() + 'Z' duplicate_vpc['ttl'] = ttl data = json.dumps(DynamoDBRecordFactory(dynamodb=DynamoDBDataFactory( NewImage=duplicate_vpc, Keys={'arn': duplicate_vpc['arn']}), eventName='MODIFY'), default=serialize) data = RecordsFactory(records=[ SQSDataFactory( body=json.dumps(SnsDataFactory(Message=data), default=serialize)) ]) data = json.loads(json.dumps(data, default=serialize)) handler(data, None) assert DurableVPCModel.count() == 1 updated_vpc = VPC.copy() updated_vpc.pop("eventSource") updated_vpc['eventTime'] = datetime( year=2017, month=5, day=12, hour=11, minute=30, second=0).isoformat() + 'Z' updated_vpc['configuration']['State'] = 'changeme' updated_vpc['ttl'] = ttl data = json.dumps(DynamoDBRecordFactory(dynamodb=DynamoDBDataFactory( NewImage=updated_vpc, Keys={'arn': VPC['arn']}), eventName='MODIFY'), default=serialize) data = RecordsFactory(records=[ SQSDataFactory( body=json.dumps(SnsDataFactory(Message=data), default=serialize)) ]) data = json.loads(json.dumps(data, default=serialize)) handler(data, None) assert DurableVPCModel.count() == 2 updated_vpc = VPC.copy() updated_vpc.pop("eventSource") updated_vpc['eventTime'] = datetime( year=2017, month=5, day=12, hour=9, minute=30, second=0).isoformat() + 'Z' updated_vpc['configuration']['CidrBlock'] = 'changeme' updated_vpc['ttl'] = ttl data = json.dumps(DynamoDBRecordFactory(dynamodb=DynamoDBDataFactory( NewImage=updated_vpc, Keys={'arn': VPC['arn']}), eventName='MODIFY'), default=serialize) data = RecordsFactory(records=[ SQSDataFactory( body=json.dumps(SnsDataFactory(Message=data), default=serialize)) ]) data = json.loads(json.dumps(data, default=serialize)) handler(data, None) assert DurableVPCModel.count() == 3 updated_vpc = VPC.copy() updated_vpc.pop("eventSource") updated_vpc['eventTime'] = datetime( year=2017, month=5, day=12, hour=9, minute=31, second=0).isoformat() + 'Z' updated_vpc.update({'Name': 'blah'}) updated_vpc['ttl'] = ttl data = json.dumps(DynamoDBRecordFactory(dynamodb=DynamoDBDataFactory( NewImage=updated_vpc, Keys={'arn': VPC['arn']}), eventName='MODIFY'), default=serialize) data = RecordsFactory(records=[ SQSDataFactory( body=json.dumps(SnsDataFactory(Message=data), default=serialize)) ]) data = json.loads(json.dumps(data, default=serialize)) handler(data, None) assert DurableVPCModel.count() == 4 deleted_vpc = VPC.copy() deleted_vpc.pop("eventSource") deleted_vpc['eventTime'] = datetime( year=2017, month=5, day=12, hour=12, minute=30, second=0).isoformat() + 'Z' deleted_vpc['ttl'] = ttl # ensure new record data = json.dumps(DynamoDBRecordFactory( dynamodb=DynamoDBDataFactory(OldImage=deleted_vpc, Keys={'arn': VPC['arn']}), eventName='REMOVE', userIdentity=UserIdentityFactory( type='Service', principalId='dynamodb.amazonaws.com')), default=serialize) data = RecordsFactory(records=[ SQSDataFactory( body=json.dumps(SnsDataFactory(Message=data), default=serialize)) ]) data = json.loads(json.dumps(data, default=serialize)) handler(data, None) assert DurableVPCModel.count() == 5