"""Integration tests for Maxwell's.""" from os import getenv import random import string import time import boto3 import pytest @pytest.fixture def track_id(): """Return a Track ID from QA.""" return 27909323 @pytest.fixture def product_id(): """Return a Product ID from QA.""" return 2340701 @pytest.fixture def project_id(): """Return a Project ID from QA.""" return 3946574 @pytest.fixture def project_cloudsearch_client(): """Initialize project cloudsearch client via boto3.""" return boto3.client( 'cloudsearchdomain', aws_access_key_id=getenv('TEST_AWS_ACCESS_KEY'), aws_secret_access_key=getenv('TEST_AWS_SECRET_ACCESS_KEY'), endpoint_url=getenv('PROJECT_CLOUDSEARCH_URL') ) @pytest.fixture def release_cloudsearch_client(): """Initialize release cloudsearch client via boto3.""" return boto3.client( 'cloudsearchdomain', aws_access_key_id=getenv('TEST_AWS_ACCESS_KEY'), aws_secret_access_key=getenv('TEST_AWS_SECRET_ACCESS_KEY'), endpoint_url=getenv('RELEASE_CLOUDSEARCH_URL') ) @pytest.fixture def track_cloudsearch_client(): """Initialize track cloudsearch client via boto3.""" return boto3.client( 'cloudsearchdomain', aws_access_key_id=getenv('TEST_AWS_ACCESS_KEY'), aws_secret_access_key=getenv('TEST_AWS_SECRET_ACCESS_KEY'), endpoint_url=getenv('TRACK_CLOUDSEARCH_URL') ) def search_cloud(client, search_term): """Execute a Cloudsearch.""" return client.search(query=search_term) def random_string(length): """Create a random string made of letters.""" letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(length)) def assert_with_timeout(call, call_args, assertions, interval=1, timeout=25): """Continue to assert expectation until condition is met or timeout is met/exceeded.""" assertions_met = False timer = 0 assertion_error = None while not assertions_met: if timer >= timeout: raise AssertionError(f'Exceeded timeout of {timeout}, assertion failed was {assertion_error}') result_of_call = call(*call_args) assertion_error = None try: assertions(result_of_call) except AssertionError as ae: assertion_error = ae if not assertion_error: assertions_met = True else: timer += interval time.sleep(interval) @pytest.mark.parametrize( 'search_client, update_function_name, record_id, field_name', [[track_cloudsearch_client(), 'rename_track', track_id(), 'track_name'], [release_cloudsearch_client(), 'rename_product', product_id(), 'release_name'], [project_cloudsearch_client(), 'rename_project', project_id(), 'project_name']]) def test_updates( workstation_api_client, search_client, update_function_name, record_id, field_name): """Test update record and cloudsearch for new name.""" new_name = random_string(20) rename_result = getattr(workstation_api_client, update_function_name)(record_id, new_name) assert rename_result.status_code == 200, \ f'Result of rename was {rename_result.status_code}, expected 200.' def assertions(response): assert response['ResponseMetadata']['HTTPStatusCode'] == 200, \ 'Result of search was {}, expected 200.'.format(response['ResponseMetadata']['HTTPStatusCode']) assert response['hits']['found'] == 1, \ 'Expected number of results to be 1, got {}'.format(response['hits']['found']) assert response['hits']['hit'][0]['fields'][field_name][0] == new_name, \ 'Expected name to equal {} got {}'.format( new_name, response['hits']['hit'][0]['fields'][field_name][0]) assert_with_timeout(search_cloud, [search_client, new_name], assertions)