package nfs import ( "testing" "github.com/darroyo/nasctl/internal/db" ) func TestBuildExportLine(t *testing.T) { tests := []struct { name string exp db.NFSExport lines []string }{ { name: "single host with options", exp: db.NFSExport{ Path: "/srv/nfs/shared", Clients: []db.NFSClient{{Host: "192.168.1.100", ReadOnly: false, Async: false, RootSquash: true, SubtreeCheck: false}}, ReadOnly: false, Async: false, RootSquash: true, SubtreeCheck: false, FSID: 1, Advanced: "{}", }, lines: []string{`/srv/nfs/shared 192.168.1.100(rw,sync,root_squash,no_subtree_check,no_all_squash,insecure,no_wdelay,nohide,fsid=1)`}, }, { name: "multiple hosts different options", exp: db.NFSExport{ Path: "/srv/nfs/shared", Clients: []db.NFSClient{ {Host: "192.168.1.0/24", ReadOnly: false, Async: false, RootSquash: true, SubtreeCheck: false, Advanced: db.NFSAdvanced{Crossmnt: true}}, {Host: "10.0.0.5", ReadOnly: true, Async: true, RootSquash: false, SubtreeCheck: false}, }, ReadOnly: false, Async: false, RootSquash: true, SubtreeCheck: false, FSID: 2, Advanced: "{}", }, lines: []string{ `/srv/nfs/shared 192.168.1.0/24(rw,sync,root_squash,no_subtree_check,no_all_squash,insecure,no_wdelay,nohide,crossmnt,fsid=2) 10.0.0.5(ro,async,no_root_squash,no_subtree_check,no_all_squash,insecure,no_wdelay,nohide,fsid=2)`, }, }, { name: "no clients fallback wildcard", exp: db.NFSExport{ Path: "/srv/nfs/public", Clients: []db.NFSClient{}, ReadOnly: true, Async: false, RootSquash: true, SubtreeCheck: false, FSID: 3, Advanced: "{}", }, lines: []string{`/srv/nfs/public *(ro,sync,root_squash,no_subtree_check,fsid=3)`}, }, { name: "advanced options per host", exp: db.NFSExport{ Path: "/srv/nfs/secure", Clients: []db.NFSClient{ {Host: "192.168.1.0/24", ReadOnly: false, Async: false, RootSquash: true, SubtreeCheck: false, Advanced: db.NFSAdvanced{AllSquash: true, Secure: true, WDelay: true, Hide: true, Crossmnt: true}}, }, ReadOnly: false, Async: false, RootSquash: true, SubtreeCheck: false, FSID: 4, Advanced: "{}", }, lines: []string{ `/srv/nfs/secure 192.168.1.0/24(rw,sync,root_squash,no_subtree_check,all_squash,secure,wdelay,hide,crossmnt,fsid=4)`, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { line := buildExportLine(tt.exp) if len(tt.lines) != 1 { t.Fatalf("expected 1 line, got test setup error") } if line != tt.lines[0] { t.Errorf("buildExportLine() = %q, want %q", line, tt.lines[0]) } }) } } func TestClientFlags(t *testing.T) { c := db.NFSClient{ Host: "192.168.1.0/24", ReadOnly: true, Async: true, RootSquash: false, SubtreeCheck: true, Advanced: db.NFSAdvanced{AllSquash: true, Secure: true}, } flags := clientFlags(c, 42) if flags != "ro,async,no_root_squash,subtree_check,all_squash,secure,no_wdelay,nohide,fsid=42" { t.Errorf("clientFlags() = %q, unexpected flags", flags) } }