-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathhooks_test.go
More file actions
115 lines (104 loc) · 3.44 KB
/
hooks_test.go
File metadata and controls
115 lines (104 loc) · 3.44 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
/*
Copyright 2022 GitHub Inc.
See https://github.com/github/gh-ost/blob/master/LICENSE
*/
package logic
import (
"bufio"
"bytes"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/github/gh-ost/go/base"
)
func TestHooksExecutorExecuteHooks(t *testing.T) {
migrationContext := base.NewMigrationContext()
migrationContext.AlterStatement = "ENGINE=InnoDB"
migrationContext.DatabaseName = "test"
migrationContext.Hostname = "test.example.com"
migrationContext.OriginalTableName = "tablename"
migrationContext.RowsDeltaEstimate = 1
migrationContext.RowsEstimate = 122
migrationContext.TotalRowsCopied = 123456
migrationContext.SetETADuration(time.Minute)
migrationContext.SetProgressPct(50)
hooksExecutor := NewHooksExecutor(migrationContext)
writeTmpHookFunc := func(testName, hookName, script string) (path string, err error) {
if path, err = os.MkdirTemp("", testName); err != nil {
return path, err
}
err = os.WriteFile(filepath.Join(path, hookName), []byte(script), 0777)
return path, err
}
t.Run("does-not-exist", func(t *testing.T) {
migrationContext.HooksPath = "/does/not/exist"
require.Nil(t, hooksExecutor.executeHooks("test-hook"))
})
t.Run("failed", func(t *testing.T) {
var err error
if migrationContext.HooksPath, err = writeTmpHookFunc(
"TestHooksExecutorExecuteHooks-failed",
"failed-hook",
"#!/bin/sh\nexit 1",
); err != nil {
panic(err)
}
defer os.RemoveAll(migrationContext.HooksPath)
require.NotNil(t, hooksExecutor.executeHooks("failed-hook"))
})
t.Run("success", func(t *testing.T) {
var err error
if migrationContext.HooksPath, err = writeTmpHookFunc(
"TestHooksExecutorExecuteHooks-success",
"success-hook",
"#!/bin/sh\nenv",
); err != nil {
panic(err)
}
defer os.RemoveAll(migrationContext.HooksPath)
var buf bytes.Buffer
hooksExecutor.writer = &buf
require.Nil(t, hooksExecutor.executeHooks("success-hook", "TEST="+t.Name()))
scanner := bufio.NewScanner(&buf)
for scanner.Scan() {
split := strings.SplitN(scanner.Text(), "=", 2)
switch split[0] {
case "GH_OST_COPIED_ROWS":
copiedRows, _ := strconv.ParseInt(split[1], 10, 64)
require.Equal(t, migrationContext.TotalRowsCopied, copiedRows)
case "GH_OST_DATABASE_NAME":
require.Equal(t, migrationContext.DatabaseName, split[1])
case "GH_OST_DDL":
require.Equal(t, migrationContext.AlterStatement, split[1])
case "GH_OST_DRY_RUN":
require.Equal(t, "false", split[1])
case "GH_OST_ESTIMATED_ROWS":
estimatedRows, _ := strconv.ParseInt(split[1], 10, 64)
require.Equal(t, int64(123), estimatedRows)
case "GH_OST_ETA_SECONDS":
etaSeconds, _ := strconv.ParseInt(split[1], 10, 64)
require.Equal(t, int64(60), etaSeconds)
case "GH_OST_EXECUTING_HOST":
require.Equal(t, migrationContext.Hostname, split[1])
case "GH_OST_GHOST_TABLE_NAME":
require.Equal(t, fmt.Sprintf("_%s_gho", migrationContext.OriginalTableName), split[1])
case "GH_OST_OLD_TABLE_NAME":
require.Equal(t, fmt.Sprintf("_%s_del", migrationContext.OriginalTableName), split[1])
case "GH_OST_PROGRESS":
progress, _ := strconv.ParseFloat(split[1], 64)
require.Equal(t, 50.0, progress)
case "GH_OST_TABLE_NAME":
require.Equal(t, migrationContext.OriginalTableName, split[1])
case "GH_OST_INSTANT_DDL":
require.Equal(t, "false", split[1])
case "TEST":
require.Equal(t, t.Name(), split[1])
}
}
})
}