|
| 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 | + "strconv" |
| 15 | + "strings" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/openark/golib/tests" |
| 20 | + |
| 21 | + "github.com/github/gh-ost/go/base" |
| 22 | +) |
| 23 | + |
| 24 | +func TestHooksExecutorExecuteHooks(t *testing.T) { |
| 25 | + migrationContext := base.NewMigrationContext() |
| 26 | + migrationContext.AlterStatement = "ENGINE=InnoDB" |
| 27 | + migrationContext.DatabaseName = "test" |
| 28 | + migrationContext.Hostname = "test.example.com" |
| 29 | + migrationContext.OriginalTableName = "tablename" |
| 30 | + migrationContext.RowsDeltaEstimate = 1 |
| 31 | + migrationContext.RowsEstimate = 122 |
| 32 | + migrationContext.TotalRowsCopied = 123456 |
| 33 | + migrationContext.SetETADuration(time.Minute) |
| 34 | + migrationContext.SetProgressPct(50) |
| 35 | + hooksExecutor := NewHooksExecutor(migrationContext) |
| 36 | + |
| 37 | + writeTmpHookFunc := func(testName, hookName, script string) (path string, err error) { |
| 38 | + if path, err = os.MkdirTemp("", testName); err != nil { |
| 39 | + return path, err |
| 40 | + } |
| 41 | + err = os.WriteFile(filepath.Join(path, hookName), []byte(script), 0777) |
| 42 | + return path, err |
| 43 | + } |
| 44 | + |
| 45 | + t.Run("does-not-exist", func(t *testing.T) { |
| 46 | + migrationContext.HooksPath = "/does/not/exist" |
| 47 | + tests.S(t).ExpectNil(hooksExecutor.executeHooks("test-hook")) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("failed", func(t *testing.T) { |
| 51 | + var err error |
| 52 | + if migrationContext.HooksPath, err = writeTmpHookFunc( |
| 53 | + "TestHooksExecutorExecuteHooks-failed", |
| 54 | + "failed-hook", |
| 55 | + "#!/bin/sh\nexit 1", |
| 56 | + ); err != nil { |
| 57 | + panic(err) |
| 58 | + } |
| 59 | + defer os.RemoveAll(migrationContext.HooksPath) |
| 60 | + tests.S(t).ExpectNotNil(hooksExecutor.executeHooks("failed-hook")) |
| 61 | + }) |
| 62 | + |
| 63 | + t.Run("success", func(t *testing.T) { |
| 64 | + var err error |
| 65 | + if migrationContext.HooksPath, err = writeTmpHookFunc( |
| 66 | + "TestHooksExecutorExecuteHooks-success", |
| 67 | + "success-hook", |
| 68 | + "#!/bin/sh\nenv", |
| 69 | + ); err != nil { |
| 70 | + panic(err) |
| 71 | + } |
| 72 | + defer os.RemoveAll(migrationContext.HooksPath) |
| 73 | + |
| 74 | + var buf bytes.Buffer |
| 75 | + hooksExecutor.writer = &buf |
| 76 | + tests.S(t).ExpectNil(hooksExecutor.executeHooks("success-hook", "TEST="+t.Name())) |
| 77 | + |
| 78 | + scanner := bufio.NewScanner(&buf) |
| 79 | + for scanner.Scan() { |
| 80 | + split := strings.SplitN(scanner.Text(), "=", 2) |
| 81 | + switch split[0] { |
| 82 | + case "GH_OST_COPIED_ROWS": |
| 83 | + copiedRows, _ := strconv.ParseInt(split[1], 10, 64) |
| 84 | + tests.S(t).ExpectEquals(copiedRows, migrationContext.TotalRowsCopied) |
| 85 | + case "GH_OST_DATABASE_NAME": |
| 86 | + tests.S(t).ExpectEquals(split[1], migrationContext.DatabaseName) |
| 87 | + case "GH_OST_DDL": |
| 88 | + tests.S(t).ExpectEquals(split[1], migrationContext.AlterStatement) |
| 89 | + case "GH_OST_DRY_RUN": |
| 90 | + tests.S(t).ExpectEquals(split[1], "false") |
| 91 | + case "GH_OST_ESTIMATED_ROWS": |
| 92 | + estimatedRows, _ := strconv.ParseInt(split[1], 10, 64) |
| 93 | + tests.S(t).ExpectEquals(estimatedRows, int64(123)) |
| 94 | + case "GH_OST_ETA_SECONDS": |
| 95 | + etaSeconds, _ := strconv.ParseInt(split[1], 10, 64) |
| 96 | + tests.S(t).ExpectEquals(etaSeconds, int64(60)) |
| 97 | + case "GH_OST_EXECUTING_HOST": |
| 98 | + tests.S(t).ExpectEquals(split[1], migrationContext.Hostname) |
| 99 | + case "GH_OST_GHOST_TABLE_NAME": |
| 100 | + tests.S(t).ExpectEquals(split[1], fmt.Sprintf("_%s_gho", migrationContext.OriginalTableName)) |
| 101 | + case "GH_OST_OLD_TABLE_NAME": |
| 102 | + tests.S(t).ExpectEquals(split[1], fmt.Sprintf("_%s_del", migrationContext.OriginalTableName)) |
| 103 | + case "GH_OST_PROGRESS": |
| 104 | + progress, _ := strconv.ParseFloat(split[1], 64) |
| 105 | + tests.S(t).ExpectEquals(progress, 50.0) |
| 106 | + case "GH_OST_TABLE_NAME": |
| 107 | + tests.S(t).ExpectEquals(split[1], migrationContext.OriginalTableName) |
| 108 | + case "TEST": |
| 109 | + tests.S(t).ExpectEquals(split[1], t.Name()) |
| 110 | + } |
| 111 | + } |
| 112 | + }) |
| 113 | +} |
0 commit comments