xref: /openbmc/openbmc-test-automation/lib/gen_robot_valid.py (revision 3e26e109037c0d68a062eec802fd554c0a6234a0)
118176322SMichael Walsh#!/usr/bin/env python
218176322SMichael Walsh
318176322SMichael Walshr"""
418176322SMichael WalshThis file contains functions useful for validating variables in robot.
518176322SMichael Walsh"""
618176322SMichael Walsh
718176322SMichael Walshimport gen_robot_print as grp
818176322SMichael Walshimport gen_valid as gv
918176322SMichael Walsh
1018176322SMichael Walshfrom robot.libraries.BuiltIn import BuiltIn
1118176322SMichael Walshfrom robot.api import logger
1218176322SMichael Walsh
1318176322SMichael Walsh
1418176322SMichael Walsh###############################################################################
1518176322SMichael Walshdef rvalid_value(var_name,
1618176322SMichael Walsh                 invalid_values=[],
1718176322SMichael Walsh                 valid_values=[]):
1818176322SMichael Walsh
1918176322SMichael Walsh    r"""
20*3e26e109SMichael Walsh    Validate a robot value.
21*3e26e109SMichael Walsh
22*3e26e109SMichael Walsh    This function is the robot wrapper for gen_robot_print.svalid_value.
2318176322SMichael Walsh
2418176322SMichael Walsh    Description of arguments:
2518176322SMichael Walsh    var_name                        The name of the variable whose value is to
2618176322SMichael Walsh                                    be validated.
2718176322SMichael Walsh    invalid_values                  A list of invalid values.  If var_value is
2818176322SMichael Walsh                                    equal to any of these, it is invalid.
2918176322SMichael Walsh                                    Note that if you specify anything for
3018176322SMichael Walsh                                    invalid_values (below), the valid_values
3118176322SMichael Walsh                                    list is not even processed.
3218176322SMichael Walsh    valid_values                    A list of invalid values.  var_value must
3318176322SMichael Walsh                                    be equal to one of these values to be
3418176322SMichael Walsh                                    considered valid.
3518176322SMichael Walsh
3618176322SMichael Walsh    Examples of robot calls and corresponding output:
3718176322SMichael Walsh
3818176322SMichael Walsh    Robot code...
3918176322SMichael Walsh    rvalid_value                    MY_PARM
4018176322SMichael Walsh
4118176322SMichael Walsh    Output...
4218176322SMichael Walsh    #(CDT) 2016/11/02 10:04:20 - **ERROR** Variable "MY_PARM" not found (i.e.
4318176322SMichael Walsh    #it's undefined).
4418176322SMichael Walsh
4518176322SMichael Walsh    or if it is defined but blank:
4618176322SMichael Walsh
4718176322SMichael Walsh    Output...
4818176322SMichael Walsh    #(CDT) 2016/11/02 10:14:24 - **ERROR** The following variable has an
4918176322SMichael Walsh    #invalid value:
5018176322SMichael Walsh    MY_PARM:
5118176322SMichael Walsh
5218176322SMichael Walsh    It must NOT be one of the following values:
5318176322SMichael Walsh    invalid_values:
5418176322SMichael Walsh      invalid_values[0]:         <blank>
5518176322SMichael Walsh
5618176322SMichael Walsh    Robot code...
5718176322SMichael Walsh    ${invalid_values}=  Create List  one  two  three
5818176322SMichael Walsh    ${MY_PARM}=  Set Variable  one
5918176322SMichael Walsh    rvalid_value                    MY_PARM  invalid_values=${invalid_values}
6018176322SMichael Walsh
6118176322SMichael Walsh    Output...
6218176322SMichael Walsh    #(CDT) 2016/11/02 10:20:05 - **ERROR** The following variable has an
6318176322SMichael Walsh    #invalid value:
6418176322SMichael Walsh    MY_PARM:                     one
6518176322SMichael Walsh
6618176322SMichael Walsh    It must NOT be one of the following values:
6718176322SMichael Walsh    invalid_values:
6818176322SMichael Walsh        invalid_values[0]:       one
6918176322SMichael Walsh        invalid_values[1]:       two
7018176322SMichael Walsh        invalid_values[2]:       three
7118176322SMichael Walsh
7218176322SMichael Walsh    """
7318176322SMichael Walsh
7418176322SMichael Walsh    # Note: get_variable_value() seems to have no trouble with local variables.
7518176322SMichael Walsh    var_value = BuiltIn().get_variable_value("${" + var_name + "}")
7618176322SMichael Walsh
7718176322SMichael Walsh    if var_value is None:
7818176322SMichael Walsh        var_value = ""
7918176322SMichael Walsh        error_message = "Variable \"" + var_name +\
8018176322SMichael Walsh                        "\" not found (i.e. it's undefined).\n"
8118176322SMichael Walsh    else:
8218176322SMichael Walsh        error_message = gv.svalid_value(var_value, invalid_values,
8318176322SMichael Walsh                                        valid_values, var_name)
8418176322SMichael Walsh    if not error_message == "":
858fb1f53eSMichael Walsh        error_message = grp.sprint_error_report(error_message)
8618176322SMichael Walsh        BuiltIn().fail(error_message)
8718176322SMichael Walsh
8818176322SMichael Walsh###############################################################################
8918176322SMichael Walsh
9018176322SMichael Walsh
9118176322SMichael Walsh###############################################################################
9218176322SMichael Walshdef rvalid_integer(var_name):
9318176322SMichael Walsh
9418176322SMichael Walsh    r"""
95*3e26e109SMichael Walsh    Validate a robot integer.
96*3e26e109SMichael Walsh
97*3e26e109SMichael Walsh    This function is the robot wrapper for gen_robot_print.svalid_integer.
9818176322SMichael Walsh
9918176322SMichael Walsh    Description of arguments:
10018176322SMichael Walsh    var_name                        The name of the variable whose value is to
10118176322SMichael Walsh                                    be validated.
10218176322SMichael Walsh
10318176322SMichael Walsh    Examples of robot calls and corresponding output:
10418176322SMichael Walsh
10518176322SMichael Walsh    Robot code...
10618176322SMichael Walsh    Rvalid Integer  MY_PARM
10718176322SMichael Walsh
10818176322SMichael Walsh    Output...
10918176322SMichael Walsh    #(CDT) 2016/11/02 10:44:43 - **ERROR** Variable "MY_PARM" not found (i.e.
11018176322SMichael Walsh    #it's undefined).
11118176322SMichael Walsh
11218176322SMichael Walsh    or if it is defined but blank:
11318176322SMichael Walsh
11418176322SMichael Walsh    Output...
11518176322SMichael Walsh    #(CDT) 2016/11/02 10:45:37 - **ERROR** Invalid integer value:
11618176322SMichael Walsh    MY_PARM:                     <blank>
11718176322SMichael Walsh
11818176322SMichael Walsh    Robot code...
11918176322SMichael Walsh    ${MY_PARM}=  Set Variable  HELLO
12018176322SMichael Walsh    Rvalid Integer  MY_PARM
12118176322SMichael Walsh
12218176322SMichael Walsh    Output...
12318176322SMichael Walsh    #(CDT) 2016/11/02 10:46:18 - **ERROR** Invalid integer value:
12418176322SMichael Walsh    MY_PARM:                     HELLO
12518176322SMichael Walsh
12618176322SMichael Walsh    """
12718176322SMichael Walsh
12818176322SMichael Walsh    # Note: get_variable_value() seems to have no trouble with local variables.
12918176322SMichael Walsh    var_value = BuiltIn().get_variable_value("${" + var_name + "}")
13018176322SMichael Walsh
13118176322SMichael Walsh    if var_value is None:
13218176322SMichael Walsh        var_value = ""
13318176322SMichael Walsh        error_message = "Variable \"" + var_name +\
13418176322SMichael Walsh                        "\" not found (i.e. it's undefined).\n"
13518176322SMichael Walsh    else:
13618176322SMichael Walsh        error_message = gv.svalid_integer(var_value, var_name)
13718176322SMichael Walsh    if not error_message == "":
1388fb1f53eSMichael Walsh        error_message = grp.sprint_error_report(error_message)
13918176322SMichael Walsh        BuiltIn().fail(error_message)
14018176322SMichael Walsh
14118176322SMichael Walsh###############################################################################
142