|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "runtime" |
| 9 | + "syscall" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/cybertec-postgresql/pg_timetable/internal/config" |
| 14 | + "github.com/cybertec-postgresql/pg_timetable/internal/log" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + "github.com/testcontainers/testcontainers-go" |
| 18 | + "github.com/testcontainers/testcontainers-go/modules/postgres" |
| 19 | + "github.com/testcontainers/testcontainers-go/wait" |
| 20 | +) |
| 21 | + |
| 22 | +// newTestLogger returns a silent logger suitable for use in tests. |
| 23 | +func newTestLogger() log.LoggerHookerIface { |
| 24 | + return log.Init(config.LoggingOpts{LogLevel: "panic", LogDBLevel: "none"}) |
| 25 | +} |
| 26 | + |
| 27 | +// setupTestContainer starts a bare PostgreSQL container and returns the |
| 28 | +// connection string along with a cleanup function. Unlike the shared |
| 29 | +// testutils helper, it does NOT initialise the pg_timetable schema so that |
| 30 | +// run() can perform that step itself. |
| 31 | +func setupTestContainer(t *testing.T) (connStr string, cleanup func()) { |
| 32 | + t.Helper() |
| 33 | + ctx := context.Background() |
| 34 | + c, err := postgres.Run( |
| 35 | + ctx, |
| 36 | + "postgres:18-alpine", |
| 37 | + postgres.WithDatabase("timetable"), |
| 38 | + postgres.WithUsername("scheduler"), |
| 39 | + postgres.WithPassword("somestrong"), |
| 40 | + testcontainers.WithWaitStrategyAndDeadline( |
| 41 | + 60*time.Second, |
| 42 | + wait.ForLog("database system is ready to accept connections").WithOccurrence(2), |
| 43 | + ), |
| 44 | + ) |
| 45 | + require.NoError(t, err, "Failed to start PostgreSQL container") |
| 46 | + cs, err := c.ConnectionString(ctx, "sslmode=disable") |
| 47 | + if err != nil { |
| 48 | + _ = c.Terminate(ctx) |
| 49 | + t.Fatalf("Failed to get connection string: %v", err) |
| 50 | + } |
| 51 | + return cs, func() { _ = c.Terminate(ctx) } |
| 52 | +} |
| 53 | + |
| 54 | +// TestPrintVersion verifies that printVersion writes the expected fields to |
| 55 | +// stdout. |
| 56 | +func TestPrintVersion(t *testing.T) { |
| 57 | + r, w, err := os.Pipe() |
| 58 | + require.NoError(t, err) |
| 59 | + oldStdout := os.Stdout |
| 60 | + os.Stdout = w |
| 61 | + |
| 62 | + printVersion() |
| 63 | + |
| 64 | + w.Close() |
| 65 | + os.Stdout = oldStdout |
| 66 | + |
| 67 | + var buf bytes.Buffer |
| 68 | + _, _ = io.Copy(&buf, r) |
| 69 | + out := buf.String() |
| 70 | + |
| 71 | + assert.Contains(t, out, "pg_timetable:") |
| 72 | + assert.Contains(t, out, "Version:") |
| 73 | + assert.Contains(t, out, "DB Schema:") |
| 74 | + assert.Contains(t, out, "Git Commit:") |
| 75 | + assert.Contains(t, out, "Built:") |
| 76 | +} |
| 77 | + |
| 78 | +// TestSetupCloseHandler verifies that sending SIGTERM causes the provided |
| 79 | +// cancel function to be called. Skipped on Windows where signal delivery to |
| 80 | +// the current process works differently. |
| 81 | +func TestSetupCloseHandler(t *testing.T) { |
| 82 | + if runtime.GOOS == "windows" { |
| 83 | + t.Skip("SIGTERM delivery to self is not supported on Windows") |
| 84 | + } |
| 85 | + |
| 86 | + ctx, cancel := context.WithCancel(context.Background()) |
| 87 | + done := make(chan struct{}) |
| 88 | + SetupCloseHandler(func() { |
| 89 | + cancel() |
| 90 | + close(done) |
| 91 | + }) |
| 92 | + |
| 93 | + p, err := os.FindProcess(os.Getpid()) |
| 94 | + require.NoError(t, err) |
| 95 | + require.NoError(t, p.Signal(syscall.SIGTERM)) |
| 96 | + |
| 97 | + select { |
| 98 | + case <-done: |
| 99 | + case <-time.After(3 * time.Second): |
| 100 | + t.Fatal("cancel was not called within 3 s of receiving SIGTERM") |
| 101 | + } |
| 102 | + assert.ErrorIs(t, ctx.Err(), context.Canceled) |
| 103 | +} |
| 104 | + |
| 105 | +// TestRunDBConnectionFailure verifies that run returns ExitCodeDBEngineError |
| 106 | +// when the database is unreachable. |
| 107 | +func TestRunDBConnectionFailure(t *testing.T) { |
| 108 | + ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second) |
| 109 | + defer cancel() |
| 110 | + |
| 111 | + cmdOpts := config.NewCmdOptions( |
| 112 | + "--clientname=test_conn_fail", |
| 113 | + // port 1 is almost universally refused immediately |
| 114 | + "--connstr=postgres://invalid:invalid@localhost:1/invalid?sslmode=disable", |
| 115 | + ) |
| 116 | + code := run(ctx, cmdOpts, newTestLogger()) |
| 117 | + assert.Equal(t, ExitCodeDBEngineError, code) |
| 118 | +} |
| 119 | + |
| 120 | +// TestRunInitOnly verifies that run initialises the database schema and exits |
| 121 | +// cleanly when the --init flag is supplied. |
| 122 | +func TestRunInitOnly(t *testing.T) { |
| 123 | + connStr, cleanup := setupTestContainer(t) |
| 124 | + defer cleanup() |
| 125 | + |
| 126 | + cmdOpts := config.NewCmdOptions( |
| 127 | + "--clientname=test_main_init", |
| 128 | + "--connstr="+connStr, |
| 129 | + "--init", |
| 130 | + ) |
| 131 | + code := run(context.Background(), cmdOpts, newTestLogger()) |
| 132 | + assert.Equal(t, ExitCodeOK, code) |
| 133 | +} |
| 134 | + |
| 135 | +// TestRunUpgrade verifies that run performs a schema upgrade and exits cleanly |
| 136 | +// when the --upgrade flag is combined with --init. |
| 137 | +func TestRunUpgrade(t *testing.T) { |
| 138 | + connStr, cleanup := setupTestContainer(t) |
| 139 | + defer cleanup() |
| 140 | + |
| 141 | + cmdOpts := config.NewCmdOptions( |
| 142 | + "--clientname=test_main_upgrade", |
| 143 | + "--connstr="+connStr, |
| 144 | + "--upgrade", |
| 145 | + "--init", |
| 146 | + ) |
| 147 | + code := run(context.Background(), cmdOpts, newTestLogger()) |
| 148 | + assert.Equal(t, ExitCodeOK, code) |
| 149 | +} |
| 150 | + |
| 151 | +// TestRunContextCancellation verifies that run returns ExitCodeOK (not |
| 152 | +// ExitCodeShutdownCommand) when the context is cancelled while the scheduler |
| 153 | +// is running. |
| 154 | +func TestRunContextCancellation(t *testing.T) { |
| 155 | + connStr, cleanup := setupTestContainer(t) |
| 156 | + defer cleanup() |
| 157 | + |
| 158 | + ctx, cancel := context.WithTimeout(context.Background(), 8*time.Second) |
| 159 | + defer cancel() |
| 160 | + |
| 161 | + cmdOpts := config.NewCmdOptions( |
| 162 | + "--clientname=test_main_cancel", |
| 163 | + "--connstr="+connStr, |
| 164 | + ) |
| 165 | + code := run(ctx, cmdOpts, newTestLogger()) |
| 166 | + assert.Equal(t, ExitCodeOK, code) |
| 167 | +} |
0 commit comments