1/*
2*/
3
4#include "my_test.h"
5#include "ma_pvio.h"
6
7static int aurora1(MYSQL *unused __attribute__((unused)))
8{
9 int rc;
10 my_bool read_only= 1;
11 char *primary, *my_schema;
12 MYSQL_RES *res;
13 MYSQL *mysql= mysql_init(NULL);
14
15 if (!mysql_real_connect(mysql, hostname, username, password, schema, port, NULL, 0))
16 {
17 diag("Error: %s", mysql_error(mysql));
18 mysql_close(mysql);
19 return FAIL;
20 }
21
22 rc= mysql_query(mysql, "DROP TABLE IF EXISTS t1");
23 check_mysql_rc(rc, mysql);
24
25 rc= mysql_query(mysql, "CREATE TABLE t1 (a int, b varchar(20))");
26 check_mysql_rc(rc, mysql);
27
28 rc= mysql_query(mysql, "INSERT INTO t1 VALUES (1, 'foo'), (2, 'bar')");
29 check_mysql_rc(rc, mysql);
30
31 mariadb_get_infov(mysql, MARIADB_CONNECTION_HOST, &primary);
32 diag("primary: %s", primary);
33
34 mysql_options(mysql, MARIADB_OPT_CONNECTION_READ_ONLY, &read_only);
35
36 /* ensure, that this is a replica, so INSERT should fail */
37 rc= mysql_query(mysql, "INSERT INTO t1 VALUES (3, 'error')");
38 if (rc)
39 diag("Expected error: %s", mysql_error(mysql));
40
41 rc= mysql_query(mysql, "SELECT a, b FROM t1");
42 check_mysql_rc(rc, mysql);
43
44 res= mysql_store_result(mysql);
45
46 diag("Num_rows: %lld", mysql_num_rows(res));
47 mysql_free_result(res);
48
49 mariadb_get_infov(mysql, MARIADB_CONNECTION_SCHEMA, &my_schema);
50 diag("db: %s", my_schema);
51
52 mysql_close(mysql);
53
54 return OK;
55}
56
57static int test_wrong_user(MYSQL *unused __attribute__((unused)))
58{
59 MYSQL *mysql= mysql_init(NULL);
60
61 if (mysql_real_connect(mysql, hostname, "wrong_user", NULL, NULL, 0, NULL, 0))
62 {
63 diag("Error expected");
64 mysql_close(mysql);
65 return FAIL;
66 }
67 mysql_close(mysql);
68 return OK;
69}
70
71static int test_reconnect(MYSQL *unused __attribute__((unused)))
72{
73 MYSQL *mysql= mysql_init(NULL);
74 MYSQL_RES *res;
75 my_bool read_only= 1;
76 int rc;
77 my_bool reconnect= 1;
78 char *aurora_host;
79
80 mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect);
81
82 if (!mysql_real_connect(mysql, hostname, username, password, schema, port, NULL, 0))
83 {
84 diag("Error: %s", mysql_error(mysql));
85 mysql_close(mysql);
86 return FAIL;
87 }
88
89 mariadb_get_infov(mysql, MARIADB_CONNECTION_HOST, &aurora_host);
90 diag("host: %s", aurora_host);
91
92 rc= mysql_query(mysql, "DROP TABLE IF EXISTS tx01");
93 check_mysql_rc(rc, mysql);
94 rc= mysql_query(mysql, "CREATE TABLE tx01 (a int)");
95 check_mysql_rc(rc, mysql);
96
97 /* we force cluster restart and promoting new primary:
98 * we wait for 50 seconds - however there is no guarantee that
99 * cluster was restarted already - so this test might fail */
100 rc= system("/usr/local/aws/bin/aws rds failover-db-cluster --db-cluster-identifier instance-1-cluster");
101
102 diag("aws return code: %d", rc);
103
104 sleep(50);
105 diag("Q1");
106 rc= mysql_query(mysql, "INSERT INTO tx01 VALUES (1)");
107 if (!rc)
108 diag("error expected!");
109 diag("Error: %s", mysql_error(mysql));
110
111 diag("Q2");
112 rc= mysql_query(mysql, "INSERT INTO tx01 VALUES (1)");
113 if (rc)
114 {
115 diag("no error expected!");
116 diag("Error: %s", mysql_error(mysql));
117 diag("host: %s", mysql->host);
118 }
119 else
120 {
121 mariadb_get_infov(mysql, MARIADB_CONNECTION_HOST, &aurora_host);
122 diag("host: %s", aurora_host);
123 }
124
125 mysql_options(mysql, MARIADB_OPT_CONNECTION_READ_ONLY, &read_only);
126
127 rc= mysql_query(mysql, "SELECT * from tx01");
128 check_mysql_rc(rc, mysql);
129
130 if ((res= mysql_store_result(mysql)))
131 {
132 diag("num_rows: %lld", mysql_num_rows(res));
133 mysql_free_result(res);
134 }
135
136 mariadb_get_infov(mysql, MARIADB_CONNECTION_HOST, &aurora_host);
137 diag("host: %s", aurora_host);
138
139 mysql_close(mysql);
140 return OK;
141}
142
143struct my_tests_st my_tests[] = {
144 {"aurora1", aurora1, TEST_CONNECTION_NONE, 0, NULL, NULL},
145 {"test_wrong_user", test_wrong_user, TEST_CONNECTION_NONE, 0, NULL, NULL},
146 {"test_reconnect", test_reconnect, TEST_CONNECTION_NONE, 0, NULL, NULL},
147 {NULL, NULL, 0, 0, NULL, NULL}
148};
149
150
151int main(int argc, char **argv)
152{
153 mysql_library_init(0,0,NULL);
154
155 if (argc > 1)
156 get_options(argc, argv);
157
158 get_envvars();
159
160 run_tests(my_tests);
161
162 mysql_server_end();
163 return(exit_status());
164}
165