"""Load tests for the GDA API Gateway.""" import os import random import time import uuid from locust import HttpUser, task from locust.exception import StopUser from sf_utils import SF_CONFIG from snowflake_executor import SnowflakeSQLExecutorGDA class GDAUser(HttpUser): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # create separate id for each GDAUser instance self.load_test_uuid = 'QALT_' + str(uuid.uuid4())[:20] # init separate counter for each GDAUser instance self.counter = 0 # get separate token for each GDAUser instance with self.client.post( 'https://qa-orchard.auth0.com/oauth/token', json={ 'client_id': os.environ.get('QA_AUTH0_CLIENT_ID'), 'client_secret': os.environ.get('QA_AUTH0_CLIENT_SECRET'), 'audience': 'https://qa-gda-gateway-auth0-jwt-authorizer', 'grant_type': 'client_credentials' }, catch_response=True) as response: print(response.json()) self.client.access_token = response.json()['access_token'] # get 10 artists for each GDAUser instance with SnowflakeSQLExecutorGDA(SF_CONFIG) as executor: result = executor.get_urls(limit=int(os.environ.get('REQUESTS_PER_MOCK_USER'))) urls = [ {'spotify_url': item[0], 'tiktok_url': item[1], 'youtube_url': item[2]} for item in result ] chartmetric_ids = [item[3] for item in result] self.urls = urls executor.mark_used_urls(chartmetric_ids) @task(1) def test_artist_submission(self): auth_header = { 'content-type': 'application/json', 'Authorization': 'Bearer {token}'.format( token=self.client.access_token)} try: urls = self.urls.pop() except IndexError as e: print(e) raise StopUser() with self.client.post( 'https://qa-gda-gateway.theorchard.io/artist-submission', headers=auth_header, json={ 'email': 'buvarov+' + str( random.randint(24, 4142355)) + '@theorchard.com', 'first_name': self.load_test_uuid, 'last_name': 'TestUvarov' + str(random.randint(24, 4142355)), 'company': 'UvaCorp' + str(random.randint(24, 4142355)), 'role': 'Artist or Band', 'spotify_url': urls.get('spotify_url'), 'amazon_music_url': None, 'deezer_url': None, 'facebook_url': None, 'instagram_url': None, 'soundcloud_url': None, 'tiktok_url': urls.get('tiktok_url'), 'twitter_url': None, 'youtube_url': urls.get('youtube_url'), 'country': 'USA', 'signing_entity': 'GBR', 'currency': 'USD', 'voucher_code': os.environ.get('VOUCHER_CODE') }, catch_response=True ) as response: print(response.json()) print(response.headers) response.raise_for_status() print(response.reason) self.counter += 1 if self.counter == int(os.environ.get('REQUESTS_PER_MOCK_USER')): with SnowflakeSQLExecutorGDA(SF_CONFIG) as executor: # wait for data to propagate via Kafka Data Highway time.sleep(600) result = executor.get_count_from_audit_event_gda_submit_application( self.load_test_uuid) print( 'Submitted 10 requests (successful and unsuccesful). ' 'Events in event.gdaSubmitApplication: ', result) raise StopUser()