vitess.io/vitess@v0.16.2/go/mysql/flavor_mariadb_test.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mysql 18 19 import ( 20 "fmt" 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 "github.com/stretchr/testify/require" 25 ) 26 27 func TestMariadbSetReplicationSourceCommand(t *testing.T) { 28 params := &ConnParams{ 29 Uname: "username", 30 Pass: "password", 31 } 32 host := "localhost" 33 port := 123 34 connectRetry := 1234 35 want := `CHANGE MASTER TO 36 MASTER_HOST = 'localhost', 37 MASTER_PORT = 123, 38 MASTER_USER = 'username', 39 MASTER_PASSWORD = 'password', 40 MASTER_CONNECT_RETRY = 1234, 41 MASTER_USE_GTID = current_pos` 42 43 conn := &Conn{flavor: mariadbFlavor101{}} 44 got := conn.SetReplicationSourceCommand(params, host, port, connectRetry) 45 assert.Equal(t, want, got, "mariadbFlavor.SetReplicationSourceCommand(%#v, %#v, %#v, %#v) = %#v, want %#v", params, host, port, connectRetry, got, want) 46 47 } 48 49 func TestMariadbSetReplicationSourceCommandSSL(t *testing.T) { 50 params := &ConnParams{ 51 Uname: "username", 52 Pass: "password", 53 SslCa: "ssl-ca", 54 SslCaPath: "ssl-ca-path", 55 SslCert: "ssl-cert", 56 SslKey: "ssl-key", 57 } 58 params.EnableSSL() 59 host := "localhost" 60 port := 123 61 connectRetry := 1234 62 want := `CHANGE MASTER TO 63 MASTER_HOST = 'localhost', 64 MASTER_PORT = 123, 65 MASTER_USER = 'username', 66 MASTER_PASSWORD = 'password', 67 MASTER_CONNECT_RETRY = 1234, 68 MASTER_SSL = 1, 69 MASTER_SSL_CA = 'ssl-ca', 70 MASTER_SSL_CAPATH = 'ssl-ca-path', 71 MASTER_SSL_CERT = 'ssl-cert', 72 MASTER_SSL_KEY = 'ssl-key', 73 MASTER_USE_GTID = current_pos` 74 75 conn := &Conn{flavor: mariadbFlavor101{}} 76 got := conn.SetReplicationSourceCommand(params, host, port, connectRetry) 77 assert.Equal(t, want, got, "mariadbFlavor.SetReplicationSourceCommand(%#v, %#v, %#v, %#v) = %#v, want %#v", params, host, port, connectRetry, got, want) 78 79 } 80 81 func TestMariadbRetrieveSourceServerId(t *testing.T) { 82 resultMap := map[string]string{ 83 "Master_Server_Id": "1", 84 "Gtid_Slave_Pos": "0-101-2320", 85 } 86 87 want := ReplicationStatus{SourceServerID: 1} 88 got, err := parseMariadbReplicationStatus(resultMap) 89 require.NoError(t, err) 90 assert.Equal(t, got.SourceServerID, want.SourceServerID, fmt.Sprintf("got SourceServerID: %v; want SourceServerID: %v", got.SourceServerID, want.SourceServerID)) 91 } 92 93 func TestMariadbRetrieveFileBasedPositions(t *testing.T) { 94 resultMap := map[string]string{ 95 "Exec_Master_Log_Pos": "1307", 96 "Relay_Master_Log_File": "master-bin.000002", 97 "Read_Master_Log_Pos": "1308", 98 "Master_Log_File": "master-bin.000003", 99 "Gtid_Slave_Pos": "0-101-2320", 100 "Relay_Log_Pos": "1309", 101 "Relay_Log_File": "relay-bin.000004", 102 } 103 104 want := ReplicationStatus{ 105 FilePosition: Position{GTIDSet: filePosGTID{file: "master-bin.000002", pos: 1307}}, 106 RelayLogSourceBinlogEquivalentPosition: Position{GTIDSet: filePosGTID{file: "master-bin.000003", pos: 1308}}, 107 RelayLogFilePosition: Position{GTIDSet: filePosGTID{file: "relay-bin.000004", pos: 1309}}, 108 } 109 got, err := parseMariadbReplicationStatus(resultMap) 110 require.NoError(t, err) 111 assert.Equalf(t, got.RelayLogFilePosition.GTIDSet, want.RelayLogFilePosition.GTIDSet, "got RelayLogFilePosition: %v; want RelayLogFilePosition: %v", got.RelayLogFilePosition.GTIDSet, want.RelayLogFilePosition.GTIDSet) 112 assert.Equal(t, got.FilePosition.GTIDSet, want.FilePosition.GTIDSet, fmt.Sprintf("got FilePosition: %v; want FilePosition: %v", got.FilePosition.GTIDSet, want.FilePosition.GTIDSet)) 113 assert.Equal(t, got.RelayLogSourceBinlogEquivalentPosition.GTIDSet, want.RelayLogSourceBinlogEquivalentPosition.GTIDSet, fmt.Sprintf("got RelayLogSourceBinlogEquivalentPosition: %v; want RelayLogSourceBinlogEquivalentPosition: %v", got.RelayLogSourceBinlogEquivalentPosition.GTIDSet, want.RelayLogSourceBinlogEquivalentPosition.GTIDSet)) 114 } 115 116 func TestMariadbShouldGetNilRelayLogPosition(t *testing.T) { 117 resultMap := map[string]string{ 118 "Exec_Master_Log_Pos": "1307", 119 "Relay_Master_Log_File": "master-bin.000002", 120 "Read_Master_Log_Pos": "1308", 121 "Master_Log_File": "master-bin.000003", 122 "Gtid_Slave_Pos": "0-101-2320", 123 } 124 got, err := parseMariadbReplicationStatus(resultMap) 125 require.NoError(t, err) 126 assert.Truef(t, got.RelayLogPosition.IsZero(), "Got a filled in RelayLogPosition. For MariaDB we should get back nil, because MariaDB does not return the retrieved GTIDSet. got: %#v", got.RelayLogPosition) 127 }