# Path: test/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2
# 
# 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/utils.py
# def lines_in(api_type, text, *expected_lines):
#     """Assert that `*expected_lines` occur in order in the lines of `text`.
# 
#     Args:
#         *expected_lines (str or RegexObject (hasattr `match`)): For each `expected_line` in expected_lines:
#             If `expected_line` has a match method it is called and must return True for a line in `text`.
#             Otherwise, if the `expected_line` starts with '^', a line in `text` must start with `expected_line[1:]`
#             Otherwise `expected line` must simply occur in a line in `text`
#     """
# 
#     assert isinstance(api_type, test_cfg.ApiType)
#     assert text
#     assert expected_lines
# 
#     return generic_lines_in(text, partial(replace_host_port, api_type), *expected_lines)
# 
# def kill_current_msg(api, job_name, num):
#     return "Killing build: " + repr(job_name) + " - " + console_url(api, job_name, num)
# 
# Path: test/framework/killer.py
# def kill(api, sleep_time, num_kills):
#     """Kill this process"""
#     pid = os.getpid()
#     ff = __file__.replace('.pyc', '.py')
#     log_file_name = api.func_name.replace('test_', '')
#     args = [sys.executable, ff, repr(pid), repr(sleep_time), repr(num_kills), log_file_name]
#     with open(log_file_name, 'w') as log_file:
#         logt(log_file, "Invoking kill subprocess.", args)
# 
#     subprocess.Popen(args)


import os, re
import pytest
from pytest import raises, xfail
from jenkinsflow.flow import serial, FailedChildJobException, FinalResultException, BuildResult
from .cfg import ApiType
from .framework import api_select
from .framework.utils import lines_in, kill_current_msg
from .framework.killer import kill
            with serial(api, timeout=70, job_name_prefix=api.job_name_prefix, kill_all=kill_all) as ctrl1:
                with ctrl1.parallel() as ctrl2:
                    ctrl2.invoke_unchecked('j1')
                    ctrl2.invoke_unchecked('j2')
                with ctrl1.parallel() as ctrl3:
                    ctrl3.invoke_unchecked('j3')
                    ctrl3.invoke_unchecked('j3') # Queue
                    ctrl3.invoke_unchecked('j3') # Queue
                    ctrl3.invoke_unchecked('j4')
                    ctrl3.invoke_unchecked('j5')

        # Invoke the flow
        flow(api, False)

        # Make sure job has actually started before entering new flow
        api.sleep(5)

        if capsys:
            sout, _ = capsys.readouterr()
            assert lines_in(api_type, sout, "unchecked job: 'jenkinsflow_test__kill_all_unchecked__j1' UNKNOWN - RUNNING")
            assert lines_in(api_type, sout, "unchecked job: 'jenkinsflow_test__kill_all_unchecked__j3' Invocation-1 UNKNOWN - RUNNING")

        # Kill the flow
        flow(api, True)

        if not capsys:
            # Not called by pytest, but from flow hudson job
            return

        sout, _ = capsys.readouterr()

# Path: test/cfg.py
# class ApiType(Enum):
#     JENKINS = 0
#     SCRIPT = 1
#     MOCK = 2
# 
# 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/utils.py
# def lines_in(api_type, text, *expected_lines):
#     """Assert that `*expected_lines` occur in order in the lines of `text`.
# 
#     Args:
#         *expected_lines (str or RegexObject (hasattr `match`)): For each `expected_line` in expected_lines:
#             If `expected_line` has a match method it is called and must return True for a line in `text`.
#             Otherwise, if the `expected_line` starts with '^', a line in `text` must start with `expected_line[1:]`
#             Otherwise `expected line` must simply occur in a line in `text`
#     """
# 
#     assert isinstance(api_type, test_cfg.ApiType)
#     assert text
#     assert expected_lines
# 
#     return generic_lines_in(text, partial(replace_host_port, api_type), *expected_lines)
# 
# def kill_current_msg(api, job_name, num):
#     return "Killing build: " + repr(job_name) + " - " + console_url(api, job_name, num)
# 
# Path: test/framework/killer.py
# def kill(api, sleep_time, num_kills):
#     """Kill this process"""
#     pid = os.getpid()
#     ff = __file__.replace('.pyc', '.py')
#     log_file_name = api.func_name.replace('test_', '')
#     args = [sys.executable, ff, repr(pid), repr(sleep_time), repr(num_kills), log_file_name]
#     with open(log_file_name, 'w') as log_file:
#         logt(log_file, "Invoking kill subprocess.", args)
# 
#     subprocess.Popen(args)


# Path: test/kill_test.py
import os, re
import pytest
from pytest import raises, xfail
from jenkinsflow.flow import serial, FailedChildJobException, FinalResultException, BuildResult
from .cfg import ApiType
from .framework import api_select
from .framework.utils import lines_in, kill_current_msg
from .framework.killer import kill

            with serial(api, timeout=70, job_name_prefix=api.job_name_prefix, kill_all=kill_all) as ctrl1:
                with ctrl1.parallel() as ctrl2:
                    ctrl2.invoke_unchecked('j1')
                    ctrl2.invoke_unchecked('j2')
                with ctrl1.parallel() as ctrl3:
                    ctrl3.invoke_unchecked('j3')
                    ctrl3.invoke_unchecked('j3') # Queue
                    ctrl3.invoke_unchecked('j3') # Queue
                    ctrl3.invoke_unchecked('j4')
                    ctrl3.invoke_unchecked('j5')

        # Invoke the flow
        flow(api, False)

        # Make sure job has actually started before entering new flow
        api.sleep(5)

        if capsys:
            sout, _ = capsys.readouterr()
            assert lines_in(api_type, sout, "unchecked job: 'jenkinsflow_test__kill_all_unchecked__j1' UNKNOWN - RUNNING")
            assert lines_in(api_type, sout, "unchecked job: 'jenkinsflow_test__kill_all_unchecked__j3' Invocation-1 UNKNOWN - RUNNING")

        # Kill the flow
        flow(api, True)

        if not capsys:
            # Not called by pytest, but from flow hudson job
            return

        sout, _ = capsys.readouterr()

        assert lines_in(