Skip to content

Commit

Permalink
error_test timeouts on translate and status (#987)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurii-klymenko authored Jun 13, 2022
1 parent 32acefd commit 5cec049
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from stix_shifter_utils.modules.base.stix_translation.empty_query_translator import EmptyQueryTranslator
import re
from time import sleep

START_STOP_PATTERN = "\s?START\s?t'\d{4}(-\d{2}){2}T\d{2}(:\d{2}){2}(\.\d+)?Z'\sSTOP\s?t'\d{4}(-\d{2}){2}T(\d{2}:){2}\d{2}.\d{1,3}Z'\s?"

ERROR_TYPE_PARSE_EXCEPTION = 'parse_exception'
ERROR_TYPE_TRANSFORM_EXCEPTION = 'transform_exception'
ERROR_TYPE_TRANSFORM_DELAY_SEC = 'transform_delay_sec_'

class QueryTranslator(EmptyQueryTranslator):

Expand All @@ -13,10 +15,13 @@ def parse_query(self, data):
raise Exception('test exception in parse query')
return super().parse_query(data)


def transform_query(self, data):
if self.options.get('error_type') == ERROR_TYPE_TRANSFORM_EXCEPTION:
error_type = self.options.get('error_type')
if error_type == ERROR_TYPE_TRANSFORM_EXCEPTION:
raise Exception('test exception in transform query')
if error_type.startswith(ERROR_TYPE_TRANSFORM_DELAY_SEC):
delay = int(error_type[len(ERROR_TYPE_TRANSFORM_DELAY_SEC):])
sleep(delay)
# Data is a STIX pattern.
# stix2-matcher will break on START STOP qualifiers so remove before returning pattern.
# Remove this when ever stix2-matcher supports proper qualifier timestamps
Expand Down
32 changes: 22 additions & 10 deletions stix_shifter_modules/error_test/stix_transmission/connector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from stix_shifter_utils.modules.base.stix_transmission.base_sync_connector import BaseSyncConnector
from stix_shifter_utils.modules.base.stix_transmission.base_connector import BaseConnector
from stix_shifter_utils.modules.base.stix_transmission.base_status_connector import Status
from stix_shifter_utils.stix_transmission.utils.RestApiClient import RestApiClient
from stix2matcher.matcher import Pattern
from stix2matcher.matcher import MatchListener
Expand All @@ -9,12 +10,13 @@

ERROR_TYPE_TIMEOUT = 'timeout'
ERROR_TYPE_BAD_CONNECTION = 'bad_connection'
ERROR_TYPE_STATUS_DELAY = 'status_delay_sec_'

class UnexpectedResponseException(Exception):
pass


class Connector(BaseSyncConnector):
class Connector(BaseConnector):
def __init__(self, connection, configuration):
self.connector = __name__.split('.')[1]
self.connection = connection
Expand Down Expand Up @@ -63,18 +65,34 @@ def ping_connection(self):
ErrorResponder.fill_error(return_obj, response_txt, ['message'], connector=self.connector)
return return_obj

def create_query_connection(self, query):
return {"success": True, "search_id": query}

def create_status_connection(self, search_id, metadata=None):
error_type = self.connection['options'].get('error_type')
if error_type.startswith(ERROR_TYPE_STATUS_DELAY):
delay = int(error_type[len(ERROR_TYPE_STATUS_DELAY):])
current_time = int(time.time())
if metadata:
stop_time = metadata['stop_time']
else:
stop_time = current_time + delay
if stop_time > current_time:
return {"success": True, "status": Status.RUNNING.value, "progress": int((delay - (stop_time - current_time)) / delay * 100), "metadata": {"stop_time": stop_time}}
return {"success": True, "status": Status.COMPLETED.value, "progress": 100}

def create_results_connection(self, search_id, offset, length):
observations = []
return_obj = dict()

response = None
if self.connection['options'].get('error_type') == ERROR_TYPE_TIMEOUT:
# httpstat.us/200?sleep=60000 for slow connection that is valid
response = self.client.call_api('https://httpstat.us/200?sleep=60000', 'get', timeout=self.timeout)
self.client.call_api('https://httpstat.us/200?sleep=60000', 'get', timeout=self.timeout)
elif self.connection['options'].get('error_type') == ERROR_TYPE_BAD_CONNECTION:
# www.google.com:81 for a bad connection that will timeout
response = self.client.call_api('https://www.google.com:81', 'get', timeout=self.timeout)
else:
if not response:
response = self.client.call_api(self.bundle_url, 'get', timeout=self.timeout)
if response.code != 200:
response_txt = response.raise_for_status()
Expand Down Expand Up @@ -117,12 +135,6 @@ def create_results_connection(self, search_id, offset, length):
ErrorResponder.fill_error(return_obj, message='Invalid STIX bundle. Malformed JSON: ' + str(ex), connector=self.connector)
return return_obj

def create_query_connection(self, query):
return {"success": True, "search_id": query}

def create_status_connection(self, search_id):
return {"success": True, "status": "COMPLETED", "progress": 100}

def delete_query_connection(self, search_id):
return_obj = dict()
return_obj['success'] = True
Expand Down

0 comments on commit 5cec049

Please sign in to comment.