xref: /openbmc/openbmc-test-automation/lib/gen_robot_keyword.py (revision 20f38712b324e61a94e174017c487a0af4b373e1)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2bcc404dfSMichael Walsh
3bcc404dfSMichael Walshr"""
4bcc404dfSMichael WalshThis module provides robot keyword execution functions such as run_key..
5bcc404dfSMichael Walsh"""
6bcc404dfSMichael Walsh
7bcc404dfSMichael Walshimport gen_print as gp
8bcc404dfSMichael Walshfrom robot.libraries.BuiltIn import BuiltIn
9bcc404dfSMichael Walsh
10bcc404dfSMichael Walsh
11*20f38712SPatrick Williamsdef run_key(keyword_buf, quiet=None, test_mode=None, ignore=0):
12bcc404dfSMichael Walsh    r"""
13bcc404dfSMichael Walsh    Run the given keyword, return the status and the keyword return values.
14bcc404dfSMichael Walsh
15410b1787SMichael Walsh    The advantage of using this function verses having robot simply run your keyword is the handling of
16410b1787SMichael Walsh    parameters like quiet, test_mode and ignore.
17bcc404dfSMichael Walsh
18bcc404dfSMichael Walsh    Description of arguments:
19bcc404dfSMichael Walsh    keyword_buf                     The keyword string to be run.
20410b1787SMichael Walsh    quiet                           Indicates whether this function should run the pissuing function to print
21410b1787SMichael Walsh                                    'Issuing: <keyword string>' to stdout.
22410b1787SMichael Walsh    test_mode                       If test_mode is set, this function will not actually run the command.  If
23410b1787SMichael Walsh                                    quiet is 0, it will print a message indicating what it would have run
24410b1787SMichael Walsh                                    (e.g. "Issuing: (test_mode) your command").
25410b1787SMichael Walsh    ignore                          Ignore errors from running keyword.  If this is 0, this function will
26410b1787SMichael Walsh                                    fail with whatever error occurred when running the keyword.
27bcc404dfSMichael Walsh
28bcc404dfSMichael Walsh    Example usage from a robot script:
29bcc404dfSMichael Walsh
30bcc404dfSMichael Walsh    ${status}  ${ret_values}=  Run Key  My Keyword \ Arg1 \ Arg2
31bcc404dfSMichael Walsh
32410b1787SMichael Walsh    Note that to get robot to pass your command + args as a single string to this function, you must escape
33410b1787SMichael Walsh    extra spaces with a backslash.
34bcc404dfSMichael Walsh
35bcc404dfSMichael Walsh    Also note that ret_values is a python list:
36bcc404dfSMichael Walsh    ret_values:
37bcc404dfSMichael Walsh      ret_values[0]:    value1
38bcc404dfSMichael Walsh      ret_values[1]:    value2
39bcc404dfSMichael Walsh    """
40bcc404dfSMichael Walsh
41bcc404dfSMichael Walsh    # Set these vars to default values if they are None.
42bcc404dfSMichael Walsh    quiet = int(gp.get_var_value(quiet, 0))
43bcc404dfSMichael Walsh    test_mode = int(gp.get_var_value(test_mode, 0))
44bcc404dfSMichael Walsh    ignore = int(ignore)
45bcc404dfSMichael Walsh
46410b1787SMichael Walsh    # Convert the keyword_buf into a list split wherever 2 or more spaces are found.
47*20f38712SPatrick Williams    keyword_list = keyword_buf.split("  ")
48410b1787SMichael Walsh    # Strip spaces from each argument to make the output look clean and uniform.
49*20f38712SPatrick Williams    keyword_list = [item.strip(" ") for item in keyword_list]
50bcc404dfSMichael Walsh
51bcc404dfSMichael Walsh    if not quiet:
52bcc404dfSMichael Walsh        # Join the list back into keyword_buf for the sake of output.
53*20f38712SPatrick Williams        keyword_buf = "  ".join(keyword_list)
54bcc404dfSMichael Walsh        gp.pissuing(keyword_buf, test_mode)
55bcc404dfSMichael Walsh
56bcc404dfSMichael Walsh    if test_mode:
57*20f38712SPatrick Williams        return "PASS", ""
58bcc404dfSMichael Walsh
59dfcbaa8fSMichael Walsh    try:
60*20f38712SPatrick Williams        status, ret_values = BuiltIn().run_keyword_and_ignore_error(
61*20f38712SPatrick Williams            *keyword_list
62*20f38712SPatrick Williams        )
63dfcbaa8fSMichael Walsh    except Exception as my_assertion_error:
64dfcbaa8fSMichael Walsh        status = "FAIL"
65dfcbaa8fSMichael Walsh        ret_values = my_assertion_error.args[0]
66bcc404dfSMichael Walsh
67*20f38712SPatrick Williams    if status != "PASS":
68bcc404dfSMichael Walsh        # Output the error message to stderr.
69*20f38712SPatrick Williams        BuiltIn().log_to_console(ret_values, stream="STDERR")
70efb3d17aSMichael Walsh        if not ignore:
71bcc404dfSMichael Walsh            # Fail with the given error message.
72bcc404dfSMichael Walsh            BuiltIn().fail(ret_values)
73bcc404dfSMichael Walsh
74bcc404dfSMichael Walsh    return status, ret_values
75bcc404dfSMichael Walsh
76bcc404dfSMichael Walsh
77*20f38712SPatrick Williamsdef run_key_u(keyword_buf, quiet=None, ignore=0):
78bcc404dfSMichael Walsh    r"""
79410b1787SMichael Walsh    Run keyword unconditionally (i.e. without regard to global test_mode setting).
80bcc404dfSMichael Walsh
81410b1787SMichael Walsh    This function will simply call the run_key function passing on all of the callers parameters except
82410b1787SMichael Walsh    test_mode which will be hard-coded to 0.  See run_key (above) for details.
83bcc404dfSMichael Walsh
84bcc404dfSMichael Walsh    See the proglog of "run_key" function above for description of arguments.
85bcc404dfSMichael Walsh    """
86bcc404dfSMichael Walsh
87bcc404dfSMichael Walsh    return run_key(keyword_buf, test_mode=0, quiet=quiet, ignore=ignore)
88