From 85389bbe1ae3647af29c7523ed740e961a1c2b3b Mon Sep 17 00:00:00 2001 From: Matt Lord Date: Tue, 7 Jan 2025 11:51:27 -0500 Subject: [PATCH] Fix unit tests Signed-off-by: Matt Lord --- go/mysql/replication/binlog_file_position.go | 12 +++++++++--- go/mysql/replication/replication_status.go | 2 +- go/mysql/replication/replication_status_test.go | 14 ++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/go/mysql/replication/binlog_file_position.go b/go/mysql/replication/binlog_file_position.go index 1159043af54..fede2603fa0 100644 --- a/go/mysql/replication/binlog_file_position.go +++ b/go/mysql/replication/binlog_file_position.go @@ -28,12 +28,14 @@ type BinlogFilePos struct { } // ParseBinlogFilePos parses a binlog file and position in the input -// format used by commands such as SHOW REPLICA STATUS and SHOW BINARY -// LOG STATUS. +// format used by internal code that processes output from MySQL such +// as SHOW REPLICA STATUS and SHOW BINARY LOG STATUS. func ParseBinlogFilePos(s string) (BinlogFilePos, error) { bfp := BinlogFilePos{} + if s == "" { + return bfp, nil + } - // Split into parts. file, posStr, ok := strings.Cut(s, ":") if !ok { return bfp, fmt.Errorf("invalid binlog file position (%v): expecting file:pos", s) @@ -64,3 +66,7 @@ func (bfp BinlogFilePos) ConvertToFlavorPosition() (pos Position, err error) { pos.GTIDSet, err = ParseFilePosGTIDSet(bfp.String()) return pos, err } + +func (bfp BinlogFilePos) Equal(b BinlogFilePos) bool { + return bfp.File == b.File && bfp.Pos == b.Pos +} diff --git a/go/mysql/replication/replication_status.go b/go/mysql/replication/replication_status.go index a72a460b1c9..913676ece0b 100644 --- a/go/mysql/replication/replication_status.go +++ b/go/mysql/replication/replication_status.go @@ -89,7 +89,7 @@ func (s *ReplicationStatus) SQLHealthy() bool { func ReplicationStatusToProto(s ReplicationStatus) *replicationdatapb.Status { replstatuspb := &replicationdatapb.Status{ Position: EncodePosition(s.Position), - RelayLogPosition: s.RelayLogPosition.String(), + RelayLogPosition: EncodePosition(s.RelayLogPosition), FilePosition: s.FilePosition.String(), RelayLogSourceBinlogEquivalentPosition: s.RelayLogSourceBinlogEquivalentPosition.String(), SourceServerId: s.SourceServerID, diff --git a/go/mysql/replication/replication_status_test.go b/go/mysql/replication/replication_status_test.go index 20e35713ec2..0941a0c6f43 100644 --- a/go/mysql/replication/replication_status_test.go +++ b/go/mysql/replication/replication_status_test.go @@ -310,12 +310,16 @@ func TestFilePosRetrieveExecutedPosition(t *testing.T) { got, err := ParseFilePosReplicationStatus(resultMap) require.NoError(t, err) assert.Equalf(t, got.Position.GTIDSet, want.Position.GTIDSet, "got Position: %v; want Position: %v", got.Position.GTIDSet, want.Position.GTIDSet) - assert.Equalf(t, got.RelayLogPosition.GTIDSet, want.RelayLogPosition, "got RelayLogPosition: %v; want RelayLogPosition: %v", got.RelayLogPosition.GTIDSet, want.RelayLogPosition.GTIDSet) + assert.Equalf(t, got.RelayLogPosition.GTIDSet, want.RelayLogPosition.GTIDSet, "got RelayLogPosition: %v; want RelayLogPosition: %v", got.RelayLogPosition.GTIDSet, want.RelayLogPosition.GTIDSet) assert.Equalf(t, got.RelayLogFilePosition, want.RelayLogFilePosition, "got RelayLogFilePosition: %v; want RelayLogFilePosition: %v", got.RelayLogFilePosition, want.RelayLogFilePosition) assert.Equalf(t, got.FilePosition, want.FilePosition, "got FilePosition: %v; want FilePosition: %v", got.FilePosition, want.FilePosition) assert.Equalf(t, got.RelayLogSourceBinlogEquivalentPosition, want.RelayLogSourceBinlogEquivalentPosition, "got RelayLogSourceBinlogEquivalentPosition: %v; want RelayLogSourceBinlogEquivalentPosition: %v", got.RelayLogSourceBinlogEquivalentPosition, want.RelayLogSourceBinlogEquivalentPosition) - assert.Equalf(t, got.Position.GTIDSet, got.FilePosition, "FilePosition and Position don't match when they should for the FilePos flavor") - assert.Equalf(t, got.RelayLogPosition.GTIDSet, got.RelayLogSourceBinlogEquivalentPosition, "RelayLogPosition and RelayLogSourceBinlogEquivalentPosition don't match when they should for the FilePos flavor") + filePos, err := got.FilePosition.ConvertToFlavorPosition() + require.NoError(t, err) + assert.Equalf(t, got.Position.GTIDSet, filePos.GTIDSet, "FilePosition and Position don't match when they should for the FilePos flavor") + filePos, err = got.RelayLogSourceBinlogEquivalentPosition.ConvertToFlavorPosition() + require.NoError(t, err) + assert.Equalf(t, got.RelayLogPosition.GTIDSet, filePos.GTIDSet, "RelayLogPosition and RelayLogSourceBinlogEquivalentPosition don't match when they should for the FilePos flavor") } func TestFilePosShouldGetPosition(t *testing.T) { @@ -332,5 +336,7 @@ func TestFilePosShouldGetPosition(t *testing.T) { require.NoError(t, err) assert.Equalf(t, got.Position.GTIDSet, want.Position.GTIDSet, "got Position: %v; want Position: %v", got.Position.GTIDSet, want.Position.GTIDSet) assert.Equalf(t, got.FilePosition, want.FilePosition, "got FilePosition: %v; want FilePosition: %v", got.FilePosition, want.FilePosition) - assert.Equalf(t, got.Position.GTIDSet, got.FilePosition, "FilePosition and Position don't match when they should for the FilePos flavor") + filePos, err := got.FilePosition.ConvertToFlavorPosition() + require.NoError(t, err) + assert.Equalf(t, got.Position.GTIDSet, filePos.GTIDSet, "FilePosition and Position don't match when they should for the FilePos flavor") }