# 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):


from pytest import raises
from jenkinsflow.flow import parallel, serial, BuildResult, FailedChildJobException, FinalResultException
from .framework import api_select
from .framework.cfg import jenkins_security


def test_propagate_unstable_serial_inner_fail_after(api_type):
    with api_select.api(__file__, api_type, login=True) as api:
        api.flow_job()
        api.job('j11', max_fails=0, expect_invocations=1, expect_order=1)
        api.job('j21', max_fails=0, expect_invocations=1, expect_order=2)
        api.job('j22_unstable', max_fails=0, expect_invocations=1, expect_order=3, final_result='unstable')
        api.job('j23_fail', max_fails=1, expect_invocations=1, expect_order=4)

        with raises(FailedChildJobException):
            with serial(api, timeout=70, job_name_prefix=api.job_name_prefix) as ctrl1:
                ctrl1.invoke('j11')
                with ctrl1.serial() as ctrl2:
                    ctrl2.invoke('j21')
                    ctrl2.invoke('j22_unstable')
                    ctrl2.invoke('j23_fail')


def test_propagate_unstable_parallel_inner_fail_before(api_type):
    with api_select.api(__file__, api_type, login=True) as api:
        api.flow_job()
        api.job('j11', max_fails=0, expect_invocations=1, expect_order=1)
        api.job('j21_fail', max_fails=1, expect_invocations=1, expect_order=2, serial=True)
        api.job('j22_unstable', max_fails=0, expect_invocations=1, expect_order=2, final_result='unstable')
        api.job('j23', max_fails=0, expect_invocations=1, expect_order=2)

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

# 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/propagate_unstable_test.py
from pytest import raises
from jenkinsflow.flow import parallel, serial, BuildResult, FailedChildJobException, FinalResultException
from .framework import api_select
from .framework.cfg import jenkins_security



def test_propagate_unstable_serial_inner_fail_after(api_type):
    with api_select.api(__file__, api_type, login=True) as api:
        api.flow_job()
        api.job('j11', max_fails=0, expect_invocations=1, expect_order=1)
        api.job('j21', max_fails=0, expect_invocations=1, expect_order=2)
        api.job('j22_unstable', max_fails=0, expect_invocations=1, expect_order=3, final_result='unstable')
        api.job('j23_fail', max_fails=1, expect_invocations=1, expect_order=4)

        with raises(FailedChildJobException):
            with serial(api, timeout=70, job_name_prefix=api.job_name_prefix) as ctrl1:
                ctrl1.invoke('j11')
                with ctrl1.serial() as ctrl2:
                    ctrl2.invoke('j21')
                    ctrl2.invoke('j22_unstable')
                    ctrl2.invoke('j23_fail')


def test_propagate_unstable_parallel_inner_fail_before(api_type):
    with api_select.api(__file__, api_type, login=True) as api:
        api.flow_job()
        api.job('j11', max_fails=0, expect_invocations=1, expect_order=1)
        api.job('j21_fail', max_fails=1, expect_invocations=1, expect_order=2, serial=True)
        api.job('j22_unstable', max_fails=0, expect_invocations=1, expect_order=2, final_result='unstable')
        api.job('j23', max_fails=0, expect_invocations=1, expect_order=2)

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

                with ctrl1.parallel() as ctrl2: