def check_mylogin_requisites(self): """ Check if the tools to manipulate mylogin.cnf are accessible. This method verifies if the MySQL client tools my_print_defaults and mysql_config_editor are accessible. A MUTLibError exception is raised if the requisites are not met. """ try: self.login_reader = MyDefaultsReader( find_my_print_defaults_tool=True) except UtilError as err: raise MUTLibError("MySQL client tools must be accessible to run " "this test (%s). E.g. Add the location of the " "MySQL client tools to your PATH." % err.errmsg) if not self.login_reader.check_login_path_support(): raise MUTLibError( "ERROR: the used my_print_defaults tool does not " "support login-path options. Used tool: %s" % self.login_reader.tool_path) try: self.edit_tool_path = get_tool_path(None, "mysql_config_editor", search_PATH=True) except UtilError as err: raise MUTLibError("MySQL client tools must be accessible to run " "this test (%s). E.g. Add the location of the " "MySQL client tools to your PATH." % err.errmsg)
def test_MyDefaultsReader(self): # Test the creation of MyDefaultsReader object # find_my_print_defaults_tool=False cfg_reader = MyDefaultsReader(find_my_print_defaults_tool=False) self.assertIsNone(cfg_reader.tool_path) self.assertDictEqual(cfg_reader._config_data, {}) # find_my_print_defaults_tool=True (by default) cfg_reader = MyDefaultsReader() self.assertDictEqual(cfg_reader._config_data, {}) self.assertTrue(os.path.isfile(cfg_reader.tool_path)) # keep the location of the tool (to use as basedir) tool_dir = os.path.dirname(cfg_reader.tool_path) # Empty the PATH - UtilError raised if tool is not found os.environ['PATH'] = '' self.assertRaises(UtilError, MyDefaultsReader) # Specify a basedir to find the tool (PATH empty) options = {'basedir': tool_dir} cfg_reader = MyDefaultsReader(options) self.assertDictEqual(cfg_reader._config_data, {}) self.assertTrue(os.path.isfile(cfg_reader.tool_path))
def test_get_option_value(self): # Test the retrieve of specific option values from .mylogin.cnf cfg_reader = MyDefaultsReader(find_my_print_defaults_tool=False) # Test the assertion: First, my_print_defaults must be found. self.assertRaises(AssertionError, cfg_reader.get_option_value, _TEST_LOGIN_PATH, _TEST_HOST) self.assertRaises(AssertionError, cfg_reader.get_option_value, _TEST_LOGIN_PATH, _TEST_USER) cfg_reader.search_my_print_defaults_tool() expected_res = {'host': _TEST_HOST, 'user': _TEST_USER} # Get user opt_value = cfg_reader.get_option_value(_TEST_LOGIN_PATH, 'user') self.assertEqual(expected_res['user'], opt_value) # Get host opt_value = cfg_reader.get_option_value(_TEST_LOGIN_PATH, 'host') self.assertEqual(expected_res['host'], opt_value) # Return None if the specified option/group does not exist opt_value = cfg_reader.get_option_value(_TEST_LOGIN_PATH, 'unknown') self.assertIsNone(opt_value) opt_value = cfg_reader.get_option_value(_TEST_UNKNOWN_LOGIN_PATH, 'user') self.assertIsNone(opt_value) opt_value = cfg_reader.get_option_value(_TEST_UNKNOWN_LOGIN_PATH, 'host') self.assertIsNone(opt_value)
def test_check_login_path_support(self): # Test if the my_print_defaults tool supports login-path options # NOTE: The version of the my_print_defaults tool as not been updated # properly (latest known version is 1.6). Therefore, not all the # my_print_defaults tools that report the version 1.6 support the use # of login-path. cfg_reader = MyDefaultsReader() self.assertTrue(cfg_reader.check_login_path_support(), "The used my_prints_defaults tool does not support " "login-path options: %s. Please add the location of a" "tool that support login-path to your PATH (at the " "beginning)." % cfg_reader.tool_path)
def test_check_login_path_support(self): # Test if the my_print_defaults tool supports login-path options # NOTE: The version of the my_print_defaults tool as not been updated # properly (latest known version is 1.6). Therefore, not all the # my_print_defaults tools that report the version 1.6 support the use # of login-path. cfg_reader = MyDefaultsReader() self.assertTrue( cfg_reader.check_login_path_support(), "The used my_prints_defaults tool does not support " "login-path options: %s. Please add the location of a" "tool that support login-path to your PATH (at the " "beginning)." % cfg_reader.tool_path)
def test_get_option_value(self): # Test the retrieve of specific option values from .mylogin.cnf cfg_reader = MyDefaultsReader(find_my_print_defaults_tool=False) # Test the assertion: First, my_print_defaults must be found. self.assertRaises(AssertionError, cfg_reader.get_option_value, _TEST_LOGIN_PATH, _TEST_HOST) self.assertRaises(AssertionError, cfg_reader.get_option_value, _TEST_LOGIN_PATH, _TEST_USER) cfg_reader.search_my_print_defaults_tool() expected_res = { 'host': _TEST_HOST, 'user': _TEST_USER } # Get user opt_value = cfg_reader.get_option_value(_TEST_LOGIN_PATH, 'user') self.assertEqual(expected_res['user'], opt_value) # Get host opt_value = cfg_reader.get_option_value(_TEST_LOGIN_PATH, 'host') self.assertEqual(expected_res['host'], opt_value) # Return None if the specified option/group does not exist opt_value = cfg_reader.get_option_value(_TEST_LOGIN_PATH, 'unknown') self.assertIsNone(opt_value) opt_value = cfg_reader.get_option_value(_TEST_UNKNOWN_LOGIN_PATH, 'user') self.assertIsNone(opt_value) opt_value = cfg_reader.get_option_value(_TEST_UNKNOWN_LOGIN_PATH, 'host') self.assertIsNone(opt_value)
def check_mylogin_requisites(self): """ Check if the tools to manipulate mylogin.cnf are accessible. This method verifies if the MySQL client tools my_print_defaults and mysql_config_editor are accessible. A MUTLibError exception is raised if the requisites are not met. """ try: self.login_reader = MyDefaultsReader( find_my_print_defaults_tool=True) except UtilError as err: raise MUTLibError("MySQL client tools must be accessible to run " "this test (%s). E.g. Add the location of the " "MySQL client tools to your PATH." % err.errmsg) if not self.login_reader.check_login_path_support(): raise MUTLibError("ERROR: the used my_print_defaults tool does not " "support login-path options. Used tool: %s" % self.login_reader.tool_path) try: self.edit_tool_path = get_tool_path(None, "mysql_config_editor", search_PATH=True) except UtilError as err: raise MUTLibError("MySQL client tools must be accessible to run " "this test (%s). E.g. Add the location of the " "MySQL client tools to your PATH." % err.errmsg)
def test_get_group_data(self): # Test the retrieve of the login-path group data from .mylogin.cnf cfg_reader = MyDefaultsReader(find_my_print_defaults_tool=False) # Test the assertion: First, my_print_defaults must be found. self.assertRaises(AssertionError, cfg_reader.get_group_data, _TEST_LOGIN_PATH) cfg_reader.search_my_print_defaults_tool() expected_res = {'host': _TEST_HOST, 'user': _TEST_USER} # 1rst time, get data from my_prints_defaults start_time1 = time.time() grp_data = cfg_reader.get_group_data(_TEST_LOGIN_PATH) end_time1 = time.time() self.assertEqual(expected_res, grp_data) # 2nd time, return previously obtained information (faster) start_time2 = time.time() grp_data = cfg_reader.get_group_data(_TEST_LOGIN_PATH) end_time2 = time.time() self.assertEqual(expected_res, grp_data) # Must be faster to retrieve the same data the second time self.assertLess((end_time2 - start_time2), (end_time1 - start_time1)) # Return None if the specified group does not exist grp_data = cfg_reader.get_group_data(_TEST_UNKNOWN_LOGIN_PATH) self.assertIsNone(grp_data)
def test_get_group_data(self): # Test the retrieve of the login-path group data from .mylogin.cnf cfg_reader = MyDefaultsReader(find_my_print_defaults_tool=False) # Test the assertion: First, my_print_defaults must be found. self.assertRaises(AssertionError, cfg_reader.get_group_data, _TEST_LOGIN_PATH) cfg_reader.search_my_print_defaults_tool() expected_res = { 'host': _TEST_HOST, 'user': _TEST_USER } # 1rst time, get data from my_prints_defaults start_time1 = time.time() grp_data = cfg_reader.get_group_data(_TEST_LOGIN_PATH) end_time1 = time.time() self.assertEqual(expected_res, grp_data) # 2nd time, return previously obtained information (faster) start_time2 = time.time() grp_data = cfg_reader.get_group_data(_TEST_LOGIN_PATH) end_time2 = time.time() self.assertEqual(expected_res, grp_data) # Must be faster to retrieve the same data the second time self.assertLess((end_time2 - start_time2), (end_time1 - start_time1)) # Return None if the specified group does not exist grp_data = cfg_reader.get_group_data(_TEST_UNKNOWN_LOGIN_PATH) self.assertIsNone(grp_data)
def test_check_tool_version(self): # Test the check of the my_print_defaults tool version # NOTE: current tool version is 1.6 (not have been update lately) cfg_reader = MyDefaultsReader() self.assertTrue( cfg_reader.check_tool_version(1, 6), "Only version 1.6 or above of my_prints_defaults tool" " is supported. Please add the location of a " "supported tool version to your PATH (at the " "beginning).") self.assertTrue(cfg_reader.check_tool_version(1, 0)) self.assertFalse(cfg_reader.check_tool_version(1, 99)) self.assertFalse(cfg_reader.check_tool_version(99, 0)) self.assertTrue(cfg_reader.check_tool_version(0, 0)) self.assertTrue(cfg_reader.check_tool_version(0, 99))
def test_check_tool_version(self): # Test the check of the my_print_defaults tool version # NOTE: current tool version is 1.6 (not have been update lately) cfg_reader = MyDefaultsReader() self.assertTrue(cfg_reader.check_tool_version(1, 6), "Only version 1.6 or above of my_prints_defaults tool" " is supported. Please add the location of a " "supported tool version to your PATH (at the " "beginning).") self.assertTrue(cfg_reader.check_tool_version(1, 0)) self.assertFalse(cfg_reader.check_tool_version(1, 99)) self.assertFalse(cfg_reader.check_tool_version(99, 0)) self.assertTrue(cfg_reader.check_tool_version(0, 0)) self.assertTrue(cfg_reader.check_tool_version(0, 99))
def test_search_my_print_defaults_tool(self): # Test the search for the my_print_defaults tool cfg_reader = MyDefaultsReader(find_my_print_defaults_tool=False) self.assertIsNone(cfg_reader.tool_path) cfg_reader.search_my_print_defaults_tool() self.assertTrue(os.path.isfile(cfg_reader.tool_path)) # keep the location of the tool (to use as search_path) tool_dir = os.path.dirname(cfg_reader.tool_path) # Empty the PATH - UtilError raised if tool is not found os.environ['PATH'] = '' cfg_reader = MyDefaultsReader(find_my_print_defaults_tool=False) self.assertIsNone(cfg_reader.tool_path) self.assertRaises(UtilError, cfg_reader.search_my_print_defaults_tool) # Specify the search paths to find the tool (PATH empty) self.assertRaises(UtilError, cfg_reader.search_my_print_defaults_tool, ['not-a-valid-path']) cfg_reader.search_my_print_defaults_tool( ['not-a-valid-path', tool_dir]) self.assertTrue(os.path.isfile(cfg_reader.tool_path))
def test_search_my_print_defaults_tool(self): # Test the search for the my_print_defaults tool cfg_reader = MyDefaultsReader(find_my_print_defaults_tool=False) self.assertIsNone(cfg_reader.tool_path) cfg_reader.search_my_print_defaults_tool() self.assertTrue(os.path.isfile(cfg_reader.tool_path)) # keep the location of the tool (to use as search_path) tool_dir = os.path.dirname(cfg_reader.tool_path) # Empty the PATH - UtilError raised if tool is not found os.environ['PATH'] = '' cfg_reader = MyDefaultsReader(find_my_print_defaults_tool=False) self.assertIsNone(cfg_reader.tool_path) self.assertRaises(UtilError, cfg_reader.search_my_print_defaults_tool) # Specify the search paths to find the tool (PATH empty) self.assertRaises(UtilError, cfg_reader.search_my_print_defaults_tool, ['not-a-valid-path']) cfg_reader.search_my_print_defaults_tool(['not-a-valid-path', tool_dir]) self.assertTrue(os.path.isfile(cfg_reader.tool_path))
def run(self): # Test "--show" password in plain text option is available on this # version of my_print_defaults. test_num = 1 test_case = 'Test "--show" password in plain text option is available' comment = "Test case {0} {1}".format(test_num, test_case) my_defaults_reader = MyDefaultsReader() if not my_defaults_reader.check_show_required(): raise MUTLibError("{0}: failed".format(comment)) else: if self.debug: print("{0}: pass".format(comment)) self.results.append("{0}: pass\n".format(comment)) # Test retrieve of expected values for passwords variables stored in # test.mylogin.cnf con_tests = { "test1": "user_1:pass_1@localhost:10001", "test2": "user_2:A_passw0rd@localhost:20002", "test3": "user_3:M4g1cw0rd@localhost:30003", "test4": "user_4:-*123 !%^@remotehost:40004", } for group in sorted(con_tests.keys()): test_num += 1 test_case = 'Test Retrieve group: "{0}"'.format(group) comment = "Test case {0} {1}".format(test_num, test_case) self.results.append("{0}\n".format(comment)) if self.debug: print(comment) # Retrieve stored values in group from .mylogin.cnf ret_dt = my_defaults_reader.get_group_data(group) # If not values, group was not found if ret_dt is None: if self.debug: print('Can not retrieve values for group: "{}"' ''.format(group)) raise MUTLibError("{0}: failed".format(comment)) con_dic = parse_connection(con_tests[group], options={"charset": "utf8"}) con_dic.pop("charset") self.results.append("Retrieved data:\n") if self.debug: print("Retrieved data:") for data in sorted(ret_dt.iteritems()): # Only password is saved as passwd key if 'passw' in data[0]: if con_dic['passwd'] != data[1]: if self.debug: print( _VALUES_DIFFER_MSG.format( "password", con_dic['passwd'], data[1])) raise MUTLibError("{0}: failed".format(comment)) else: msg = _VALUES_EQUAL_MSG.format("password", con_dic['passwd'], data[1]) if self.debug: print(msg) self.results.append("{0}\n".format(msg)) elif str(con_dic[data[0]]) != data[1]: if self.debug: print( _VALUES_DIFFER_MSG.format(data[0], con_dic[data[0]], data[1])) raise MUTLibError("{0}: failed".format(comment)) else: msg = _VALUES_EQUAL_MSG.format(data[0], con_dic[data[0]], data[1]) if self.debug: print(msg) self.results.append("{0}\n".format(msg)) # None check failed, mark as pass self.results.append("Test result: pass\n") if self.debug: print("Result: pass") return True
def parse_connection(connection_values, my_defaults_reader=None, options=None): """Parse connection values. The function parses a connection specification of one of the forms:: - user[:password]@host[:port][:socket] - login-path[:port][:socket] A dictionary is returned containing the connection parameters. The function is designed so that it shall be possible to use it with a ``connect`` call in the following manner:: options = parse_connection(spec) conn = mysql.connector.connect(**options) conn_values[in] Connection values in the form: user:password@host:port:socket or login-path:port:socket my_defaults_reader[in] Instance of MyDefaultsReader to read the information of the login-path from configuration files. By default, the value is None. options[in] Dictionary of options (e.g. basedir), from the used utility. By default, it set with an empty dictionary. Note: also supports options values from optparse. Notes: This method validates IPv4 addresses and standard IPv6 addresses. This method accepts quoted host portion strings. If the host is marked with quotes, the code extracts this without validation and assigns it to the host variable in the returned tuple. This allows users to specify host names and IP addresses that are outside of the supported validation. Returns dictionary (user, passwd, host, port, socket) or raise an exception if parsing error """ if options is None: options = {} def _match(pattern, search_str): """Returns the groups from string search or raise FormatError if it does not match with the pattern. """ grp = pattern.match(search_str) if not grp: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) return grp.groups() # SSL options, must not be overwritten with those from options. ssl_ca = None ssl_cert = None ssl_key = None ssl = None # Split on the '@' to determine the connection string format. # The user/password may have the '@' character, split by last occurrence. conn_format = connection_values.rsplit('@', 1) if len(conn_format) == 1: # No '@' so try config-path and login-path # The config_path and login-path collide on their first element and # only differs on their secondary optional values. # 1. Try match config_path and his optional value group. If both # matches and the connection data can be retrieved, return the data. # If errors occurs in this step raise them immediately. # 2. If config_path matches but group does not, and connection data # can not be retrieved, do not raise errors and try to math # login_path on step 4. # 3. If connection data is retrieved on step 2, then try login_path on # next step to overwrite values from the new configuration. # 4. If login_path matches, check is .mylogin.cnf exists, if it doesn't # and data configuration was found verify it for missing values and # continue if they are not any missing. # 5. If .mylogin.cnf exists and data configuration is found, overwrite # any previews value from config_path if there is any. # 6. If login_path matches a secondary value but the configuration data # could not be retrieved, do not continue and raise any error. # 7. In case errors have occurred trying to get data from config_path, # and group did not matched, and in addition no secondary value, # matched from login_path (port of socket) mention that config_path # and login_path were not able to retrieve the connection data. # try login_path and overwrite the values. # Handle the format: config-path[[group]] config_path, group = _match(_CONN_CONFIGPATH, conn_format[0]) port = None socket = None config_path_data = None login_path_data = None config_path_err_msg = None login_path = None if config_path: try: # If errors occurs, and group matched: raise any errors as the # group is exclusive of config_path. config_path_data = handle_config_path(config_path, group) except UtilError as err: if group: raise else: # Convert first letter to lowercase to include in error # message with the correct case. config_path_err_msg = \ err.errmsg[0].lower() + err.errmsg[1:] if group is None: # the conn_format can still be a login_path so continue # No '@' then handle has in the format: login-path[:port][:socket] login_path, port, socket = _match(_CONN_LOGINPATH, conn_format[0]) # Check if the login configuration file (.mylogin.cnf) exists if login_path and not my_login_config_exists(): if not config_path_data: util_err_msg = (".mylogin.cnf was not found at is default " "location: {0}. Please configure your " "login-path data before using it (use the " "mysql_config_editor tool)." "".format(my_login_config_path())) if config_path_err_msg and not (port or socket): util_err_msg = ("{0} In addition, {1}" "").format(util_err_msg, config_path_err_msg) raise UtilError(util_err_msg) else: # If needed, create a MyDefaultsReader and search for # my_print_defaults tool. if not my_defaults_reader: try: my_defaults_reader = MyDefaultsReader(options) except UtilError as err: if config_path_err_msg and not (port or socket): util_err_msg = ("{0} In addition, {1}" "").format(err.errmsg, config_path_err_msg) raise UtilError(util_err_msg) else: raise elif not my_defaults_reader.tool_path: my_defaults_reader.search_my_print_defaults_tool() # Check if the my_print_default tool is able to read a # login-path from the mylogin configuration file if not my_defaults_reader.check_login_path_support(): util_err_msg = ("the used my_print_defaults tool does not " "support login-path options: {0}. " "Please confirm that the location to a " "tool with login-path support is included " "in the PATH (at the beginning)." "".format(my_defaults_reader.tool_path)) if config_path_err_msg and not (port or socket): util_err_msg = ("{0} In addition, {1}" "").format(util_err_msg, config_path_err_msg) raise UtilError(util_err_msg) # Read and parse the login-path data (i.e., user, password and # host) login_path_data = my_defaults_reader.get_group_data(login_path) if config_path_data or login_path_data: if config_path_data: if not login_path_data: login_path_data = config_path_data else: # Overwrite values from login_path_data config_path_data.update(login_path_data) login_path_data = config_path_data user = login_path_data.get('user', None) passwd = login_path_data.get('password', None) host = login_path_data.get('host', None) if not port: port = login_path_data.get('port', None) if not socket: socket = login_path_data.get('socket', None) if os.name == "posix" and socket is not None: # if we are on unix systems and used a socket, hostname can be # safely assumed as being localhost so it is not required required_options = ('user', 'socket') host = 'localhost' if host is None else host else: required_options = ('user', 'host', 'port') missing_options = [ opt for opt in required_options if locals()[opt] is None ] # If we are on unix and port is missing, user might have specified # a socket instead if os.name == "posix" and "port" in missing_options: i = missing_options.index("port") if socket: # If we have a socket, port is not needed missing_options.pop(i) else: # if we don't have neither a port nor a socket, we need # either a port or a socket missing_options[i] = "port or socket" if missing_options: message = ",".join(missing_options) if len(missing_options) > 1: comma_idx = message.rfind(",") message = "{0} and {1}".format(message[:comma_idx], message[comma_idx + 1:]) pluralize = "s" if len(missing_options) > 1 else "" raise UtilError("Missing connection value{0} for " "{1} option{0}".format(pluralize, message)) # optional options, available only on config_path_data if config_path_data: ssl_ca = config_path_data.get('ssl-ca', None) ssl_cert = config_path_data.get('ssl-cert', None) ssl_key = config_path_data.get('ssl-key', None) ssl = config_path_data.get('ssl', None) else: if login_path and not config_path: raise UtilError("No login credentials found for login-path: " "{0}. Please review the used connection string" ": {1}".format(login_path, connection_values)) elif not login_path and config_path: raise UtilError("No login credentials found for config-path: " "{0}. Please review the used connection string" ": {1}".format(login_path, connection_values)) elif login_path and config_path: raise UtilError("No login credentials found for either " "login-path: '{0}' nor config-path: '{1}'. " "Please review the used connection string: {2}" "".format(login_path, config_path, connection_values)) elif len(conn_format) == 2: # Check to see if the user attempted to pass a list of connections. # This is true if there is at least one comma and multiple @ symbols. if ((connection_values.find(',') > 0) and (connection_values.find('@') > 1)): raise FormatError(_MULTIPLE_CONNECTIONS.format(connection_values)) # Handle as in the format: user[:password]@host[:port][:socket] userpass, hostportsock = conn_format # Get user, password match = _CONN_USERPASS.match(userpass) if not match: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) user = match.group('user') if user is None: # No password provided user = match.group('suser').rstrip(':') passwd = match.group('passwd') # Handle host, port and socket if len(hostportsock) <= 0: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) if hostportsock[0] in ['"', "'"]: # need to strip the quotes host, port, socket = _match(_CONN_QUOTEDHOST, hostportsock) if host[0] == '"': host = host.strip('"') if host[0] == "'": host = host.strip("'") else: host, port, socket, _ = parse_server_address(hostportsock) else: # Unrecognized format raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) # Get character-set from options if isinstance(options, dict): charset = options.get("charset", None) # If one SSL option was found before, not mix with those in options. if not ssl_cert and not ssl_ca and not ssl_key and not ssl: ssl_cert = options.get("ssl_cert", None) ssl_ca = options.get("ssl_ca", None) ssl_key = options.get("ssl_key", None) ssl = options.get("ssl", None) else: # options is an instance of optparse.Values try: charset = options.charset # pylint: disable=E1103 except AttributeError: charset = None # If one SSL option was found before, not mix with those in options. if not ssl_cert and not ssl_ca and not ssl_key and not ssl: try: ssl_cert = options.ssl_cert # pylint: disable=E1103 except AttributeError: ssl_cert = None try: ssl_ca = options.ssl_ca # pylint: disable=E1103 except AttributeError: ssl_ca = None try: ssl_key = options.ssl_key # pylint: disable=E1103 except AttributeError: ssl_key = None try: ssl = options.ssl # pylint: disable=E1103 except AttributeError: ssl = None # Set parsed connection values connection = { "user": user, "host": host, "port": int(port) if port else 3306, "passwd": passwd if passwd else '' } if charset: connection["charset"] = charset if ssl_cert: connection["ssl_cert"] = ssl_cert if ssl_ca: connection["ssl_ca"] = ssl_ca if ssl_key: connection["ssl_key"] = ssl_key if ssl: connection["ssl"] = ssl # Handle optional parameters. They are only stored in the dict if # they were provided in the specifier. if socket is not None and os.name == "posix": connection['unix_socket'] = socket return connection
PARSE_ERR_OPTS_REQ_GREATER_OR_EQUAL.format( opt="--switchover-interval", value=30)) # option --slave is required (mandatory) if not opt.slave: parser.error(PARSE_ERR_OPTS_REQ.format(opt="--slave")) # option --masters is required (mandatory) if not opt.masters: parser.error(PARSE_ERR_OPTS_REQ.format(opt="--masters")) # option --rpl-user is required (mandatory) if not opt.rpl_user: parser.error(PARSE_ERR_OPTS_REQ.format(opt="--rpl-user")) config_reader = MyDefaultsReader(opt, False) # Parse slave connection values try: slave_vals = parse_connection(opt.slave, config_reader, opt) except FormatError: _, err, _ = sys.exc_info() parser.error("Slave connection values invalid: {0}.".format(err)) except UtilError: _, err, _ = sys.exc_info() parser.error("Slave connection values invalid: {0}." "".format(err.errmsg)) # Parse masters connection values masters_vals = [] masters = opt.masters.split(",")
def parse_user_password(userpass_values, my_defaults_reader=None, options={}): """ This function parses a string with the user/password credentials. This function parses the login string, determines the used format, i.e. user[:password] or login-path. If the ':' (colon) is not in the login string, the it can refer to a login-path or to a username (without a password). In this case, first it is assumed that the specified value is a login-path and the function attempts to retrieve the associated username and password, in a quiet way (i.e., without raising exceptions). If it fails to retrieve the login-path data, then the value is assumed to be a username. userpass_values[in] String indicating the user/password credentials. It must be in the form: user[:password] or login-path. my_defaults_reader[in] Instance of MyDefaultsReader to read the information of the login-path from configuration files. By default, the value is None. options[in] Dictionary of options (e.g. basedir), from the used utility. By default, it set with an empty dictionary. Note: also supports options values from optparse. Returns a tuple with the username and password. """ # Split on the ':' to determine if a login-path is used. login_values = userpass_values.split(':') if len(login_values) == 1: # Format is login-path or user (without a password): First, assume it # is a login-path and quietly try to retrieve the user and password. # If it fails, assume a user name is being specified. #Check if the login configuration file (.mylogin.cnf) exists if login_values[0] and not my_login_config_exists(): return login_values[0], None if not my_defaults_reader: # Attempt to create the MyDefaultsReader try: my_defaults_reader = MyDefaultsReader(options) except UtilError: # Raise an UtilError when my_print_defaults tool is not found. return login_values[0], None elif not my_defaults_reader.tool_path: # Try to find the my_print_defaults tool try: my_defaults_reader.search_my_print_defaults_tool() except UtilError: # Raise an UtilError when my_print_defaults tool is not found. return login_values[0], None # Check if the my_print_default tool is able to read a login-path from # the mylogin configuration file if not my_defaults_reader.check_login_path_support(): return login_values[0], None # Read and parse the login-path data (i.e., user and password) try: loginpath_data = my_defaults_reader.get_group_data(login_values[0]) if loginpath_data: user = loginpath_data.get('user', None) passwd = loginpath_data.get('password', None) return user, passwd else: return login_values[0], None except UtilError: # Raise an UtilError if unable to get the login-path group data return login_values[0], None elif len(login_values) == 2: # Format is user:password; return a tuple with the user and password return login_values[0], login_values[1] else: # Invalid user credentials format return FormatError("Unable to parse the specified user credentials " "(accepted formats: <user>[:<password> or " "<login-path>): %s" % userpass_values)
def parse_connection(connection_values, my_defaults_reader=None, options={}): """Parse connection values. The function parses a connection specification of one of the forms:: - user[:password]@host[:port][:socket] - login-path[:port][:socket] A dictionary is returned containing the connection parameters. The function is designed so that it shall be possible to use it with a ``connect`` call in the following manner:: options = parse_connection(spec) conn = mysql.connector.connect(**options) conn_values[in] Connection values in the form: user:password@host:port:socket or login-path:port:socket my_defaults_reader[in] Instance of MyDefaultsReader to read the information of the login-path from configuration files. By default, the value is None. options[in] Dictionary of options (e.g. basedir), from the used utility. By default, it set with an empty dictionary. Note: also supports options values from optparse. Notes: This method validates IPv4 addresses and standard IPv6 addresses. This method accepts quoted host portion strings. If the host is marked with quotes, the code extracts this without validation and assigns it to the host variable in the returned tuple. This allows users to specify host names and IP addresses that are outside of the supported validation. Returns dictionary (user, passwd, host, port, socket) or raise an exception if parsing error """ def _match(pattern, search_str): grp = pattern.match(search_str) if not grp: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) return grp.groups() # Split on the '@' to determine the connection string format. conn_format = connection_values.split('@') if len(conn_format) == 1: # No '@' then handle has in the format: login-path[:port][:socket] login_path, port, socket = _match(_CONN_LOGINPATH, conn_format[0]) #Check if the login configuration file (.mylogin.cnf) exists if login_path and not my_login_config_exists(): raise UtilError(".mylogin.cnf was not found at is default " "location: %s." "Please configure your login-path data before " "using it (use the mysql_config_editor tool)." % my_login_config_path()) # If needed, create a MyDefaultsReader and search for my_print_defaults # tool. if not my_defaults_reader: my_defaults_reader = MyDefaultsReader(options) elif not my_defaults_reader.tool_path: my_defaults_reader.search_my_print_defaults_tool() # Check if the my_print_default tool is able to read a login-path from # the mylogin configuration file if not my_defaults_reader.check_login_path_support(): raise UtilError("the used my_print_defaults tool does not " "support login-path options: %s. " "Please confirm that the location to a tool with " "login-path support is included in the PATH " "(at the beginning)." % my_defaults_reader.tool_path) # Read and parse the login-path data (i.e., user, password and host) login_path_data = my_defaults_reader.get_group_data(login_path) if login_path_data: user = login_path_data.get('user', None) passwd = login_path_data.get('password', None) host = login_path_data.get('host', None) if not port: port = login_path_data.get('port', 3306) if not socket: socket = login_path_data.get('socket', None) else: raise UtilError("No login credentials found for login-path: %s. " "Please review the used connection string: %s" % (login_path, connection_values)) elif len(conn_format) == 2: # Handle as in the format: user[:password]@host[:port][:socket] userpass, hostportsock = conn_format # Get user, password user, passwd = _match(_CONN_USERPASS, userpass) # Handle host, port and socket if len(hostportsock) <= 0: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) if hostportsock[0] in ['"', "'"]: # need to strip the quotes host, port, socket = _match(_CONN_QUOTEDHOST, hostportsock) if host[0] == '"': host = host.strip('"') if host[0] == "'": host = host.strip("'") elif len(hostportsock.split(":")) <= 3: # if fewer colons, must be IPv4 host, port, socket = _match(_CONN_IPv4, hostportsock) else: host, port, socket = _match(_CONN_IPv6, hostportsock) else: # Unrecognized format raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) # Set parsed connection values connection = { "user" : user, "host" : host, "port" : int(port) if port else 3306, "passwd" : passwd if passwd else '' } # Handle optional parameters. They are only stored in the dict if # they were provided in the specifier. if socket is not None and os.name == "posix": connection['unix_socket'] = socket return connection
class System_test(object): """The System_test class is used by the MySQL Utilities Test (MUT) facility to perform system tests against MySQL utilitites. This class is the base class from which all tests are derived. The following utilities are provided: - Execute a utility as a subprocess and return result and populate a text file to capture output - Check number of servers for a test - Check a result file To create a test, subclass this class and supply definitions for the following abstract methods: - check_prerequisites - check conditions for test - setup - perform any database setup here - run - execute test cases - get_result - return result to MUT - cleanup - perform any tear down here Note: Place test case comments in the class documentation section. This will be printed by the --verbose option. """ __metaclass__ = ABCMeta # Register abstract base class def __init__(self, servers, res_dir, utildir, verbose=False, debug=False): """Constructor servers[in] A list of Server objects res_dir[in] Path to test result files utildir[in] Path to utilty scripts verbose[in] print extra data during operations (optional) default value = False debug[in] Turn on debugging mode for a single test default value = False """ self.res_fname = None # Name of intermediate result file self.results = [] # List for storing results self.servers = servers # Server_list class self.res_dir = res_dir # Current test result directory self.utildir = utildir # Location of utilities being tested self.verbose = verbose # Option for verbosity self.debug = debug # Option for diagnostic work def __del__(self): """Destructor Reset all parameters. """ for result in self.results: del result def check_gtid_unsafe(self, on=False): """Check for gtid enabled base server If on is True, method ensures server0 has the server variable DISABLE_GTID_UNSAFE_STATEMENTS=ON, else if on is False, method ensures server0 does not have DISABLE_GTID_UNSAFE_STATEMENTS=ON. Returns bool - False if no DISABE_GTID_UNSAFE_STATEMENTS variable found, else throws exception if criteria not met. """ if on: # Need servers with DISABLE_GTID_UNSAFE_STATEMENTS self.server0 = self.servers.get_server(0) res = self.server0.show_server_variable("DISABLE_GTID_UNSAFE_" "STATEMENTS") if res != [] and res[0][1] != "ON": raise MUTLibError( "Test requires DISABLE_GTID_UNSAFE_STATEMENTS" " = ON") else: # Need servers without DISABLE_GTID_UNSAFE_STATEMENTS self.server0 = self.servers.get_server(0) res = self.server0.show_server_variable("DISABLE_GTID_UNSAFE_" "STATEMENTS") if res != [] and res[0][1] == "ON": raise MUTLibError( "Test requires DISABLE_GTID_UNSAFE_STATEMENTS" " = OFF or a server prior to version 5.6.5.") return False def check_mylogin_requisites(self): """ Check if the tools to manipulate mylogin.cnf are accessible. This method verifies if the MySQL client tools my_print_defaults and mysql_config_editor are accessible. A MUTLibError exception is raised if the requisites are not met. """ try: self.login_reader = MyDefaultsReader( find_my_print_defaults_tool=True) except UtilError as err: raise MUTLibError("MySQL client tools must be accessible to run " "this test (%s). E.g. Add the location of the " "MySQL client tools to your PATH." % err.errmsg) if not self.login_reader.check_login_path_support(): raise MUTLibError( "ERROR: the used my_print_defaults tool does not " "support login-path options. Used tool: %s" % self.login_reader.tool_path) try: self.edit_tool_path = get_tool_path(None, "mysql_config_editor", search_PATH=True) except UtilError as err: raise MUTLibError("MySQL client tools must be accessible to run " "this test (%s). E.g. Add the location of the " "MySQL client tools to your PATH." % err.errmsg) def create_login_path_data(self, login_path, user, host): """Add the specified login-path data to .mylogin.cnf. Execute mysql_config_editor tool to create a new login-path entry to the .mylogin.cnf file. Note: the use of password is not supported because it is not read from the stdin by the tool (apparently for security reasons). """ assert self.edit_tool_path, ("The tool mysql_config_editor is not " "accessible. First, use method " "check_mylogin_requisites.") cmd = [self.edit_tool_path] cmd.append('set') cmd.append('--login-path=%s' % login_path) cmd.append('--host=%s' % host) cmd.append('--user=%s' % user) # Create a temporary file to redirect stdout out_file = tempfile.TemporaryFile() # Execute command to create login-path data proc = subprocess.Popen(cmd, stdout=out_file, stdin=subprocess.PIPE) # Overwrite login-path if already exists (i.e. answer 'y' to question) proc.communicate('y') def remove_login_path_data(self, login_path): """Remove the specified login-path data from .mylogin.cnf. Execute mysql_config_editor tool to remove the specified login-path entry from the .mylogin.cnf file. """ assert self.edit_tool_path, ("The tool mysql_config_editor is not " "accessible. First, use method " "check_mylogin_requisites.") cmd = [self.edit_tool_path] cmd.append('remove') cmd.append('--login-path=%s' % login_path) # Create a temporary file to redirect stdout out_file = tempfile.TemporaryFile() # Execute command to remove login-path data if self.verbose: subprocess.call(cmd, stdout=out_file) else: # Redirect stderr to null null_file = open(os.devnull, "w+b") subprocess.call(cmd, stdout=out_file, stderr=null_file) def exec_util(self, cmd, file_out, abspath=False): """Execute Utility This method executes a MySQL utility using the utildir specified in MUT. It returns the return value from the completion of the command and writes the output to the file supplied. cmd[in] The command to execute including all parameters file_out[in] Path and filename of a file to write output abspath[in] Use absolute path and not current directory Returns return value of process run. """ return _exec_util(cmd, file_out, self.utildir, self.debug, abspath) def check_num_servers(self, num_servers): """Check the number of servers available. num_servers[in] The minimal number of Server objects required Returns True - servers available, False - not enough servers """ if self.servers.num_servers() >= num_servers: return True return False def get_connection_parameters(self, server): """Return a string that comprises the normal connection parameters common to MySQL utilities for a particular server. server[in] A Server object Returns string """ str1 = "--user=%s --host=%s " % (server.user, server.host) if server.passwd: str1 += "--password=%s " % server.passwd if server.socket: str2 = "--socket=%s " % (server.socket) else: str2 = "--port=%s " % (server.port) return str1 + str2 def get_connection_values(self, server): """Return a tuple that comprises the connection parameters for a particular server. server[in] A Server object Returns (user, password, host, port, socket) """ if server is None: raise MUTLibError("Server not initialized!") return (server.user, server.passwd, server.host, server.port, server.socket, server.role) def build_connection_string(self, server): """Return a connection string server[in] A Server object Returns string of the form user:password@host:port:socket """ conn_val = self.get_connection_values(server) conn_str = "%s" % conn_val[0] if conn_val[1]: conn_str += ":%s" % conn_val[1] conn_str += "@%s:" % conn_val[2] if conn_val[3]: conn_str += "%s" % conn_val[3] if conn_val[4] is not None and conn_val[4] != "": conn_str += ":%s " % conn_val[4] return conn_str def run_test_case(self, exp_result, command, comments, debug=False): """Execute a test case and save the results. Call this method to run a test case and save the results to the results list. exp_result[in] The expected result (returns True if matches) command[in] Execution command (e.g. ./mysqlclonedb.py --help) comments[in] Comments to put in result list debug[in] Print debug information during execution Returns True if result matches expected result """ if self.debug or debug: print "\n%s" % comments res = self.exec_util(command, self.res_fname) if comments: self.results.append(comments + "\n") self.record_results(self.res_fname) return res == exp_result def run_test_case_result(self, command, comments, debug=False): """Execute a test case and save the results returning actual result. Call this method to run a test case and save the results to the results list. command[in] Execution command (e.g. ./mysqlclonedb.py --help) comments[in] Comments to put in result list debug[in] Print debug information during execution Returns int - actual result """ if self.debug or debug: print "\n%s" % comments res = self.exec_util(command, self.res_fname) if comments: self.results.append(comments + "\n") self.record_results(self.res_fname) return res def replace_result(self, prefix, str): """Replace a string in the results with a new, deterministic string. prefix[in] starting prefix of string to mask str[in] replacement string """ linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: self.results.pop(linenum) self.results.insert(linenum, str) linenum += 1 def remove_result(self, prefix): """Remove a string in the results. prefix[in] starting prefix of string to mask """ linenums = [] linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: linenums.append(int(linenum)) linenum += 1 # Must remove lines in reverse order for linenum in range(len(linenums) - 1, -1, -1): self.results.pop(linenums[linenum]) def remove_result_and_lines_before(self, prefix, lines=1): """Remove lines in the results. prefix[in] starting prefix of string to mask lines[in] number of lines to remove previously to the prefix line. """ linenums = [] linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: linenums.append(int(linenum)) for line2rm in range(linenum - lines, linenum): if line2rm > -1: linenums.append(int(line2rm)) linenum += 1 linenums.sort() # Must remove lines in reverse order for linenum in range(len(linenums) - 1, -1, -1): self.results.pop(linenums[linenum]) def replace_substring(self, target, replacement): """Replace a target substring in the entire result file. target[in] target string to replace replacement[in] string to replace """ linenum = 0 for line in self.results: if line.find(target): self.results.pop(linenum) replace_line = line.replace(target, replacement) self.results.insert(linenum, replace_line) linenum += 1 def mask_result(self, prefix, target, mask): """Mask out a portion of a string for the results. str[in] string to mask prefix[in] starting prefix of string to mask target[in] substring to search for to mask mask[in] mask string (e.g. '######") """ linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: loc = line.find(target) if loc >= 0: start = loc + len(mask) self.results.pop(linenum) if start > len(line): self.results.insert(linenum, line[0:loc] + mask + "\n") else: self.results.insert(linenum, line[0:loc] + mask + line[start:]) linenum += 1 def mask_result_portion(self, prefix, target, end_target, mask): """Mask out a portion of a string for the results using a end target to make the masked area a specific length. str[in] string to mask prefix[in] starting prefix of string to mask target[in] substring to search for to mask end_target[in] substring to mark end of mask mask[in] mask string (e.g. '######") """ linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: loc = line.find(target) if loc >= 0: end = line.find(end_target) if end >= 0: self.results.pop(linenum) if end > len(line): self.results.insert(linenum, line[0:loc] + mask + "\n") else: self.results.insert( linenum, line[0:loc] + mask + line[end:]) linenum += 1 def mask_column_result(self, prefix, separator, num_col, mask): """Mask out a column portion of a string for the results. str[in] string to mask prefix[in] starting prefix of string to mask separator[in] separator for columns (e.g. ',') num_col[in] number of column to mask mask[in] mask string (e.g. '######") """ linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: pos = 0 for i in range(0, num_col): loc = line.find(separator, pos) if i + 1 == num_col: next = line.find(separator, loc) if next < 0: start = len(line) else: start = next self.results.pop(linenum) if start >= len(line): self.results.insert(linenum, line[0:pos] + mask + "\n") else: self.results.insert( linenum, line[0:pos] + mask + line[start:]) else: pos = loc + 1 if loc < 0: break linenum += 1 def check_objects(self, server, db, events=True): """Check number of objects. Creates a string containing the number of objects for a given database. server[in] Server object to query db[in] name of database to check Returns string """ from mysql.utilities.common.database import Database db_source = Database(server, db) db_source.init() res = db_source.get_db_objects("TABLE") str = "OBJECT COUNTS: tables = %s, " % (len(res)) res = db_source.get_db_objects("VIEW") str += "views = %s, " % (len(res)) res = db_source.get_db_objects("TRIGGER") str += "triggers = %s, " % (len(res)) res = db_source.get_db_objects("PROCEDURE") str += "procedures = %s, " % (len(res)) res = db_source.get_db_objects("FUNCTION") str += "functions = %s, " % (len(res)) if events: res = db_source.get_db_objects("EVENT") str += "events = %s \n" % (len(res)) return str def compare(self, name, actual): """Compare an actual set of return values to the result file for this test. name[in] test name (use __name__) actual[in] String list of the actual results Returns: (bool, diff) where: (True, None) = results identical (False, "result file missing") = result file missing (False, <string list>) = results differ """ # # Check to see if result file exists first. # res_fname = os.path.normpath( os.path.join(self.res_dir, name + ".result")) if not os.access(res_fname, os.F_OK): actual.insert(0, "Result file missing - actual results:\n\n") return (False, actual) # # Use ndiff to compare to known result file # res_file = open(res_fname) diff = difflib.ndiff(res_file.readlines(), actual) # # Now convert the diff to a string list and write reject file # rej_fname = os.path.normpath( os.path.join(self.res_dir, name + ".reject")) rej_file = open(rej_fname, 'w+') rej_list = [] try: while 1: str = diff.next() if str[0] in ['-', '+', '?']: rej_list.append(str) rej_file.write(str) except: pass rej_file.close() # Write preamble if there are differences if not rej_list == []: rej_list.insert(0, "Result file mismatch:\n") # If test passed, delete the reject file if it exists elif os.access(rej_fname, os.F_OK): os.unlink(rej_fname) return (rej_list == [], rej_list) def record_results(self, fname): """Saves the results from a file to the self.results list. fname[in] Name of results file from exec_util """ f_res = open(fname) for line in f_res.readlines(): self.results.append(line) f_res.close() def save_result_file(self, name, results): """Saves a result file for the test. name[in] Test name (use __name__) results[in] String list of the results Returns True - success, False - fail """ if results: res_fname = os.path.normpath( os.path.join(self.res_dir, name + ".result")) res_file = open(res_fname, 'w+') if not res_file: return False for str in results: res_file.write(str) res_file.close() return True def is_long(self): """Is test marked as a long running test? Override this method to specify the test is a long-running test. """ return False @abstractmethod def check_prerequisites(self): """Check preprequisites for test. This method is used to check any prerequisites for a test such as the number of servers needed, environment variables, etc. Returns: True = servers available, False = not enough servers, skip """ pass @abstractmethod def setup(self): """Setup conditions for test. This method is used to setup any conditions for a test such as loading test data or setting server variables. Note: if setup fails, cleanup() is still called. Consider this when implementing complex setup procedures. Returns: True = no errors, False = errors, skip test """ pass @abstractmethod def run(self): """Execute a test. This method is used to execute the test cases in the test. One or more calls to exec_util() may be performed here, but results are saved here and checked later. Returns: True = no errors, False = errors occurred """ pass @abstractmethod def get_result(self): """Return results of test to MUT. This method is used to decided if the test passed. It is in this method where the results of the run() method are checked. This allows separation of the evaluation of the test form the execution and other steps. Returns: tuple (bool, string list) where: (True, None) = test passed (False, <string list>) - test failed. String list to be displayed Note: Formatting for string list should be done by the callee. The caller prints exactly what is returned. """ pass @abstractmethod def record(self): """Record test results for comparison. This method is used to record any test results for a result compare- type test. To do so, call self.save_result_file(__name__, strlist) where strlist is the output the test will compare to determine success. Note: If your test is not a comparative test, you can simply return True (success). In this case, the --record option has no effect. Returns: True - success, False - error """ pass @abstractmethod def cleanup(self): """Perform any post-test cleanup. This method is used to remove the setup conditions from the server. Returns: True = no errors, False = errors occurred """ pass
def parse_user_password(userpass_values, my_defaults_reader=None, options={}): """ This function parses a string with the user/password credentials. This function parses the login string, determines the used format, i.e. user[:password] or login-path. If the ':' (colon) is not in the login string, the it can refer to a login-path or to a username (without a password). In this case, first it is assumed that the specified value is a login-path and the function attempts to retrieve the associated username and password, in a quiet way (i.e., without raising exceptions). If it fails to retrieve the login-path data, then the value is assumed to be a username. userpass_values[in] String indicating the user/password credentials. It must be in the form: user[:password] or login-path. my_defaults_reader[in] Instance of MyDefaultsReader to read the information of the login-path from configuration files. By default, the value is None. options[in] Dictionary of options (e.g. basedir), from the used utility. By default, it set with an empty dictionary. Note: also supports options values from optparse. Returns a tuple with the username and password. """ # Split on the ':' to determine if a login-path is used. login_values = userpass_values.split(':') if len(login_values) == 1: # Format is login-path or user (without a password): First, assume it # is a login-path and quietly try to retrieve the user and password. # If it fails, assume a user name is being specified. #Check if the login configuration file (.mylogin.cnf) exists if login_values[0] and not my_login_config_exists(): return login_values[0], None if not my_defaults_reader: # Attempt to create the MyDefaultsReader try: my_defaults_reader = MyDefaultsReader(options) except UtilError: # Raise an UtilError when my_print_defaults tool is not found. return login_values[0], None elif not my_defaults_reader.tool_path: # Try to find the my_print_defaults tool try: my_defaults_reader.search_my_print_defaults_tool() except UtilError: # Raise an UtilError when my_print_defaults tool is not found. return login_values[0], None # Check if the my_print_default tool is able to read a login-path from # the mylogin configuration file if not my_defaults_reader.check_login_path_support(): return login_values[0], None # Read and parse the login-path data (i.e., user and password) try: loginpath_data = my_defaults_reader.get_group_data(login_values[0]) if loginpath_data: user = loginpath_data.get('user', None) passwd = loginpath_data.get('password', None) return user, passwd else: return login_values[0], None except UtilError: # Raise an UtilError if unable to get the login-path group data return login_values[0], None elif len(login_values) == 2: # Format is user:password; return a tuple with the user and password return login_values[0], login_values[1] else: # Invalid user credentials format return FormatError("Unable to parse the specified user credentials " "(accepted formats: <user>[:<password> or " "<login-path>): %s" % userpass_values)
def parse_connection(connection_values, my_defaults_reader=None, options={}): """Parse connection values. The function parses a connection specification of one of the forms:: - user[:password]@host[:port][:socket] - login-path[:port][:socket] A dictionary is returned containing the connection parameters. The function is designed so that it shall be possible to use it with a ``connect`` call in the following manner:: options = parse_connection(spec) conn = mysql.connector.connect(**options) conn_values[in] Connection values in the form: user:password@host:port:socket or login-path:port:socket my_defaults_reader[in] Instance of MyDefaultsReader to read the information of the login-path from configuration files. By default, the value is None. options[in] Dictionary of options (e.g. basedir), from the used utility. By default, it set with an empty dictionary. Note: also supports options values from optparse. Notes: This method validates IPv4 addresses and standard IPv6 addresses. This method accepts quoted host portion strings. If the host is marked with quotes, the code extracts this without validation and assigns it to the host variable in the returned tuple. This allows users to specify host names and IP addresses that are outside of the supported validation. Returns dictionary (user, passwd, host, port, socket) or raise an exception if parsing error """ def _match(pattern, search_str): grp = pattern.match(search_str) if not grp: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) return grp.groups() # Split on the '@' to determine the connection string format. conn_format = connection_values.split('@') if len(conn_format) == 1: # No '@' then handle has in the format: login-path[:port][:socket] login_path, port, socket = _match(_CONN_LOGINPATH, conn_format[0]) #Check if the login configuration file (.mylogin.cnf) exists if login_path and not my_login_config_exists(): raise UtilError(".mylogin.cnf was not found at is default " "location: %s." "Please configure your login-path data before " "using it (use the mysql_config_editor tool)." % my_login_config_path()) # If needed, create a MyDefaultsReader and search for my_print_defaults # tool. if not my_defaults_reader: my_defaults_reader = MyDefaultsReader(options) elif not my_defaults_reader.tool_path: my_defaults_reader.search_my_print_defaults_tool() # Check if the my_print_default tool is able to read a login-path from # the mylogin configuration file if not my_defaults_reader.check_login_path_support(): raise UtilError("the used my_print_defaults tool does not " "support login-path options: %s. " "Please confirm that the location to a tool with " "login-path support is included in the PATH " "(at the beginning)." % my_defaults_reader.tool_path) # Read and parse the login-path data (i.e., user, password and host) login_path_data = my_defaults_reader.get_group_data(login_path) if login_path_data: user = login_path_data.get('user', None) passwd = login_path_data.get('password', None) host = login_path_data.get('host', None) if not port: port = login_path_data.get('port', 3306) if not socket: socket = login_path_data.get('socket', None) else: raise UtilError("No login credentials found for login-path: %s. " "Please review the used connection string: %s" % (login_path, connection_values)) elif len(conn_format) == 2: # Handle as in the format: user[:password]@host[:port][:socket] userpass, hostportsock = conn_format # Get user, password user, passwd = _match(_CONN_USERPASS, userpass) # Handle host, port and socket if len(hostportsock) <= 0: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) if hostportsock[0] in ['"', "'"]: # need to strip the quotes host, port, socket = _match(_CONN_QUOTEDHOST, hostportsock) if host[0] == '"': host = host.strip('"') if host[0] == "'": host = host.strip("'") elif len( hostportsock.split(":")) <= 3: # if fewer colons, must be IPv4 host, port, socket = _match(_CONN_IPv4, hostportsock) else: host, port, socket = _match(_CONN_IPv6, hostportsock) else: # Unrecognized format raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) # Set parsed connection values connection = { "user": user, "host": host, "port": int(port) if port else 3306, "passwd": passwd if passwd else '' } # Handle optional parameters. They are only stored in the dict if # they were provided in the specifier. if socket is not None and os.name == "posix": connection['unix_socket'] = socket return connection
def parse_connection(connection_values, my_defaults_reader=None, options=None): """Parse connection values. The function parses a connection specification of one of the forms:: - user[:password]@host[:port][:socket] - login-path[:port][:socket] A dictionary is returned containing the connection parameters. The function is designed so that it shall be possible to use it with a ``connect`` call in the following manner:: options = parse_connection(spec) conn = mysql.connector.connect(**options) conn_values[in] Connection values in the form: user:password@host:port:socket or login-path:port:socket my_defaults_reader[in] Instance of MyDefaultsReader to read the information of the login-path from configuration files. By default, the value is None. options[in] Dictionary of options (e.g. basedir), from the used utility. By default, it set with an empty dictionary. Note: also supports options values from optparse. Notes: This method validates IPv4 addresses and standard IPv6 addresses. This method accepts quoted host portion strings. If the host is marked with quotes, the code extracts this without validation and assigns it to the host variable in the returned tuple. This allows users to specify host names and IP addresses that are outside of the supported validation. Returns dictionary (user, passwd, host, port, socket) or raise an exception if parsing error """ if options is None: options = {} def _match(pattern, search_str): """Returns the groups from string search or raise FormatError if it does not match with the pattern. """ grp = pattern.match(search_str) if not grp: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) return grp.groups() # SSL options, must not be overwritten with those from options. ssl_ca = None ssl_cert = None ssl_key = None ssl = None # Split on the '@' to determine the connection string format. # The user/password may have the '@' character, split by last occurrence. conn_format = connection_values.rsplit('@', 1) if len(conn_format) == 1: # No '@' so try config-path and login-path # The config_path and login-path collide on their first element and # only differs on their secondary optional values. # 1. Try match config_path and his optional value group. If both # matches and the connection data can be retrieved, return the data. # If errors occurs in this step raise them immediately. # 2. If config_path matches but group does not, and connection data # can not be retrieved, do not raise errors and try to math # login_path on step 4. # 3. If connection data is retrieved on step 2, then try login_path on # next step to overwrite values from the new configuration. # 4. If login_path matches, check is .mylogin.cnf exists, if it doesn't # and data configuration was found verify it for missing values and # continue if they are not any missing. # 5. If .mylogin.cnf exists and data configuration is found, overwrite # any previews value from config_path if there is any. # 6. If login_path matches a secondary value but the configuration data # could not be retrieved, do not continue and raise any error. # 7. In case errors have occurred trying to get data from config_path, # and group did not matched, and in addition no secondary value, # matched from login_path (port of socket) mention that config_path # and login_path were not able to retrieve the connection data. # try login_path and overwrite the values. # Handle the format: config-path[[group]] config_path, group = _match(_CONN_CONFIGPATH, conn_format[0]) port = None socket = None config_path_data = None login_path_data = None config_path_err_msg = None login_path = None if config_path: try: # If errors occurs, and group matched: raise any errors as the # group is exclusive of config_path. config_path_data = handle_config_path(config_path, group) except UtilError as err: if group: raise else: # Convert first letter to lowercase to include in error # message with the correct case. config_path_err_msg = \ err.errmsg[0].lower() + err.errmsg[1:] if group is None: # the conn_format can still be a login_path so continue # No '@' then handle has in the format: login-path[:port][:socket] login_path, port, socket = _match(_CONN_LOGINPATH, conn_format[0]) # Check if the login configuration file (.mylogin.cnf) exists if login_path and not my_login_config_exists(): if not config_path_data: util_err_msg = (".mylogin.cnf was not found at is default " "location: {0}. Please configure your " "login-path data before using it (use the " "mysql_config_editor tool)." "".format(my_login_config_path())) if config_path_err_msg and not (port or socket): util_err_msg = ("{0} In addition, {1}" "").format(util_err_msg, config_path_err_msg) raise UtilError(util_err_msg) else: # If needed, create a MyDefaultsReader and search for # my_print_defaults tool. if not my_defaults_reader: try: my_defaults_reader = MyDefaultsReader(options) except UtilError as err: if config_path_err_msg and not (port or socket): util_err_msg = ("{0} In addition, {1}" "").format(err.errmsg, config_path_err_msg) raise UtilError(util_err_msg) else: raise elif not my_defaults_reader.tool_path: my_defaults_reader.search_my_print_defaults_tool() # Check if the my_print_default tool is able to read a # login-path from the mylogin configuration file if not my_defaults_reader.check_login_path_support(): util_err_msg = ("the used my_print_defaults tool does not " "support login-path options: {0}. " "Please confirm that the location to a " "tool with login-path support is included " "in the PATH (at the beginning)." "".format(my_defaults_reader.tool_path)) if config_path_err_msg and not (port or socket): util_err_msg = ("{0} In addition, {1}" "").format(util_err_msg, config_path_err_msg) raise UtilError(util_err_msg) # Read and parse the login-path data (i.e., user, password and # host) login_path_data = my_defaults_reader.get_group_data(login_path) if config_path_data or login_path_data: if config_path_data: if not login_path_data: login_path_data = config_path_data else: # Overwrite values from login_path_data config_path_data.update(login_path_data) login_path_data = config_path_data user = login_path_data.get('user', None) passwd = login_path_data.get('password', None) host = login_path_data.get('host', None) if not port: port = login_path_data.get('port', None) if not socket: socket = login_path_data.get('socket', None) if os.name == "posix" and socket is not None: # if we are on unix systems and used a socket, hostname can be # safely assumed as being localhost so it is not required required_options = ('user', 'socket') host = 'localhost' if host is None else host else: required_options = ('user', 'host', 'port') missing_options = [opt for opt in required_options if locals()[opt] is None] # If we are on unix and port is missing, user might have specified # a socket instead if os.name == "posix" and "port" in missing_options: i = missing_options.index("port") if socket: # If we have a socket, port is not needed missing_options.pop(i) else: # if we don't have neither a port nor a socket, we need # either a port or a socket missing_options[i] = "port or socket" if missing_options: message = ",".join(missing_options) if len(missing_options) > 1: comma_idx = message.rfind(",") message = "{0} and {1}".format(message[:comma_idx], message[comma_idx + 1:]) pluralize = "s" if len(missing_options) > 1 else "" raise UtilError("Missing connection value{0} for " "{1} option{0}".format(pluralize, message)) # optional options, available only on config_path_data if config_path_data: ssl_ca = config_path_data.get('ssl-ca', None) ssl_cert = config_path_data.get('ssl-cert', None) ssl_key = config_path_data.get('ssl-key', None) ssl = config_path_data.get('ssl', None) else: if login_path and not config_path: raise UtilError("No login credentials found for login-path: " "{0}. Please review the used connection string" ": {1}".format(login_path, connection_values)) elif not login_path and config_path: raise UtilError("No login credentials found for config-path: " "{0}. Please review the used connection string" ": {1}".format(login_path, connection_values)) elif login_path and config_path: raise UtilError("No login credentials found for either " "login-path: '{0}' nor config-path: '{1}'. " "Please review the used connection string: {2}" "".format(login_path, config_path, connection_values)) elif len(conn_format) == 2: # Handle as in the format: user[:password]@host[:port][:socket] userpass, hostportsock = conn_format # Get user, password match = _CONN_USERPASS.match(userpass) if not match: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) user = match.group('user') if user is None: # No password provided user = match.group('suser').rstrip(':') passwd = match.group('passwd') # Handle host, port and socket if len(hostportsock) <= 0: raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) if hostportsock[0] in ['"', "'"]: # need to strip the quotes host, port, socket = _match(_CONN_QUOTEDHOST, hostportsock) if host[0] == '"': host = host.strip('"') if host[0] == "'": host = host.strip("'") else: host, port, socket, _ = parse_server_address(hostportsock) else: # Unrecognized format raise FormatError(_BAD_CONN_FORMAT.format(connection_values)) # Get character-set from options if isinstance(options, dict): charset = options.get("charset", None) # If one SSL option was found before, not mix with those in options. if not ssl_cert and not ssl_ca and not ssl_key and not ssl: ssl_cert = options.get("ssl_cert", None) ssl_ca = options.get("ssl_ca", None) ssl_key = options.get("ssl_key", None) ssl = options.get("ssl", None) else: # options is an instance of optparse.Values try: charset = options.charset # pylint: disable=E1103 except AttributeError: charset = None # If one SSL option was found before, not mix with those in options. if not ssl_cert and not ssl_ca and not ssl_key and not ssl: try: ssl_cert = options.ssl_cert # pylint: disable=E1103 except AttributeError: ssl_cert = None try: ssl_ca = options.ssl_ca # pylint: disable=E1103 except AttributeError: ssl_ca = None try: ssl_key = options.ssl_key # pylint: disable=E1103 except AttributeError: ssl_key = None try: ssl = options.ssl # pylint: disable=E1103 except AttributeError: ssl = None # Set parsed connection values connection = { "user": user, "host": host, "port": int(port) if port else 3306, "passwd": passwd if passwd else '' } if charset: connection["charset"] = charset if ssl_cert: connection["ssl_cert"] = ssl_cert if ssl_ca: connection["ssl_ca"] = ssl_ca if ssl_key: connection["ssl_key"] = ssl_key if ssl: connection["ssl"] = ssl # Handle optional parameters. They are only stored in the dict if # they were provided in the specifier. if socket is not None and os.name == "posix": connection['unix_socket'] = socket return connection
class System_test(object): """The System_test class is used by the MySQL Utilities Test (MUT) facility to perform system tests against MySQL utilitites. This class is the base class from which all tests are derived. The following utilities are provided: - Execute a utility as a subprocess and return result and populate a text file to capture output - Check number of servers for a test - Check a result file To create a test, subclass this class and supply definitions for the following abstract methods: - check_prerequisites - check conditions for test - setup - perform any database setup here - run - execute test cases - get_result - return result to MUT - cleanup - perform any tear down here Note: Place test case comments in the class documentation section. This will be printed by the --verbose option. """ __metaclass__ = ABCMeta # Register abstract base class def __init__(self, servers, res_dir, utildir, verbose=False, debug=False): """Constructor servers[in] A list of Server objects res_dir[in] Path to test result files utildir[in] Path to utilty scripts verbose[in] print extra data during operations (optional) default value = False debug[in] Turn on debugging mode for a single test default value = False """ self.res_fname = None # Name of intermediate result file self.results = [] # List for storing results self.servers = servers # Server_list class self.res_dir = res_dir # Current test result directory self.utildir = utildir # Location of utilities being tested self.verbose = verbose # Option for verbosity self.debug = debug # Option for diagnostic work def __del__(self): """Destructor Reset all parameters. """ for result in self.results: del result def check_gtid_unsafe(self, on = False): """Check for gtid enabled base server If on is True, method ensures server0 has the server variable DISABLE_GTID_UNSAFE_STATEMENTS=ON, else if on is False, method ensures server0 does not have DISABLE_GTID_UNSAFE_STATEMENTS=ON. Returns bool - False if no DISABE_GTID_UNSAFE_STATEMENTS variable found, else throws exception if criteria not met. """ if on: # Need servers with DISABLE_GTID_UNSAFE_STATEMENTS self.server0 = self.servers.get_server(0) res = self.server0.show_server_variable("DISABLE_GTID_UNSAFE_" "STATEMENTS") if res != [] and res[0][1] != "ON": raise MUTLibError("Test requires DISABLE_GTID_UNSAFE_STATEMENTS" " = ON") else: # Need servers without DISABLE_GTID_UNSAFE_STATEMENTS self.server0 = self.servers.get_server(0) res = self.server0.show_server_variable("DISABLE_GTID_UNSAFE_" "STATEMENTS") if res != [] and res[0][1] == "ON": raise MUTLibError("Test requires DISABLE_GTID_UNSAFE_STATEMENTS" " = OFF or a server prior to version 5.6.5.") return False def check_mylogin_requisites(self): """ Check if the tools to manipulate mylogin.cnf are accessible. This method verifies if the MySQL client tools my_print_defaults and mysql_config_editor are accessible. A MUTLibError exception is raised if the requisites are not met. """ try: self.login_reader = MyDefaultsReader( find_my_print_defaults_tool=True) except UtilError as err: raise MUTLibError("MySQL client tools must be accessible to run " "this test (%s). E.g. Add the location of the " "MySQL client tools to your PATH." % err.errmsg) if not self.login_reader.check_login_path_support(): raise MUTLibError("ERROR: the used my_print_defaults tool does not " "support login-path options. Used tool: %s" % self.login_reader.tool_path) try: self.edit_tool_path = get_tool_path(None, "mysql_config_editor", search_PATH=True) except UtilError as err: raise MUTLibError("MySQL client tools must be accessible to run " "this test (%s). E.g. Add the location of the " "MySQL client tools to your PATH." % err.errmsg) def create_login_path_data(self, login_path, user, host): """Add the specified login-path data to .mylogin.cnf. Execute mysql_config_editor tool to create a new login-path entry to the .mylogin.cnf file. Note: the use of password is not supported because it is not read from the stdin by the tool (apparently for security reasons). """ assert self.edit_tool_path, ("The tool mysql_config_editor is not " "accessible. First, use method " "check_mylogin_requisites.") cmd = [self.edit_tool_path] cmd.append('set') cmd.append('--login-path=%s' % login_path) cmd.append('--host=%s' % host) cmd.append('--user=%s' % user) # Create a temporary file to redirect stdout out_file = tempfile.TemporaryFile() # Execute command to create login-path data proc = subprocess.Popen(cmd, stdout=out_file, stdin=subprocess.PIPE) # Overwrite login-path if already exists (i.e. answer 'y' to question) proc.communicate('y') def remove_login_path_data(self, login_path): """Remove the specified login-path data from .mylogin.cnf. Execute mysql_config_editor tool to remove the specified login-path entry from the .mylogin.cnf file. """ assert self.edit_tool_path, ("The tool mysql_config_editor is not " "accessible. First, use method " "check_mylogin_requisites.") cmd = [self.edit_tool_path] cmd.append('remove') cmd.append('--login-path=%s' % login_path) # Create a temporary file to redirect stdout out_file = tempfile.TemporaryFile() # Execute command to remove login-path data if self.verbose: subprocess.call(cmd, stdout=out_file) else: # Redirect stderr to null null_file = open(os.devnull, "w+b") subprocess.call(cmd, stdout=out_file, stderr=null_file) def exec_util(self, cmd, file_out, abspath=False): """Execute Utility This method executes a MySQL utility using the utildir specified in MUT. It returns the return value from the completion of the command and writes the output to the file supplied. cmd[in] The command to execute including all parameters file_out[in] Path and filename of a file to write output abspath[in] Use absolute path and not current directory Returns return value of process run. """ return _exec_util(cmd, file_out, self.utildir, self.debug, abspath) def check_num_servers(self, num_servers): """Check the number of servers available. num_servers[in] The minimal number of Server objects required Returns True - servers available, False - not enough servers """ if self.servers.num_servers() >= num_servers: return True return False def get_connection_parameters(self, server): """Return a string that comprises the normal connection parameters common to MySQL utilities for a particular server. server[in] A Server object Returns string """ str1 = "--user=%s --host=%s " % (server.user, server.host) if server.passwd: str1 += "--password=%s " % server.passwd if server.socket: str2 = "--socket=%s " % (server.socket) else: str2 = "--port=%s " % (server.port) return str1 + str2 def get_connection_values(self, server): """Return a tuple that comprises the connection parameters for a particular server. server[in] A Server object Returns (user, password, host, port, socket) """ if server is None: raise MUTLibError("Server not initialized!") return (server.user, server.passwd, server.host, server.port, server.socket, server.role) def build_connection_string(self, server): """Return a connection string server[in] A Server object Returns string of the form user:password@host:port:socket """ conn_val = self.get_connection_values(server) conn_str = "%s" % conn_val[0] if conn_val[1]: conn_str += ":%s" % conn_val[1] conn_str += "@%s:" % conn_val[2] if conn_val[3]: conn_str += "%s" % conn_val[3] if conn_val[4] is not None and conn_val[4] != "": conn_str += ":%s " % conn_val[4] return conn_str def run_test_case(self, exp_result, command, comments, debug=False): """Execute a test case and save the results. Call this method to run a test case and save the results to the results list. exp_result[in] The expected result (returns True if matches) command[in] Execution command (e.g. ./mysqlclonedb.py --help) comments[in] Comments to put in result list debug[in] Print debug information during execution Returns True if result matches expected result """ if self.debug or debug: print "\n%s" % comments res = self.exec_util(command, self.res_fname) if comments: self.results.append(comments + "\n") self.record_results(self.res_fname) return res == exp_result def run_test_case_result(self, command, comments, debug=False): """Execute a test case and save the results returning actual result. Call this method to run a test case and save the results to the results list. command[in] Execution command (e.g. ./mysqlclonedb.py --help) comments[in] Comments to put in result list debug[in] Print debug information during execution Returns int - actual result """ if self.debug or debug: print "\n%s" % comments res = self.exec_util(command, self.res_fname) if comments: self.results.append(comments + "\n") self.record_results(self.res_fname) return res def replace_result(self, prefix, str): """Replace a string in the results with a new, deterministic string. prefix[in] starting prefix of string to mask str[in] replacement string """ linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: self.results.pop(linenum) self.results.insert(linenum, str) linenum += 1 def remove_result(self, prefix): """Remove a string in the results. prefix[in] starting prefix of string to mask """ linenums = [] linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: linenums.append(int(linenum)) linenum += 1 # Must remove lines in reverse order for linenum in range(len(linenums)-1, -1, -1): self.results.pop(linenums[linenum]) def remove_result_and_lines_before(self, prefix, lines=1): """Remove lines in the results. prefix[in] starting prefix of string to mask lines[in] number of lines to remove previously to the prefix line. """ linenums = [] linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: linenums.append(int(linenum)) for line2rm in range(linenum-lines,linenum): if line2rm > - 1: linenums.append(int(line2rm)) linenum += 1 linenums.sort() # Must remove lines in reverse order for linenum in range(len(linenums) - 1, - 1, - 1): self.results.pop(linenums[linenum]) def replace_substring(self, target, replacement): """Replace a target substring in the entire result file. target[in] target string to replace replacement[in] string to replace """ linenum = 0 for line in self.results: if line.find(target): self.results.pop(linenum) replace_line = line.replace(target, replacement) self.results.insert(linenum, replace_line) linenum += 1 def mask_result(self, prefix, target, mask): """Mask out a portion of a string for the results. str[in] string to mask prefix[in] starting prefix of string to mask target[in] substring to search for to mask mask[in] mask string (e.g. '######") """ linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: loc = line.find(target) if loc >= 0: start = loc + len(mask) self.results.pop(linenum) if start > len(line): self.results.insert(linenum, line[0:loc] + mask + "\n") else: self.results.insert(linenum, line[0:loc] + mask + line[start:]) linenum += 1 def mask_result_portion(self, prefix, target, end_target, mask): """Mask out a portion of a string for the results using a end target to make the masked area a specific length. str[in] string to mask prefix[in] starting prefix of string to mask target[in] substring to search for to mask end_target[in] substring to mark end of mask mask[in] mask string (e.g. '######") """ linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: loc = line.find(target) if loc >= 0: end = line.find(end_target) if end >= 0: self.results.pop(linenum) if end > len(line): self.results.insert(linenum, line[0:loc] + mask + "\n") else: self.results.insert(linenum, line[0:loc] + mask + line[end:]) linenum += 1 def mask_column_result(self, prefix, separator, num_col, mask): """Mask out a column portion of a string for the results. str[in] string to mask prefix[in] starting prefix of string to mask separator[in] separator for columns (e.g. ',') num_col[in] number of column to mask mask[in] mask string (e.g. '######") """ linenum = 0 for line in self.results: index = line.find(prefix) if index == 0: pos = 0 for i in range(0, num_col): loc = line.find(separator, pos) if i+1 == num_col: next = line.find(separator, loc) if next < 0: start = len(line) else: start = next self.results.pop(linenum) if start >= len(line): self.results.insert(linenum, line[0:pos] + mask + "\n") else: self.results.insert(linenum, line[0:pos] + mask + line[start:]) else: pos = loc + 1 if loc < 0: break linenum += 1 def check_objects(self, server, db, events=True): """Check number of objects. Creates a string containing the number of objects for a given database. server[in] Server object to query db[in] name of database to check Returns string """ from mysql.utilities.common.database import Database db_source = Database(server, db) db_source.init() res = db_source.get_db_objects("TABLE") str = "OBJECT COUNTS: tables = %s, " % (len(res)) res = db_source.get_db_objects("VIEW") str += "views = %s, " % (len(res)) res = db_source.get_db_objects("TRIGGER") str += "triggers = %s, " % (len(res)) res = db_source.get_db_objects("PROCEDURE") str += "procedures = %s, " % (len(res)) res = db_source.get_db_objects("FUNCTION") str += "functions = %s, " % (len(res)) if events: res = db_source.get_db_objects("EVENT") str += "events = %s \n" % (len(res)) return str def compare(self, name, actual): """Compare an actual set of return values to the result file for this test. name[in] test name (use __name__) actual[in] String list of the actual results Returns: (bool, diff) where: (True, None) = results identical (False, "result file missing") = result file missing (False, <string list>) = results differ """ # # Check to see if result file exists first. # res_fname = os.path.normpath(os.path.join(self.res_dir, name + ".result")) if not os.access(res_fname, os.F_OK): actual.insert(0, "Result file missing - actual results:\n\n") return (False, actual) # # Use ndiff to compare to known result file # res_file = open(res_fname) diff = difflib.ndiff(res_file.readlines(), actual) # # Now convert the diff to a string list and write reject file # rej_fname = os.path.normpath(os.path.join(self.res_dir, name + ".reject")) rej_file = open(rej_fname, 'w+') rej_list = [] try: while 1: str = diff.next() if str[0] in ['-', '+', '?']: rej_list.append(str) rej_file.write(str) except: pass rej_file.close() # Write preamble if there are differences if not rej_list == []: rej_list.insert(0, "Result file mismatch:\n") # If test passed, delete the reject file if it exists elif os.access(rej_fname, os.F_OK): os.unlink(rej_fname) return (rej_list == [], rej_list) def record_results(self, fname): """Saves the results from a file to the self.results list. fname[in] Name of results file from exec_util """ f_res = open(fname) for line in f_res.readlines(): self.results.append(line) f_res.close() def save_result_file(self, name, results): """Saves a result file for the test. name[in] Test name (use __name__) results[in] String list of the results Returns True - success, False - fail """ if results: res_fname = os.path.normpath(os.path.join(self.res_dir, name + ".result")) res_file = open(res_fname, 'w+') if not res_file: return False for str in results: res_file.write(str) res_file.close() return True def is_long(self): """Is test marked as a long running test? Override this method to specify the test is a long-running test. """ return False @abstractmethod def check_prerequisites(self): """Check preprequisites for test. This method is used to check any prerequisites for a test such as the number of servers needed, environment variables, etc. Returns: True = servers available, False = not enough servers, skip """ pass @abstractmethod def setup(self): """Setup conditions for test. This method is used to setup any conditions for a test such as loading test data or setting server variables. Note: if setup fails, cleanup() is still called. Consider this when implementing complex setup procedures. Returns: True = no errors, False = errors, skip test """ pass @abstractmethod def run(self): """Execute a test. This method is used to execute the test cases in the test. One or more calls to exec_util() may be performed here, but results are saved here and checked later. Returns: True = no errors, False = errors occurred """ pass @abstractmethod def get_result(self): """Return results of test to MUT. This method is used to decided if the test passed. It is in this method where the results of the run() method are checked. This allows separation of the evaluation of the test form the execution and other steps. Returns: tuple (bool, string list) where: (True, None) = test passed (False, <string list>) - test failed. String list to be displayed Note: Formatting for string list should be done by the callee. The caller prints exactly what is returned. """ pass @abstractmethod def record(self): """Record test results for comparison. This method is used to record any test results for a result compare- type test. To do so, call self.save_result_file(__name__, strlist) where strlist is the output the test will compare to determine success. Note: If your test is not a comparative test, you can simply return True (success). In this case, the --record option has no effect. Returns: True - success, False - error """ pass @abstractmethod def cleanup(self): """Perform any post-test cleanup. This method is used to remove the setup conditions from the server. Returns: True = no errors, False = errors occurred """ pass