309 lines
9.8 KiB
Go
309 lines
9.8 KiB
Go
package nas
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// fakeConnector stands in for a platform strategy so the service's own
|
|
// behaviour — probing, path resolution, teardown — can be tested without a NAS.
|
|
type fakeConnector struct {
|
|
basePath string
|
|
attachErr error
|
|
owned bool
|
|
attachCount int
|
|
detachCount int
|
|
detached Attachment
|
|
}
|
|
|
|
func (f *fakeConnector) Name() string { return "fake" }
|
|
|
|
func (f *fakeConnector) Attach(context.Context, Location, NASConfig) (Attachment, error) {
|
|
f.attachCount++
|
|
if f.attachErr != nil {
|
|
return Attachment{}, f.attachErr
|
|
}
|
|
return Attachment{BasePath: f.basePath, Owned: f.owned, Notes: []string{"fake attachment"}}, nil
|
|
}
|
|
|
|
func (f *fakeConnector) Detach(_ context.Context, att Attachment) error {
|
|
f.detachCount++
|
|
f.detached = att
|
|
return nil
|
|
}
|
|
|
|
func newTestService(t *testing.T, configuredPath string, conn Connector) *NASService {
|
|
t.Helper()
|
|
svc, err := New(NASConfig{Path: configuredPath})
|
|
if err != nil {
|
|
t.Fatalf("New(%q) failed: %v", configuredPath, err)
|
|
}
|
|
svc.connector = conn
|
|
return svc
|
|
}
|
|
|
|
func TestNewRejectsUnusableDestination(t *testing.T) {
|
|
if _, err := New(NASConfig{Path: ""}); err == nil {
|
|
t.Error("an empty NAS path should be rejected")
|
|
}
|
|
if _, err := New(NASConfig{Path: `\\server`}); err == nil {
|
|
t.Error("a server without a share should be rejected")
|
|
}
|
|
}
|
|
|
|
func TestNewDoesNoIO(t *testing.T) {
|
|
// Constructing the service must not touch the network or filesystem, so
|
|
// that modes which never transfer can build one harmlessly.
|
|
svc, err := New(NASConfig{Path: `\\nas\dci\streams`})
|
|
if err != nil {
|
|
t.Fatalf("New failed: %v", err)
|
|
}
|
|
if svc.IsConnected() {
|
|
t.Error("service should not report connected before Connect")
|
|
}
|
|
if svc.LocalPath() != "" {
|
|
t.Errorf("no path should be resolved before Connect, got %q", svc.LocalPath())
|
|
}
|
|
}
|
|
|
|
func TestConnectResolvesAndProbes(t *testing.T) {
|
|
base := filepath.Join(t.TempDir(), "streams")
|
|
conn := &fakeConnector{basePath: base}
|
|
svc := newTestService(t, `\\nas\dci\streams`, conn)
|
|
|
|
if err := svc.Connect(context.Background()); err != nil {
|
|
t.Fatalf("Connect failed: %v", err)
|
|
}
|
|
|
|
if !svc.IsConnected() {
|
|
t.Error("service should report connected")
|
|
}
|
|
if svc.LocalPath() != base {
|
|
t.Errorf("LocalPath = %q, want %q", svc.LocalPath(), base)
|
|
}
|
|
// Connect creates the destination directory if it is missing.
|
|
if info, err := os.Stat(base); err != nil || !info.IsDir() {
|
|
t.Errorf("Connect should have created %s: %v", base, err)
|
|
}
|
|
// The write probe must clean up after itself.
|
|
if _, err := os.Stat(filepath.Join(base, ".connection_test")); !os.IsNotExist(err) {
|
|
t.Errorf("probe file should have been removed, stat err = %v", err)
|
|
}
|
|
}
|
|
|
|
func TestConnectSurfacesAttachFailure(t *testing.T) {
|
|
conn := &fakeConnector{attachErr: fmt.Errorf("share is not mounted")}
|
|
svc := newTestService(t, `\\nas\dci`, conn)
|
|
|
|
err := svc.Connect(context.Background())
|
|
if err == nil || !strings.Contains(err.Error(), "not mounted") {
|
|
t.Fatalf("expected the connector's error to surface, got: %v", err)
|
|
}
|
|
if svc.IsConnected() {
|
|
t.Error("a failed attach must not leave the service marked connected")
|
|
}
|
|
}
|
|
|
|
func TestConnectFailsWhenDestinationUnwritable(t *testing.T) {
|
|
// A file where the directory should be: reachable, but unusable.
|
|
blocker := filepath.Join(t.TempDir(), "blocker")
|
|
if err := os.WriteFile(blocker, []byte("x"), 0644); err != nil {
|
|
t.Fatalf("setup failed: %v", err)
|
|
}
|
|
|
|
svc := newTestService(t, "/tmp/whatever", &fakeConnector{basePath: filepath.Join(blocker, "streams")})
|
|
if err := svc.Connect(context.Background()); err == nil {
|
|
t.Fatal("expected an unusable destination to fail the connect")
|
|
}
|
|
if svc.IsConnected() {
|
|
t.Error("service should not report connected after a failed probe")
|
|
}
|
|
}
|
|
|
|
func TestFailedProbeReleasesTheAttachment(t *testing.T) {
|
|
// A mount created by Attach must not outlive a failed Connect, or a failed
|
|
// startup leaves a mount behind on the host.
|
|
blocker := filepath.Join(t.TempDir(), "blocker")
|
|
if err := os.WriteFile(blocker, []byte("x"), 0644); err != nil {
|
|
t.Fatalf("setup failed: %v", err)
|
|
}
|
|
|
|
conn := &fakeConnector{basePath: filepath.Join(blocker, "streams"), owned: true}
|
|
svc := newTestService(t, `\\nas\dci`, conn)
|
|
|
|
if err := svc.Connect(context.Background()); err == nil {
|
|
t.Fatal("expected Connect to fail")
|
|
}
|
|
|
|
if conn.detachCount != 1 {
|
|
t.Errorf("Detach called %d times after a failed connect, want 1", conn.detachCount)
|
|
}
|
|
if !conn.detached.Owned {
|
|
t.Error("Detach should have been told the attachment was owned")
|
|
}
|
|
if svc.LocalPath() != "" {
|
|
t.Errorf("a released attachment should expose no path, got %q", svc.LocalPath())
|
|
}
|
|
// The diagnosis of how far it got must survive for the caller to report.
|
|
if info := strings.Join(svc.ConnectionInfo(), "\n"); !strings.Contains(info, "fake attachment") {
|
|
t.Errorf("connection notes should survive a failed connect:\n%s", info)
|
|
}
|
|
|
|
// A later Disconnect must not detach a second time.
|
|
if err := svc.Disconnect(context.Background()); err != nil {
|
|
t.Fatalf("Disconnect failed: %v", err)
|
|
}
|
|
if conn.detachCount != 2 || conn.detached.Owned {
|
|
t.Errorf("Disconnect after release should be a no-op teardown, detachCount=%d owned=%v",
|
|
conn.detachCount, conn.detached.Owned)
|
|
}
|
|
}
|
|
|
|
func TestJoinUsesResolvedPath(t *testing.T) {
|
|
base := t.TempDir()
|
|
svc := newTestService(t, `\\nas\dci\streams`, &fakeConnector{basePath: base})
|
|
if err := svc.Connect(context.Background()); err != nil {
|
|
t.Fatalf("Connect failed: %v", err)
|
|
}
|
|
|
|
// Crucially the join is against the resolved path, not the configured UNC
|
|
// path — that is the whole point of the indirection.
|
|
if got, want := svc.Join("event", "1080p"), filepath.Join(base, "event", "1080p"); got != want {
|
|
t.Errorf("Join = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestFileExistsAndGetFileSizeUseResolvedPath(t *testing.T) {
|
|
base := t.TempDir()
|
|
svc := newTestService(t, `\\nas\dci`, &fakeConnector{basePath: base})
|
|
if err := svc.Connect(context.Background()); err != nil {
|
|
t.Fatalf("Connect failed: %v", err)
|
|
}
|
|
|
|
rel := filepath.Join("event", "seg.ts")
|
|
if err := os.MkdirAll(filepath.Join(base, "event"), 0755); err != nil {
|
|
t.Fatalf("setup failed: %v", err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(base, rel), []byte("0123456789"), 0644); err != nil {
|
|
t.Fatalf("setup failed: %v", err)
|
|
}
|
|
|
|
exists, err := svc.FileExists(rel, 10)
|
|
if err != nil || !exists {
|
|
t.Errorf("FileExists = %v, %v; want true, nil", exists, err)
|
|
}
|
|
|
|
// A size mismatch means a partial transfer, which should not count.
|
|
exists, err = svc.FileExists(rel, 99)
|
|
if err != nil || exists {
|
|
t.Errorf("FileExists with wrong size = %v, %v; want false, nil", exists, err)
|
|
}
|
|
|
|
exists, err = svc.FileExists(filepath.Join("event", "missing.ts"), 0)
|
|
if err != nil || exists {
|
|
t.Errorf("FileExists for missing file = %v, %v; want false, nil", exists, err)
|
|
}
|
|
|
|
size, err := svc.GetFileSize(rel)
|
|
if err != nil || size != 10 {
|
|
t.Errorf("GetFileSize = %d, %v; want 10, nil", size, err)
|
|
}
|
|
}
|
|
|
|
func TestDisconnectDelegatesAndResets(t *testing.T) {
|
|
conn := &fakeConnector{basePath: t.TempDir(), owned: true}
|
|
svc := newTestService(t, `\\nas\dci`, conn)
|
|
if err := svc.Connect(context.Background()); err != nil {
|
|
t.Fatalf("Connect failed: %v", err)
|
|
}
|
|
|
|
if err := svc.Disconnect(context.Background()); err != nil {
|
|
t.Fatalf("Disconnect failed: %v", err)
|
|
}
|
|
|
|
if conn.detachCount != 1 {
|
|
t.Errorf("Detach called %d times, want 1", conn.detachCount)
|
|
}
|
|
if !conn.detached.Owned {
|
|
t.Error("Detach should receive the attachment so it knows what it owns")
|
|
}
|
|
if svc.IsConnected() {
|
|
t.Error("service should not report connected after Disconnect")
|
|
}
|
|
}
|
|
|
|
func TestConnectionInfoDescribesResolution(t *testing.T) {
|
|
base := t.TempDir()
|
|
svc := newTestService(t, `\\nas\dci\streams`, &fakeConnector{basePath: base})
|
|
|
|
// Before connecting, only configuration is known.
|
|
before := strings.Join(svc.ConnectionInfo(), "\n")
|
|
if !strings.Contains(before, `\\nas\dci\streams`) || !strings.Contains(before, "fake") {
|
|
t.Errorf("pre-connect info should name the destination and strategy:\n%s", before)
|
|
}
|
|
|
|
if err := svc.Connect(context.Background()); err != nil {
|
|
t.Fatalf("Connect failed: %v", err)
|
|
}
|
|
|
|
after := strings.Join(svc.ConnectionInfo(), "\n")
|
|
if !strings.Contains(after, "resolved path: "+base) {
|
|
t.Errorf("post-connect info should report where the destination resolved to:\n%s", after)
|
|
}
|
|
}
|
|
|
|
func TestVerifyTransferDetectsSizeMismatch(t *testing.T) {
|
|
dir := t.TempDir()
|
|
src := filepath.Join(dir, "src.ts")
|
|
dst := filepath.Join(dir, "dst.ts")
|
|
if err := os.WriteFile(src, []byte("0123456789"), 0644); err != nil {
|
|
t.Fatalf("setup failed: %v", err)
|
|
}
|
|
if err := os.WriteFile(dst, []byte("012"), 0644); err != nil {
|
|
t.Fatalf("setup failed: %v", err)
|
|
}
|
|
|
|
svc := newTestService(t, dir, &fakeConnector{basePath: dir})
|
|
if err := svc.VerifyTransfer(src, dst); err == nil {
|
|
t.Error("a truncated destination should fail verification")
|
|
}
|
|
|
|
if err := os.WriteFile(dst, []byte("0123456789"), 0644); err != nil {
|
|
t.Fatalf("setup failed: %v", err)
|
|
}
|
|
if err := svc.VerifyTransfer(src, dst); err != nil {
|
|
t.Errorf("matching sizes should verify: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCopyFile(t *testing.T) {
|
|
dir := t.TempDir()
|
|
src := filepath.Join(dir, "src.ts")
|
|
dst := filepath.Join(dir, "dst.ts")
|
|
payload := []byte("segment payload")
|
|
if err := os.WriteFile(src, payload, 0644); err != nil {
|
|
t.Fatalf("setup failed: %v", err)
|
|
}
|
|
|
|
svc := newTestService(t, dir, &fakeConnector{basePath: dir})
|
|
if err := svc.CopyFile(context.Background(), src, dst); err != nil {
|
|
t.Fatalf("CopyFile failed: %v", err)
|
|
}
|
|
|
|
got, err := os.ReadFile(dst)
|
|
if err != nil {
|
|
t.Fatalf("failed to read copy: %v", err)
|
|
}
|
|
if string(got) != string(payload) {
|
|
t.Errorf("copied contents = %q, want %q", got, payload)
|
|
}
|
|
|
|
if err := svc.CopyFile(context.Background(), filepath.Join(dir, "missing.ts"), dst); err == nil {
|
|
t.Error("copying a missing source should fail")
|
|
}
|
|
}
|