Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,310 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// newArchiveTestAgent builds a minimal agent whose primary volumes root lives
|
||||
// in a temp dir, mirroring newTestAgent but with an upload limit so the
|
||||
// files/archive endpoint accepts request bodies.
|
||||
func newArchiveTestAgent(t *testing.T) (*Agent, string) {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
vol := filepath.Join(root, "servers")
|
||||
if err := os.MkdirAll(vol, 0o750); err != nil {
|
||||
t.Fatalf("mkdir volumes: %v", err)
|
||||
}
|
||||
return &Agent{dataRoot: root, volumesDir: vol, uploadLimit: 1 << 20}, vol
|
||||
}
|
||||
|
||||
func writeArchiveFixture(t *testing.T, serverDir string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Join(serverDir, "sub"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(serverDir, "a.txt"), []byte("alpha"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(serverDir, "sub", "b.txt"), []byte("beta"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func callFilesArchive(t *testing.T, a *Agent, name string, paths []string, destDir string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
body, err := json.Marshal(map[string]any{"paths": paths, "destDir": destDir})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := httptest.NewRequest(http.MethodPost, "/v1/servers/"+name+"/files/archive", bytes.NewReader(body))
|
||||
rec := httptest.NewRecorder()
|
||||
a.handleFilesArchive(rec, req, name)
|
||||
return rec
|
||||
}
|
||||
|
||||
func TestHandleFilesArchiveRefusesMissingSelectedSource(t *testing.T) {
|
||||
agent, volumes := newArchiveTestAgent(t)
|
||||
serverDir := filepath.Join(volumes, "alpha")
|
||||
writeArchiveFixture(t, serverDir)
|
||||
|
||||
response := callFilesArchive(t, agent, "alpha", []string{"a.txt", "missing.txt"}, "")
|
||||
if response.Code != http.StatusConflict {
|
||||
t.Errorf("expected a conflict for a stale selection, got %d: %s", response.Code, response.Body)
|
||||
}
|
||||
entries, err := os.ReadDir(serverDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if entry.Name() != "a.txt" && entry.Name() != "sub" {
|
||||
t.Errorf("failed archive left unexpected entry %q", entry.Name())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// readTarMembers decodes the produced archive and returns member names in
|
||||
// stream order plus the extracted content of each regular file. Any stream
|
||||
// misalignment (the old self-inclusion bug) surfaces as a tar read error.
|
||||
func readTarMembers(t *testing.T, path string) ([]string, map[string]string) {
|
||||
t.Helper()
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
t.Fatalf("open produced archive: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
gz, err := gzip.NewReader(f)
|
||||
if err != nil {
|
||||
t.Fatalf("gzip header: %v", err)
|
||||
}
|
||||
defer gz.Close()
|
||||
tr := tar.NewReader(gz)
|
||||
var names []string
|
||||
contents := map[string]string{}
|
||||
for {
|
||||
hdr, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("tar stream corrupt: %v", err)
|
||||
}
|
||||
names = append(names, hdr.Name)
|
||||
if hdr.Typeflag == tar.TypeReg {
|
||||
data, err := io.ReadAll(tr)
|
||||
if err != nil {
|
||||
t.Fatalf("read member %s: %v", hdr.Name, err)
|
||||
}
|
||||
contents[hdr.Name] = string(data)
|
||||
}
|
||||
}
|
||||
return names, contents
|
||||
}
|
||||
|
||||
func assertNoSelfArchiveMember(t *testing.T, names []string) {
|
||||
t.Helper()
|
||||
for _, n := range names {
|
||||
base := filepath.Base(strings.TrimSuffix(n, "/"))
|
||||
if strings.HasPrefix(base, "archive-") || strings.HasPrefix(base, ".archive-") {
|
||||
t.Fatalf("archive contains its own output as member %q", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// adpanel:archive-self-exclusion - the files/archive endpoint must never read
|
||||
// its own growing output back into the tar stream when the destination lives
|
||||
// inside the walked source, and the produced archive must stay extractable
|
||||
// with exactly the non-excluded members.
|
||||
func TestHandleFilesArchiveExcludesOwnOutput(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
paths []string
|
||||
destDir string
|
||||
wantMembers []string
|
||||
wantContents map[string]string
|
||||
}{
|
||||
{
|
||||
name: "dest inside walked root",
|
||||
paths: []string{"."},
|
||||
destDir: "archives",
|
||||
wantMembers: []string{"srv1/a.txt", "srv1/archives/", "srv1/sub/", "srv1/sub/b.txt"},
|
||||
wantContents: map[string]string{"srv1/a.txt": "alpha", "srv1/sub/b.txt": "beta"},
|
||||
},
|
||||
{
|
||||
name: "dest in subdir of walked root",
|
||||
paths: []string{"sub"},
|
||||
destDir: "sub/out",
|
||||
wantMembers: []string{"sub/", "sub/b.txt", "sub/out/"},
|
||||
wantContents: map[string]string{"sub/b.txt": "beta"},
|
||||
},
|
||||
{
|
||||
name: "dest is walked root itself",
|
||||
paths: []string{"."},
|
||||
destDir: ".",
|
||||
wantMembers: []string{"srv1/a.txt", "srv1/sub/", "srv1/sub/b.txt"},
|
||||
wantContents: map[string]string{"srv1/a.txt": "alpha", "srv1/sub/b.txt": "beta"},
|
||||
},
|
||||
{
|
||||
name: "dest outside walked source unchanged",
|
||||
paths: []string{"sub"},
|
||||
destDir: "archives",
|
||||
wantMembers: []string{"sub/", "sub/b.txt"},
|
||||
wantContents: map[string]string{"sub/b.txt": "beta"},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
a, vol := newArchiveTestAgent(t)
|
||||
serverDir := filepath.Join(vol, "srv1")
|
||||
writeArchiveFixture(t, serverDir)
|
||||
|
||||
rec := callFilesArchive(t, a, "srv1", tc.paths, tc.destDir)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("bad response json: %v", err)
|
||||
}
|
||||
if resp["ok"] != true {
|
||||
t.Fatalf("expected ok:true, got %v", resp)
|
||||
}
|
||||
archiveName, _ := resp["name"].(string)
|
||||
if !strings.HasPrefix(archiveName, "archive-") || !strings.HasSuffix(archiveName, ".tar.gz") {
|
||||
t.Fatalf("unexpected archive name %q", archiveName)
|
||||
}
|
||||
if want := filepath.Join(tc.destDir, archiveName); resp["path"] != want {
|
||||
t.Fatalf("response path = %v, want %q", resp["path"], want)
|
||||
}
|
||||
|
||||
destDir := filepath.Join(serverDir, filepath.FromSlash(tc.destDir))
|
||||
archivePath := filepath.Join(destDir, archiveName)
|
||||
if _, err := os.Stat(archivePath); err != nil {
|
||||
t.Fatalf("final archive missing: %v", err)
|
||||
}
|
||||
|
||||
// No in-flight temp file may survive in the destination.
|
||||
entries, _ := os.ReadDir(destDir)
|
||||
for _, e := range entries {
|
||||
if isArchiveTempName(e.Name()) {
|
||||
t.Fatalf("temp archive left behind: %s", e.Name())
|
||||
}
|
||||
}
|
||||
|
||||
names, contents := readTarMembers(t, archivePath)
|
||||
assertNoSelfArchiveMember(t, names)
|
||||
if got, want := strings.Join(names, ","), strings.Join(tc.wantMembers, ","); got != want {
|
||||
t.Fatalf("members = [%s], want [%s]", got, want)
|
||||
}
|
||||
for member, want := range tc.wantContents {
|
||||
if got := contents[member]; got != want {
|
||||
t.Fatalf("member %s content corrupted: %q", member, got)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleFilesArchiveRestoresEmptyFolder(t *testing.T) {
|
||||
agent, volumes := newArchiveTestAgent(t)
|
||||
serverDir := filepath.Join(volumes, "srv1")
|
||||
if err := os.MkdirAll(filepath.Join(serverDir, "empty"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
archive := callFilesArchive(t, agent, "srv1", []string{"empty"}, "restore")
|
||||
if archive.Code != http.StatusOK {
|
||||
t.Fatalf("archive status = %d: %s", archive.Code, archive.Body.String())
|
||||
}
|
||||
var result struct{ Name string }
|
||||
if err := json.Unmarshal(archive.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
extract := fsPost(t, agent.handleFSExtract, "/v1/fs/extract", map[string]any{
|
||||
"path": filepath.Join(serverDir, "restore", result.Name), "server": "srv1",
|
||||
})
|
||||
if extract.Code != http.StatusOK {
|
||||
t.Fatalf("extract status = %d: %s", extract.Code, extract.Body.String())
|
||||
}
|
||||
if info, err := os.Stat(filepath.Join(serverDir, "restore", "empty")); err != nil || !info.IsDir() {
|
||||
t.Fatalf("empty directory not restored: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Only the CURRENT run's output is excluded; archives produced by earlier runs
|
||||
// keep getting included like any other file.
|
||||
func TestHandleFilesArchiveIncludesPreviousArchives(t *testing.T) {
|
||||
a, vol := newArchiveTestAgent(t)
|
||||
serverDir := filepath.Join(vol, "srv1")
|
||||
writeArchiveFixture(t, serverDir)
|
||||
dest := filepath.Join(serverDir, "archives")
|
||||
if err := os.MkdirAll(dest, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prev := filepath.Join(dest, "archive-20250101-000000.tar.gz")
|
||||
if err := os.WriteFile(prev, []byte("old archive"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rec := callFilesArchive(t, a, "srv1", []string{"archives"}, "archives")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
var resp map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("bad response json: %v", err)
|
||||
}
|
||||
archiveName, _ := resp["name"].(string)
|
||||
|
||||
names, contents := readTarMembers(t, filepath.Join(dest, archiveName))
|
||||
// No in-flight temp file may leak into the archive; the previous run's
|
||||
// archive must be there like any other file.
|
||||
for _, n := range names {
|
||||
if isArchiveTempName(filepath.Base(strings.TrimSuffix(n, "/"))) {
|
||||
t.Fatalf("temp archive leaked into member list: %q", n)
|
||||
}
|
||||
}
|
||||
if got, want := strings.Join(names, ","), "archives/,archives/archive-20250101-000000.tar.gz"; got != want {
|
||||
t.Fatalf("members = [%s], want [%s]", got, want)
|
||||
}
|
||||
if got := contents["archives/archive-20250101-000000.tar.gz"]; got != "old archive" {
|
||||
t.Fatalf("previous archive content corrupted: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// The pre-archive storage quota must still reject the request before any
|
||||
// archive or temp file is created.
|
||||
func TestHandleFilesArchiveQuotaStillEnforced(t *testing.T) {
|
||||
a, vol := newArchiveTestAgent(t)
|
||||
serverDir := filepath.Join(vol, "srv1")
|
||||
writeArchiveFixture(t, serverDir)
|
||||
if err := os.WriteFile(filepath.Join(serverDir, "big.bin"), make([]byte, 2<<20), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.MkdirAll(a.metaDir(), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(a.metaPath("srv1"), []byte(`{"resources": {"storageMb": 1}}`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rec := callFilesArchive(t, a, "srv1", []string{"."}, "archives")
|
||||
if rec.Code != http.StatusInsufficientStorage {
|
||||
t.Fatalf("expected 507, got %d: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "disk_limit_exceeded") {
|
||||
t.Fatalf("expected disk_limit_exceeded, got %s", rec.Body.String())
|
||||
}
|
||||
dest := filepath.Join(serverDir, "archives")
|
||||
if entries, _ := os.ReadDir(dest); len(entries) != 0 {
|
||||
t.Fatalf("destination touched despite quota rejection: %d entries", len(entries))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user