2021-05-25 16:12:07 +00:00
|
|
|
package sqlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-10-29 15:29:10 +00:00
|
|
|
"embed"
|
2021-10-28 20:02:15 +00:00
|
|
|
"fmt"
|
2021-11-01 20:30:18 +00:00
|
|
|
"io/fs"
|
2021-10-28 20:02:15 +00:00
|
|
|
"os"
|
2021-05-25 16:12:07 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-11-01 15:07:54 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kit/migration"
|
2023-12-27 16:21:09 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kit/platform/errors"
|
2021-05-25 16:12:07 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/sqlite/test_migrations"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-10-28 20:02:15 +00:00
|
|
|
"go.uber.org/zap"
|
2021-05-25 16:12:07 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
|
|
|
)
|
|
|
|
|
2021-06-10 22:40:57 +00:00
|
|
|
type tableInfo struct {
|
|
|
|
Cid int `db:"cid"`
|
|
|
|
Name string `db:"name"`
|
|
|
|
Db_type string `db:"type"`
|
|
|
|
Notnull int `db:"notnull"`
|
|
|
|
Dflt_value interface{} `db:"dflt_value"`
|
|
|
|
Pk int `db:"pk"`
|
|
|
|
}
|
|
|
|
|
2021-05-25 16:12:07 +00:00
|
|
|
func TestUp(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
test: use `T.TempDir` to create temporary test directory (#23258)
* test: use `T.TempDir` to create temporary test directory
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestSendWrite on Windows
=== FAIL: replications/internal TestSendWrite (0.29s)
logger.go:130: 2022-06-23T13:00:54.290Z DEBUG Created new durable queue for replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestSendWrite1627281409\\001\\replicationq\\0000000000000001"}
logger.go:130: 2022-06-23T13:00:54.457Z ERROR Error in replication stream {"replication_id": "0000000000000001", "error": "remote timeout", "retries": 1}
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestSendWrite1627281409\001\replicationq\0000000000000001\1: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestStore_BadShard on Windows
=== FAIL: tsdb TestStore_BadShard (0.09s)
logger.go:130: 2022-06-23T12:18:21.827Z INFO Using data dir {"service": "store", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestStore_BadShard1363295568\\001"}
logger.go:130: 2022-06-23T12:18:21.827Z INFO Compaction settings {"service": "store", "max_concurrent_compactions": 2, "throughput_bytes_per_second": 50331648, "throughput_bytes_per_second_burst": 50331648}
logger.go:130: 2022-06-23T12:18:21.828Z INFO Open store (start) {"service": "store", "op_name": "tsdb_open", "op_event": "start"}
logger.go:130: 2022-06-23T12:18:21.828Z INFO Open store (end) {"service": "store", "op_name": "tsdb_open", "op_event": "end", "op_elapsed": "77.3µs"}
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestStore_BadShard1363295568\002\data\db0\rp0\1\index\0\L0-00000001.tsl: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestPartition_PrependLogFile_Write_Fail and TestPartition_Compact_Write_Fail on Windows
=== FAIL: tsdb/index/tsi1 TestPartition_PrependLogFile_Write_Fail/write_MANIFEST (0.06s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestPartition_PrependLogFile_Write_Failwrite_MANIFEST656030081\002\0\L0-00000003.tsl: The process cannot access the file because it is being used by another process.
--- FAIL: TestPartition_PrependLogFile_Write_Fail/write_MANIFEST (0.06s)
=== FAIL: tsdb/index/tsi1 TestPartition_Compact_Write_Fail/write_MANIFEST (0.08s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestPartition_Compact_Write_Failwrite_MANIFEST3398667527\002\0\L0-00000003.tsl: The process cannot access the file because it is being used by another process.
--- FAIL: TestPartition_Compact_Write_Fail/write_MANIFEST (0.08s)
We must close the open file descriptor otherwise the temporary file
cannot be cleaned up on Windows.
Fixes: 619eb1cae6 ("fix: restore in-memory Manifest on write error")
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestReplicationStartMissingQueue on Windows
=== FAIL: TestReplicationStartMissingQueue (1.60s)
logger.go:130: 2023-03-17T10:42:07.269Z DEBUG Created new durable queue for replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestReplicationStartMissingQueue76668607\\001\\replicationq\\0000000000000001"}
logger.go:130: 2023-03-17T10:42:07.305Z INFO Opened replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestReplicationStartMissingQueue76668607\\001\\replicationq\\0000000000000001"}
testing.go:1206: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestReplicationStartMissingQueue76668607\001\replicationq\0000000000000001\1: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: update TestWAL_DiskSize
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestWAL_DiskSize on Windows
=== FAIL: tsdb/engine/tsm1 TestWAL_DiskSize (2.65s)
testing.go:1206: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestWAL_DiskSize2736073801\001\_00006.wal: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
---------
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2023-03-21 20:22:11 +00:00
|
|
|
store := NewTestStore(t)
|
2021-11-01 20:30:18 +00:00
|
|
|
|
|
|
|
upsOnlyAll, err := test_migrations.AllUp.ReadDir(".")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
upsOnlyFirst, err := test_migrations.FirstUp.ReadDir(".")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
migrator := NewMigrator(store, zaptest.NewLogger(t))
|
2021-05-25 16:12:07 +00:00
|
|
|
|
2021-10-29 15:29:10 +00:00
|
|
|
// empty db contains no migrations
|
|
|
|
names, err := store.allMigrationNames()
|
2021-05-25 16:12:07 +00:00
|
|
|
require.NoError(t, err)
|
2021-10-29 15:29:10 +00:00
|
|
|
require.Equal(t, []string(nil), names)
|
2021-05-25 16:12:07 +00:00
|
|
|
|
2021-10-29 15:29:10 +00:00
|
|
|
// run the first migrations
|
2021-11-01 20:30:18 +00:00
|
|
|
migrateUpAndCheck(t, migrator, store, test_migrations.FirstUp, upsOnlyFirst)
|
2021-05-25 16:12:07 +00:00
|
|
|
|
2021-10-29 15:29:10 +00:00
|
|
|
// run the rest of the migrations
|
2021-11-01 20:30:18 +00:00
|
|
|
migrateUpAndCheck(t, migrator, store, test_migrations.AllUp, upsOnlyAll)
|
2021-06-10 22:40:57 +00:00
|
|
|
|
2021-10-29 15:29:10 +00:00
|
|
|
// test_table_1 had the "id" column renamed to "org_id"
|
|
|
|
var table1Info []*tableInfo
|
2021-06-10 22:40:57 +00:00
|
|
|
err = store.DB.Select(&table1Info, "PRAGMA table_info(test_table_1)")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, table1Info, 3)
|
|
|
|
require.Equal(t, "org_id", table1Info[0].Name)
|
|
|
|
|
2021-10-29 15:29:10 +00:00
|
|
|
// test_table_2 was created correctly
|
|
|
|
var table2Info []*tableInfo
|
2021-06-10 22:40:57 +00:00
|
|
|
err = store.DB.Select(&table2Info, "PRAGMA table_info(test_table_2)")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, table2Info, 3)
|
|
|
|
require.Equal(t, "user_id", table2Info[0].Name)
|
2021-05-25 16:12:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-29 15:29:10 +00:00
|
|
|
func TestUpErrors(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("only unknown migration exists", func(t *testing.T) {
|
test: use `T.TempDir` to create temporary test directory (#23258)
* test: use `T.TempDir` to create temporary test directory
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestSendWrite on Windows
=== FAIL: replications/internal TestSendWrite (0.29s)
logger.go:130: 2022-06-23T13:00:54.290Z DEBUG Created new durable queue for replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestSendWrite1627281409\\001\\replicationq\\0000000000000001"}
logger.go:130: 2022-06-23T13:00:54.457Z ERROR Error in replication stream {"replication_id": "0000000000000001", "error": "remote timeout", "retries": 1}
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestSendWrite1627281409\001\replicationq\0000000000000001\1: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestStore_BadShard on Windows
=== FAIL: tsdb TestStore_BadShard (0.09s)
logger.go:130: 2022-06-23T12:18:21.827Z INFO Using data dir {"service": "store", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestStore_BadShard1363295568\\001"}
logger.go:130: 2022-06-23T12:18:21.827Z INFO Compaction settings {"service": "store", "max_concurrent_compactions": 2, "throughput_bytes_per_second": 50331648, "throughput_bytes_per_second_burst": 50331648}
logger.go:130: 2022-06-23T12:18:21.828Z INFO Open store (start) {"service": "store", "op_name": "tsdb_open", "op_event": "start"}
logger.go:130: 2022-06-23T12:18:21.828Z INFO Open store (end) {"service": "store", "op_name": "tsdb_open", "op_event": "end", "op_elapsed": "77.3µs"}
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestStore_BadShard1363295568\002\data\db0\rp0\1\index\0\L0-00000001.tsl: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestPartition_PrependLogFile_Write_Fail and TestPartition_Compact_Write_Fail on Windows
=== FAIL: tsdb/index/tsi1 TestPartition_PrependLogFile_Write_Fail/write_MANIFEST (0.06s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestPartition_PrependLogFile_Write_Failwrite_MANIFEST656030081\002\0\L0-00000003.tsl: The process cannot access the file because it is being used by another process.
--- FAIL: TestPartition_PrependLogFile_Write_Fail/write_MANIFEST (0.06s)
=== FAIL: tsdb/index/tsi1 TestPartition_Compact_Write_Fail/write_MANIFEST (0.08s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestPartition_Compact_Write_Failwrite_MANIFEST3398667527\002\0\L0-00000003.tsl: The process cannot access the file because it is being used by another process.
--- FAIL: TestPartition_Compact_Write_Fail/write_MANIFEST (0.08s)
We must close the open file descriptor otherwise the temporary file
cannot be cleaned up on Windows.
Fixes: 619eb1cae6 ("fix: restore in-memory Manifest on write error")
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestReplicationStartMissingQueue on Windows
=== FAIL: TestReplicationStartMissingQueue (1.60s)
logger.go:130: 2023-03-17T10:42:07.269Z DEBUG Created new durable queue for replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestReplicationStartMissingQueue76668607\\001\\replicationq\\0000000000000001"}
logger.go:130: 2023-03-17T10:42:07.305Z INFO Opened replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestReplicationStartMissingQueue76668607\\001\\replicationq\\0000000000000001"}
testing.go:1206: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestReplicationStartMissingQueue76668607\001\replicationq\0000000000000001\1: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: update TestWAL_DiskSize
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestWAL_DiskSize on Windows
=== FAIL: tsdb/engine/tsm1 TestWAL_DiskSize (2.65s)
testing.go:1206: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestWAL_DiskSize2736073801\001\_00006.wal: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
---------
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2023-03-21 20:22:11 +00:00
|
|
|
store := NewTestStore(t)
|
2021-10-29 15:29:10 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
migrator := NewMigrator(store, zaptest.NewLogger(t))
|
|
|
|
require.NoError(t, migrator.Up(ctx, test_migrations.MigrationTable))
|
|
|
|
require.NoError(t, store.execTrans(ctx, `INSERT INTO migrations (name) VALUES ("0010_some_bad_migration")`))
|
2021-11-01 20:30:18 +00:00
|
|
|
require.Equal(t, migration.ErrInvalidMigration("0010_some_bad_migration"), migrator.Up(ctx, test_migrations.AllUp))
|
2021-10-29 15:29:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("known + unknown migrations exist", func(t *testing.T) {
|
test: use `T.TempDir` to create temporary test directory (#23258)
* test: use `T.TempDir` to create temporary test directory
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestSendWrite on Windows
=== FAIL: replications/internal TestSendWrite (0.29s)
logger.go:130: 2022-06-23T13:00:54.290Z DEBUG Created new durable queue for replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestSendWrite1627281409\\001\\replicationq\\0000000000000001"}
logger.go:130: 2022-06-23T13:00:54.457Z ERROR Error in replication stream {"replication_id": "0000000000000001", "error": "remote timeout", "retries": 1}
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestSendWrite1627281409\001\replicationq\0000000000000001\1: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestStore_BadShard on Windows
=== FAIL: tsdb TestStore_BadShard (0.09s)
logger.go:130: 2022-06-23T12:18:21.827Z INFO Using data dir {"service": "store", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestStore_BadShard1363295568\\001"}
logger.go:130: 2022-06-23T12:18:21.827Z INFO Compaction settings {"service": "store", "max_concurrent_compactions": 2, "throughput_bytes_per_second": 50331648, "throughput_bytes_per_second_burst": 50331648}
logger.go:130: 2022-06-23T12:18:21.828Z INFO Open store (start) {"service": "store", "op_name": "tsdb_open", "op_event": "start"}
logger.go:130: 2022-06-23T12:18:21.828Z INFO Open store (end) {"service": "store", "op_name": "tsdb_open", "op_event": "end", "op_elapsed": "77.3µs"}
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestStore_BadShard1363295568\002\data\db0\rp0\1\index\0\L0-00000001.tsl: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestPartition_PrependLogFile_Write_Fail and TestPartition_Compact_Write_Fail on Windows
=== FAIL: tsdb/index/tsi1 TestPartition_PrependLogFile_Write_Fail/write_MANIFEST (0.06s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestPartition_PrependLogFile_Write_Failwrite_MANIFEST656030081\002\0\L0-00000003.tsl: The process cannot access the file because it is being used by another process.
--- FAIL: TestPartition_PrependLogFile_Write_Fail/write_MANIFEST (0.06s)
=== FAIL: tsdb/index/tsi1 TestPartition_Compact_Write_Fail/write_MANIFEST (0.08s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestPartition_Compact_Write_Failwrite_MANIFEST3398667527\002\0\L0-00000003.tsl: The process cannot access the file because it is being used by another process.
--- FAIL: TestPartition_Compact_Write_Fail/write_MANIFEST (0.08s)
We must close the open file descriptor otherwise the temporary file
cannot be cleaned up on Windows.
Fixes: 619eb1cae6 ("fix: restore in-memory Manifest on write error")
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestReplicationStartMissingQueue on Windows
=== FAIL: TestReplicationStartMissingQueue (1.60s)
logger.go:130: 2023-03-17T10:42:07.269Z DEBUG Created new durable queue for replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestReplicationStartMissingQueue76668607\\001\\replicationq\\0000000000000001"}
logger.go:130: 2023-03-17T10:42:07.305Z INFO Opened replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestReplicationStartMissingQueue76668607\\001\\replicationq\\0000000000000001"}
testing.go:1206: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestReplicationStartMissingQueue76668607\001\replicationq\0000000000000001\1: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: update TestWAL_DiskSize
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestWAL_DiskSize on Windows
=== FAIL: tsdb/engine/tsm1 TestWAL_DiskSize (2.65s)
testing.go:1206: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestWAL_DiskSize2736073801\001\_00006.wal: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
---------
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2023-03-21 20:22:11 +00:00
|
|
|
store := NewTestStore(t)
|
2021-10-29 15:29:10 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
migrator := NewMigrator(store, zaptest.NewLogger(t))
|
2021-11-01 20:30:18 +00:00
|
|
|
require.NoError(t, migrator.Up(ctx, test_migrations.FirstUp))
|
2021-10-29 15:29:10 +00:00
|
|
|
require.NoError(t, store.execTrans(ctx, `INSERT INTO migrations (name) VALUES ("0010_some_bad_migration")`))
|
2021-11-01 20:30:18 +00:00
|
|
|
require.Equal(t, migration.ErrInvalidMigration("0010_some_bad_migration"), migrator.Up(ctx, test_migrations.AllUp))
|
2021-10-29 15:29:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-28 20:02:15 +00:00
|
|
|
func TestUpWithBackups(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
test: use `T.TempDir` to create temporary test directory (#23258)
* test: use `T.TempDir` to create temporary test directory
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestSendWrite on Windows
=== FAIL: replications/internal TestSendWrite (0.29s)
logger.go:130: 2022-06-23T13:00:54.290Z DEBUG Created new durable queue for replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestSendWrite1627281409\\001\\replicationq\\0000000000000001"}
logger.go:130: 2022-06-23T13:00:54.457Z ERROR Error in replication stream {"replication_id": "0000000000000001", "error": "remote timeout", "retries": 1}
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestSendWrite1627281409\001\replicationq\0000000000000001\1: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestStore_BadShard on Windows
=== FAIL: tsdb TestStore_BadShard (0.09s)
logger.go:130: 2022-06-23T12:18:21.827Z INFO Using data dir {"service": "store", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestStore_BadShard1363295568\\001"}
logger.go:130: 2022-06-23T12:18:21.827Z INFO Compaction settings {"service": "store", "max_concurrent_compactions": 2, "throughput_bytes_per_second": 50331648, "throughput_bytes_per_second_burst": 50331648}
logger.go:130: 2022-06-23T12:18:21.828Z INFO Open store (start) {"service": "store", "op_name": "tsdb_open", "op_event": "start"}
logger.go:130: 2022-06-23T12:18:21.828Z INFO Open store (end) {"service": "store", "op_name": "tsdb_open", "op_event": "end", "op_elapsed": "77.3µs"}
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestStore_BadShard1363295568\002\data\db0\rp0\1\index\0\L0-00000001.tsl: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestPartition_PrependLogFile_Write_Fail and TestPartition_Compact_Write_Fail on Windows
=== FAIL: tsdb/index/tsi1 TestPartition_PrependLogFile_Write_Fail/write_MANIFEST (0.06s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestPartition_PrependLogFile_Write_Failwrite_MANIFEST656030081\002\0\L0-00000003.tsl: The process cannot access the file because it is being used by another process.
--- FAIL: TestPartition_PrependLogFile_Write_Fail/write_MANIFEST (0.06s)
=== FAIL: tsdb/index/tsi1 TestPartition_Compact_Write_Fail/write_MANIFEST (0.08s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestPartition_Compact_Write_Failwrite_MANIFEST3398667527\002\0\L0-00000003.tsl: The process cannot access the file because it is being used by another process.
--- FAIL: TestPartition_Compact_Write_Fail/write_MANIFEST (0.08s)
We must close the open file descriptor otherwise the temporary file
cannot be cleaned up on Windows.
Fixes: 619eb1cae6 ("fix: restore in-memory Manifest on write error")
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestReplicationStartMissingQueue on Windows
=== FAIL: TestReplicationStartMissingQueue (1.60s)
logger.go:130: 2023-03-17T10:42:07.269Z DEBUG Created new durable queue for replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestReplicationStartMissingQueue76668607\\001\\replicationq\\0000000000000001"}
logger.go:130: 2023-03-17T10:42:07.305Z INFO Opened replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestReplicationStartMissingQueue76668607\\001\\replicationq\\0000000000000001"}
testing.go:1206: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestReplicationStartMissingQueue76668607\001\replicationq\0000000000000001\1: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: update TestWAL_DiskSize
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestWAL_DiskSize on Windows
=== FAIL: tsdb/engine/tsm1 TestWAL_DiskSize (2.65s)
testing.go:1206: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestWAL_DiskSize2736073801\001\_00006.wal: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
---------
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2023-03-21 20:22:11 +00:00
|
|
|
store := NewTestStore(t)
|
2021-10-28 20:02:15 +00:00
|
|
|
|
|
|
|
logger := zaptest.NewLogger(t)
|
|
|
|
migrator := NewMigrator(store, logger)
|
|
|
|
backupPath := fmt.Sprintf("%s.bak", store.path)
|
|
|
|
migrator.SetBackupPath(backupPath)
|
|
|
|
|
2021-11-01 20:30:18 +00:00
|
|
|
upsOnlyAll, err := test_migrations.AllUp.ReadDir(".")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
upsOnlyFirst, err := test_migrations.FirstUp.ReadDir(".")
|
2021-10-28 20:02:15 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-01 20:30:18 +00:00
|
|
|
|
|
|
|
// Run the first migrations.
|
|
|
|
migrateUpAndCheck(t, migrator, store, test_migrations.FirstUp, upsOnlyFirst)
|
2021-10-28 20:02:15 +00:00
|
|
|
|
|
|
|
// Backup file shouldn't exist, because there was nothing to back up.
|
|
|
|
_, err = os.Stat(backupPath)
|
|
|
|
require.True(t, os.IsNotExist(err))
|
|
|
|
|
|
|
|
// Run the remaining migrations.
|
2021-11-01 20:30:18 +00:00
|
|
|
migrateUpAndCheck(t, migrator, store, test_migrations.AllUp, upsOnlyAll)
|
2021-10-28 20:02:15 +00:00
|
|
|
|
|
|
|
// Backup file should now exist.
|
|
|
|
_, err = os.Stat(backupPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Open a 2nd store using the backup file.
|
|
|
|
backupStore, err := NewSqlStore(backupPath, zap.NewNop())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer backupStore.Close()
|
|
|
|
|
2021-10-29 15:29:10 +00:00
|
|
|
// Backup store contains the first migrations records.
|
|
|
|
backupNames, err := backupStore.allMigrationNames()
|
2021-10-28 20:02:15 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-01 20:30:18 +00:00
|
|
|
migrationNamesMatch(t, backupNames, upsOnlyFirst)
|
2021-10-28 20:02:15 +00:00
|
|
|
|
2021-11-01 20:30:18 +00:00
|
|
|
// Run the remaining migrations on the backup and verify that it now contains the rest of the migration records.
|
2021-10-28 20:02:15 +00:00
|
|
|
backupMigrator := NewMigrator(backupStore, logger)
|
2021-11-01 20:30:18 +00:00
|
|
|
migrateUpAndCheck(t, backupMigrator, store, test_migrations.AllUp, upsOnlyAll)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDown(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
test: use `T.TempDir` to create temporary test directory (#23258)
* test: use `T.TempDir` to create temporary test directory
This commit replaces `os.MkdirTemp` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `os.MkdirTemp`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestSendWrite on Windows
=== FAIL: replications/internal TestSendWrite (0.29s)
logger.go:130: 2022-06-23T13:00:54.290Z DEBUG Created new durable queue for replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestSendWrite1627281409\\001\\replicationq\\0000000000000001"}
logger.go:130: 2022-06-23T13:00:54.457Z ERROR Error in replication stream {"replication_id": "0000000000000001", "error": "remote timeout", "retries": 1}
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestSendWrite1627281409\001\replicationq\0000000000000001\1: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestStore_BadShard on Windows
=== FAIL: tsdb TestStore_BadShard (0.09s)
logger.go:130: 2022-06-23T12:18:21.827Z INFO Using data dir {"service": "store", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestStore_BadShard1363295568\\001"}
logger.go:130: 2022-06-23T12:18:21.827Z INFO Compaction settings {"service": "store", "max_concurrent_compactions": 2, "throughput_bytes_per_second": 50331648, "throughput_bytes_per_second_burst": 50331648}
logger.go:130: 2022-06-23T12:18:21.828Z INFO Open store (start) {"service": "store", "op_name": "tsdb_open", "op_event": "start"}
logger.go:130: 2022-06-23T12:18:21.828Z INFO Open store (end) {"service": "store", "op_name": "tsdb_open", "op_event": "end", "op_elapsed": "77.3µs"}
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestStore_BadShard1363295568\002\data\db0\rp0\1\index\0\L0-00000001.tsl: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestPartition_PrependLogFile_Write_Fail and TestPartition_Compact_Write_Fail on Windows
=== FAIL: tsdb/index/tsi1 TestPartition_PrependLogFile_Write_Fail/write_MANIFEST (0.06s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestPartition_PrependLogFile_Write_Failwrite_MANIFEST656030081\002\0\L0-00000003.tsl: The process cannot access the file because it is being used by another process.
--- FAIL: TestPartition_PrependLogFile_Write_Fail/write_MANIFEST (0.06s)
=== FAIL: tsdb/index/tsi1 TestPartition_Compact_Write_Fail/write_MANIFEST (0.08s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestPartition_Compact_Write_Failwrite_MANIFEST3398667527\002\0\L0-00000003.tsl: The process cannot access the file because it is being used by another process.
--- FAIL: TestPartition_Compact_Write_Fail/write_MANIFEST (0.08s)
We must close the open file descriptor otherwise the temporary file
cannot be cleaned up on Windows.
Fixes: 619eb1cae6 ("fix: restore in-memory Manifest on write error")
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestReplicationStartMissingQueue on Windows
=== FAIL: TestReplicationStartMissingQueue (1.60s)
logger.go:130: 2023-03-17T10:42:07.269Z DEBUG Created new durable queue for replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestReplicationStartMissingQueue76668607\\001\\replicationq\\0000000000000001"}
logger.go:130: 2023-03-17T10:42:07.305Z INFO Opened replication stream {"id": "0000000000000001", "path": "C:\\Users\\circleci\\AppData\\Local\\Temp\\TestReplicationStartMissingQueue76668607\\001\\replicationq\\0000000000000001"}
testing.go:1206: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestReplicationStartMissingQueue76668607\001\replicationq\0000000000000001\1: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: update TestWAL_DiskSize
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestWAL_DiskSize on Windows
=== FAIL: tsdb/engine/tsm1 TestWAL_DiskSize (2.65s)
testing.go:1206: TempDir RemoveAll cleanup: remove C:\Users\circleci\AppData\Local\Temp\TestWAL_DiskSize2736073801\001\_00006.wal: The process cannot access the file because it is being used by another process.
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
---------
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2023-03-21 20:22:11 +00:00
|
|
|
store := NewTestStore(t)
|
2021-11-01 20:30:18 +00:00
|
|
|
|
|
|
|
upsOnlyAll, err := test_migrations.AllUp.ReadDir(".")
|
|
|
|
require.NoError(t, err)
|
2021-10-28 20:02:15 +00:00
|
|
|
|
2021-11-01 20:30:18 +00:00
|
|
|
upsOnlyFirst, err := test_migrations.FirstUp.ReadDir(".")
|
2021-10-28 20:02:15 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-01 20:30:18 +00:00
|
|
|
|
|
|
|
migrator := NewMigrator(store, zaptest.NewLogger(t))
|
|
|
|
|
|
|
|
// no up migrations, then some down migrations
|
|
|
|
migrateDownAndCheck(t, migrator, store, test_migrations.FirstDown, []fs.DirEntry{}, 0)
|
|
|
|
|
|
|
|
// all up migrations, then all down migrations
|
|
|
|
migrateUpAndCheck(t, migrator, store, test_migrations.AllUp, upsOnlyAll)
|
|
|
|
migrateDownAndCheck(t, migrator, store, test_migrations.AllDown, []fs.DirEntry{}, 0)
|
|
|
|
|
|
|
|
// first of the up migrations, then first of the down migrations
|
|
|
|
migrateUpAndCheck(t, migrator, store, test_migrations.FirstUp, upsOnlyFirst)
|
|
|
|
migrateDownAndCheck(t, migrator, store, test_migrations.FirstDown, []fs.DirEntry{}, 0)
|
|
|
|
|
|
|
|
// first of the up migrations, then all of the down migrations
|
|
|
|
migrateUpAndCheck(t, migrator, store, test_migrations.FirstUp, upsOnlyFirst)
|
|
|
|
migrateDownAndCheck(t, migrator, store, test_migrations.AllDown, []fs.DirEntry{}, 0)
|
|
|
|
|
|
|
|
// all up migrations, then some of the down migrations (using untilMigration)
|
|
|
|
migrateUpAndCheck(t, migrator, store, test_migrations.AllUp, upsOnlyAll)
|
|
|
|
migrateDownAndCheck(t, migrator, store, test_migrations.AllDown, upsOnlyFirst, 2)
|
2021-10-28 20:02:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 16:12:07 +00:00
|
|
|
func TestScriptVersion(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
filename string
|
|
|
|
want int
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"single digit number",
|
|
|
|
"0001_some_file_name.sql",
|
|
|
|
1,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"larger number",
|
|
|
|
"0921_another_file.sql",
|
|
|
|
921,
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"bad name",
|
|
|
|
"not_numbered_correctly.sql",
|
|
|
|
0,
|
|
|
|
&errors.Error{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
2022-08-24 17:22:09 +00:00
|
|
|
tt := tt // capture range variable
|
2021-05-25 16:12:07 +00:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
got, err := scriptVersion(tt.filename)
|
|
|
|
require.Equal(t, tt.want, got)
|
|
|
|
if tt.wantErr == nil {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 15:29:10 +00:00
|
|
|
|
|
|
|
func TestDropExtension(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
input string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
input: "0001_some_migration",
|
|
|
|
want: "0001_some_migration",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "0001_some_migration.sql",
|
|
|
|
want: "0001_some_migration",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "0001_some_migration.down.sql",
|
|
|
|
want: "0001_some_migration",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
input: "0001_some_migration.something.anything.else",
|
|
|
|
want: "0001_some_migration",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
got := dropExtension(tt.input)
|
|
|
|
require.Equal(t, tt.want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-01 20:30:18 +00:00
|
|
|
func migrateUpAndCheck(t *testing.T, m *Migrator, s *SqlStore, source embed.FS, expected []fs.DirEntry) {
|
2021-10-29 15:29:10 +00:00
|
|
|
t.Helper()
|
2021-11-01 20:30:18 +00:00
|
|
|
|
|
|
|
require.NoError(t, m.Up(context.Background(), source))
|
|
|
|
names, err := s.allMigrationNames()
|
|
|
|
require.NoError(t, err)
|
|
|
|
migrationNamesMatch(t, names, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrateDownAndCheck(t *testing.T, m *Migrator, s *SqlStore, source embed.FS, expected []fs.DirEntry, untilMigration int) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
require.NoError(t, m.Down(context.Background(), untilMigration, source))
|
|
|
|
names, err := s.allMigrationNames()
|
2021-10-29 15:29:10 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-01 20:30:18 +00:00
|
|
|
migrationNamesMatch(t, names, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func migrationNamesMatch(t *testing.T, names []string, files []fs.DirEntry) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
require.Equal(t, len(names), len(files))
|
2021-10-29 15:29:10 +00:00
|
|
|
|
2021-11-01 20:30:18 +00:00
|
|
|
for idx := range files {
|
|
|
|
require.Equal(t, dropExtension(files[idx].Name()), names[idx])
|
2021-10-29 15:29:10 +00:00
|
|
|
}
|
|
|
|
}
|