|
| 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 | +} |
0 commit comments