Files
s3/weed/server/volume_grpc_copy_writefile_test.go
zooqueen dd74d3e6d6 Replace AGPL MinIO fork with Apache-2.0 SeaweedFS fork; rebrand CLI weed->s3
hanzoai/s3 was an AGPL-3.0 MinIO fork whose network-copyleft blocks embedding
and reselling as a managed service. Replace it wholesale with a fork of
SeaweedFS (github.com/seaweedfs/seaweedfs, Apache-2.0, Go) which ships a
built-in S3 gateway + Filer.

- License: AGPL-3.0 -> Apache-2.0 (upstream LICENSE kept verbatim, incl.
  "Copyright 2025 Chris Lu"); NOTICE added crediting SeaweedFS. No AGPL/BUSL/
  SSPL/GPL or enterprise ("ee") files remain.
- Module: go.mod path set to github.com/hanzoai/s3 with a self-replace
  (replace github.com/seaweedfs/seaweedfs => ./) so the ~1800 internal Go files
  keep their upstream import paths with zero churn. Internal identifiers and
  import paths are intentionally NOT renamed.
- CLI rebrand: the binary is built as `s3` (was `weed`); root usage, per-command
  help/usage/flag strings, shell autocomplete program name, and the product name
  in CLI output now read "Hanzo S3" / `s3`. Users never see `weed`.
  (e.g. `s3 server`, `s3 filer`, `s3 master`, `s3 volume`, `s3 s3`, `s3 mount`.)
- Build: `make` (or `go build -o s3 ./weed`) builds the `s3` binary green;
  `cmd/weed-db` and `cmd/weed-sql` also build; `go vet ./weed/command/` clean.
- README/LLM.md rewritten for Hanzo S3; AGENTS.md/CLAUDE.md symlinks preserved.
- Dropped a 33MB prebuilt Mach-O `stress` binary upstream had committed; `.gitignore`
  updated to ignore the `s3` build output.
2026-06-18 23:41:25 -07:00

205 lines
6.5 KiB
Go

