|
| 1 | +/* |
| 2 | +Copyright 2025 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 command |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "io" |
| 22 | + "os" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +func captureStdout(f func()) string { |
| 30 | + old := os.Stdout |
| 31 | + r, w, _ := os.Pipe() |
| 32 | + os.Stdout = w |
| 33 | + |
| 34 | + f() |
| 35 | + |
| 36 | + w.Close() |
| 37 | + os.Stdout = old |
| 38 | + |
| 39 | + var buf bytes.Buffer |
| 40 | + _, _ = io.Copy(&buf, r) |
| 41 | + return buf.String() |
| 42 | +} |
| 43 | + |
| 44 | +func TestPosition(t *testing.T) { |
| 45 | + tests := []struct { |
| 46 | + name string |
| 47 | + args []string |
| 48 | + expectedOutput string |
| 49 | + expectedError string |
| 50 | + }{ |
| 51 | + { |
| 52 | + name: "equal - same positions", |
| 53 | + args: []string{"equal", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5"}, |
| 54 | + expectedOutput: "true\n", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "equal - different positions", |
| 58 | + args: []string{"equal", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-6"}, |
| 59 | + expectedOutput: "false\n", |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "equal - different server uuids", |
| 63 | + args: []string{"equal", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "MySQL56/4e11fa47-71ca-11e1-9e33-c80aa9429562:1-5"}, |
| 64 | + expectedOutput: "false\n", |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "at_least - pos1 ahead of pos2", |
| 68 | + args: []string{"at_least", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-10", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5"}, |
| 69 | + expectedOutput: "true\n", |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "at_least - pos1 behind pos2", |
| 73 | + args: []string{"at_least", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-10"}, |
| 74 | + expectedOutput: "false\n", |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "at_least - equal positions", |
| 78 | + args: []string{"at_least", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5"}, |
| 79 | + expectedOutput: "true\n", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "append - valid gtid", |
| 83 | + args: []string{"append", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:6"}, |
| 84 | + expectedOutput: "3e11fa47-71ca-11e1-9e33-c80aa9429562:1-6\n", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "append - gtid from different server", |
| 88 | + args: []string{"append", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "MySQL56/4e11fa47-71ca-11e1-9e33-c80aa9429562:1"}, |
| 89 | + expectedOutput: "3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5,4e11fa47-71ca-11e1-9e33-c80aa9429562:1\n", |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "append - to position with multiple GTIDs", |
| 93 | + args: []string{"append", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5,4e11fa47-71ca-11e1-9e33-c80aa9429562:1-3", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:6"}, |
| 94 | + expectedOutput: "3e11fa47-71ca-11e1-9e33-c80aa9429562:1-6,4e11fa47-71ca-11e1-9e33-c80aa9429562:1-3\n", |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "equal - invalid pos1", |
| 98 | + args: []string{"equal", "invalid-position", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5"}, |
| 99 | + expectedError: "parse error", |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "equal - invalid pos2", |
| 103 | + args: []string{"equal", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "invalid-position"}, |
| 104 | + expectedError: "parse error", |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "at_least - invalid pos1", |
| 108 | + args: []string{"at_least", "invalid-position", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5"}, |
| 109 | + expectedError: "parse error", |
| 110 | + }, |
| 111 | + { |
| 112 | + name: "at_least - invalid pos2", |
| 113 | + args: []string{"at_least", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "invalid-position"}, |
| 114 | + expectedError: "parse error", |
| 115 | + }, |
| 116 | + { |
| 117 | + name: "append - invalid position", |
| 118 | + args: []string{"append", "invalid-position", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:6"}, |
| 119 | + expectedError: "parse error", |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "append - invalid gtid", |
| 123 | + args: []string{"append", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "invalid-gtid"}, |
| 124 | + expectedError: "parse error", |
| 125 | + }, |
| 126 | + } |
| 127 | + |
| 128 | + for _, tt := range tests { |
| 129 | + t.Run(tt.name, func(t *testing.T) { |
| 130 | + var output string |
| 131 | + var err error |
| 132 | + |
| 133 | + if tt.expectedError == "" { |
| 134 | + output = captureStdout(func() { |
| 135 | + err = commandPosition(Position, tt.args) |
| 136 | + }) |
| 137 | + } else { |
| 138 | + err = commandPosition(Position, tt.args) |
| 139 | + } |
| 140 | + |
| 141 | + if tt.expectedError != "" { |
| 142 | + require.Error(t, err) |
| 143 | + assert.Contains(t, err.Error(), tt.expectedError) |
| 144 | + } else { |
| 145 | + require.NoError(t, err) |
| 146 | + assert.Equal(t, tt.expectedOutput, output) |
| 147 | + } |
| 148 | + }) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func TestPositionCommand_ArgsValidation(t *testing.T) { |
| 153 | + tests := []struct { |
| 154 | + name string |
| 155 | + args []string |
| 156 | + expectedError string |
| 157 | + }{ |
| 158 | + { |
| 159 | + name: "invalid operation", |
| 160 | + args: []string{"invalid_op", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5", "MySQL56/3e11fa47-71ca-11e1-9e33-c80aa9429562:1-5"}, |
| 161 | + expectedError: "invalid operation invalid_op", |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "no args", |
| 165 | + args: []string{}, |
| 166 | + expectedError: "accepts 3 arg(s), received 0", |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "one arg", |
| 170 | + args: []string{"equal"}, |
| 171 | + expectedError: "accepts 3 arg(s), received 1", |
| 172 | + }, |
| 173 | + { |
| 174 | + name: "two args", |
| 175 | + args: []string{"equal", "pos1"}, |
| 176 | + expectedError: "accepts 3 arg(s), received 2", |
| 177 | + }, |
| 178 | + { |
| 179 | + name: "four args", |
| 180 | + args: []string{"equal", "pos1", "pos2", "extra"}, |
| 181 | + expectedError: "accepts 3 arg(s), received 4", |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + for _, tt := range tests { |
| 186 | + t.Run(tt.name, func(t *testing.T) { |
| 187 | + err := Position.Args(Position, tt.args) |
| 188 | + require.Error(t, err) |
| 189 | + assert.Contains(t, err.Error(), tt.expectedError) |
| 190 | + }) |
| 191 | + } |
| 192 | +} |
0 commit comments