def durable_security_group_table(): from historical.security_group.models import DurableSecurityGroupModel mock_dynamodb2().start() yield DurableSecurityGroupModel.create_table(read_capacity_units=1, write_capacity_units=1, wait=True) mock_dynamodb2().stop()
def test_durable_table(durable_security_group_table): from historical.security_group.models import DurableSecurityGroupModel # we are explicit about our eventTimes because as RANGE_KEY it will need to be unique. sg = SECURITY_GROUP.copy() sg['eventTime'] = datetime(2017, 5, 11, 23, 30) sg.pop("eventSource") DurableSecurityGroupModel(**sg).save() items = list(DurableSecurityGroupModel.query('arn:aws:ec2:us-east-1:123456789012:security-group/sg-1234568')) assert len(items) == 1 assert not getattr(items[0], "ttl", None) sg['eventTime'] = datetime(2017, 5, 12, 23, 30) DurableSecurityGroupModel(**sg).save() items = list(DurableSecurityGroupModel.query('arn:aws:ec2:us-east-1:123456789012:security-group/sg-1234568')) assert len(items) == 2
def test_differ(current_security_group_table, durable_security_group_table, mock_lambda_environment): from historical.security_group.models import DurableSecurityGroupModel from historical.security_group.differ import handler from historical.models import TTL_EXPIRY ttl = int(time.time() + TTL_EXPIRY) new_group = SECURITY_GROUP.copy() new_group.pop("eventSource") new_group['eventTime'] = datetime(year=2017, month=5, day=12, hour=10, minute=30, second=0).isoformat() + 'Z' new_group["ttl"] = ttl data = json.dumps(DynamoDBRecordFactory(dynamodb=DynamoDBDataFactory( NewImage=new_group, Keys={ 'arn': new_group['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, mock_lambda_environment) assert DurableSecurityGroupModel.count() == 1 # ensure no new record for the same data duplicate_group = SECURITY_GROUP.copy() duplicate_group.pop("eventSource") duplicate_group['eventTime'] = datetime(year=2017, month=5, day=12, hour=11, minute=30, second=0).isoformat() + 'Z' duplicate_group["ttl"] = ttl data = json.dumps(DynamoDBRecordFactory(dynamodb=DynamoDBDataFactory( NewImage=duplicate_group, Keys={ 'arn': duplicate_group['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, mock_lambda_environment) assert DurableSecurityGroupModel.count() == 1 updated_group = SECURITY_GROUP.copy() updated_group.pop("eventSource") updated_group['eventTime'] = datetime(year=2017, month=5, day=12, hour=11, minute=30, second=0).isoformat() + 'Z' updated_group['configuration']['Description'] = 'changeme' updated_group["ttl"] = ttl data = json.dumps(DynamoDBRecordFactory(dynamodb=DynamoDBDataFactory( NewImage=updated_group, Keys={ 'arn': SECURITY_GROUP['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, mock_lambda_environment) assert DurableSecurityGroupModel.count() == 2 updated_group = SECURITY_GROUP.copy() updated_group.pop("eventSource") updated_group['eventTime'] = datetime(year=2017, month=5, day=12, hour=9, minute=30, second=0).isoformat() + 'Z' updated_group['configuration']['IpPermissions'][0]['IpRanges'][0]['CidrIp'] = 'changeme' updated_group["ttl"] = ttl data = json.dumps(DynamoDBRecordFactory(dynamodb=DynamoDBDataFactory( NewImage=updated_group, Keys={ 'arn': SECURITY_GROUP['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, mock_lambda_environment) assert DurableSecurityGroupModel.count() == 3 deleted_group = SECURITY_GROUP.copy() deleted_group.pop("eventSource") deleted_group['eventTime'] = datetime(year=2017, month=5, day=12, hour=12, minute=30, second=0).isoformat() + 'Z' deleted_group["ttl"] = ttl # ensure new record data = json.dumps(DynamoDBRecordFactory(dynamodb=DynamoDBDataFactory( OldImage=deleted_group, Keys={ 'arn': SECURITY_GROUP['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, mock_lambda_environment) assert DurableSecurityGroupModel.count() == 4