vitess.io/vitess@v0.16.2/go/mysql/flavor_mysql_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  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestMysql56SetReplicationSourceCommand(t *testing.T) {
    27  	params := &ConnParams{
    28  		Uname: "username",
    29  		Pass:  "password",
    30  	}
    31  	host := "localhost"
    32  	port := 123
    33  	connectRetry := 1234
    34  	want := `CHANGE MASTER TO
    35    MASTER_HOST = 'localhost',
    36    MASTER_PORT = 123,
    37    MASTER_USER = 'username',
    38    MASTER_PASSWORD = 'password',
    39    MASTER_CONNECT_RETRY = 1234,
    40    MASTER_AUTO_POSITION = 1`
    41  
    42  	conn := &Conn{flavor: mysqlFlavor57{}}
    43  	got := conn.SetReplicationSourceCommand(params, host, port, connectRetry)
    44  	assert.Equal(t, want, got, "mysqlFlavor.SetReplicationSourceCommand(%#v, %#v, %#v, %#v) = %#v, want %#v", params, host, port, connectRetry, got, want)
    45  
    46  }
    47  
    48  func TestMysql56SetReplicationSourceCommandSSL(t *testing.T) {
    49  	params := &ConnParams{
    50  		Uname:     "username",
    51  		Pass:      "password",
    52  		SslCa:     "ssl-ca",
    53  		SslCaPath: "ssl-ca-path",
    54  		SslCert:   "ssl-cert",
    55  		SslKey:    "ssl-key",
    56  	}
    57  	params.EnableSSL()
    58  	host := "localhost"
    59  	port := 123
    60  	connectRetry := 1234
    61  	want := `CHANGE MASTER TO
    62    MASTER_HOST = 'localhost',
    63    MASTER_PORT = 123,
    64    MASTER_USER = 'username',
    65    MASTER_PASSWORD = 'password',
    66    MASTER_CONNECT_RETRY = 1234,
    67    MASTER_SSL = 1,
    68    MASTER_SSL_CA = 'ssl-ca',
    69    MASTER_SSL_CAPATH = 'ssl-ca-path',
    70    MASTER_SSL_CERT = 'ssl-cert',
    71    MASTER_SSL_KEY = 'ssl-key',
    72    MASTER_AUTO_POSITION = 1`
    73  
    74  	conn := &Conn{flavor: mysqlFlavor57{}}
    75  	got := conn.SetReplicationSourceCommand(params, host, port, connectRetry)
    76  	assert.Equal(t, want, got, "mysqlFlavor.SetReplicationSourceCommand(%#v, %#v, %#v, %#v) = %#v, want %#v", params, host, port, connectRetry, got, want)
    77  
    78  }
    79  
    80  func TestMysqlRetrieveSourceServerId(t *testing.T) {
    81  	resultMap := map[string]string{
    82  		"Master_Server_Id": "1",
    83  	}
    84  
    85  	want := ReplicationStatus{SourceServerID: 1}
    86  	got, err := parseMysqlReplicationStatus(resultMap)
    87  	require.NoError(t, err)
    88  	assert.Equalf(t, got.SourceServerID, want.SourceServerID, "got SourceServerID: %v; want SourceServerID: %v", got.SourceServerID, want.SourceServerID)
    89  }
    90  
    91  func TestMysqlRetrieveFileBasedPositions(t *testing.T) {
    92  	resultMap := map[string]string{
    93  		"Exec_Master_Log_Pos":   "1307",
    94  		"Relay_Master_Log_File": "master-bin.000002",
    95  		"Read_Master_Log_Pos":   "1308",
    96  		"Master_Log_File":       "master-bin.000003",
    97  		"Relay_Log_Pos":         "1309",
    98  		"Relay_Log_File":        "relay-bin.000004",
    99  	}
   100  
   101  	want := ReplicationStatus{
   102  		FilePosition:                           Position{GTIDSet: filePosGTID{file: "master-bin.000002", pos: 1307}},
   103  		RelayLogSourceBinlogEquivalentPosition: Position{GTIDSet: filePosGTID{file: "master-bin.000003", pos: 1308}},
   104  		RelayLogFilePosition:                   Position{GTIDSet: filePosGTID{file: "relay-bin.000004", pos: 1309}},
   105  	}
   106  	got, err := parseMysqlReplicationStatus(resultMap)
   107  	require.NoError(t, err)
   108  	assert.Equalf(t, got.FilePosition.GTIDSet, want.FilePosition.GTIDSet, "got FilePosition: %v; want FilePosition: %v", got.FilePosition.GTIDSet, want.FilePosition.GTIDSet)
   109  	assert.Equalf(t, got.RelayLogFilePosition.GTIDSet, want.RelayLogFilePosition.GTIDSet, "got RelayLogFilePosition: %v; want RelayLogFilePosition: %v", got.RelayLogFilePosition.GTIDSet, want.RelayLogFilePosition.GTIDSet)
   110  	assert.Equalf(t, got.RelayLogSourceBinlogEquivalentPosition.GTIDSet, want.RelayLogSourceBinlogEquivalentPosition.GTIDSet, "got RelayLogSourceBinlogEquivalentPosition: %v; want RelayLogSourceBinlogEquivalentPosition: %v", got.RelayLogSourceBinlogEquivalentPosition.GTIDSet, want.RelayLogSourceBinlogEquivalentPosition.GTIDSet)
   111  }
   112  
   113  func TestMysqlShouldGetRelayLogPosition(t *testing.T) {
   114  	resultMap := map[string]string{
   115  		"Executed_Gtid_Set":     "3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5",
   116  		"Retrieved_Gtid_Set":    "3e11fa47-71ca-11e1-9e33-c80aa9429562:6-9",
   117  		"Exec_Master_Log_Pos":   "1307",
   118  		"Relay_Master_Log_File": "master-bin.000002",
   119  		"Read_Master_Log_Pos":   "1308",
   120  		"Master_Log_File":       "master-bin.000003",
   121  	}
   122  
   123  	sid, _ := ParseSID("3e11fa47-71ca-11e1-9e33-c80aa9429562")
   124  	want := ReplicationStatus{
   125  		Position:         Position{GTIDSet: Mysql56GTIDSet{sid: []interval{{start: 1, end: 5}}}},
   126  		RelayLogPosition: Position{GTIDSet: Mysql56GTIDSet{sid: []interval{{start: 1, end: 9}}}},
   127  	}
   128  	got, err := parseMysqlReplicationStatus(resultMap)
   129  	require.NoError(t, err)
   130  	assert.Equalf(t, got.RelayLogPosition.GTIDSet.String(), want.RelayLogPosition.GTIDSet.String(), "got RelayLogPosition: %v; want RelayLogPosition: %v", got.RelayLogPosition.GTIDSet, want.RelayLogPosition.GTIDSet)
   131  }
   132  
   133  func TestMysqlShouldGetPosition(t *testing.T) {
   134  	resultMap := map[string]string{
   135  		"Executed_Gtid_Set": "3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5",
   136  		"Position":          "1307",
   137  		"File":              "source-bin.000003",
   138  	}
   139  
   140  	sid, _ := ParseSID("3e11fa47-71ca-11e1-9e33-c80aa9429562")
   141  	want := PrimaryStatus{
   142  		Position:     Position{GTIDSet: Mysql56GTIDSet{sid: []interval{{start: 1, end: 5}}}},
   143  		FilePosition: Position{GTIDSet: filePosGTID{file: "source-bin.000003", pos: 1307}},
   144  	}
   145  	got, err := parseMysqlPrimaryStatus(resultMap)
   146  	require.NoError(t, err)
   147  	assert.Equalf(t, got.Position.GTIDSet.String(), want.Position.GTIDSet.String(), "got Position: %v; want Position: %v", got.Position.GTIDSet, want.Position.GTIDSet)
   148  	assert.Equalf(t, got.FilePosition.GTIDSet.String(), want.FilePosition.GTIDSet.String(), "got FilePosition: %v; want FilePosition: %v", got.FilePosition.GTIDSet, want.FilePosition.GTIDSet)
   149  }