Background jobs run for each active team on its own Server and under its own locks, a few teams at a time and from a different team each round, so the work is shared between copies of the app and no team holds up another. Each team's files are under its own prefix, and an OAuth sign-in comes back only to the team that started it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
210 lines
5.9 KiB
Go
210 lines
5.9 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"supermarket/internal/config"
|
|
)
|
|
|
|
// TestS3 names an S3 bucket to test on, as
|
|
// http://ACCESS_KEY:SECRET_KEY@host:port/bucket, e.g. a Garage in Docker.
|
|
// Each run uses its own prefix in it, and empties it afterwards.
|
|
const TestS3 = "SUPERMARKET_TEST_S3"
|
|
|
|
func stores(t *testing.T) map[string]Store {
|
|
t.Helper()
|
|
out := map[string]Store{}
|
|
d, err := OpenDir(t.TempDir())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out["folder"] = d
|
|
if raw := os.Getenv(TestS3); raw != "" {
|
|
u, err := url.Parse(raw)
|
|
if err != nil || u.User == nil {
|
|
t.Fatalf("%s: %v", TestS3, err)
|
|
}
|
|
pw, _ := u.User.Password()
|
|
s, err := OpenS3(config.Storage{
|
|
Endpoint: u.Scheme + "://" + u.Host, Bucket: strings.Trim(u.Path, "/"), Region: "garage",
|
|
Prefix: fmt.Sprintf("test-%d-%d", os.Getpid(), time.Now().UnixNano()),
|
|
AccessKey: u.User.Username(), SecretKey: pw,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
ctx := context.Background()
|
|
all, _ := s.List(ctx, "")
|
|
for _, f := range all {
|
|
s.Delete(ctx, f.Key)
|
|
}
|
|
})
|
|
out["s3"] = s
|
|
}
|
|
return out
|
|
}
|
|
|
|
func read(t *testing.T, st Store, key string) string {
|
|
t.Helper()
|
|
r, err := st.Get(context.Background(), key)
|
|
if err != nil {
|
|
t.Fatalf("get %s: %v", key, err)
|
|
}
|
|
defer r.Close()
|
|
b, err := io.ReadAll(r)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
// Every store behaves the same.
|
|
func TestStores(t *testing.T) {
|
|
ctx := context.Background()
|
|
for name, st := range stores(t) {
|
|
t.Run(name, func(t *testing.T) {
|
|
if err := Check(ctx, st); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.Put(ctx, "exports/a.txt", strings.NewReader("first"), 5); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.Put(ctx, "exports/a.txt", strings.NewReader("second, longer"), -1); err != nil { // replaced; size unknown
|
|
t.Fatal(err)
|
|
}
|
|
st.Put(ctx, "exports/2026/b.bin", strings.NewReader(strings.Repeat("x", 70000)), 70000)
|
|
st.Put(ctx, "uploads/c.png", strings.NewReader("png"), 3)
|
|
if got := read(t, st, "exports/a.txt"); got != "second, longer" {
|
|
t.Errorf("read %q", got)
|
|
}
|
|
info, err := st.Stat(ctx, "exports/2026/b.bin")
|
|
if err != nil || info.Size != 70000 || info.Key != "exports/2026/b.bin" || time.Since(info.Modified) > time.Hour {
|
|
t.Errorf("stat: %+v %v", info, err)
|
|
}
|
|
list, err := st.List(ctx, "exports/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var keys []string
|
|
for _, f := range list {
|
|
keys = append(keys, f.Key)
|
|
}
|
|
if strings.Join(keys, " ") != "exports/2026/b.bin exports/a.txt" {
|
|
t.Errorf("list: %v", keys)
|
|
}
|
|
if all, _ := st.List(ctx, ""); len(all) != 3 {
|
|
t.Errorf("everything: %v (the check file should be gone)", all)
|
|
}
|
|
|
|
// Missing files.
|
|
if _, err := st.Get(ctx, "exports/none"); !errors.Is(err, ErrNotExist) {
|
|
t.Errorf("get missing: %v", err)
|
|
}
|
|
if _, err := st.Stat(ctx, "exports"); !errors.Is(err, ErrNotExist) { // a folder isn't a file
|
|
t.Errorf("stat folder: %v", err)
|
|
}
|
|
if err := st.Delete(ctx, "exports/a.txt"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.Delete(ctx, "exports/a.txt"); err != nil {
|
|
t.Errorf("deleting twice: %v", err)
|
|
}
|
|
if _, err := st.Stat(ctx, "exports/a.txt"); !errors.Is(err, ErrNotExist) {
|
|
t.Errorf("after delete: %v", err)
|
|
}
|
|
|
|
// Names that would reach outside the store.
|
|
for _, bad := range []string{"", "/etc/passwd", "../x", "a/../../x", "a//b", "a/./b", "a/", `a\b`, ".."} {
|
|
if err := st.Put(ctx, bad, strings.NewReader("x"), 1); err == nil {
|
|
t.Errorf("put %q accepted", bad)
|
|
}
|
|
}
|
|
if _, err := st.List(ctx, "../"); err == nil {
|
|
t.Error("list ../ accepted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A folder store's files are the user's only, and a half-written file is
|
|
// never seen.
|
|
func TestDirFiles(t *testing.T) {
|
|
root := t.TempDir() + "/files"
|
|
d, err := OpenDir(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d.Put(context.Background(), "a/b.txt", strings.NewReader("x"), 1)
|
|
for _, p := range []string{root, root + "/a", root + "/a/b.txt"} {
|
|
fi, err := os.Stat(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if fi.Mode().Perm()&0o077 != 0 {
|
|
t.Errorf("%s is %v", p, fi.Mode().Perm())
|
|
}
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
if err := d.Put(ctx, "a/c.txt", strings.NewReader("never"), 5); err == nil {
|
|
t.Error("a cancelled write went through")
|
|
}
|
|
if list, _ := d.List(context.Background(), ""); len(list) != 1 {
|
|
t.Errorf("left behind: %v", list)
|
|
}
|
|
d.Delete(context.Background(), "a/b.txt")
|
|
if _, err := os.Stat(root + "/a"); !os.IsNotExist(err) {
|
|
t.Error("the empty folder stayed")
|
|
}
|
|
}
|
|
|
|
// A wrong key is caught by Check, saying where.
|
|
func TestS3WrongKey(t *testing.T) {
|
|
raw := os.Getenv(TestS3)
|
|
if raw == "" {
|
|
t.Skip("set " + TestS3)
|
|
}
|
|
u, _ := url.Parse(raw)
|
|
s, err := OpenS3(config.Storage{Endpoint: u.Scheme + "://" + u.Host, Bucket: strings.Trim(u.Path, "/"), Region: "garage",
|
|
AccessKey: u.User.Username(), SecretKey: "wrong"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := Check(context.Background(), s); err == nil || !strings.Contains(err.Error(), "writing to s3://") {
|
|
t.Fatalf("check: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestUnder(t *testing.T) {
|
|
base, err := Open(config.Storage{Dir: t.TempDir()})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := context.Background()
|
|
a, b := Under(base, "teams/1"), Under(base, "teams/2/")
|
|
if err := a.Put(ctx, "photos/x.png", strings.NewReader("A"), 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := b.Stat(ctx, "photos/x.png"); err == nil {
|
|
t.Error("team 2 sees team 1's file")
|
|
}
|
|
if i, err := base.Stat(ctx, "teams/1/photos/x.png"); err != nil || i.Size != 1 {
|
|
t.Errorf("in the store: %+v %v", i, err)
|
|
}
|
|
if list, _ := a.List(ctx, "photos/"); len(list) != 1 || list[0].Key != "photos/x.png" {
|
|
t.Errorf("list %+v", list)
|
|
}
|
|
if err := a.Put(ctx, "../2/photos/y.png", strings.NewReader("x"), 1); err == nil {
|
|
t.Error("reached outside its prefix")
|
|
}
|
|
}
|