forked from github/gh-ost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
272 lines (249 loc) · 8.87 KB
/
context_test.go
File metadata and controls
272 lines (249 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
Copyright 2021 GitHub Inc.
See https://github.com/github/gh-ost/blob/master/LICENSE
*/
package base
import (
"errors"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/openark/golib/log"
"github.com/stretchr/testify/require"
)
func init() {
log.SetLevel(log.ERROR)
}
func TestGetTableNames(t *testing.T) {
{
context := NewMigrationContext()
context.OriginalTableName = "some_table"
require.Equal(t, "_some_table_del", context.GetOldTableName())
require.Equal(t, "_some_table_gho", context.GetGhostTableName())
require.Equal(t, "_some_table_ghc", context.GetChangelogTableName(), "_some_table_ghc")
}
{
context := NewMigrationContext()
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890"
require.Equal(t, "_a1234567890123456789012345678901234567890123456789012345678_del", context.GetOldTableName())
require.Equal(t, "_a1234567890123456789012345678901234567890123456789012345678_gho", context.GetGhostTableName())
require.Equal(t, "_a1234567890123456789012345678901234567890123456789012345678_ghc", context.GetChangelogTableName())
}
{
context := NewMigrationContext()
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890123"
oldTableName := context.GetOldTableName()
require.Equal(t, "_a1234567890123456789012345678901234567890123456789012345678_del", oldTableName)
}
{
context := NewMigrationContext()
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890123"
context.TimestampOldTable = true
longForm := "Jan 2, 2006 at 3:04pm (MST)"
context.StartTime, _ = time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
oldTableName := context.GetOldTableName()
require.Equal(t, "_a1234567890123456789012345678901234567890123_20130203195400_del", oldTableName)
}
{
context := NewMigrationContext()
context.OriginalTableName = "foo_bar_baz"
context.ForceTmpTableName = "tmp"
require.Equal(t, "_tmp_del", context.GetOldTableName())
require.Equal(t, "_tmp_gho", context.GetGhostTableName())
require.Equal(t, "_tmp_ghc", context.GetChangelogTableName())
}
}
func TestGetTriggerNames(t *testing.T) {
{
context := NewMigrationContext()
context.TriggerSuffix = "_gho"
require.Equal(t, "my_trigger"+context.TriggerSuffix, context.GetGhostTriggerName("my_trigger"))
}
{
context := NewMigrationContext()
context.TriggerSuffix = "_gho"
context.RemoveTriggerSuffix = true
require.Equal(t, "my_trigger"+context.TriggerSuffix, context.GetGhostTriggerName("my_trigger"))
}
{
context := NewMigrationContext()
context.TriggerSuffix = "_gho"
context.RemoveTriggerSuffix = true
require.Equal(t, "my_trigger", context.GetGhostTriggerName("my_trigger_gho"))
}
{
context := NewMigrationContext()
context.TriggerSuffix = "_gho"
context.RemoveTriggerSuffix = false
require.Equal(t, "my_trigger_gho_gho", context.GetGhostTriggerName("my_trigger_gho"))
}
}
func TestValidateGhostTriggerLengthBelowMaxLength(t *testing.T) {
// Tests simulate the real call pattern: GetGhostTriggerName first, then validate the result.
{
// Short trigger name with suffix appended: well under 64 chars
context := NewMigrationContext()
context.TriggerSuffix = "_gho"
ghostName := context.GetGhostTriggerName("my_trigger") // "my_trigger_gho" = 14 chars
require.True(t, context.ValidateGhostTriggerLengthBelowMaxLength(ghostName))
}
{
// 64-char original + "_ghost" suffix = 70 chars → exceeds limit
context := NewMigrationContext()
context.TriggerSuffix = "_ghost"
ghostName := context.GetGhostTriggerName(strings.Repeat("my_trigger_ghost", 4)) // 64 + 6 = 70
require.False(t, context.ValidateGhostTriggerLengthBelowMaxLength(ghostName))
}
{
// 48-char original + "_ghost" suffix = 54 chars → valid
context := NewMigrationContext()
context.TriggerSuffix = "_ghost"
ghostName := context.GetGhostTriggerName(strings.Repeat("my_trigger_ghost", 3)) // 48 + 6 = 54
require.True(t, context.ValidateGhostTriggerLengthBelowMaxLength(ghostName))
}
{
// RemoveTriggerSuffix: 64-char name ending in "_ghost" → suffix removed → 58 chars → valid
context := NewMigrationContext()
context.TriggerSuffix = "_ghost"
context.RemoveTriggerSuffix = true
ghostName := context.GetGhostTriggerName(strings.Repeat("my_trigger_ghost", 4)) // suffix removed → 58
require.True(t, context.ValidateGhostTriggerLengthBelowMaxLength(ghostName))
}
{
// RemoveTriggerSuffix: name doesn't end in suffix → suffix appended → 65 + 6 = 71 chars → exceeds
context := NewMigrationContext()
context.TriggerSuffix = "_ghost"
context.RemoveTriggerSuffix = true
ghostName := context.GetGhostTriggerName(strings.Repeat("my_trigger_ghost", 4) + "X") // no match, appended → 71
require.False(t, context.ValidateGhostTriggerLengthBelowMaxLength(ghostName))
}
{
// RemoveTriggerSuffix: 70-char name ending in "_ghost" → suffix removed → 64 chars → exactly at limit → valid
context := NewMigrationContext()
context.TriggerSuffix = "_ghost"
context.RemoveTriggerSuffix = true
ghostName := context.GetGhostTriggerName(strings.Repeat("my_trigger_ghost", 4) + "_ghost") // suffix removed → 64
require.True(t, context.ValidateGhostTriggerLengthBelowMaxLength(ghostName))
}
{
// Edge case: exactly 64 chars after transformation → valid (boundary test)
context := NewMigrationContext()
context.TriggerSuffix = "_ght"
originalName := strings.Repeat("x", 60) // 60 chars
ghostName := context.GetGhostTriggerName(originalName) // 60 + 4 = 64
require.Equal(t, 64, len(ghostName))
require.True(t, context.ValidateGhostTriggerLengthBelowMaxLength(ghostName))
}
{
// Edge case: 65 chars after transformation → exceeds (boundary test)
context := NewMigrationContext()
context.TriggerSuffix = "_ght"
originalName := strings.Repeat("x", 61) // 61 chars
ghostName := context.GetGhostTriggerName(originalName) // 61 + 4 = 65
require.Equal(t, 65, len(ghostName))
require.False(t, context.ValidateGhostTriggerLengthBelowMaxLength(ghostName))
}
}
func TestReadConfigFile(t *testing.T) {
{
context := NewMigrationContext()
context.ConfigFile = "/does/not/exist"
if err := context.ReadConfigFile(); err == nil {
t.Fatal("Expected .ReadConfigFile() to return an error, got nil")
}
}
{
f, err := os.CreateTemp("", t.Name())
if err != nil {
t.Fatalf("Failed to create tmp file: %v", err)
}
defer os.Remove(f.Name())
f.Write([]byte("[client]"))
context := NewMigrationContext()
context.ConfigFile = f.Name()
if err := context.ReadConfigFile(); err != nil {
t.Fatalf(".ReadConfigFile() failed: %v", err)
}
}
{
f, err := os.CreateTemp("", t.Name())
if err != nil {
t.Fatalf("Failed to create tmp file: %v", err)
}
defer os.Remove(f.Name())
f.Write([]byte("[client]\nuser=test\npassword=123456"))
context := NewMigrationContext()
context.ConfigFile = f.Name()
if err := context.ReadConfigFile(); err != nil {
t.Fatalf(".ReadConfigFile() failed: %v", err)
}
if context.config.Client.User != "test" {
t.Fatalf("Expected client user %q, got %q", "test", context.config.Client.User)
} else if context.config.Client.Password != "123456" {
t.Fatalf("Expected client password %q, got %q", "123456", context.config.Client.Password)
}
}
{
f, err := os.CreateTemp("", t.Name())
if err != nil {
t.Fatalf("Failed to create tmp file: %v", err)
}
defer os.Remove(f.Name())
f.Write([]byte("[osc]\nmax_load=10"))
context := NewMigrationContext()
context.ConfigFile = f.Name()
if err := context.ReadConfigFile(); err != nil {
t.Fatalf(".ReadConfigFile() failed: %v", err)
}
if context.config.Osc.Max_Load != "10" {
t.Fatalf("Expected osc 'max_load' %q, got %q", "10", context.config.Osc.Max_Load)
}
}
}
func TestSetAbortError_StoresFirstError(t *testing.T) {
ctx := NewMigrationContext()
err1 := errors.New("first error")
err2 := errors.New("second error")
ctx.SetAbortError(err1)
ctx.SetAbortError(err2)
got := ctx.GetAbortError()
if got != err1 { //nolint:errorlint // Testing pointer equality for sentinel error
t.Errorf("Expected first error %v, got %v", err1, got)
}
}
func TestSetAbortError_ThreadSafe(t *testing.T) {
ctx := NewMigrationContext()
var wg sync.WaitGroup
errs := []error{
errors.New("error 1"),
errors.New("error 2"),
errors.New("error 3"),
}
// Launch 3 goroutines trying to set error concurrently
for _, err := range errs {
wg.Add(1)
go func(e error) {
defer wg.Done()
ctx.SetAbortError(e)
}(err)
}
wg.Wait()
// Should store exactly one of the errors
got := ctx.GetAbortError()
if got == nil {
t.Fatal("Expected error to be stored, got nil")
}
// Verify it's one of the errors we sent
found := false
for _, err := range errs {
if got == err { //nolint:errorlint // Testing pointer equality for sentinel error
found = true
break
}
}
if !found {
t.Errorf("Stored error %v not in list of sent errors", got)
}
}