コード例 #1
0
def generate_single_instance_event(
    instance,
    occurred_at,
    event_type=None,
    instance_type=None,
    subnet=None,
    no_instance_type=False,
    no_subnet=False,
    cloud_type=AWS_PROVIDER_STRING,
):
    """
    Generate single AwsInstanceEvent for testing.

    The ``powered_time`` is a datetime.datetime that defines when
    the instance event (either powering on or powering off) occurred.

    Args:
        instance (Instance): instance that owns the events.
        occurred_at (datetime.datetime): Time that the instance occurred.
        event_type (str): AWS event type
        instance_type (str): Optional AWS instance type.
        subnet (str): Optional subnet ID where instance runs.
        no_instance_type (bool): If true, don't assign an instance type.
        no_subnet (bool): If true, don't create and assign a subnet.

    Returns:
        AwsInstanceEvent: The created AwsInstanceEvent.

    """
    if no_instance_type:
        instance_type = None
    elif instance_type is None:
        instance_type = helper.get_random_instance_type(cloud_type=cloud_type)

    if event_type is None:
        event_type = InstanceEvent.TYPE.power_off

    if cloud_type == AZURE_PROVIDER_STRING:
        cloud_provider_event = AzureInstanceEvent.objects.create(
            instance_type=instance_type, )

    else:
        if no_subnet:
            subnet = None
        elif subnet is None:
            subnet = helper.generate_dummy_subnet_id()

        cloud_provider_event = AwsInstanceEvent.objects.create(
            subnet=subnet,
            instance_type=instance_type,
        )
    event = InstanceEvent.objects.create(
        instance=instance,
        event_type=event_type,
        occurred_at=occurred_at,
        content_object=cloud_provider_event,
    )
    return event
コード例 #2
0
ファイル: helper.py プロジェクト: Nanyte25/cloudigrade
def generate_aws_instance_events(instance,
                                 powered_times,
                                 ec2_ami_id=None,
                                 instance_type=None,
                                 subnet=None):
    """
    Generate list of AwsInstanceEvents for the AwsInstance for testing.

    Any optional arguments not provided will be randomly generated.

    The ``powered_times`` defines when the instance should be considered
    running for sake of the event types. The first element of the tuple
    is a datetime.datetime of when a "power on" event occurs, and the second
    element is a datetime.datetime of when a "power off" event occurs.

    Args:
        instance (AwsInstance): instance that owns the events.
        powered_times (list[tuple]): Time periods the instance is powered on.
        ec2_ami_id (str): Optional EC2 AMI ID the instance runs.
        instance_type (str): Optional AWS instance type.
        subnet (str): Optional subnet ID where instance runs.

    Returns:
        list(AwsInstanceEvent): The list of created AwsInstanceEvents.

    """
    if ec2_ami_id is None:
        ec2_ami_id = helper.generate_dummy_image_id()
    if instance_type is None:
        instance_type = random.choice(helper.SOME_EC2_INSTANCE_TYPES)
    if subnet is None:
        subnet = helper.generate_dummy_subnet_id()

    events = []
    for power_on_time, power_off_time in powered_times:
        if power_on_time is not None:
            event = generate_single_aws_instance_event(
                instance=instance,
                powered_time=power_on_time,
                event_type=InstanceEvent.TYPE.power_on,
                ec2_ami_id=ec2_ami_id,
                instance_type=instance_type,
                subnet=subnet)
            events.append(event)
        if power_off_time is not None:
            event = generate_single_aws_instance_event(
                instance=instance,
                powered_time=power_off_time,
                event_type=InstanceEvent.TYPE.power_off,
                ec2_ami_id=ec2_ami_id,
                instance_type=instance_type,
                subnet=subnet)
            events.append(event)
    return events
コード例 #3
0
def generate_cloudtrail_instance_event(
    instance,
    occurred_at,
    event_type=None,
    instance_type=None,
    subnet=None,
    no_instance_type=False,
    no_subnet=False,
):
    """
    Generate a single CloudTrailInstanceEvent for testing.

    Any optional arguments not provided will be randomly generated.

    Args:
        instance (Instance): instance that owns the events.
        occurred_at (datetime): Time periods the instance is powered on.
        instance_type (str): Optional AWS instance type.
        subnet (str): Optional subnet ID where instance runs.
        no_instance_type (bool): If true, instance_type is not set

    Returns:
        CloudTrailInstanceEvent: The created CloudTrailInstanceEvent.

    """
    if no_instance_type:
        instance_type = None
    elif instance_type is None:
        instance_type = helper.get_random_instance_type()

    if no_subnet:
        subnet = None
    elif subnet is None:
        subnet = helper.generate_dummy_subnet_id()

    if event_type is None:
        event_type = InstanceEvent.TYPE.power_off

    event = CloudTrailInstanceEvent(
        occurred_at=occurred_at,
        event_type=event_type,
        instance_type=instance_type,
        aws_account_id=instance.cloud_account.id,
        region=instance.machine_image.content_object.region,
        ec2_instance_id=instance.content_object.ec2_instance_id,
        ec2_ami_id=instance.machine_image.content_object.ec2_ami_id,
        subnet_id=subnet,
    )
    return event
コード例 #4
0
 def test_generate_dummy_describe_instance_with_values(self):
     """Assert generated instance contains given values."""
     image_id = helper.generate_dummy_image_id()
     instance_id = helper.generate_dummy_instance_id()
     subnet_id = helper.generate_dummy_subnet_id()
     state = aws.InstanceState.shutting_down
     instance_type = random.choice(helper.SOME_EC2_INSTANCE_TYPES)
     instance = helper.generate_dummy_describe_instance(
         instance_id, image_id, subnet_id, state, instance_type)
     self.assertEqual(instance['ImageId'], image_id)
     self.assertEqual(instance['InstanceId'], instance_id)
     self.assertEqual(instance['InstanceType'], instance_type)
     self.assertEqual(instance['SubnetId'], subnet_id)
     self.assertEqual(instance['State']['Code'], state.value)
     self.assertEqual(instance['State']['Name'], state.name)
