Files
baby-nas/internal/importer/nfs_test.go
T
darroyo 512feaffd7 feat(nfs): per-host NFS options (IP/CIDR with own ro/async/squash flags)
This is a backward-compatible MINOR bump (0.4.0 → 0.5.0).

BREAKING NOTES (for users upgrading from pre-0.5.0):
- The nfs_exports.clients column schema changed from []string to
  []NFSClient (per-host options). A migration (0005) transforms existing
  string arrays into object arrays, taking export-level options as
  defaults for each host.
- ValidateNFSClient now only accepts IPv4 (192.168.1.1) or IPv4/CIDR
  (192.168.1.0/24). Hostnames, wildcards, netgroups are rejected.
- If you use NASCTL_IMPORT_ON_BOOT, re-import your /etc/exports to pick
  up per-host options.

What changed:
- NFSClient type: {host, read_only, async, root_squash, subtree_check, advanced}
- NFSExport.Clients is now []NFSClient (was []string)
- export-level flags (ro/async/root_squash/subtree_check/advanced) are
  preserved as template defaults for newly added hosts in the UI.
- buildExportLine generates: path host1(ro,sync,...) host2(rw,async,...) fsid=N
- ValidateNFSClient: strict IPv4/CIDR only (0-255 octets, /0-32 prefix)
- parseExportLine now parses per-host options from /etc/exports (previously
  only the first host's options were kept, others were discarded)
- UI: per-host rows with toggles (ro/async/root_squash/subtree_check) and
  advanced options (all_squash, secure, wdelay, hide, crossmnt)
2026-07-06 11:30:27 -04:00

152 lines
3.5 KiB
Go

package importer
import (
"testing"
)
func TestParseExports(t *testing.T) {
tests := []struct {
name string
input string
wantLen int
wantErr bool
}{
{
name: "basic export single host",
input: `/srv/nfs/shared 192.168.1.0/24(rw,sync,no_subtree_check)
`,
wantLen: 1,
},
{
name: "multiple clients per host different options",
input: `/srv/nfs/shared 192.168.1.0/24(rw,sync,no_root_squash) 10.0.0.5(ro,async,root_squash)
`,
wantLen: 1,
},
{
name: "multiple exports",
input: `# This is a comment
/srv/nfs/data 192.168.1.0/24(rw,sync)
/srv/nfs/public 10.0.0.0/8(ro,sync)
# another comment
/srv/nfs/backup 172.16.0.0/12(ro,sync,no_subtree_check)
`,
wantLen: 3,
},
{
name: "single IP host",
input: `/srv/nfs/public 192.168.1.50(ro,sync,no_root_squash)
`,
wantLen: 1,
},
{
name: "empty input",
input: "",
wantLen: 0,
},
{
name: "only comments",
input: `# comment 1
# comment 2
`,
wantLen: 0,
},
{
name: "multiple spaces between entries",
input: `/srv/nfs/shared 192.168.1.0/24(rw,sync) 10.0.0.0/8(ro)
`,
wantLen: 1,
},
{
name: "client without options inherits from first",
input: `/srv/nfs/shared 192.168.1.0/24(rw,sync,no_root_squash) 10.0.0.5
`,
wantLen: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := parseExports([]byte(tt.input))
if (err != nil) != tt.wantErr {
t.Errorf("parseExports() error = %v, wantErr %v", err, tt.wantErr)
return
}
if len(got) != tt.wantLen {
t.Errorf("parseExports() got %d exports, want %d", len(got), tt.wantLen)
for i, e := range got {
t.Logf(" export[%d]: path=%q clients=%v fsid=%d", i, e.Path, e.Clients, e.FSID)
}
}
})
}
}
func TestParseExportLine(t *testing.T) {
tests := []struct {
name string
line string
wantPath string
wantCount int
}{
{
name: "single client with options",
line: `/srv/nfs/shared 192.168.1.100(rw,sync,no_subtree_check)`,
wantPath: `/srv/nfs/shared`,
wantCount: 1,
},
{
name: "multiple clients different options",
line: `/srv/nfs/shared 192.168.1.0/24(rw) 10.0.0.0/8(ro)`,
wantPath: `/srv/nfs/shared`,
wantCount: 2,
},
{
name: "single IP client",
line: `/srv/nfs/public 192.168.1.50(ro,sync)`,
wantPath: `/srv/nfs/public`,
wantCount: 1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, ok := parseExportLine(tt.line)
if !ok {
t.Errorf("parseExportLine(%q) returned false", tt.line)
return
}
if got.Path != tt.wantPath {
t.Errorf("parseExportLine(%q) path = %q, want %q", tt.line, got.Path, tt.wantPath)
}
if len(got.Clients) != tt.wantCount {
t.Errorf("parseExportLine(%q) clients = %v, want %d", tt.line, got.Clients, tt.wantCount)
}
for i, c := range got.Clients {
t.Logf(" client[%d]: host=%q ro=%v async=%v root_squash=%v subtree_check=%v", i, c.Host, c.ReadOnly, c.Async, c.RootSquash, c.SubtreeCheck)
}
})
}
}
func TestSplitExportsClients(t *testing.T) {
tests := []struct {
input string
expected []string
}{
{`192.168.1.0/24(rw,sync) 10.0.0.0/8(ro)`, []string{`192.168.1.0/24(rw,sync)`, `10.0.0.0/8(ro)`}},
{`*(ro)`, []string{`*(ro)`}},
{`192.168.1.100(rw)`, []string{`192.168.1.100(rw)`}},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got := splitExportsClients(tt.input)
if len(got) != len(tt.expected) {
t.Errorf("splitExportsClients(%q) = %v, want %v", tt.input, got, tt.expected)
}
})
}
}