Skip to content

Commit e53bbce

Browse files
Add basic test for hooks
1 parent 1df37c2 commit e53bbce

File tree

4 files changed

+96
-19
lines changed

4 files changed

+96
-19
lines changed

go/logic/hooks.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package logic
77

88
import (
99
"fmt"
10+
"io"
1011
"os"
1112
"os/exec"
1213
"path/filepath"
@@ -34,18 +35,16 @@ const (
3435

3536
type HooksExecutor struct {
3637
migrationContext *base.MigrationContext
38+
output io.Writer
3739
}
3840

3941
func NewHooksExecutor(migrationContext *base.MigrationContext) *HooksExecutor {
4042
return &HooksExecutor{
4143
migrationContext: migrationContext,
44+
output: os.Stderr,
4245
}
4346
}
4447

45-
func (this *HooksExecutor) initHooks() error {
46-
return nil
47-
}
48-
4948
func (this *HooksExecutor) applyEnvironmentVariables(extraVariables ...string) []string {
5049
env := os.Environ()
5150
env = append(env, fmt.Sprintf("GH_OST_DATABASE_NAME=%s", this.migrationContext.DatabaseName))
@@ -76,13 +75,13 @@ func (this *HooksExecutor) applyEnvironmentVariables(extraVariables ...string) [
7675
}
7776

7877
// executeHook executes a command, and sets relevant environment variables
79-
// combined output & error are printed to gh-ost's standard error.
78+
// combined output & error are printed to the configured io.Writer.
8079
func (this *HooksExecutor) executeHook(hook string, extraVariables ...string) error {
8180
cmd := exec.Command(hook)
8281
cmd.Env = this.applyEnvironmentVariables(extraVariables...)
8382

8483
combinedOutput, err := cmd.CombinedOutput()
85-
fmt.Fprintln(os.Stderr, string(combinedOutput))
84+
fmt.Fprintln(this.output, string(combinedOutput))
8685
return log.Errore(err)
8786
}
8887

go/logic/hooks_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright 2022 GitHub Inc.
3+
See https://github.com/github/gh-ost/blob/master/LICENSE
4+
*/
5+
6+
package logic
7+
8+
import (
9+
"bufio"
10+
"bytes"
11+
"fmt"
12+
"os"
13+
"path/filepath"
14+
"strings"
15+
"testing"
16+
17+
"github.com/openark/golib/tests"
18+
19+
"github.com/github/gh-ost/go/base"
20+
)
21+
22+
func TestHooksExecutorExecuteHooks(t *testing.T) {
23+
migrationContext := base.NewMigrationContext()
24+
migrationContext.AlterStatement = "ENGINE=InnoDB"
25+
migrationContext.DatabaseName = "test"
26+
migrationContext.OriginalTableName = "tablename"
27+
hooksExecutor := NewHooksExecutor(migrationContext)
28+
29+
writeTmpHookFunc := func(testName, hookName, script string) (hooksPath string, err error) {
30+
if hooksPath, err = os.MkdirTemp("", testName); err != nil {
31+
return hooksPath, err
32+
}
33+
err = os.WriteFile(filepath.Join(hooksPath, hookName), []byte(script), 0777)
34+
return hooksPath, err
35+
}
36+
37+
t.Run("does-not-exist", func(t *testing.T) {
38+
migrationContext.HooksPath = "/does/not/exist"
39+
tests.S(t).ExpectNil(hooksExecutor.executeHooks("test-hook"))
40+
})
41+
42+
t.Run("failed", func(t *testing.T) {
43+
var err error
44+
if migrationContext.HooksPath, err = writeTmpHookFunc(
45+
"TestHooksExecutorExecuteHooks-failed",
46+
"failed-hook",
47+
"#!/bin/sh\nexit 1",
48+
); err != nil {
49+
panic(err)
50+
}
51+
defer os.RemoveAll(migrationContext.HooksPath)
52+
tests.S(t).ExpectNotNil(hooksExecutor.executeHooks("failed-hook"))
53+
})
54+
55+
t.Run("success", func(t *testing.T) {
56+
var err error
57+
if migrationContext.HooksPath, err = writeTmpHookFunc(
58+
"TestHooksExecutorExecuteHooks-success",
59+
"success-hook",
60+
"#!/bin/sh\nenv",
61+
); err != nil {
62+
panic(err)
63+
}
64+
defer os.RemoveAll(migrationContext.HooksPath)
65+
66+
var buf bytes.Buffer
67+
hooksExecutor.output = &buf
68+
tests.S(t).ExpectNil(hooksExecutor.executeHooks("success-hook", "TEST="+t.Name()))
69+
70+
scanner := bufio.NewScanner(&buf)
71+
for scanner.Scan() {
72+
split := strings.SplitN(scanner.Text(), "=", 2)
73+
switch split[0] {
74+
case "GH_OST_DATABASE_NAME":
75+
tests.S(t).ExpectEquals(split[1], migrationContext.DatabaseName)
76+
case "GH_OST_DDL":
77+
tests.S(t).ExpectEquals(split[1], migrationContext.AlterStatement)
78+
case "GH_OST_GHOST_TABLE_NAME":
79+
tests.S(t).ExpectEquals(split[1], fmt.Sprintf("_%s_gho", migrationContext.OriginalTableName))
80+
case "GH_OST_OLD_TABLE_NAME":
81+
tests.S(t).ExpectEquals(split[1], fmt.Sprintf("_%s_del", migrationContext.OriginalTableName))
82+
case "GH_OST_TABLE_NAME":
83+
tests.S(t).ExpectEquals(split[1], migrationContext.OriginalTableName)
84+
case "TEST":
85+
tests.S(t).ExpectEquals(split[1], t.Name())
86+
}
87+
}
88+
})
89+
}

go/logic/migrator.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type Migrator struct {
9898
func NewMigrator(context *base.MigrationContext, appVersion string) *Migrator {
9999
migrator := &Migrator{
100100
appVersion: appVersion,
101+
hooksExecutor: NewHooksExecutor(context),
101102
migrationContext: context,
102103
parser: sql.NewAlterTableParser(),
103104
ghostTableMigrated: make(chan bool),
@@ -113,15 +114,6 @@ func NewMigrator(context *base.MigrationContext, appVersion string) *Migrator {
113114
return migrator
114115
}
115116

116-
// initiateHooksExecutor
117-
func (this *Migrator) initiateHooksExecutor() (err error) {
118-
this.hooksExecutor = NewHooksExecutor(this.migrationContext)
119-
if err := this.hooksExecutor.initHooks(); err != nil {
120-
return err
121-
}
122-
return nil
123-
}
124-
125117
// sleepWhileTrue sleeps indefinitely until the given function returns 'false'
126118
// (or fails with error)
127119
func (this *Migrator) sleepWhileTrue(operation func() (bool, error)) error {
@@ -342,9 +334,6 @@ func (this *Migrator) Migrate() (err error) {
342334

343335
go this.listenOnPanicAbort()
344336

345-
if err := this.initiateHooksExecutor(); err != nil {
346-
return err
347-
}
348337
if err := this.hooksExecutor.onStartup(); err != nil {
349338
return err
350339
}

script/test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ script/build
1414
cd .gopath/src/github.com/github/gh-ost
1515

1616
echo "Running unit tests"
17-
go test ./go/...
17+
go test -v -covermode=atomic ./go/...

0 commit comments

Comments
 (0)