コード例 #5
0
ファイル: helper.py プロジェクト: Nanyte25/cloudigrade
def generate_single_aws_instance_event(instance,
                                       powered_time=None,
                                       event_type=None,
                                       ec2_ami_id=None,
                                       instance_type=None,
                                       subnet=None):
    """
    Generate single AwsInstanceEvent for testing.

    The ``powered_time`` is a datetime.datetime that defines when
    the instance event (either powering on or powering off) occurred.

    Args:
        instance (AwsInstance): instance that owns the events.
        powered_time (datetime): Time that the instance is powered on.
        event_type (str): AWS event type
        ec2_ami_id (str): Optional EC2 AMI ID the instance runs.
        instance_type (str): Optional AWS instance type.
        subnet (str): Optional subnet ID where instance runs.

    Returns:
        AwsInstanceEvent: The created AwsInstanceEvent.

    """
    if ec2_ami_id is None:
        ec2_ami_id = helper.generate_dummy_image_id()
    if instance_type is None:
        instance_type = random.choice(helper.SOME_EC2_INSTANCE_TYPES)
    if subnet is None:
        subnet = helper.generate_dummy_subnet_id()
    if event_type is None:
        event_type = InstanceEvent.TYPE.power_off

    image, __ = AwsMachineImage.objects.get_or_create(
        account=instance.account,
        ec2_ami_id=ec2_ami_id,
    )
    event = AwsInstanceEvent.objects.create(
        instance=instance,
        machineimage=image,
        event_type=event_type,
        occurred_at=powered_time,
        subnet=subnet,
        instance_type=instance_type,
    )
    return event
コード例 #6
0
 def test_generate_dummy_describe_instance_with_values(self):
     """Assert generated instance contains given values."""
     image_id = helper.generate_dummy_image_id()
     instance_id = helper.generate_dummy_instance_id()
     subnet_id = helper.generate_dummy_subnet_id()
     state = aws.InstanceState.shutting_down
     instance_type = helper.get_random_instance_type()
     device_mapping = helper.generate_dummy_block_device_mapping()
     instance = helper.generate_dummy_describe_instance(
         instance_id=instance_id,
         image_id=image_id,
         subnet_id=subnet_id,
         state=state,
         instance_type=instance_type,
         device_mappings=[device_mapping],
     )
     self.assertEqual(instance["ImageId"], image_id)
     self.assertEqual(instance["InstanceId"], instance_id)
     self.assertEqual(instance["InstanceType"], instance_type)
     self.assertEqual(instance["SubnetId"], subnet_id)
     self.assertEqual(instance["State"]["Code"], state.value)
     self.assertEqual(instance["State"]["Name"], state.name)
     self.assertEqual(instance["State"]["Name"], state.name)
     self.assertEqual(instance["BlockDeviceMappings"], [device_mapping])
コード例 #7
0
 def test_generate_dummy_subnet_id(self):
     """Assert generation of an appropriate EC2 subnet ID."""
     volume_id = helper.generate_dummy_subnet_id()
     self.assertTrue(volume_id.startswith('subnet-'))
     self.assertEqual(len(volume_id), 15)
コード例 #8
0
def generate_instance_events(
    instance,
    powered_times,
    instance_type=None,
    subnet=None,
    no_instance_type=False,
    cloud_type=AWS_PROVIDER_STRING,
):
    """
    Generate list of InstanceEvents for the Instance for testing.

    Any optional arguments not provided will be randomly generated.

    The ``powered_times`` defines when the instance should be considered
    running for sake of the event types. The first element of the tuple
    is a datetime.datetime of when a "power on" event occurs, and the second
    element is a datetime.datetime of when a "power off" event occurs.

    Power-off events will never be created with an image defined in this helper
    function because in most real-world cases they do not have one.

    Args:
        instance (Instance): instance that owns the events.
        powered_times (list[tuple]): Time periods the instance is powered on.
        instance_type (str): Optional AWS instance type.
        subnet (str): Optional subnet ID where instance runs.
        no_instance_type (bool): If true, instance_type is not set

    Returns:
        list(InstanceEvent): The list of created AwsInstanceEvents.

    """
    if no_instance_type:
        instance_type = None
    elif instance_type is None:
        instance_type = helper.get_random_instance_type(cloud_type=cloud_type)
    if subnet is None:
        subnet = helper.generate_dummy_subnet_id()

    events = []
    for power_on_time, power_off_time in powered_times:
        if power_on_time is not None:
            event = generate_single_instance_event(
                instance=instance,
                occurred_at=power_on_time,
                event_type=InstanceEvent.TYPE.power_on,
                instance_type=instance_type,
                subnet=subnet,
                no_instance_type=no_instance_type,
                cloud_type=cloud_type,
            )
            events.append(event)
        if power_off_time is not None:
            # power_off events typically do *not* have an image defined.
            # So, ignore inputs and *always* set ec2_ami_id=None.
            event = generate_single_instance_event(
                instance=instance,
                occurred_at=power_off_time,
                event_type=InstanceEvent.TYPE.power_off,
                instance_type=instance_type,
                subnet=subnet,
                no_instance_type=no_instance_type,
                cloud_type=cloud_type,
            )
            events.append(event)
    return events