################################################################################
# Validate that when a member joins a group, if the recovery users are
# asymmetrical configured, the joiner will fail to complete recovery after
# cloning the donor.
#
# Test:
# 0. The test requires two servers: M1 and M2.
# 1. Install clone plugin on server1.
#    Change the recovery user to a non-existing one, once
#    server2 clones server1 it will try to use that user and fail.
#    Bootstrap a group on server1.
# 2. Install clone plugin on server2.
#    Restart server with a monitoring process (mysqld_safe) if needed
# 3. Join the group, clone will be done, but the recovery
#    after restart will fail due to the non-existent recovery user.
# 4. Cleanup
#

--source include/have_mysqld_monitoring_process.inc
--source include/have_clone_plugin.inc
--source include/force_restart.inc
--source include/have_group_replication_plugin.inc
--let $rpl_skip_group_replication_start= 1
--source include/group_replication.inc

# Validate plugins
--let plugins = CLONE_PLUGIN,GROUP_REPLICATION
--source include/check_plugin_dir.inc

--echo
--echo ############################################################
--echo # 1. Install clone plugin on server1.
--echo #    Change the recovery user to a non-existing one, once
--echo #    server2 clones server1 it will try to use that user and
--echo #    fail.
--echo #    Bootstrap a group on server1.
--let $rpl_connection_name= server1
--source include/rpl_connection.inc

# Install the clone plugin
--replace_result $CLONE_PLUGIN CLONE_PLUGIN
--eval INSTALL PLUGIN clone SONAME '$CLONE_PLUGIN'

# Change the recovery user to a non-existing one.
# This information will be cloned into server 2
--disable_warnings
--eval CHANGE REPLICATION SOURCE TO SOURCE_USER='non-existent', SOURCE_PASSWORD='foo' FOR CHANNEL 'group_replication_recovery'
--enable_warnings

# Add some data for recovery
CREATE TABLE t1 (c1 INT NOT NULL PRIMARY KEY) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1);
INSERT INTO t1 VALUES (2);

--source include/start_and_bootstrap_group_replication.inc

--echo
--echo #####################################################################
--echo # 2. Install clone plugin on server2.
--echo #    Restart server with a monitoring process (mysqld_safe) if needed

# Install the clone plugin
--let $rpl_connection_name= server2
--source include/rpl_connection.inc

--replace_result $CLONE_PLUGIN CLONE_PLUGIN
--eval INSTALL PLUGIN clone SONAME '$CLONE_PLUGIN'

--let $_group_replication_local_address= `SELECT @@GLOBAL.group_replication_local_address`
--let $_group_replication_group_seeds= `SELECT @@GLOBAL.group_replication_group_seeds`

--let $plugin_list= $GROUP_REPLICATION
--source include/spawn_monitoring_process.inc

# Configure GR for starting now and after restart
--disable_query_log
--eval SET PERSIST  group_replication_group_name= "$group_replication_group_name"
--eval SET PERSIST  group_replication_local_address= "$_group_replication_local_address"
--eval SET PERSIST  group_replication_group_seeds= "$_group_replication_group_seeds"
SET PERSIST group_replication_start_on_boot= ON;
SET PERSIST group_replication_recovery_retry_count= 1;
--enable_query_log

--echo
--echo #####################################################################
--echo # 3. Join the group, clone will be done, but the recovery
--echo #    after restart will fail due to the non-existent recovery user.

SET GLOBAL group_replication_clone_threshold= 1;

START GROUP_REPLICATION;

--source include/wait_until_disconnected.inc

--let $rpl_server_number= 2
--source include/rpl_reconnect.inc

--let $group_replication_member_state= ERROR
--source include/gr_wait_for_member_state.inc

--let $assert_text= Clone must be complete
--let $assert_cond= [SELECT state="Completed" FROM performance_schema.clone_status] = 1;
--source include/assert.inc

--echo
--echo ############################################################
--echo # 4. Cleanup.
--let $rpl_connection_name= server1
--source include/rpl_connection.inc
DROP TABLE t1;

--let $rpl_connection_name= server2
--source include/rpl_connection.inc
--source include/stop_group_replication.inc
DROP TABLE t1;

RESET PERSIST IF EXISTS group_replication_group_name;
RESET PERSIST IF EXISTS group_replication_local_address;
RESET PERSIST IF EXISTS group_replication_group_seeds;
RESET PERSIST IF EXISTS group_replication_start_on_boot;
RESET PERSIST IF EXISTS group_replication_recovery_retry_count;

SET SESSION sql_log_bin=0;
call mtr.add_suppression("This member will start distributed recovery using clone. It is due to the number of missing transactions being higher than the configured threshold of 1.");
call mtr.add_suppression("There was an error when connecting to the donor server. Please check that group_replication_recovery channel credentials and all MEMBER_HOST column values of performance_schema.replication_group_members table are correct and DNS resolvable.");
call mtr.add_suppression("For details please check performance_schema.replication_connection_status table and error log messages of Slave I/O for channel group_replication_recovery.");
call mtr.add_suppression("Maximum number of retries when trying to connect to a donor reached. Aborting group replication incremental recovery.");
call mtr.add_suppression("Fatal error during the incremental recovery process of Group Replication. The server will leave the group.");
call mtr.add_suppression("The server was automatically set into read only mode after an error was detected.");
call mtr.add_suppression("Skipping leave operation: concurrent attempt to leave the group is on-going.");
call mtr.add_suppression("Clone removing all user data for provisioning: Started");
call mtr.add_suppression("Clone removing all user data for provisioning: Finished");
SET SESSION sql_log_bin=1;

--source include/clean_monitoring_process.inc

--source include/group_replication_end.inc