package weed_server
import (
"context"
"errors"
"io"
"os"
"path/filepath"
"testing"
"google.golang.org/grpc/metadata"
"github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// fakeCopyFileStream is a synthetic VolumeServer_CopyFileClient used to
// drive writeToFile's failure paths in tests. The pre-fix code left a
// partial / 0-byte destination file on the disk when the stream errored
// mid-copy; with the fix, writeToFile now removes the incomplete file
// so callers (notably VolumeEcShardsCopy distributing .ecx) don't end
// up with stubs that mount-time code mistakes for valid empty indexes.
type fakeCopyFileStream struct {
responses []*volume_server_pb.CopyFileResponse
finalErr error
index int
}
func (s *fakeCopyFileStream) Recv() (*volume_server_pb.CopyFileResponse, error) {
if s.index >= len(s.responses) {
if s.finalErr != nil {
return nil, s.finalErr
}
return nil, io.EOF
}
r := s.responses[s.index]
s.index++
return r, nil
}
func (s *fakeCopyFileStream) Header() (metadata.MD, error) { return metadata.MD{}, nil }
func (s *fakeCopyFileStream) Trailer() metadata.MD { return metadata.MD{} }
func (s *fakeCopyFileStream) CloseSend() error { return nil }
func (s *fakeCopyFileStream) Context() context.Context { return context.Background() }
func (s *fakeCopyFileStream) SendMsg(any) error { return nil }
func (s *fakeCopyFileStream) RecvMsg(any) error { return nil }
func TestWriteToFile_RemovesPartialFileOnStreamError(t *testing.T) {
dir := t.TempDir()
dst := filepath.Join(dir, "vol_42.ecx")
stream := &fakeCopyFileStream{
responses: []*volume_server_pb.CopyFileResponse{
// Real bytes flow first — modifiedTsNs is non-zero so the
// existing "source file not found" cleanup at the bottom of
// writeToFile does NOT fire; the new mid-stream cleanup is
// the only path that can remove the file.
{FileContent: []byte("partial data"), ModifiedTsNs: 1234567890},
},
finalErr: errors.New("simulated mid-stream failure"),
}
_, err := writeToFile(stream, dst, util.NewWriteThrottler(0), false, false, nil)
if err == nil {
t.Fatalf("writeToFile should propagate the stream error")
}
if _, statErr := os.Stat(dst); !os.IsNotExist(statErr) {
t.Errorf("incomplete file should be removed; stat err = %v", statErr)
}
}
func TestWriteToFile_RemovesEmptyFileOnImmediateStreamError(t *testing.T) {
dir := t.TempDir()
dst := filepath.Join(dir, "vol_42.ecx")
stream := &fakeCopyFileStream{
// No FileContent at all; stream errors on the first Recv.
// progressedBytes == 0 and modifiedTsNs == 0, so without the
// mid-stream cleanup this would leave a 0-byte file from the
// O_TRUNC at OpenFile time.
finalErr: errors.New("simulated immediate failure"),
}
_, err := writeToFile(stream, dst, util.NewWriteThrottler(0), false, false, nil)
if err == nil {
t.Fatalf("writeToFile should propagate the stream error")
}
if _, statErr := os.Stat(dst); !os.IsNotExist(statErr) {
t.Errorf("0-byte file should be removed; stat err = %v", statErr)
}
}
func TestWriteToFile_PreservesAppendModeOnError(t *testing.T) {
dir := t.TempDir()
dst := filepath.Join(dir, "vol_42.ecj")
// Pre-existing content the caller owns — isAppend=true tells
// writeToFile not to touch it on cleanup.
if err := os.WriteFile(dst, []byte("pre-existing journal data"), 0o644); err != nil {
t.Fatalf("seed file: %v", err)
}
stream := &fakeCopyFileStream{
finalErr: errors.New("simulated failure"),
}
_, err := writeToFile(stream, dst, util.NewWriteThrottler(0), true, false, nil)
if err == nil {
t.Fatalf("writeToFile should propagate the stream error")
}
info, statErr := os.Stat(dst)
if statErr != nil {
t.Fatalf("append-mode file should be preserved on error; stat: %v", statErr)
}
if info.Size() == 0 {
t.Errorf("append-mode file unexpectedly truncated to 0 bytes")
}
}
func TestWriteToFile_SucceedsOnCleanStream(t *testing.T) {
dir := t.TempDir()
dst := filepath.Join(dir, "vol_42.ecx")
want := []byte("hello ecx index")
stream := &fakeCopyFileStream{
responses: []*volume_server_pb.CopyFileResponse{
{FileContent: want, ModifiedTsNs: 9},
},
}
if _, err := writeToFile(stream, dst, util.NewWriteThrottler(0), false, false, nil); err != nil {
t.Fatalf("writeToFile failed on clean stream: %v", err)
}
got, err := os.ReadFile(dst)
if err != nil {
t.Fatalf("read back: %v", err)
}
if string(got) != string(want) {
t.Errorf("contents = %q, want %q", got, want)
}
}
// An optional copy whose source lacks the file must leave a valid destination intact.
func TestWriteToFile_PreservesDestinationWhenOptionalSourceMissing(t *testing.T) {
dir := t.TempDir()
dst := filepath.Join(dir, "vol_42.ecsum")
original := []byte("valid existing ecsum sidecar")
if err := os.WriteFile(dst, original, 0o644); err != nil {
t.Fatalf("seed file: %v", err)
}
stream := &fakeCopyFileStream{} // no responses -> source file absent
if _, err := writeToFile(stream, dst, util.NewWriteThrottler(0), false, true, nil); err != nil {
t.Fatalf("writeToFile should not error when an optional source is absent: %v", err)
}
got, err := os.ReadFile(dst)
if err != nil {
t.Fatalf("destination sidecar should be preserved; read: %v", err)
}
if string(got) != string(original) {
t.Errorf("destination overwritten/truncated: got %q, want %q", got, original)
}
if _, statErr := os.Stat(dst + ".copying"); !os.IsNotExist(statErr) {
t.Errorf("temp .copying file should be cleaned up; stat err = %v", statErr)
}
}
// An optional copy whose source has the file commits it over any stale destination.
func TestWriteToFile_CommitsOptionalCopyWhenSourcePresent(t *testing.T) {
dir := t.TempDir()
dst := filepath.Join(dir, "vol_42.ecsum")
if err := os.WriteFile(dst, []byte("stale"), 0o644); err != nil {
t.Fatalf("seed file: %v", err)
}
want := []byte("fresh ecsum from source")
stream := &fakeCopyFileStream{
responses: []*volume_server_pb.CopyFileResponse{
{FileContent: want, ModifiedTsNs: 42},
},
}
if _, err := writeToFile(stream, dst, util.NewWriteThrottler(0), false, true, nil); err != nil {
t.Fatalf("writeToFile failed on present optional source: %v", err)
}
got, err := os.ReadFile(dst)
if err != nil {
t.Fatalf("read back: %v", err)
}
if string(got) != string(want) {
t.Errorf("contents = %q, want %q", got, want)
}
if _, statErr := os.Stat(dst + ".copying"); !os.IsNotExist(statErr) {
t.Errorf("temp .copying file should be renamed away; stat err = %v", statErr)
}
}