mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* test/postgres_flat: S3 against a filer on the postgres_flat store A weed mini whose filer.toml enables postgres_flat in a schema of its own. Objects, listings, copies, multipart uploads, versions and bucket deletion are checked through S3 and against the rows of the bucket's table. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * test/postgres_flat: a FUSE mount against the postgres_flat store Files, a directory longer than a listing page, renames, hard and symbolic links, tree removal and a bucket made through the mount, checked against the rows, then read back by a second mount with an empty metadata cache. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * ci: run the postgres_flat S3 and FUSE tests against Postgres Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * ci: run the postgres_flat tests on pull requests to any branch Like the S3 workflows, so a stacked pull request runs them too, and only when the code they reach changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * test/postgres_flat: fail fast when weed mini or the mount exits ProcessState is only set by Wait, which ran only at cleanup, so a mini or mount that died at startup was noticed only at the 45s or 90s deadline. Reap each command as it starts and check for its exit while waiting. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * ci: run the postgres_flat tests on changes anywhere under weed/ The tests run master, volume, filer, S3 and a mount, so changes in packages such as weed/pb, weed/wdclient or weed/credential reach them too. Match the FUSE integration workflow and also trigger on the fusermount action the job uses. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test/postgres_flat: versioned buckets on the postgres_flat store Versions and delete markers are rows under the key's .versions folder in the bucket's table. Covers version listings in one page and one version a page, delete markers, promoting the previous version, the null version under suspended versioning, and dropping the table once every version and the bucket are deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * ci: run the S3 versioning suite on a postgres_flat filer The whole test/s3/versioning suite, stress tests included, against a weed mini whose filer.toml enables postgres_flat. The job first checks that filemeta has the flat layout, so a filer on its default store cannot pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * test/postgres_flat: set up shared objects before the subtests The S3 and versioning suites wrote their objects and the second bucket inside the first subtest, so running one later subtest with -run left the version ids empty and panicked. The parent test now writes them and the subtests only check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test/postgres_flat: helpers fail the test that calls them paths, tableExists, count, filerList, start and mount take the caller's *testing.T, so a failure inside a subtest fails that subtest instead of calling FailNow on the parent and stopping its siblings. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011UpvafvK8JpagF94MHWqwK * ci: wait for the filemeta table before the versioning suite Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test/postgres_flat: POSIX subtests set up their own fixtures Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test/postgres_flat: absent versions must answer 404 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
238 lines
8.2 KiB
Go
238 lines
8.2 KiB
Go
//go:build linux
|
|
|
|
package postgres_flat
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// mount mounts the filer root with a metadata cache of its own, so a new
|
|
// mount reads everything back from the store.
|
|
func (c *cluster) mount(t *testing.T, name string) string {
|
|
t.Helper()
|
|
if _, err := os.Stat("/dev/fuse"); err != nil {
|
|
t.Skip("no /dev/fuse")
|
|
}
|
|
dir := filepath.Join(c.baseDir, name)
|
|
cacheDir := filepath.Join(c.baseDir, name+"-cache")
|
|
require.NoError(t, os.MkdirAll(dir, 0o755))
|
|
require.NoError(t, os.MkdirAll(cacheDir, 0o755))
|
|
done := c.start(t, name, "mount", "-filer="+c.filerAddr, "-dir="+dir, "-filer.path=/", "-cacheDir="+cacheDir, "-allowOthers=false")
|
|
t.Cleanup(func() {
|
|
_ = exec.Command("fusermount3", "-u", dir).Run()
|
|
_ = exec.Command("fusermount", "-u", dir).Run()
|
|
})
|
|
|
|
var parent syscall.Stat_t
|
|
require.NoError(t, syscall.Stat(c.baseDir, &parent))
|
|
probe := filepath.Join(dir, ".probe-"+name)
|
|
deadline := time.Now().Add(90 * time.Second)
|
|
for {
|
|
var st syscall.Stat_t
|
|
if syscall.Stat(dir, &st) == nil && st.Dev != parent.Dev {
|
|
if err := os.WriteFile(probe, []byte("probe"), 0o644); err == nil {
|
|
require.NoError(t, os.Remove(probe))
|
|
return dir
|
|
}
|
|
}
|
|
if exited(done) || time.Now().After(deadline) {
|
|
t.Fatalf("mount %s not ready:\n%s", name, c.logTail(name))
|
|
}
|
|
time.Sleep(200 * time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func unmount(t *testing.T, dir string) {
|
|
t.Helper()
|
|
out, err := exec.Command("fusermount3", "-u", dir).CombinedOutput()
|
|
if err != nil {
|
|
out, err = exec.Command("fusermount", "-u", dir).CombinedOutput()
|
|
}
|
|
require.NoError(t, err, "unmount %s: %s", dir, out)
|
|
}
|
|
|
|
// tree lists every file under root with its content, and every directory
|
|
// with a trailing slash.
|
|
func tree(t *testing.T, root string) map[string]string {
|
|
t.Helper()
|
|
got := map[string]string{}
|
|
require.NoError(t, filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil || p == root {
|
|
return err
|
|
}
|
|
rel, _ := filepath.Rel(root, p)
|
|
switch {
|
|
case d.IsDir():
|
|
got[rel+"/"] = ""
|
|
case d.Type()&fs.ModeSymlink != 0:
|
|
target, err := os.Readlink(p)
|
|
got[rel+" -> "+target] = ""
|
|
return err
|
|
default:
|
|
data, err := os.ReadFile(p)
|
|
got[rel] = string(data)
|
|
return err
|
|
}
|
|
return nil
|
|
}))
|
|
return got
|
|
}
|
|
|
|
func TestPosix(t *testing.T) {
|
|
c := startCluster(t)
|
|
mnt := c.mount(t, "mount")
|
|
client := c.s3Client()
|
|
root := filepath.Join(mnt, "posix")
|
|
require.NoError(t, os.MkdirAll(root, 0o755))
|
|
write := func(t *testing.T, rel, body string) {
|
|
t.Helper()
|
|
p := filepath.Join(root, rel)
|
|
require.NoError(t, os.MkdirAll(filepath.Dir(p), 0o755))
|
|
require.NoError(t, os.WriteFile(p, []byte(body), 0o644))
|
|
}
|
|
read := func(t *testing.T, rel string) string {
|
|
t.Helper()
|
|
data, err := os.ReadFile(filepath.Join(root, rel))
|
|
require.NoError(t, err)
|
|
return string(data)
|
|
}
|
|
kvRows := func(t *testing.T) int { return c.count(t, `SELECT count(*) FROM filemeta_kv`) }
|
|
|
|
t.Run("files and directories are rows keyed by their path", func(t *testing.T) {
|
|
write(t, "a/b/f.txt", "f")
|
|
write(t, "a/g.txt", "g")
|
|
require.Equal(t, []string{"/posix", "/posix/a", "/posix/a/b", "/posix/a/b/f.txt", "/posix/a/g.txt"}, c.paths(t, "filemeta", "/posix"))
|
|
require.NoError(t, os.WriteFile(filepath.Join(root, "a/g.txt"), []byte("g2"), 0o644))
|
|
require.Equal(t, "g2", read(t, "a/g.txt"))
|
|
require.NoError(t, os.Chmod(filepath.Join(root, "a/g.txt"), 0o600))
|
|
st, err := os.Stat(filepath.Join(root, "a/g.txt"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, os.FileMode(0o600), st.Mode().Perm())
|
|
})
|
|
|
|
t.Run("a directory longer than a listing page", func(t *testing.T) {
|
|
require.NoError(t, os.Mkdir(filepath.Join(root, "many"), 0o755))
|
|
var want []string
|
|
for i := 0; i < filer.PaginationSize+100; i++ {
|
|
name := fmt.Sprintf("f%05d", i)
|
|
want = append(want, name)
|
|
f, err := os.Create(filepath.Join(root, "many", name))
|
|
require.NoError(t, err)
|
|
require.NoError(t, f.Close())
|
|
}
|
|
entries, err := os.ReadDir(filepath.Join(root, "many"))
|
|
require.NoError(t, err)
|
|
var got []string
|
|
for _, e := range entries {
|
|
got = append(got, e.Name())
|
|
}
|
|
require.Equal(t, want, got)
|
|
require.Equal(t, want, c.filerList(t, "/posix/many", 100))
|
|
require.Equal(t, len(want), c.count(t, `SELECT count(*) FROM filemeta WHERE directory = '/posix/many'`))
|
|
})
|
|
|
|
t.Run("rename moves rows", func(t *testing.T) {
|
|
write(t, "mv/src/one.txt", "one")
|
|
write(t, "mv/src/sub/two.txt", "two")
|
|
require.NoError(t, os.Rename(filepath.Join(root, "mv/src/one.txt"), filepath.Join(root, "mv/uno.txt")))
|
|
require.Equal(t, "one", read(t, "mv/uno.txt"))
|
|
require.NoError(t, os.Rename(filepath.Join(root, "mv/src"), filepath.Join(root, "mv/dst")))
|
|
require.Equal(t, "two", read(t, "mv/dst/sub/two.txt"))
|
|
require.Equal(t, []string{"/posix/mv", "/posix/mv/dst", "/posix/mv/dst/sub", "/posix/mv/dst/sub/two.txt", "/posix/mv/uno.txt"}, c.paths(t, "filemeta", "/posix/mv"))
|
|
})
|
|
|
|
t.Run("hard links keep their record in the key-value table", func(t *testing.T) {
|
|
before := kvRows(t)
|
|
write(t, "links/orig.txt", "linked")
|
|
require.NoError(t, os.Link(filepath.Join(root, "links/orig.txt"), filepath.Join(root, "links/hard.txt")))
|
|
require.Greater(t, kvRows(t), before)
|
|
require.Equal(t, "linked", read(t, "links/hard.txt"))
|
|
st, err := os.Stat(filepath.Join(root, "links/hard.txt"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, uint64(2), uint64(st.Sys().(*syscall.Stat_t).Nlink))
|
|
require.NoError(t, os.Remove(filepath.Join(root, "links/orig.txt")))
|
|
require.Equal(t, "linked", read(t, "links/hard.txt"))
|
|
|
|
require.NoError(t, os.Symlink("hard.txt", filepath.Join(root, "links/soft")))
|
|
target, err := os.Readlink(filepath.Join(root, "links/soft"))
|
|
require.NoError(t, err)
|
|
require.Equal(t, "hard.txt", target)
|
|
require.Equal(t, "linked", read(t, "links/soft"))
|
|
})
|
|
|
|
t.Run("remove a tree", func(t *testing.T) {
|
|
write(t, "gone/x/y/z.txt", "z")
|
|
write(t, "gone/w.txt", "w")
|
|
write(t, "gone2/kept.txt", "kept")
|
|
require.NoError(t, os.RemoveAll(filepath.Join(root, "gone")))
|
|
require.Empty(t, c.paths(t, "filemeta", "/posix/gone/"))
|
|
require.Equal(t, []string{"/posix/gone2", "/posix/gone2/kept.txt"}, c.paths(t, "filemeta", "/posix/gone"))
|
|
})
|
|
|
|
t.Run("bucket made through the mount", func(t *testing.T) {
|
|
bucket := "pgflat-posix"
|
|
bucketDir := filepath.Join(mnt, "buckets", bucket)
|
|
require.NoError(t, os.Mkdir(bucketDir, 0o755))
|
|
eventually(t, "the bucket has a table", func() bool { return c.tableExists(t, bucket) })
|
|
require.NoError(t, os.MkdirAll(filepath.Join(bucketDir, "k"), 0o755))
|
|
require.NoError(t, os.WriteFile(filepath.Join(bucketDir, "k/v.txt"), []byte("from mount"), 0o644))
|
|
require.Equal(t, []string{"/k", "/k/v.txt"}, c.paths(t, bucket, "/"))
|
|
|
|
ctx := context.Background()
|
|
out, err := client.GetObject(ctx, &s3.GetObjectInput{Bucket: aws.String(bucket), Key: aws.String("k/v.txt")})
|
|
require.NoError(t, err)
|
|
data, err := io.ReadAll(out.Body)
|
|
out.Body.Close()
|
|
require.NoError(t, err)
|
|
require.Equal(t, "from mount", string(data))
|
|
|
|
_, err = client.PutObject(ctx, &s3.PutObjectInput{Bucket: aws.String(bucket), Key: aws.String("k/s3.txt"), Body: strings.NewReader("from s3")})
|
|
require.NoError(t, err)
|
|
eventually(t, "the mount sees the S3 object", func() bool {
|
|
data, err := os.ReadFile(filepath.Join(bucketDir, "k/s3.txt"))
|
|
return err == nil && string(data) == "from s3"
|
|
})
|
|
|
|
require.NoError(t, os.RemoveAll(bucketDir))
|
|
eventually(t, "the bucket table is dropped", func() bool { return !c.tableExists(t, bucket) })
|
|
})
|
|
|
|
t.Run("a new mount reads the same tree from the store", func(t *testing.T) {
|
|
write(t, "remount/sub/deep.txt", "deep")
|
|
require.NoError(t, os.Symlink("sub/deep.txt", filepath.Join(root, "remount/link")))
|
|
want := tree(t, root)
|
|
require.Contains(t, want, "remount/sub/deep.txt")
|
|
require.Contains(t, want, "remount/link -> sub/deep.txt")
|
|
unmount(t, mnt)
|
|
fresh := c.mount(t, "remount")
|
|
got := tree(t, filepath.Join(fresh, "posix"))
|
|
require.Equal(t, keys(want), keys(got))
|
|
require.Equal(t, want, got)
|
|
})
|
|
}
|
|
|
|
func keys(m map[string]string) []string {
|
|
var ks []string
|
|
for k := range m {
|
|
ks = append(ks, k)
|
|
}
|
|
sort.Strings(ks)
|
|
return ks
|
|
}
|