###########################################################################
#
# This file contains test for (3) test suite.
#
# Consult WL#2789 for more information.
#
###########################################################################

#
# Check the options-management commands:
#  - UNSET;
#  - FLUSH INSTANCES;
#
# Let's test the commands on the option 'server_id'. It's expected that
# originally the instances have the following server ids:
#  - mysqld1: 1
#  - mysqld2: 2
#
# The test case:
#
#  1. UNSET <instance_id>.server_id;
#
#   Do the step for both instances.
#
#   1.1. check that the configuration file has been updated (i.e.  does not
#     contain 'server_id=' line for the instance);
#   1.2. (for mysqld1) check that the running instance has not been affected:
#     connect to the instance and check that 'SHOW VARIABLES LIKE 'server_id''
#     returns non-zero value;
#   1.3. check that internal cache of Instance Manager is not affected (i.e.
#     SHOW INSTANCE OPTIONS <instance> contains non-zero value for server_id);
#
#  2. FLUSH INSTANCES;
#
#   Do the step for both instances.
#
#   2.1. check that the configuration file has not been updated (i.e.  does not
#     contain 'server_id=' for the instance);
#   2.2. (for mysqld1) check that the running instance has not been affected:
#     connect to the instance and check that 'SHOW VARIABLES LIKE 'server_id''
#     returns non-zero value;
#   2.3. check that internal cache of Instance Manager has been updated (i.e.
#     SHOW INSTANCE OPTIONS <instance> does not contain 'server_id=' line).
#

###########################################################################

--source include/im_check_os.inc

###########################################################################
#
# 0. Check starting conditions.
#
###########################################################################

# - check the configuration file;

--exec grep server_id $MYSQL_TEST_DIR/var/im.cnf ;

# - check the running instances.

--connect (mysql1_con,localhost,root,,mysql,$IM_MYSQLD1_PORT,$IM_MYSQLD1_SOCK)

--connection mysql1_con

SHOW VARIABLES LIKE 'server_id';

--connection default

# - check the internal cache.
#   TODO: we should check only server_id option here.

# SHOW INSTANCE OPTIONS mysqld1;
# SHOW INSTANCE OPTIONS mysqld2;

###########################################################################
#
# 1. UNSET <instance_id>.server_id;
#
###########################################################################

# * mysqld1

UNSET mysqld1.server_id;

#   - check that the configuration file has been updated (i.e.  does not
#     contain 'server_id=' line for the instance);

--exec grep server_id $MYSQL_TEST_DIR/var/im.cnf ;

#   - check that the running instance has not been affected: connect to the
#     instance and check that 'SHOW VARIABLES LIKE 'server_id'' returns non-zero
#     value;

--connection mysql1_con

SHOW VARIABLES LIKE 'server_id';

--connection default

#   - check that internal cache of Instance Manager is not affected (i.e.  SHOW
#     INSTANCE OPTIONS <instance> contains non-zero value for server_id);
#     TODO: we should check only server_id option here.

# SHOW INSTANCE OPTIONS mysqld1;

# * mysqld2

UNSET mysqld2.server_id;

#   - check that the configuration file has been updated (i.e.  does not
#     contain 'server_id=' line for the instance);

--exec grep server_id $MYSQL_TEST_DIR/var/im.cnf || true;

#   - check that internal cache of Instance Manager is not affected (i.e.  SHOW
#     INSTANCE OPTIONS <instance> contains non-zero value for server_id);
#     TODO: we should check only server_id option here.

# SHOW INSTANCE OPTIONS mysqld2;

###########################################################################
#
# 2. FLUSH INSTANCES;
#
###########################################################################

FLUSH INSTANCES;

#   - check that the configuration file has not been updated (i.e.  does not
#     contain 'server_id=' for the instance);

--exec grep server_id $MYSQL_TEST_DIR/var/im.cnf || true;

#   - (for mysqld1) check that the running instance has not been affected:
#     connect to the instance and check that 'SHOW VARIABLES LIKE 'server_id''
#     returns non-zero value;

--connection mysql1_con

SHOW VARIABLES LIKE 'server_id';

--connection default

#   - check that internal cache of Instance Manager has been updated (i.e.
#     SHOW INSTANCE OPTIONS <instance> does not contain 'server_id=' line).
#     TODO: we should check only server_id option here.

# SHOW INSTANCE OPTIONS mysqld1;
# SHOW INSTANCE OPTIONS mysqld2;