From 9e4e1ade67678a983352f3ff52b15e7068399d74 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Wed, 2 Dec 2026 00:13:02 -0800 Subject: [PATCH] filer: conformance suite for filer stores, run against Postgres and MySQL in CI (#13250) * filer/store_test: conformance suite for filer stores Entries in and out of buckets, byte-ordered and prefixed listings, key-value records, transactions, bucket tables and the snapshot scan. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer: run postgres2 and mysql2 through the conformance suite RUN_POSTGRES_TESTS and RUN_MYSQL_TESTS enable them, as RUN_REDIS_TESTS does the Redis stores; each test gets a schema or database of its own. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * ci: give the go test job Postgres and MySQL services Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * filer/store_test: create the shared fixtures before the subtests The scan and delete-folder-children subtests read the order directory that only the list subtest created, so selecting either one with -run failed on a conforming store. Co-Authored-By: Claude Opus 5.5 * filer/store_test: roll back a transaction an assertion abandons A failed assertion between BeginTransaction and the commit left the transaction open with its locks, and the cleanup's DROP SCHEMA then waited on them until the go test timeout. Co-Authored-By: Claude Opus 5.5 * filer/store_test: check limited listings without trimming the result The limit-1 checks sliced the listing to one name, so a store that ignored the limit passed, and one that returned nothing panicked. Allow only the one look-ahead entry the SQL stores return and compare the first name. Co-Authored-By: Claude Opus 5.5 * filer/store_test: give the exclusive limit check three eligible names Starting after a%b left only two names under the prefix, so a store that ignored the limit still met the one look-ahead bound. Co-Authored-By: Claude Opus 5.5 * filer/store_test: check that a bucket drop removes nested entries A store that reports CanDropWholeBucket gets no recursive walk from the filer, so its DeleteFolderChildren on the bucket has to remove every descendant. With only a direct child in bkt2, a store that deleted the direct children and kept the table passed. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- .github/workflows/go.yml | 24 +++ weed/filer/mysql2/mysql2_store_test.go | 17 ++ weed/filer/postgres2/postgres2_store_test.go | 17 ++ weed/filer/store_test/conformance.go | 215 +++++++++++++++++++ weed/filer/store_test/mysql.go | 47 ++++ weed/filer/store_test/postgres.go | 52 +++++ 6 files changed, 372 insertions(+) create mode 100644 weed/filer/mysql2/mysql2_store_test.go create mode 100644 weed/filer/postgres2/postgres2_store_test.go create mode 100644 weed/filer/store_test/conformance.go create mode 100644 weed/filer/store_test/mysql.go create mode 100644 weed/filer/store_test/postgres.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 0a06fbf6be..d17df74058 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -125,6 +125,28 @@ jobs: --health-interval 10s --health-timeout 5s --health-retries 5 + postgres: + image: postgres:17 + env: + POSTGRES_PASSWORD: postgres + ports: + - 5432:5432 + options: >- + --health-cmd "pg_isready -U postgres" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + mysql: + image: mysql:8.4 + env: + MYSQL_ROOT_PASSWORD: root + ports: + - 3306:3306 + options: >- + --health-cmd "mysqladmin ping -uroot -proot" + --health-interval 10s + --health-timeout 5s + --health-retries 10 steps: - name: Check out code into the Go module directory uses: actions/checkout@v7 @@ -136,6 +158,8 @@ jobs: - name: Test env: RUN_REDIS_TESTS: "1" + RUN_POSTGRES_TESTS: "1" + RUN_MYSQL_TESTS: "1" run: cd weed; go test -tags "elastic gocdk sqlite ydb tarantool tikv rclone" -v ./... test-32bit: diff --git a/weed/filer/mysql2/mysql2_store_test.go b/weed/filer/mysql2/mysql2_store_test.go new file mode 100644 index 0000000000..4154634ed1 --- /dev/null +++ b/weed/filer/mysql2/mysql2_store_test.go @@ -0,0 +1,17 @@ +package mysql2 + +import ( + "testing" + + "github.com/seaweedfs/seaweedfs/weed/filer/store_test" +) + +func TestStoreConformance(t *testing.T) { + db := store_test.NewMysqlTestDatabase(t) + store := &MysqlStore2{} + if err := store.initialize("", "", true, db.User, db.Password, db.Host, db.Port, db.Database, 10, 0, 60, false); err != nil { + t.Fatal(err) + } + defer store.Shutdown() + store_test.TestFilerStoreConformance(t, store) +} diff --git a/weed/filer/postgres2/postgres2_store_test.go b/weed/filer/postgres2/postgres2_store_test.go new file mode 100644 index 0000000000..4b4bc708d1 --- /dev/null +++ b/weed/filer/postgres2/postgres2_store_test.go @@ -0,0 +1,17 @@ +package postgres2 + +import ( + "testing" + + "github.com/seaweedfs/seaweedfs/weed/filer/store_test" +) + +func TestStoreConformance(t *testing.T) { + db := store_test.NewPostgresTestDatabase(t) + store := &PostgresStore2{} + if err := store.initialize("", "", true, db.User, db.Password, db.Hostname, db.Port, db.Database, db.Schema, db.SSLMode, "", "", "", "", false, 10, 0, 60); err != nil { + t.Fatal(err) + } + defer store.Shutdown() + store_test.TestFilerStoreConformance(t, store) +} diff --git a/weed/filer/store_test/conformance.go b/weed/filer/store_test/conformance.go new file mode 100644 index 0000000000..371bddcade --- /dev/null +++ b/weed/filer/store_test/conformance.go @@ -0,0 +1,215 @@ +package store_test + +import ( + "context" + "errors" + "fmt" + "os" + "sort" + "testing" + "time" + + "github.com/seaweedfs/seaweedfs/weed/filer" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/util" + "github.com/stretchr/testify/require" +) + +// TestFilerStoreConformance checks a store against the behavior the filer +// relies on: entries in and out of buckets, listings, key-value records, +// transactions and, when the store offers them, bucket tables and scans. +func TestFilerStoreConformance(t *testing.T, store filer.FilerStore) { + ctx := context.Background() + now := time.Now() + dir := func(p string) *filer.Entry { + return &filer.Entry{FullPath: util.FullPath(p), Attr: filer.Attr{Mode: os.ModeDir | 0755, Mtime: now, Crtime: now}} + } + file := func(p string, size uint64) *filer.Entry { + return &filer.Entry{FullPath: util.FullPath(p), Attr: filer.Attr{Mode: 0644, Mtime: now, Crtime: now, FileSize: size}} + } + insert := func(t *testing.T, entries ...*filer.Entry) { + t.Helper() + for _, e := range entries { + require.NoError(t, store.InsertEntry(ctx, e), "insert %s", e.FullPath) + } + } + list := func(t *testing.T, d, start string, inclusive bool, limit int64, prefix string) []string { + t.Helper() + var names []string + _, err := store.ListDirectoryPrefixedEntries(ctx, util.FullPath(d), start, inclusive, limit, prefix, func(e *filer.Entry) (bool, error) { + names = append(names, e.Name()) + return true, nil + }) + require.NoError(t, err, "list %s", d) + return names + } + notFound := func(t *testing.T, p string) { + t.Helper() + _, err := store.FindEntry(ctx, util.FullPath(p)) + require.ErrorIs(t, err, filer_pb.ErrNotFound, "find %s", p) + } + + if bucketAware, ok := store.(filer.BucketAware); ok { + bucketAware.OnBucketCreation("bkt1") + bucketAware.OnBucketCreation("bkt2") + } + insert(t, dir("/buckets"), dir("/buckets/bkt1"), dir("/buckets/bkt2"), dir("/buckets/bkt1/a"), dir("/buckets/bkt1/a/b"), + file("/buckets/bkt1/top.txt", 1), file("/buckets/bkt1/a/x.txt", 2), file("/buckets/bkt1/a/b/y.txt", 3), + file("/buckets/bkt2/top.txt", 4), dir("/buckets/bkt2/sub"), file("/buckets/bkt2/sub/deep.txt", 7), + dir("/topics"), file("/topics/t.log", 5), file("/root.txt", 6), dir("/buckets/bkt1/order")) + orderNames := []string{"B", "a", "a_b", "a%b", "aXb", "a!b", "z", "~", "é", "日本"} + for _, n := range orderNames { + insert(t, file("/buckets/bkt1/order/"+n, 1)) + } + + t.Run("find and update", func(t *testing.T) { + e, err := store.FindEntry(ctx, "/buckets/bkt1/a/x.txt") + require.NoError(t, err) + require.Equal(t, uint64(2), e.FileSize) + e.FileSize = 20 + require.NoError(t, store.UpdateEntry(ctx, e)) + e, err = store.FindEntry(ctx, "/buckets/bkt1/a/x.txt") + require.NoError(t, err) + require.Equal(t, uint64(20), e.FileSize) + insert(t, file("/buckets/bkt1/a/x.txt", 2)) + e, err = store.FindEntry(ctx, "/buckets/bkt1/a/x.txt") + require.NoError(t, err) + require.Equal(t, uint64(2), e.FileSize, "insert over an entry replaces it") + e, err = store.FindEntry(ctx, "/buckets/bkt2/top.txt") + require.NoError(t, err) + require.Equal(t, uint64(4), e.FileSize, "same name in another bucket") + e, err = store.FindEntry(ctx, "/buckets/bkt1") + require.NoError(t, err) + require.True(t, e.IsDirectory()) + notFound(t, "/buckets/bkt1/missing") + }) + + t.Run("list", func(t *testing.T) { + require.Equal(t, []string{"a", "order", "top.txt"}, list(t, "/buckets/bkt1", "", false, 100, "")) + require.Equal(t, []string{"x.txt"}, list(t, "/buckets/bkt1/a", "", false, 100, "x")) + require.Equal(t, []string{"bkt1", "bkt2"}, list(t, "/buckets", "", false, 100, "")) + require.Equal(t, []string{"buckets", "root.txt", "topics"}, list(t, "/", "", false, 100, "")) + + sorted := append([]string(nil), orderNames...) + sort.Strings(sorted) + require.Equal(t, sorted, list(t, "/buckets/bkt1/order", "", false, 100, ""), "byte order") + require.Equal(t, []string{"a_b"}, list(t, "/buckets/bkt1/order", "", false, 100, "a_"), "_ is literal") + require.Equal(t, []string{"a%b"}, list(t, "/buckets/bkt1/order", "", false, 100, "a%"), "% is literal") + require.Equal(t, []string{"a!b"}, list(t, "/buckets/bkt1/order", "", false, 100, "a!"), "! is literal") + // A store may hand over one look-ahead entry beyond the limit. + first := func(names []string) string { + t.Helper() + require.NotEmpty(t, names) + require.LessOrEqual(t, len(names), 2) + return names[0] + } + require.Equal(t, "a%b", first(list(t, "/buckets/bkt1/order", "a!b", false, 1, "a"))) + require.Equal(t, "a%b", first(list(t, "/buckets/bkt1/order", "a%b", true, 1, "a"))) + require.Equal(t, []string{"aXb", "a_b"}, list(t, "/buckets/bkt1/order", "a%b", false, 100, "a")) + require.Empty(t, list(t, "/buckets/bkt1/missing", "", false, 100, "")) + + var counted int + last, err := store.ListDirectoryEntries(ctx, "/buckets/bkt1/order", "", false, 100, func(*filer.Entry) (bool, error) { + counted++ + return counted < 3, nil + }) + require.NoError(t, err) + require.Equal(t, 3, counted) + require.Equal(t, sorted[2], last) + }) + + t.Run("key-value", func(t *testing.T) { + keys := [][]byte{[]byte("short"), []byte("hardlink-00000001"), []byte("/looks/like/a/path"), {0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, '/', 'x'}, {0}} + for i, k := range keys { + require.NoError(t, store.KvPut(ctx, k, []byte(fmt.Sprintf("v%d", i)))) + } + require.NoError(t, store.KvPut(ctx, keys[1], []byte("v1b"))) + for i, k := range keys { + v, err := store.KvGet(ctx, k) + require.NoError(t, err) + want := fmt.Sprintf("v%d", i) + if i == 1 { + want = "v1b" + } + require.Equal(t, want, string(v), "key %q", k) + } + values, err := store.KvBatchGet(ctx, append(keys, []byte("missing"))) + require.NoError(t, err) + require.Len(t, values, len(keys)) + require.Equal(t, "v1b", string(values[string(keys[1])])) + require.Equal(t, "v3", string(values[string(keys[3])])) + require.NoError(t, store.KvDelete(ctx, keys[0])) + _, err = store.KvGet(ctx, keys[0]) + require.True(t, errors.Is(err, filer.ErrKvNotFound), "deleted key: %v", err) + require.Equal(t, []string{"buckets", "root.txt", "topics"}, list(t, "/", "", false, 100, ""), "key-value records are not entries") + }) + + t.Run("transaction", func(t *testing.T) { + begin := func() context.Context { + txCtx, err := store.BeginTransaction(ctx) + require.NoError(t, err) + t.Cleanup(func() { store.RollbackTransaction(txCtx) }) + return txCtx + } + txCtx := begin() + require.NoError(t, store.InsertEntry(txCtx, file("/buckets/bkt1/rolledback", 1))) + require.NoError(t, store.RollbackTransaction(txCtx)) + notFound(t, "/buckets/bkt1/rolledback") + + txCtx = begin() + require.NoError(t, store.InsertEntry(txCtx, file("/buckets/bkt1/committed", 1))) + require.NoError(t, store.KvPut(txCtx, []byte("tx-key"), []byte("tx-value"))) + require.NoError(t, store.CommitTransaction(txCtx)) + _, err := store.FindEntry(ctx, "/buckets/bkt1/committed") + require.NoError(t, err) + v, err := store.KvGet(ctx, []byte("tx-key")) + require.NoError(t, err) + require.Equal(t, "tx-value", string(v)) + require.NoError(t, store.DeleteEntry(ctx, "/buckets/bkt1/committed")) + notFound(t, "/buckets/bkt1/committed") + }) + + if scanner, ok := store.(filer.EntryScanner); ok { + t.Run("scan", func(t *testing.T) { + scan := func(root string) map[string]bool { + seen := map[string]bool{} + require.NoError(t, scanner.ScanEntries(ctx, util.FullPath(root), func(_ context.Context, e *filer.Entry) error { + require.False(t, seen[string(e.FullPath)], "%s seen twice", e.FullPath) + seen[string(e.FullPath)] = true + return nil + })) + return seen + } + all := scan("/") + for _, p := range []string{"/buckets", "/buckets/bkt1", "/buckets/bkt1/a/b/y.txt", "/buckets/bkt2/top.txt", "/topics/t.log", "/root.txt", "/buckets/bkt1/order/日本"} { + require.True(t, all[p], "scan / misses %s", p) + } + for p := range all { + require.Equal(t, byte('/'), p[0], "scan / returned %q", p) + } + bkt1 := scan("/buckets/bkt1/a") + require.Equal(t, map[string]bool{"/buckets/bkt1/a": true, "/buckets/bkt1/a/x.txt": true, "/buckets/bkt1/a/b": true, "/buckets/bkt1/a/b/y.txt": true}, bkt1) + }) + } + + t.Run("delete folder children", func(t *testing.T) { + require.NoError(t, store.DeleteFolderChildren(ctx, "/buckets/bkt1/order")) + require.Empty(t, list(t, "/buckets/bkt1/order", "", false, 100, "")) + _, err := store.FindEntry(ctx, "/buckets/bkt1/a/x.txt") + require.NoError(t, err, "a sibling directory keeps its children") + _, err = store.FindEntry(ctx, "/buckets/bkt1/order") + require.NoError(t, err, "the folder itself stays") + }) + + if bucketAware, ok := store.(filer.BucketAware); ok && bucketAware.CanDropWholeBucket() { + t.Run("drop bucket", func(t *testing.T) { + require.NoError(t, store.DeleteFolderChildren(ctx, "/buckets/bkt2")) + for _, p := range []string{"/buckets/bkt2/top.txt", "/buckets/bkt2/sub", "/buckets/bkt2/sub/deep.txt"} { + notFound(t, p) + } + _, err := store.FindEntry(ctx, "/buckets/bkt1/top.txt") + require.NoError(t, err) + bucketAware.OnBucketDeletion("bkt2") + }) + } +} diff --git a/weed/filer/store_test/mysql.go b/weed/filer/store_test/mysql.go new file mode 100644 index 0000000000..506068e730 --- /dev/null +++ b/weed/filer/store_test/mysql.go @@ -0,0 +1,47 @@ +package store_test + +import ( + "database/sql" + "fmt" + "os" + "strconv" + "testing" + "time" + + _ "github.com/go-sql-driver/mysql" +) + +// MysqlTestDatabase is the MySQL a store test runs against. +type MysqlTestDatabase struct { + Host, User, Password, Database string + Port int +} + +// NewMysqlTestDatabase gives the test a database of its own, dropped after +// it. MYSQL_HOST, MYSQL_PORT, MYSQL_USER and MYSQL_PASSWORD override the defaults. +func NewMysqlTestDatabase(t *testing.T) MysqlTestDatabase { + if os.Getenv("RUN_MYSQL_TESTS") != "1" { + t.Skip("mysql tests are disabled. Start a mysql server and set RUN_MYSQL_TESTS=1 to enable, it defaults to root:root@127.0.0.1:3306.") + } + port, _ := strconv.Atoi(envOr("MYSQL_PORT", "3306")) + db := MysqlTestDatabase{ + Host: envOr("MYSQL_HOST", "127.0.0.1"), + Port: port, + User: envOr("MYSQL_USER", "root"), + Password: envOr("MYSQL_PASSWORD", "root"), + Database: fmt.Sprintf("test_%d", time.Now().UnixNano()), + } + conn, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/", db.User, db.Password, db.Host, db.Port)) + if err != nil { + t.Fatal(err) + } + if _, err := conn.Exec("CREATE DATABASE `" + db.Database + "`"); err != nil { + conn.Close() + t.Fatal(err) + } + t.Cleanup(func() { + conn.Exec("DROP DATABASE `" + db.Database + "`") + conn.Close() + }) + return db +} diff --git a/weed/filer/store_test/postgres.go b/weed/filer/store_test/postgres.go new file mode 100644 index 0000000000..fdb576523b --- /dev/null +++ b/weed/filer/store_test/postgres.go @@ -0,0 +1,52 @@ +package store_test + +import ( + "database/sql" + "fmt" + "os" + "strconv" + "testing" + "time" + + _ "github.com/jackc/pgx/v5/stdlib" + "github.com/seaweedfs/seaweedfs/weed/util/pgxutil" +) + +// NewPostgresTestDatabase gives the test a schema of its own, dropped after +// it. PGHOST, PGPORT, PGUSER, PGPASSWORD and PGDATABASE override the defaults. +func NewPostgresTestDatabase(t *testing.T) pgxutil.DSNOptions { + if os.Getenv("RUN_POSTGRES_TESTS") != "1" { + t.Skip("postgres tests are disabled. Start a postgres server and set RUN_POSTGRES_TESTS=1 to enable, it defaults to postgres:postgres@127.0.0.1:5432/postgres.") + } + port, _ := strconv.Atoi(envOr("PGPORT", "5432")) + opts := pgxutil.DSNOptions{ + Hostname: envOr("PGHOST", "127.0.0.1"), + Port: port, + User: envOr("PGUSER", "postgres"), + Password: envOr("PGPASSWORD", "postgres"), + Database: envOr("PGDATABASE", "postgres"), + SSLMode: "disable", + } + dsn, _ := pgxutil.BuildDSN(opts) + conn, err := sql.Open("pgx", dsn) + if err != nil { + t.Fatal(err) + } + opts.Schema = fmt.Sprintf("test_%d", time.Now().UnixNano()) + if _, err := conn.Exec(`CREATE SCHEMA "` + opts.Schema + `"`); err != nil { + conn.Close() + t.Fatal(err) + } + t.Cleanup(func() { + conn.Exec(`DROP SCHEMA "` + opts.Schema + `" CASCADE`) + conn.Close() + }) + return opts +} + +func envOr(name, fallback string) string { + if v := os.Getenv(name); v != "" { + return v + } + return fallback +}