def image_fixture(request, aws_profile):
    """Power on instances for each image and terminate after tests."""
    images = []
    for image_to_test in request.param:
        aws_profile_name = aws_profile['name']
        # Create some instances to detect on creation, random choice from every
        # configured image type (private, owned, marketplace, community)
        image_type, image_name, expected_state = image_to_test
        source_images = [
            image for image in aws_profile['images'][image_type]
            if image['name'] == image_name
        ]
        assert source_images, f'Found no images from profile! {aws_profile}'
        source_image = source_images[0]
        # Run an instance
        instance_id = aws_utils.run_instances_by_name(
            aws_profile_name, image_type, image_name, count=1)[0]
        request.addfinalizer(functools.partial(
            aws_utils.terminate_instance,
            (aws_profile_name, instance_id)
        ))
        final_image_data = ImageData(image_type,
                                     image_name,
                                     source_image,
                                     instance_id)
        images.append(final_image_data)
    yield images
Exemple #2
0
def aws_instance_instigator():
    """Run instances in an aws account for testing purposes."""
    cfg = config.get_config()
    profile = cfg['aws_profiles'][0]
    profile_name = profile['name']
    image_type = 'owned'
    image_name = 'rhel-extra-detection-methods'

    instance_ids = aws_utils.run_instances_by_name(profile_name,
                                                   image_type,
                                                   image_name,
                                                   count=2)
    print(instance_ids)
def image_fixture(request, aws_profile):
    """Power on an instance for use in module and terminate it after tests."""
    aws_profile_name = aws_profile['name']
    # Create some instances to detect on creation, random choice from every
    # configured image type (private, owned, marketplace, community)
    image_type, image_name, expected_state = request.param
    source_image = [
        image for image in aws_profile['images'][image_type]
        if image['name'] == image_name
    ][0]
    # Run an instance
    instance_id = aws_utils.run_instances_by_name(aws_profile_name,
                                                  image_type,
                                                  image_name,
                                                  count=1)[0]

    yield ImageData(image_type, image_name, source_image, instance_id)

    # Terminate the instance after the module completes
    aws_utils.terminate_instance((aws_profile_name, instance_id))
def test_inspection(test_case, request):
    """Ensure instances are inspected.

    :id: 45BBB27E-F38D-415F-B64F-B2543D1132DE
    :description: Ensure images are inspected for all running instances.
    :steps: 1) Create a cloud account
        2) Run instances based off of a non-windows image
        3) Send a GET to '/api/cloudigrade/v2/instances/' with a timeout and
            expect to get the instances we created
        4) Send a GET to '/api/cloudigrade/v2/images/' and expect to get the
            image that the instances were based off of.
        5) Keep checking to see that the images progress from "pending",
            "preparing", "inspecting", to "inspected"
    :expectedresults:
        1) We get 200 responses for our GET requests and information about
            the images includes inspection state information.
        2) The images are eventually inspected.
    """
    aws_profile = config.get_config()['aws_profiles'][0]
    aws_profile_name = aws_profile['name']

    # Purge leftover sqs messages in _ready_volumes queue
    aws_utils.purge_queue_messages()
    # Make sure clusters are scaled down at start of inspection
    aws_utils.scale_down_houndigrade()

    # Delete any preexisting accounts in cloudigrade
    delete_preexisting_accounts(aws_profile)

    # Add AWS account to cloudigrade
    arn = aws_profile['arn']
    client = api.ClientV2()
    acct_data_params = {
        'account_arn': arn,
        'name': uuid4(),
        'cloud_type': 'aws',
    }

    # Create an account
    add_acct_response = client.request('post',
                                       'accounts/',
                                       data=acct_data_params)
    assert add_acct_response.status_code == 201

    # Start an instance for initial discovery
    image_type, image_name, expected_state = test_case
    ec2_ami_id = ''
    for image in aws_profile['images'][image_type]:
        if image_name == image['name']:
            ec2_ami_id = image['image_id']

    instance_id = aws_utils.run_instances_by_name(aws_profile_name,
                                                  image_type,
                                                  image_name,
                                                  count=1)[0]
    request.addfinalizer(
        functools.partial(aws_utils.terminate_instance,
                          (aws_profile_name, instance_id)))

    instances = _get_object_with_timeout(client, 'instances/', MEDIUM_TIMEOUT)
    images = _get_object_with_timeout(client, 'images/', MEDIUM_TIMEOUT)

    # Check that images and instances show up in Cloudigrade account
    assert _get_instance_id_with_ec2_instance_id(instance_id, instances) is \
        not None

    image_id = _image_id_with_ec2_image_id(ec2_ami_id, images)
    assert image_id is not None

    # Check that Cloudigrade eventually inspects images.
    inspection_results = _wait_for_inspection_with_timeout(
        client, image_id, LONG_TIMEOUT, expected_state)
    assert inspection_results is True
def test_discovery(test_case, request):
    """Ensure instances are discovered on account creation.

    :id: 509260DA-9980-4F9D-85D9-54C30B99DA56
    :description: Ensure running instances are discovered.
    :steps: 1) Run an image in AWS
        2) Create an account in Cloudigrade - send a POST with the cloud
            account information to '/api/cloudigrade/v2/'
        4) Send a GET to '/api/cloudigrade/v2/instances/' and expect to get the
            instances we created
        5) Send a GET to '/api/cloudigrade/v2/images/' and expect to get the
            image that the instances were based off of.
    :expectedresults:
        1) The server returns a 201 response with the information
            of the created account.
        2) We get 200 responses for our GET requests and information about
            the images includes inspection state information.
    """
    aws_profile = config.get_config()['aws_profiles'][0]
    aws_profile_name = aws_profile['name']
    # Delete any preexisting accounts in cloudigrade
    delete_preexisting_accounts(aws_profile)
    # Purge leftover sqs messages in _ready_volumes queue
    aws_utils.purge_queue_messages()
    # Run an instance
    image_type, image_name, expected_state = test_case
    ec2_ami_id = ''
    for image in aws_profile['images'][image_type]:
        if image_name == image['name']:
            ec2_ami_id = image['image_id']

    # Start an instance for initial discovery
    instance_id = aws_utils.run_instances_by_name(aws_profile_name,
                                                  image_type,
                                                  image_name,
                                                  count=1)[0]

    print(f'Instance id: {instance_id}')
    print(f'Image_id: {ec2_ami_id}')

    request.addfinalizer(
        functools.partial(aws_utils.terminate_instance,
                          (aws_profile_name, instance_id)))

    # Add AWS account to cloudigrade
    arn = aws_profile['arn']
    client = api.ClientV2()
    acct_data_params = {
        'account_arn': arn,
        'name': uuid4(),
        'cloud_type': 'aws',
    }
    # Create an account
    add_acct_response = client.request('post',
                                       'accounts/',
                                       data=acct_data_params)
    assert add_acct_response.status_code == 201

    # Validate that started instance is in cloudigrade
    acct_id = add_acct_response.json()['account_id']
    acct_image = client.request('get', f'accounts/{acct_id}/')
    arn = acct_image.json()['content_object']['account_arn']
    instances = _get_object_with_timeout(
        client,
        'instances/',
        MEDIUM_TIMEOUT,
    )
    assert _get_instance_id_with_ec2_instance_id(instance_id, instances) is \
        not None

    images = _get_object_with_timeout(client, 'images/', MEDIUM_TIMEOUT)
    assert _image_id_with_ec2_image_id(ec2_ami_id, images) is not None