# Path: tibanna/exceptions.py
# class AWSEMErrorHandler(object):
# 
#     def __init__(self):
#         self.ErrorList = self._ErrorList  # initial error list, custom errors can be added
# 
#     class AWSEMError(object):
#         def __init__(self, error_type, pattern_in_log, multiline=False):
#             self.error_type = error_type
#             if multiline:
#                 self.pattern_in_log = re.compile(pattern_in_log, re.MULTILINE)
#             else:
#                 self.pattern_in_log = pattern_in_log
# 
#     def add_custom_errors(self, custom_err_list):
#         """add custom errors to ErrorList.
#         custom_err_list is a list of dictionaries w/ keys 'error_type', 'pattern', 'multiline'"""
#         for err in custom_err_list:
#             self.ErrorList.append(self.AWSEMError(err['error_type'], err['pattern'], err.get('multiline', False)))
# 
#     @property
#     def _ErrorList(self):
#         """add any specific error types with recognizable strings or patterns here.
#         the order is important. The earlier ones are checked first and if there is a match,
#         the later ones will not be checked."""
#         return [
#             # input download failure due to not enough disk space
#             self.AWSEMError('Not enough space for input files', 'download failed: .+ No space left on device'),
#             # Docker pull failure due to not enough root disk space
#             self.AWSEMError('No space for docker', 'failed to register layer.+no space left on device'),
#             # not enough disk space
#             self.AWSEMError('Not enough space', '.+No space left on device'),
#             # CWL missing input error
#             self.AWSEMError('CWL missing input', 'Missing required input parameter\n.+\n', True),
#             # Bucket access error
#             self.AWSEMError('Bucket/file access denied', 'when calling the ListObjectsV2 operation: Access Denied')
#         ]
# 
#     def parse_log(self, log):
#         # for ex in self.AWSEMErrorExceptionList:
#         for ex in self.ErrorList:
#             res = re.search(ex.pattern_in_log, log)
#             if res:
#                 match = res.string[res.regs[0][0]:res.regs[0][1]]
#                 match = re.sub('\n', ' ', match)  # \n not recognized and subsequent content is dropped from Exception
#                 match = re.sub(' +', ' ', match)
#                 msg = "%s: %s" % (ex.error_type, match)
#                 return AWSEMJobErrorException(msg)
#         return
# 
#     @property
#     def general_awsem_check_log_msg_template(self):
#         return "check log using tibanna log --job-id=%s [--sfn=stepfunction]"
# 
#     def general_awsem_check_log_msg(self, job_id):
#         return self.general_awsem_check_log_msg_template % job_id
# 
#     @property
#     def general_awsem_error_msg_template(self):
#         return "Job encountered an error " + self.general_awsem_check_log_msg_template
# 
#     def general_awsem_error_msg(self, job_id):
#         return self.general_awsem_error_msg_template % job_id
# 
# class AWSEMJobErrorException(Exception):
#     """There is an error from a worklow run on the EC2 AWSEM instance."""
#     pass


import pytest
from tibanna.exceptions import (
    AWSEMErrorHandler,
    AWSEMJobErrorException
)

def test_awsem_exception_not_enough_space_for_input():
    log = "sometext some text some other text " + \
          "download failed: s3://somebucket/somefile to ../../data1/input/somefile " + \
          "[Errno 28] No space left on device " + \
          "some other text some other text"
    eh = AWSEMErrorHandler()
    res = eh.parse_log(log)
    assert res
    with pytest.raises(AWSEMJobErrorException) as exec_info:
        raise res
    assert 'Not enough space for input files' in str(exec_info.value)


def test_awsem_exception_no_space_for_docker():
    log = "failed to register layer: Error processing tar file(exit status 1): " + \
          "write /miniconda3/pkgs/python-3.7.6-h0371630_2.tar.bz2: no space left on device" + \
          "some text some text"
    eh = AWSEMErrorHandler()
    res = eh.parse_log(log)
    assert res
    with pytest.raises(AWSEMJobErrorException) as exec_info:
        raise res
    assert 'No space for docker' in str(exec_info.value)
    assert 'tar.bz2: no space left' in str(exec_info.value)


def test_awsem_exception_no_space():
    log = '[fputs] No space left on device' + \
          'some text some text'

