1
1
# -*- coding: utf-8 -*-
2
2
# MySQL Connector/Python - MySQL driver written in Python.
3
- # Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3
+ # Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
4
4
5
5
# MySQL Connector/Python is licensed under the terms of the GPLv2
6
6
# <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
32
32
import mysqlx
33
33
34
34
if mysqlx .compat .PY3 :
35
- from urllib .parse import quote
35
+ from urllib .parse import quote_plus
36
36
else :
37
- from urllib import quote
37
+ from urllib import quote_plus
38
38
39
39
LOGGER = logging .getLogger (tests .LOGGER_NAME )
40
40
72
72
("unicode:áé'í'óú@127.0.0.1" ,
73
73
{"schema" : "" , "host" : "127.0.0.1" , "password" : "áé'í'óú" ,
74
74
"port" : 33060 , "user" : "unicode" }),
75
+ ("root:@[localhost, 127.0.0.1:88, [::]:99, [a1:b1::]]" ,
76
+ {"routers" : [{"host" : "localhost" , "port" : 33060 },
77
+ {"host" : "127.0.0.1" , "port" : 88 },
78
+ {"host" : "::" , "port" : 99 },
79
+ {"host" : "a1:b1::" , "port" : 33060 }],
80
+ "user" : "root" , "password" : "" , "schema" : "" }),
81
+ ("root:@[a1:a2:a3:a4:a5:a6:a7:a8]]" ,
82
+ {"host" : "a1:a2:a3:a4:a5:a6:a7:a8" , "schema" : "" ,
83
+ "port" : 33060 , "user" : "root" , "password" : "" }),
84
+ ("root:@localhost" , {"user" : "root" , "password" : "" ,
85
+ "host" : "localhost" , "port" : 33060 , "schema" : "" }),
86
+ ("root:@[a1:b1::]" , {"user" : "root" , "password" : "" ,
87
+ "host" : "a1:b1::" , "port" : 33060 , "schema" : "" }),
88
+ ("root:@[a1:b1::]:88" , {"user" : "root" , "password" : "" ,
89
+ "host" : "a1:b1::" , "port" : 88 , "schema" : "" }),
90
+ ("root:@[[a1:b1::]:88]" , {"user" : "root" , "password" : "" ,
91
+ "routers" : [{"host" : "a1:b1::" , "port" :88 }], "schema" : "" }),
92
+ ("root:@[(address=localhost:99, priority=99)]" ,
93
+ {"user" : "root" , "password" : "" , "schema" : "" ,
94
+ "routers" : [{"host" : "localhost" , "port" : 99 , "priority" : 99 }]})
75
95
)
76
96
77
97
89
109
"priority" : 98 }], "password" : "password" , "user" : "user" }),
90
110
)
91
111
112
+ def build_uri (** kwargs ):
113
+ uri = "mysqlx://{0}:{1}" .format (kwargs ["user" ], kwargs ["password" ])
114
+
115
+ if "host" in kwargs :
116
+ host = "[{0}]" .format (kwargs ["host" ]) \
117
+ if ":" in kwargs ["host" ] else kwargs ["host" ]
118
+ uri = "{0}@{1}" .format (uri , host )
119
+ elif "routers" in kwargs :
120
+ routers = []
121
+ for router in kwargs ["routers" ]:
122
+ fmt = "(address={host}{port}, priority={priority})" \
123
+ if "priority" in router else "{host}{port}"
124
+ host = "[{0}]" .format (router ["host" ]) if ":" in router ["host" ] \
125
+ else router ["host" ]
126
+ port = ":{0}" .format (router ["port" ]) if "port" in router else ""
127
+
128
+ routers .append (fmt .format (host = host , port = port ,
129
+ priority = router .get ("priority" , None )))
130
+
131
+ uri = "{0}@[{1}]" .format (uri , "," .join (routers ))
132
+ else :
133
+ raise mysqlx .errors .ProgrammingError ("host or routers required." )
134
+
135
+ if "port" in kwargs :
136
+ uri = "{0}:{1}" .format (uri , kwargs ["port" ])
137
+ if "schema" in kwargs :
138
+ uri = "{0}/{1}" .format (uri , kwargs ["schema" ])
139
+
140
+ query = []
141
+ if "ssl_ca" in kwargs :
142
+ query .append ("ssl-ca={0}" .format (kwargs ["ssl_ca" ]))
143
+ if "ssl_cert" in kwargs :
144
+ query .append ("ssl-cert={0}" .format (kwargs ["ssl_cert" ]))
145
+ if "ssl_key" in kwargs :
146
+ query .append ("ssl-key={0}" .format (kwargs ["ssl_key" ]))
147
+
148
+ if len (query ) > 0 :
149
+ uri = "{0}?{1}" .format (uri , "&" .join (query ))
150
+
151
+ return uri
152
+
92
153
93
154
@unittest .skipIf (tests .MYSQL_VERSION < (5 , 7 , 12 ), "XPlugin not compatible" )
94
155
class MySQLxXSessionTests (tests .MySQLxTests ):
@@ -117,15 +178,16 @@ def test___init__(self):
117
178
118
179
# XSession to a farm using one of many routers (prios)
119
180
# Loop during connect because of network error (succeed)
120
- uri = ( "mysqlx://{0}:{1}@[(address= bad_host, priority= 100),"
121
- "(address={2}:{3}, priority=98)]"
122
- "" . format (user , password , host , port ) )
181
+ routers = [{ "host" : " bad_host" , " priority" : 100 },
182
+ { "host" : host , "port" : port , " priority" : 98 }]
183
+ uri = build_uri (user = user , password = password , routers = routers )
123
184
session = mysqlx .get_session (uri )
124
185
session .close ()
125
186
126
187
# XSession to a farm using one of many routers (incomplete prios)
127
- uri = ("mysqlx://{0}:{1}@[(address=bad_host, priority=100), {2}:{3}]"
128
- "" .format (user , password , host , port ))
188
+ routers = [{"host" : "bad_host" , "priority" : 100 },
189
+ {"host" : host , "port" : port }]
190
+ uri = build_uri (user = user , password = password , routers = routers )
129
191
self .assertRaises (mysqlx .errors .ProgrammingError ,
130
192
mysqlx .get_session , uri )
131
193
try :
@@ -134,9 +196,9 @@ def test___init__(self):
134
196
self .assertEqual (4000 , err .errno )
135
197
136
198
# XSession to a farm using invalid priorities (out of range)
137
- uri = ( "mysqlx://{0}:{1}@[(address= bad_host, priority= 100), "
138
- "(address={2}:{3}, priority= 101)]"
139
- "" . format (user , password , host , port ) )
199
+ routers = [{ "host" : " bad_host" , " priority" : 100 },
200
+ { "host" : host , "port" : port , " priority" : 101 }]
201
+ uri = build_uri (user = user , password = password , routers = routers )
140
202
self .assertRaises (mysqlx .errors .ProgrammingError ,
141
203
mysqlx .get_session , uri )
142
204
try :
@@ -145,19 +207,18 @@ def test___init__(self):
145
207
self .assertEqual (4007 , err .errno )
146
208
147
209
# Establish an XSession to a farm using one of many routers (no prios)
148
- uri = ( "mysqlx://{0}:{1}@[ bad_host, {2}:{3}]"
149
- "" . format (user , password , host , port ) )
210
+ routers = [{ "host" : " bad_host" } , {"host" : host , "port" : port }]
211
+ uri = build_uri (user = user , password = password , routers = routers )
150
212
session = mysqlx .get_session (uri )
151
213
session .close ()
152
214
153
215
# Break loop during connect (non-network error)
154
- uri = ("mysqlx://{0}:{1}@[bad_host, {2}:{3}]"
155
- "" .format (user , "bad_pass" , host , port ))
216
+ uri = build_uri (user = user , password = "bad_pass" , routers = routers )
156
217
self .assertRaises (mysqlx .errors .InterfaceError ,
157
218
mysqlx .get_session , uri )
158
219
159
220
# Break loop during connect (none left)
160
- uri = "mysqlx://{0}:{1}@[bad_host, another_bad_host]"
221
+ uri = "mysqlx://{0}:{1}@[bad_host, another_bad_host]" . format ( user , password )
161
222
self .assertRaises (mysqlx .errors .InterfaceError ,
162
223
mysqlx .get_session , uri )
163
224
try :
@@ -211,12 +272,11 @@ def test_mysqlx_socket(self):
211
272
212
273
213
274
def test_connection_uri (self ):
214
- uri = ("mysqlx://{user}:{password}@{host}:{port}/{schema}"
215
- "" .format (user = self .connect_kwargs ["user" ],
275
+ uri = build_uri (user = self .connect_kwargs ["user" ],
216
276
password = self .connect_kwargs ["password" ],
217
277
host = self .connect_kwargs ["host" ],
218
278
port = self .connect_kwargs ["port" ],
219
- schema = self .connect_kwargs ["schema" ]))
279
+ schema = self .connect_kwargs ["schema" ])
220
280
session = mysqlx .get_session (uri )
221
281
self .assertIsInstance (session , mysqlx .XSession )
222
282
@@ -361,16 +421,23 @@ def test_ssl_connection(self):
361
421
362
422
session .close ()
363
423
364
- uri = ("mysqlx://{0}:{1}@{2}?ssl-ca={3}&ssl-cert={4}&ssl-key={5}"
365
- "" .format (config ["user" ], config ["password" ], config ["host" ],
366
- quote (config ["ssl-ca" ]), quote (config ["ssl-cert" ]),
367
- quote (config ["ssl-key" ])))
424
+ ssl_ca = "{0}{1}" .format (config ["ssl-ca" ][0 ],
425
+ quote_plus (config ["ssl-ca" ][1 :]))
426
+ ssl_key = "{0}{1}" .format (config ["ssl-key" ][0 ],
427
+ quote_plus (config ["ssl-key" ][1 :]))
428
+ ssl_cert = "{0}{1}" .format (config ["ssl-ca" ][0 ],
429
+ quote_plus (config ["ssl-cert" ][1 :]))
430
+ uri = build_uri (user = config ["user" ], password = config ["password" ],
431
+ host = config ["host" ], ssl_ca = ssl_ca ,
432
+ ssl_cert = ssl_cert , ssl_key = ssl_key )
368
433
session = mysqlx .get_session (uri )
369
434
370
- uri = ("mysqlx://{0}:{1}@{2}?ssl-ca=({3})&ssl-cert=({4})&ssl-key=({5})"
371
- "" .format (config ["user" ], config ["password" ], config ["host" ],
372
- config ["ssl-ca" ], config ["ssl-cert" ],
373
- config ["ssl-key" ]))
435
+ ssl_ca = "({0})" .format (config ["ssl-ca" ])
436
+ ssl_cert = "({0})" .format (config ["ssl-cert" ])
437
+ ssl_key = "({0})" .format (config ["ssl-key" ])
438
+ uri = build_uri (user = config ["user" ], password = config ["password" ],
439
+ host = config ["host" ], ssl_ca = ssl_ca ,
440
+ ssl_cert = ssl_cert , ssl_key = ssl_key )
374
441
session = mysqlx .get_session (uri )
375
442
376
443
@@ -395,12 +462,11 @@ def test___init__(self):
395
462
self .assertRaises (TypeError , mysqlx .NodeSession , bad_config )
396
463
397
464
def test_connection_uri (self ):
398
- uri = ("mysqlx://{user}:{password}@{host}:{port}/{schema}"
399
- "" .format (user = self .connect_kwargs ["user" ],
400
- password = self .connect_kwargs ["password" ],
401
- host = self .connect_kwargs ["host" ],
402
- port = self .connect_kwargs ["port" ],
403
- schema = self .connect_kwargs ["schema" ]))
465
+ uri = build_uri (user = self .connect_kwargs ["user" ],
466
+ password = self .connect_kwargs ["password" ],
467
+ host = self .connect_kwargs ["host" ],
468
+ port = self .connect_kwargs ["port" ],
469
+ schema = self .connect_kwargs ["schema" ])
404
470
session = mysqlx .get_node_session (uri )
405
471
self .assertIsInstance (session , mysqlx .NodeSession )
406
472
@@ -535,14 +601,21 @@ def test_ssl_connection(self):
535
601
536
602
session .close ()
537
603
538
- uri = ("mysqlx://{0}:{1}@{2}?ssl-ca={3}&ssl-cert={4}&ssl-key={5}"
539
- "" .format (config ["user" ], config ["password" ], config ["host" ],
540
- quote (config ["ssl-ca" ]), quote (config ["ssl-cert" ]),
541
- quote (config ["ssl-key" ])))
604
+ ssl_ca = "{0}{1}" .format (config ["ssl-ca" ][0 ],
605
+ quote_plus (config ["ssl-ca" ][1 :]))
606
+ ssl_key = "{0}{1}" .format (config ["ssl-key" ][0 ],
607
+ quote_plus (config ["ssl-key" ][1 :]))
608
+ ssl_cert = "{0}{1}" .format (config ["ssl-ca" ][0 ],
609
+ quote_plus (config ["ssl-cert" ][1 :]))
610
+ uri = build_uri (user = config ["user" ], password = config ["password" ],
611
+ host = config ["host" ], ssl_ca = ssl_ca ,
612
+ ssl_cert = ssl_cert , ssl_key = ssl_key )
542
613
session = mysqlx .get_node_session (uri )
543
614
544
- uri = ("mysqlx://{0}:{1}@{2}?ssl-ca=({3})&ssl-cert=({4})&ssl-key=({5})"
545
- "" .format (config ["user" ], config ["password" ], config ["host" ],
546
- config ["ssl-ca" ], config ["ssl-cert" ],
547
- config ["ssl-key" ]))
615
+ ssl_ca = "({0})" .format (config ["ssl-ca" ])
616
+ ssl_cert = "({0})" .format (config ["ssl-cert" ])
617
+ ssl_key = "({0})" .format (config ["ssl-key" ])
618
+ uri = build_uri (user = config ["user" ], password = config ["password" ],
619
+ host = config ["host" ], ssl_ca = ssl_ca ,
620
+ ssl_cert = ssl_cert , ssl_key = ssl_key )
548
621
session = mysqlx .get_node_session (uri )
0 commit comments