Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6fac733d authored by Brenda J. Butler's avatar Brenda J. Butler Committed by David S. Miller
Browse files

tools: tc-testing: Refactor test-runner



Split the test_runner function into the loop part (test_runner)
and the contents (run_one_test) for maintainability.
It makes it a little easier to catch exceptions
in an individual test, and keep going (and flush a bunch
of tap results for the skipped tests).

Signed-off-by: default avatarBrenda J. Butler <bjb@mojatatu.com>
Acked-by: default avatarLucas Bates <lucasb@mojatatu.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent f87c7f64
Loading
Loading
Loading
Loading
+52 −29
Original line number Diff line number Diff line
@@ -85,29 +85,13 @@ def prepare_env(cmdlist):
            print("\nError message:")
            print(foutput)
            print("\nAborting test run.")
            ns_destroy()
            exit(1)


def test_runner(filtered_tests, args):
    """
    Driver function for the unit tests.
            # ns_destroy()
            raise Exception('prepare_env did not complete successfully')

    Prints information about the tests being run, executes the setup and
    teardown commands and the command under test itself. Also determines
    success/failure based on the information in the test case and generates
    TAP output accordingly.
    """
    testlist = filtered_tests
    tcount = len(testlist)
    index = 1
    tap = str(index) + ".." + str(tcount) + "\n"

    for tidx in testlist:
def run_one_test(index, tidx):
    result = True
    tresult = ""
        if "flower" in tidx["category"] and args.device == None:
            continue
    tap = ""
    print("Test " + tidx["id"] + ": " + tidx["name"])
    prepare_env(tidx["setup"])
    (p, procout) = exec_cmd(tidx["cmdUnderTest"])
@@ -118,17 +102,17 @@ def test_runner(filtered_tests, args):
        print("exit:", exit_code, int(tidx["expExitCode"]))
        print(procout)
    else:
            match_pattern = re.compile(str(tidx["matchPattern"]), re.DOTALL)
        match_pattern = re.compile(str(tidx["matchPattern"]),
                                   re.DOTALL | re.MULTILINE)
        (p, procout) = exec_cmd(tidx["verifyCmd"])
        match_index = re.findall(match_pattern, procout)
        if len(match_index) != int(tidx["matchCount"]):
            result = False

        if result == True:
            tresult += "ok "
        else:
            tresult += "not ok "
        tap += tresult + str(index) + " " + tidx["id"] + " " + tidx["name"] + "\n"
    if not result:
        tresult += "not "
    tresult += "ok {} - {} # {}\n".format(str(index), tidx['id'], tidx["name"])
    tap += tresult

    if result == False:
        tap += procout
@@ -138,6 +122,45 @@ def test_runner(filtered_tests, args):

    return tap

def test_runner(filtered_tests, args):
    """
    Driver function for the unit tests.

    Prints information about the tests being run, executes the setup and
    teardown commands and the command under test itself. Also determines
    success/failure based on the information in the test case and generates
    TAP output accordingly.
    """
    testlist = filtered_tests
    tcount = len(testlist)
    index = 1
    tap = str(index) + ".." + str(tcount) + "\n"

    for tidx in testlist:
        if "flower" in tidx["category"] and args.device == None:
            continue
        try:
            badtest = tidx  # in case it goes bad
            tap += run_one_test(index, tidx)
        except Exception as ee:
            print('Exception {} (caught in test_runner, running test {} {} {})'.
                  format(ee, index, tidx['id'], tidx['name']))
            break
        index += 1

    count = index
    tap += 'about to flush the tap output if tests need to be skipped\n'
    if tcount + 1 != index:
        for tidx in testlist[index - 1:]:
            msg = 'skipped - previous setup or teardown failed'
            tap += 'ok {} - {} # {} {} {} \n'.format(
                count, tidx['id'], msg, index, badtest.get('id', '--Unknown--'))
            count += 1

    tap += 'done flushing skipped test tap output\n'

    return tap


def ns_create():
    """