# Path: test/framework/api_select.py
# def api(file_name, api_type, login=None, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
#         username=None, password=None):
# 
# Path: test/framework/abort_job.py
# def abort(api, job_name, sleep_time):
#     """Call this script as a subprocess"""
#     if api.api_type == ApiType.MOCK:
#         return
# 
#     ff = __file__.replace('.pyc', '.py')
#     args = [sys.executable, ff, api.file_name, api.api_type.name, api.func_name.replace('test_', ''), job_name, str(sleep_time)]
#     with open(job_name, 'w') as log_file:
#         logt(log_file, "Invoking abort subprocess.", args)
#     subprocess.Popen(args)
# 
# Path: test/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2


import os
import pytest
from pytest import raises
from jenkinsflow.flow import parallel, serial, FailedChildJobException, FailedChildJobsException
from .framework import api_select
from .framework.abort_job import abort
from .cfg import ApiType
# Copyright (c) 2012 - 2015 Lars Hupfeldt Nielsen, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.





here = os.path.abspath(os.path.dirname(__file__))


@pytest.mark.not_apis(ApiType.SCRIPT)
def test_abort_retry_serial_toplevel(api_type):
    with api_select.api(__file__, api_type) as api:
        api.flow_job()
        api.job('j11', max_fails=0, expect_invocations=1, expect_order=1)
        api.job('j12_abort', max_fails=0, expect_invocations=1, expect_order=2, exec_time=20, serial=True, final_result='ABORTED')
        api.job('j13', max_fails=0, expect_invocations=0, expect_order=None, serial=True)

        abort(api, 'j12_abort', 10)

        with raises(FailedChildJobException):
            with serial(api, timeout=70, job_name_prefix=api.job_name_prefix, max_tries=2) as ctrl1:
                ctrl1.invoke('j11')
                ctrl1.invoke('j12_abort')
                ctrl1.invoke('j13')


@pytest.mark.not_apis(ApiType.SCRIPT)
def test_abort_retry_parallel_toplevel(api_type):

# Path: test/framework/api_select.py
# def api(file_name, api_type, login=None, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None,
#         username=None, password=None):
# 
# Path: test/framework/abort_job.py
# def abort(api, job_name, sleep_time):
#     """Call this script as a subprocess"""
#     if api.api_type == ApiType.MOCK:
#         return
# 
#     ff = __file__.replace('.pyc', '.py')
#     args = [sys.executable, ff, api.file_name, api.api_type.name, api.func_name.replace('test_', ''), job_name, str(sleep_time)]
#     with open(job_name, 'w') as log_file:
#         logt(log_file, "Invoking abort subprocess.", args)
#     subprocess.Popen(args)
# 
# Path: test/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2


# Path: test/abort_retry_test.py
import os
import pytest
from pytest import raises
from jenkinsflow.flow import parallel, serial, FailedChildJobException, FailedChildJobsException
from .framework import api_select
from .framework.abort_job import abort
from .cfg import ApiType

# Copyright (c) 2012 - 2015 Lars Hupfeldt Nielsen, Hupfeldt IT
# All rights reserved. This work is under a BSD license, see LICENSE.TXT.





here = os.path.abspath(os.path.dirname(__file__))


@pytest.mark.not_apis(ApiType.SCRIPT)
def test_abort_retry_serial_toplevel(api_type):
    with api_select.api(__file__, api_type) as api:
        api.flow_job()
        api.job('j11', max_fails=0, expect_invocations=1, expect_order=1)
        api.job('j12_abort', max_fails=0, expect_invocations=1, expect_order=2, exec_time=20, serial=True, final_result='ABORTED')
        api.job('j13', max_fails=0, expect_invocations=0, expect_order=None, serial=True)

        abort(api, 'j12_abort', 10)

        with raises(FailedChildJobException):
            with serial(api, timeout=70, job_name_prefix=api.job_name_prefix, max_tries=2) as ctrl1:
                ctrl1.invoke('j11')
                ctrl1.invoke('j12_abort')
                ctrl1.invoke('j13')


@pytest.mark.not_apis(ApiType.SCRIPT)
def test_abort_retry_parallel_toplevel(api_type):

    with api_select.api(__file__, api_type) as api: