Ejemplo n.º 1
0
def test(mocker, mock_fs, args, sipp_retcode, expected_ret,
         expected_elapsed_min, expected_elapsed_delta):
    """Testing SIPpTest basic sanity
    """
    dirpath = tempfile.mkdtemp(prefix="sipplauncher_test_Test")
    gen_file_struct(dirpath, mock_fs)

    def my_pysipp_process_run(self):
        time.sleep(SIPP_MOCK_RUN_TIME)  # emulate SIPp run
        sys.exit(sipp_retcode)

    mocker.patch('sipplauncher.PysippProcess.PysippProcess.run',
                 new=my_pysipp_process_run)
    logging.getLogger("pysipp").propagate = 0

    parser = generate_parser()
    parsed_args = parser.parse_args(shlex.split(args))
    parsed_args.testsuite = dirpath
    check_and_patch_args(parsed_args)

    start = time.time()
    ret = run(parsed_args)
    end = time.time()
    elapsed = end - start

    assert (ret == expected_ret)
    assert (expected_elapsed_min <= elapsed
            and elapsed <= expected_elapsed_min + expected_elapsed_delta)

    shutil.rmtree(dirpath)
Ejemplo n.º 2
0
def test(mock_fs, args, expected):
    """Testing command-line argument combinations
    """
    dirpath = tempfile.mkdtemp(prefix="sipplauncher_test_args_")
    gen_file_struct(dirpath, mock_fs)

    with cd(dirpath):
        parser = generate_parser()
        if isinstance(expected, BaseException):
            with pytest.raises(type(expected)):
                parsed_args = parser.parse_args(shlex.split(args))
                check_and_patch_args(parsed_args)
        else:
            parsed_args = parser.parse_args(shlex.split(args))
            check_and_patch_args(parsed_args)
Ejemplo n.º 3
0
def test(mocker, mock_fs, args, expected):
    """Testing SIPpTest basic sanity
    """
    dirpath = tempfile.mkdtemp(prefix="sipplauncher_test_Test")
    gen_file_struct(dirpath, mock_fs)

    mocker.patch('sipplauncher.PysippProcess.PysippProcess.run',
                 new=lambda x: sys.exit(0))
    logging.getLogger("pysipp").propagate = 0

    def do_test(dirpath, args):
        test = SIPpTest(os.path.join(dirpath, TEST_NAME))
        test.pre_run(args)
        try:
            # First test __do_run() method. It can raise exception.
            if test.state == STATES[1]:
                test._SIPpTest__do_run("", args)

            # Then test run() method. It shouldn't raise exception.
            test.run("", args)
        finally:
            # Always post_run after successful pre_run
            # to not to leave network config from previous run
            test.post_run(args)

        # Issue #9: Create dynamic execution test temp folder for each test execution
        exists = os.path.isdir(test._SIPpTest__temp_folder)
        assert (exists == args.leave_temp)

        return test.state

    parser = generate_parser()
    parsed_args = parser.parse_args(shlex.split(args))
    parsed_args.testsuite = dirpath

    if isinstance(expected, BaseException):
        # Exception is expected
        with pytest.raises(type(expected)):
            check_and_patch_args(parsed_args)
            do_test(dirpath, parsed_args)
    else:
        # Exception is not expected
        check_and_patch_args(parsed_args)
        do_test(dirpath, parsed_args) == expected

    shutil.rmtree(dirpath)
def test(mocker, mock_fs, args, expected):
    """Testing SIPpTest keyword replacement
    """
    dirpath = tempfile.mkdtemp(
        prefix="sipplauncher_test_Test_keyword_replacement_")
    sipplauncher.utils.Utils.gen_file_struct(dirpath, mock_fs)

    test = SIPpTest(os.path.join(dirpath, TEST_NAME))

    def gen_ua_ip():
        for ip in UA_IP:
            yield ip

    ip_gen = gen_ua_ip()

    # Replace original random IP generator with the custom generator.
    # The latter generates IP consecutively from UA_IP list.
    # Therefore IP allocation happens not randomly, but in known order.
    # Knowing generated IP addresses beforehand allows us to unit-test the functionality.
    mocker.patch('sipplauncher.Network.SIPpNetwork.add_random_ip',
                 new=lambda x: next(ip_gen))
    mocker.patch('sipplauncher.utils.Utils.generate_id',
                 return_value=TEST_RUN_ID)

    parser = generate_parser()
    parsed_args = parser.parse_args(shlex.split(args))
    parsed_args.testsuite = dirpath
    check_and_patch_args(parsed_args)

    test.pre_run(parsed_args)

    assert (test.state == STATES[1])

    def check_folder(fs_path, root):
        for a, b in iter(root.items()):
            tmp_fs_path = os.path.join(fs_path, a)
            with open(tmp_fs_path, 'r') as f:
                content = f.read()
                assert (content == b)

    check_folder(test._SIPpTest__temp_folder, expected)

    test.post_run(parsed_args)

    shutil.rmtree(dirpath)
Ejemplo n.º 5
0
def test(mocker, mock_fs, args, expected):
    """Testing SIPpTest basic sanity
    """
    dirpath = tempfile.mkdtemp(prefix="sipplauncher_test_Test")
    gen_file_struct(dirpath, mock_fs)

    # mock_call() is called from spawned process.
    # Therfore, we need to use Queue to get data back to us.
    # Just list in memory won't work.
    q = Queue()

    def mock_call(self):
        q.put([ agent.scen_file for agent in self._agents ])

    mocker.patch('pysipp.agent.ScenarioType.__call__', new=mock_call)

    logging.getLogger("pysipp").propagate = 0
    logging.getLogger("scapy.runtime").setLevel(logging.ERROR) # to not to spoil test output with warnings

    parser = generate_parser()
    parsed_args = parser.parse_args(shlex.split(args))
    parsed_args.testsuite = dirpath

    # Exception is not expected
    check_and_patch_args(parsed_args)

    test = SIPpTest(os.path.join(dirpath, TEST_NAME))
    test.pre_run(parsed_args)
    try:
        test.run("", parsed_args)
    finally:
        # Always post_run to not to leave network config from previous run
        test.post_run(parsed_args)

    res = []
    while not q.empty():
        res.append(q.get())

    assert(res == expected)

    shutil.rmtree(dirpath)