# Path: tibanna/exceptions.py
# class AWSEMErrorHandler(object):
# 
#     def __init__(self):
#         self.ErrorList = self._ErrorList  # initial error list, custom errors can be added
# 
#     class AWSEMError(object):
#         def __init__(self, error_type, pattern_in_log, multiline=False):
#             self.error_type = error_type
#             if multiline:
#                 self.pattern_in_log = re.compile(pattern_in_log, re.MULTILINE)
#             else:
#                 self.pattern_in_log = pattern_in_log
# 
#     def add_custom_errors(self, custom_err_list):
#         """add custom errors to ErrorList.
#         custom_err_list is a list of dictionaries w/ keys 'error_type', 'pattern', 'multiline'"""
#         for err in custom_err_list:
#             self.ErrorList.append(self.AWSEMError(err['error_type'], err['pattern'], err.get('multiline', False)))
# 
#     @property
#     def _ErrorList(self):
#         """add any specific error types with recognizable strings or patterns here.
#         the order is important. The earlier ones are checked first and if there is a match,
#         the later ones will not be checked."""
#         return [
#             # input download failure due to not enough disk space
#             self.AWSEMError('Not enough space for input files', 'download failed: .+ No space left on device'),
#             # Docker pull failure due to not enough root disk space
#             self.AWSEMError('No space for docker', 'failed to register layer.+no space left on device'),
#             # not enough disk space
#             self.AWSEMError('Not enough space', '.+No space left on device'),
#             # CWL missing input error
#             self.AWSEMError('CWL missing input', 'Missing required input parameter\n.+\n', True),
#             # Bucket access error
#             self.AWSEMError('Bucket/file access denied', 'when calling the ListObjectsV2 operation: Access Denied')
#         ]
# 
#     def parse_log(self, log):
#         # for ex in self.AWSEMErrorExceptionList:
#         for ex in self.ErrorList:
#             res = re.search(ex.pattern_in_log, log)
#             if res:
#                 match = res.string[res.regs[0][0]:res.regs[0][1]]
#                 match = re.sub('\n', ' ', match)  # \n not recognized and subsequent content is dropped from Exception
#                 match = re.sub(' +', ' ', match)
#                 msg = "%s: %s" % (ex.error_type, match)
#                 return AWSEMJobErrorException(msg)
#         return
# 
#     @property
#     def general_awsem_check_log_msg_template(self):
#         return "check log using tibanna log --job-id=%s [--sfn=stepfunction]"
# 
#     def general_awsem_check_log_msg(self, job_id):
#         return self.general_awsem_check_log_msg_template % job_id
# 
#     @property
#     def general_awsem_error_msg_template(self):
#         return "Job encountered an error " + self.general_awsem_check_log_msg_template
# 
#     def general_awsem_error_msg(self, job_id):
#         return self.general_awsem_error_msg_template % job_id
# 
# class AWSEMJobErrorException(Exception):
#     """There is an error from a worklow run on the EC2 AWSEM instance."""
#     pass


# Path: tests/tibanna/unicorn/test_exceptions.py
import pytest
from tibanna.exceptions import (
    AWSEMErrorHandler,
    AWSEMJobErrorException
)


def test_awsem_exception_not_enough_space_for_input():
    log = "sometext some text some other text " + \
          "download failed: s3://somebucket/somefile to ../../data1/input/somefile " + \
          "[Errno 28] No space left on device " + \
          "some other text some other text"
    eh = AWSEMErrorHandler()
    res = eh.parse_log(log)
    assert res
    with pytest.raises(AWSEMJobErrorException) as exec_info:
        raise res
    assert 'Not enough space for input files' in str(exec_info.value)


def test_awsem_exception_no_space_for_docker():
    log = "failed to register layer: Error processing tar file(exit status 1): " + \
          "write /miniconda3/pkgs/python-3.7.6-h0371630_2.tar.bz2: no space left on device" + \
          "some text some text"
    eh = AWSEMErrorHandler()
    res = eh.parse_log(log)
    assert res
    with pytest.raises(AWSEMJobErrorException) as exec_info:
        raise res
    assert 'No space for docker' in str(exec_info.value)
    assert 'tar.bz2: no space left' in str(exec_info.value)


def test_awsem_exception_no_space():
    log = '[fputs] No space left on device' + \
          'some text some text'

    eh = AWSEMErrorHandler()