def setup(self): self.res_fname = "result.txt" result = replicate.test.setup(self) # Note: server1 is master, server2, server3 are slaves. # server3 is a new slave with nothing on it. self.server3 = self.servers.spawn_server( "new_slave", kill=True, mysqld='"--log-bin=mysql-bin"') self._drop_all() self.server1.exec_query("STOP SLAVE") self.server1.exec_query("RESET SLAVE") self.server2.exec_query("STOP SLAVE") self.server2.exec_query("RESET SLAVE") try: for cmd in _MASTER_DB_CMDS: self.server1.exec_query(cmd) except MUTLibError: raise data_file = os.path.normpath("./std_data/basic_data.sql") try: self.server1.read_and_exec_SQL(data_file, self.debug) self.server2.read_and_exec_SQL(data_file, self.debug) except MUTLibError as err: raise MUTLibError("Failed to read commands from file {0}: " "{1}".format(data_file, err.errmsg)) master_str = "--master={0}".format( self.build_connection_string(self.server1)) slave_str = " --slave={0}".format( self.build_connection_string(self.server2)) conn_str = master_str + slave_str cmd = "mysqlreplicate.py --rpl-user=rpl:rpl {0}".format(conn_str) try: self.exec_util(cmd, self.res_fname) except MUTLibError: raise # server1 is now a master server, lets treat it accordingly self.server1 = Master.fromServer(self.server1) try: self.server1.connect() except UtilError as err: raise MUTLibError("Cannot connect to spawned " "server: {0}".format(err.errmsg)) # server2 is now a slave, lets treat it accordingly self.server2 = Slave.fromServer(self.server2) try: self.server2.connect() except UtilError as err: raise MUTLibError("Cannot connect to spawned " "server: {0}".format(err.errmsg)) return result
def run_test_case(self, actual_result, test_num, master, source, destination, cmd_list, db_list, cmd_opts, comment, expected_results, restart_replication=False, skip_wait=False): results = [comment] # Drop all databases and reestablish replication if restart_replication: # Rollback here to avoid active transaction error for STOP SLAVE # with 5.5 servers (versions > 5.5.0). if self.servers.get_server(0).check_version_compat(5, 5, 0): destination.rollback() destination.exec_query("STOP SLAVE") destination.exec_query("RESET SLAVE") for db in db_list: self.drop_db(destination, db) master_str = "--master={0}".format( self.build_connection_string(master)) slave_str = " --slave={0}".format( self.build_connection_string(destination)) conn_str = master_str + slave_str cmd = "mysqlreplicate.py --rpl-user=rpl:rpl {0}".format(conn_str) try: self.exec_util(cmd, self.res_fname) except MUTLibError: raise # Convert object instance of master server to Master, if needed if not isinstance(master, Master): master = Master.fromServer(master) try: master.connect() except UtilError as err: raise MUTLibError("Cannot connect to spawned " "server: {0}".format(err.errmsg)) # Convert object instance of destination server to Slave, if needed if not isinstance(destination, Slave): destination = Slave.fromServer(destination) try: destination.connect() except UtilError as err: raise MUTLibError("Cannot connect to spawned " "server: {0}".format(err.errmsg)) # Check databases on slave and save results for 'BEFORE' check results.append(self._check_result(destination, "SHOW DATABASES " "LIKE 'util_test'")) results.append(self._check_result(destination, "SELECT COUNT(*) " "FROM util_test.t1")) results.append(self._check_result(destination, "SHOW DATABASES " "LIKE 'master_db1'")) results.append(self._check_result(destination, "SELECT COUNT(*) " "FROM master_db1.t1")) # Run the commands for cmd_str in cmd_list: try: res = self.exec_util(cmd_str + cmd_opts, self.res_fname) results.insert(1, res) # save result at front of list if res != actual_result: return False except MUTLibError: raise # Wait for slave to connect to master if not skip_wait: if self.debug: print "# Waiting for slave to connect to master", try: self.wait_for_slave_connection(destination, _MAX_ATTEMPTS) except MUTLibError: raise if self.debug: print "done." # Check databases on slave and save results for 'AFTER' check results.append(self._check_result(destination, "SHOW DATABASES " "LIKE 'util_test'")) results.append(self._check_result(destination, "SELECT COUNT(*) " "FROM util_test.t1")) results.append(self._check_result(destination, "SHOW DATABASES " "LIKE 'master_db1'")) results.append(self._check_result(destination, "SELECT COUNT(*) " "FROM master_db1.t1")) # Add something to master and check slave master.exec_query("INSERT INTO master_db1.t1 VALUES (10), (11)") # Wait for slave to catch up if not skip_wait: if self.debug: print "# Waiting for slave to sync", bin_info = master.get_binlog_info() if bin_info is None: # server is no longer acting as a master raise MUTLibError("The server '{0}' is no longer a master" "server".format(master.role)) # pylint: disable=W0633 binlog_file, binlog_pos = bin_info # Wait for slave to catch up with master, using the binlog # Note: This test requires servers without GTIDs (prior to 5.6.5) synced = destination.wait_for_slave(binlog_file, binlog_pos, _SYNC_TIMEOUT) if not synced: raise MUTLibError("Slave did not catch up with master") if self.debug: print "done." # ROLLBACK to close any active transaction leading to wrong values for # the next SELECT COUNT(*) with 5.5 servers (versions > 5.5.0). if self.servers.get_server(0).check_version_compat(5, 5, 0): destination.rollback() results.append(self._check_result(destination, "SELECT COUNT(*) " "FROM master_db1.t1")) if self.debug: print comment print "Expected Results:", expected_results[test_num - 1] print " Actual Results:", results[1:] self.results.append(results) return True
def run_test_case(self, actual_result, test_num, master, source, destination, cmd_list, db_list, cmd_opts, comment, expected_results, restart_replication=False, skip_wait=False): results = [comment] # Drop all databases and reestablish replication if restart_replication: # Rollback here to avoid active transaction error for STOP SLAVE # with 5.5 servers (versions > 5.5.0). if self.servers.get_server(0).check_version_compat(5, 5, 0): destination.rollback() destination.exec_query("STOP SLAVE") destination.exec_query("RESET SLAVE") for db in db_list: self.drop_db(destination, db) master_str = "--master={0}".format( self.build_connection_string(master)) slave_str = " --slave={0}".format( self.build_connection_string(destination)) conn_str = master_str + slave_str cmd = "mysqlreplicate.py --rpl-user=rpl:rpl {0}".format(conn_str) try: self.exec_util(cmd, self.res_fname) except MUTLibError: raise # Convert object instance of master server to Master, if needed if not isinstance(master, Master): master = Master.fromServer(master) try: master.connect() except UtilError as err: raise MUTLibError("Cannot connect to spawned " "server: {0}".format(err.errmsg)) # Convert object instance of destination server to Slave, if needed if not isinstance(destination, Slave): destination = Slave.fromServer(destination) try: destination.connect() except UtilError as err: raise MUTLibError("Cannot connect to spawned " "server: {0}".format(err.errmsg)) # Check databases on slave and save results for 'BEFORE' check results.append( self._check_result(destination, "SHOW DATABASES " "LIKE 'util_test'")) results.append( self._check_result(destination, "SELECT COUNT(*) " "FROM util_test.t1")) results.append( self._check_result(destination, "SHOW DATABASES " "LIKE 'master_db1'")) results.append( self._check_result(destination, "SELECT COUNT(*) " "FROM master_db1.t1")) # Run the commands for cmd_str in cmd_list: try: res = self.exec_util(cmd_str + cmd_opts, self.res_fname) results.insert(1, res) # save result at front of list if res != actual_result: return False except MUTLibError: raise # Wait for slave to connect to master if not skip_wait: if self.debug: print "# Waiting for slave to connect to master", try: self.wait_for_slave_connection(destination, _MAX_ATTEMPTS) except MUTLibError: raise if self.debug: print "done." # Check databases on slave and save results for 'AFTER' check results.append( self._check_result(destination, "SHOW DATABASES " "LIKE 'util_test'")) results.append( self._check_result(destination, "SELECT COUNT(*) " "FROM util_test.t1")) results.append( self._check_result(destination, "SHOW DATABASES " "LIKE 'master_db1'")) results.append( self._check_result(destination, "SELECT COUNT(*) " "FROM master_db1.t1")) # Add something to master and check slave master.exec_query("INSERT INTO master_db1.t1 VALUES (10), (11)") # Wait for slave to catch up if not skip_wait: if self.debug: print "# Waiting for slave to sync", bin_info = master.get_binlog_info() if bin_info is None: # server is no longer acting as a master raise MUTLibError("The server '{0}' is no longer a master" "server".format(master.role)) # pylint: disable=W0633 binlog_file, binlog_pos = bin_info # Wait for slave to catch up with master, using the binlog # Note: This test requires servers without GTIDs (prior to 5.6.5) synced = destination.wait_for_slave(binlog_file, binlog_pos, _SYNC_TIMEOUT) if not synced: raise MUTLibError("Slave did not catch up with master") if self.debug: print "done." # ROLLBACK to close any active transaction leading to wrong values for # the next SELECT COUNT(*) with 5.5 servers (versions > 5.5.0). if self.servers.get_server(0).check_version_compat(5, 5, 0): destination.rollback() results.append( self._check_result(destination, "SELECT COUNT(*) " "FROM master_db1.t1")) if self.debug: print comment print "Expected Results:", expected_results[test_num - 1] print " Actual Results:", results[1:] self.results.append(results) return True