storage_pool_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. package main
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. fs "imuslab.com/arozos/mod/filesystem"
  10. "imuslab.com/arozos/mod/storage/access"
  11. )
  12. type testRules map[string]access.Rule
  13. func (m testRules) Rule(uuid string) (access.Rule, bool) {
  14. rule, ok := m[uuid]
  15. return rule, ok
  16. }
  17. func findDrive(t *testing.T, drives []driveInfo, pool string, uuid string) driveInfo {
  18. t.Helper()
  19. for _, drive := range drives {
  20. if drive.UUID == uuid && drive.Pool == pool {
  21. return drive
  22. }
  23. }
  24. t.Fatalf("no drive %s in pool %s: %+v", uuid, pool, drives)
  25. return driveInfo{}
  26. }
  27. func TestBuildDriveListStates(t *testing.T) {
  28. pools := []poolState{
  29. {
  30. owner: storagePoolSystem,
  31. live: []*fs.FileSystemHandler{
  32. {UUID: "user", Name: "User"},
  33. {UUID: "tmp", Name: "tmp"},
  34. {UUID: "share", Name: "Shared with me", ReadOnly: true},
  35. {UUID: "cluster", Name: "Cluster"},
  36. {UUID: "media", Name: "Media"},
  37. {UUID: "paused", Name: "Paused", Closed: true},
  38. {UUID: "deleted", Name: "Deleted"},
  39. },
  40. config: []fs.FileSystemOption{
  41. {Uuid: "media", Name: "Media"},
  42. {Uuid: "paused", Name: "Paused"},
  43. {Uuid: "unplugged", Name: "Unplugged", Path: "/mnt/usb", Access: "readonly"},
  44. },
  45. },
  46. {
  47. owner: "family",
  48. live: []*fs.FileSystemHandler{{UUID: "photos", Name: "Photos"}},
  49. config: []fs.FileSystemOption{{Uuid: "photos", Name: "Photos"}},
  50. },
  51. }
  52. tests := []struct {
  53. pool string
  54. uuid string
  55. wantState string
  56. wantKind string
  57. editable bool
  58. }{
  59. {storagePoolSystem, "user", driveStateOnline, driveKindFixed, false},
  60. {storagePoolSystem, "tmp", driveStateOnline, driveKindFixed, false},
  61. {storagePoolSystem, "share", driveStateOnline, driveKindBuiltin, true},
  62. {storagePoolSystem, "cluster", driveStateOnline, driveKindBuiltin, true},
  63. {storagePoolSystem, "media", driveStateOnline, driveKindConfigured, true},
  64. {storagePoolSystem, "paused", driveStateStopped, driveKindConfigured, true},
  65. {storagePoolSystem, "deleted", driveStateRemoved, driveKindConfigured, true},
  66. {storagePoolSystem, "unplugged", driveStateOffline, driveKindConfigured, true},
  67. {"family", "photos", driveStateOnline, driveKindConfigured, true},
  68. }
  69. drives := buildDriveList(pools, testRules{})
  70. for _, tt := range tests {
  71. t.Run(tt.uuid, func(t *testing.T) {
  72. got := findDrive(t, drives, tt.pool, tt.uuid)
  73. if got.State != tt.wantState {
  74. t.Errorf("State = %q, want %q", got.State, tt.wantState)
  75. }
  76. if got.Kind != tt.wantKind {
  77. t.Errorf("Kind = %q, want %q", got.Kind, tt.wantKind)
  78. }
  79. if got.Editable != tt.editable {
  80. t.Errorf("Editable = %v, want %v", got.Editable, tt.editable)
  81. }
  82. })
  83. }
  84. if !findDrive(t, drives, storagePoolSystem, "unplugged").ReadOnly {
  85. t.Errorf("an offline drive lost its read only setting")
  86. }
  87. }
  88. func TestBuildDriveListRules(t *testing.T) {
  89. pools := []poolState{
  90. {owner: storagePoolSystem, live: []*fs.FileSystemHandler{{UUID: "share"}, {UUID: "media"}, {UUID: "docs"}}},
  91. {owner: "family", live: []*fs.FileSystemHandler{{UUID: "photos"}}},
  92. }
  93. saved := access.Rule{Other: access.None, Groups: map[string]string{"family": access.Read}}
  94. drives := buildDriveList(pools, testRules{"docs": saved})
  95. tests := []struct {
  96. pool string
  97. uuid string
  98. want access.Rule
  99. }{
  100. {storagePoolSystem, "share", access.Rule{Other: access.Read}},
  101. {storagePoolSystem, "media", access.Rule{Other: access.ReadWrite}},
  102. {storagePoolSystem, "docs", saved},
  103. {"family", "photos", access.Rule{Other: access.None}},
  104. }
  105. for _, tt := range tests {
  106. if got := findDrive(t, drives, tt.pool, tt.uuid).Rule; !reflect.DeepEqual(got, tt.want) {
  107. t.Errorf("rule of %s = %+v, want %+v", tt.uuid, got, tt.want)
  108. }
  109. }
  110. }
  111. // What the page gets must never hold a login, which is what the old raw
  112. // config endpoint leaked
  113. func TestDriveListCarriesNoLogin(t *testing.T) {
  114. pools := []poolState{{
  115. owner: storagePoolSystem,
  116. live: []*fs.FileSystemHandler{{
  117. UUID: "nas",
  118. StartOptions: fs.FileSystemOption{Uuid: "nas", Username: "secret-user", Password: "secret-pass"},
  119. }},
  120. config: []fs.FileSystemOption{
  121. {Uuid: "nas", Username: "secret-user", Password: "secret-pass"},
  122. {Uuid: "offline", Username: "secret-user", Password: "secret-pass"},
  123. },
  124. }}
  125. js, err := json.Marshal(buildDriveList(pools, testRules{}))
  126. if err != nil {
  127. t.Fatalf("Marshal() = %v", err)
  128. }
  129. if strings.Contains(string(js), "secret") {
  130. t.Fatalf("the drive list holds a login: %s", js)
  131. }
  132. }
  133. func TestDuplicateDriveIDs(t *testing.T) {
  134. pools := []poolState{
  135. {owner: "system", live: []*fs.FileSystemHandler{{UUID: "media"}, {UUID: "data"}}},
  136. {owner: "family", config: []fs.FileSystemOption{{Uuid: "data"}}},
  137. {owner: "guests", live: []*fs.FileSystemHandler{{UUID: "own"}}, config: []fs.FileSystemOption{{Uuid: "own"}}},
  138. }
  139. if got := duplicateDriveIDs(pools); !reflect.DeepEqual(got, []string{"data"}) {
  140. t.Fatalf("duplicateDriveIDs() = %v", got)
  141. }
  142. drives := buildDriveList(pools, testRules{})
  143. if !findDrive(t, drives, "system", "data").Duplicate || !findDrive(t, drives, "family", "data").Duplicate {
  144. t.Errorf("a duplicate id was not flagged on both drives")
  145. }
  146. if findDrive(t, drives, "guests", "own").Duplicate {
  147. t.Errorf("a drive both running and configured counted as a duplicate")
  148. }
  149. }
  150. func TestApplyClusterDriveState(t *testing.T) {
  151. mounted := []driveInfo{{UUID: "cluster", State: driveStateOnline, Editable: true}}
  152. got := applyClusterDriveState(mounted, true, testRules{})
  153. if len(got) != 1 || !got[0].Replicated || !got[0].Editable || got[0].State != driveStateOnline {
  154. t.Errorf("mounted drive in a cluster = %+v", got)
  155. }
  156. got = applyClusterDriveState([]driveInfo{}, false, testRules{})
  157. if len(got) != 1 {
  158. t.Fatalf("no row for a drive that is not mounted: %+v", got)
  159. }
  160. if got[0].State != driveStateNotActive || got[0].Editable {
  161. t.Errorf("a node outside a cluster = %+v, want not active and not editable", got[0])
  162. }
  163. got = applyClusterDriveState([]driveInfo{}, true, testRules{})
  164. if got[0].State != driveStateNotActive || !got[0].Editable {
  165. t.Errorf("a cluster with no volume yet = %+v, want not active but editable", got[0])
  166. }
  167. }
  168. func TestPrepareDriveRule(t *testing.T) {
  169. tests := []struct {
  170. name string
  171. uuid string
  172. home string
  173. rule access.Rule
  174. want access.Rule
  175. wantErr bool
  176. }{
  177. {name: "plain rule", uuid: "media", rule: access.Rule{Other: access.Read}, want: access.Rule{Other: access.Read}},
  178. {name: "home drive", uuid: "user", rule: access.Rule{Other: access.None}, wantErr: true},
  179. {name: "tmp drive", uuid: "tmp", rule: access.Rule{Other: access.None}, wantErr: true},
  180. {name: "invalid level", uuid: "media", rule: access.Rule{Other: "writeonly"}, wantErr: true},
  181. {name: "share can be hidden", uuid: "share", rule: access.Rule{Other: access.None}, want: access.Rule{Other: access.None}},
  182. {name: "share cannot be writable", uuid: "share", rule: access.Rule{Other: access.ReadWrite}, wantErr: true},
  183. {name: "share row cannot be writable", uuid: "share", rule: access.Rule{Other: access.Read, Groups: map[string]string{"a": access.ReadWrite}}, wantErr: true},
  184. {
  185. name: "a row for the home group is dropped",
  186. uuid: "photos",
  187. home: "family",
  188. rule: access.Rule{Other: access.None, Groups: map[string]string{"family": access.None, "guests": access.Read}},
  189. want: access.Rule{Other: access.None, Groups: map[string]string{"guests": access.Read}},
  190. },
  191. }
  192. for _, tt := range tests {
  193. t.Run(tt.name, func(t *testing.T) {
  194. rule := tt.rule.Clone()
  195. err := prepareDriveRule(tt.uuid, tt.home, &rule)
  196. if (err != nil) != tt.wantErr {
  197. t.Fatalf("error = %v, wantErr %v", err, tt.wantErr)
  198. }
  199. if !tt.wantErr && !reflect.DeepEqual(rule, tt.want) {
  200. t.Errorf("rule = %+v, want %+v", rule, tt.want)
  201. }
  202. })
  203. }
  204. }
  205. func TestValidateNewDrive(t *testing.T) {
  206. folder := t.TempDir()
  207. pools := []poolState{
  208. {owner: "system", config: []fs.FileSystemOption{{Uuid: "media"}}},
  209. {owner: "family", live: []*fs.FileSystemHandler{{UUID: "photos"}}},
  210. }
  211. valid := func(uuid string) *fs.FileSystemOption {
  212. return &fs.FileSystemOption{Name: "Drive", Uuid: uuid, Path: folder, Access: "readwrite", Hierarchy: "public", Filesystem: "ext4"}
  213. }
  214. tests := []struct {
  215. name string
  216. uuid string
  217. wantErr bool
  218. }{
  219. {"new id", "docs", false},
  220. {"id with dash and underscore", "my-drive_2", false},
  221. {"id configured in another pool", "media", true},
  222. {"id running in another pool", "photos", true},
  223. {"reserved home drive", "user", true},
  224. {"reserved cluster drive", "cluster", true},
  225. {"reserved share drive", "share", true},
  226. {"a colon would split the path", "a:b", true},
  227. {"a slash would split the path", "a/b", true},
  228. {"a space", "my drive", true},
  229. }
  230. for _, tt := range tests {
  231. t.Run(tt.name, func(t *testing.T) {
  232. if err := validateNewDrive(valid(tt.uuid), pools); (err != nil) != tt.wantErr {
  233. t.Errorf("error = %v, wantErr %v", err, tt.wantErr)
  234. }
  235. })
  236. }
  237. }
  238. func useTestStorageConfig(t *testing.T, options []fs.FileSystemOption) string {
  239. t.Helper()
  240. filename := filepath.Join(t.TempDir(), "storage.json")
  241. content, err := json.Marshal(options)
  242. if err != nil {
  243. t.Fatalf("Marshal() = %v", err)
  244. }
  245. if err := os.WriteFile(filename, content, 0644); err != nil {
  246. t.Fatalf("WriteFile() = %v", err)
  247. }
  248. previous := *storage_config_file
  249. *storage_config_file = filename
  250. t.Cleanup(func() { *storage_config_file = previous })
  251. return filename
  252. }
  253. func TestEditingADriveKeepsItsRowAndLogin(t *testing.T) {
  254. folder := t.TempDir()
  255. useTestStorageConfig(t, []fs.FileSystemOption{
  256. {Name: "First", Uuid: "first", Path: folder, Access: "readwrite", Hierarchy: "public", Filesystem: "ext4"},
  257. {Name: "NAS", Uuid: "nas", Path: "10.0.0.2/share", Access: "readwrite", Hierarchy: "public", Filesystem: "smb", Username: "kept", Password: "stored"},
  258. {Name: "Last", Uuid: "last", Path: folder, Access: "readwrite", Hierarchy: "public", Filesystem: "ext4"},
  259. })
  260. edited := fs.FileSystemOption{Name: "Renamed", Uuid: "nas", Path: "10.0.0.3/share", Access: "readonly", Hierarchy: "public", Filesystem: "smb"}
  261. if err := setFSHConfigByGroupAndId(storagePoolSystem, "nas", &edited); err != nil {
  262. t.Fatalf("setFSHConfigByGroupAndId() = %v", err)
  263. }
  264. got, err := readPoolConfig(storagePoolSystem)
  265. if err != nil {
  266. t.Fatalf("readPoolConfig() = %v", err)
  267. }
  268. if len(got) != 3 {
  269. t.Fatalf("%d rows, want 3: %+v", len(got), got)
  270. }
  271. if got[1].Uuid != "nas" || got[1].Name != "Renamed" || got[1].Path != "10.0.0.3/share" {
  272. t.Errorf("row in the wrong place or not updated: %+v", got[1])
  273. }
  274. if got[1].Username != "kept" || got[1].Password != "stored" {
  275. t.Errorf("the stored login was lost: %+v", got[1])
  276. }
  277. if edited.Password != "stored" {
  278. t.Errorf("the option to mount has no login: %+v", edited)
  279. }
  280. }
  281. func TestEditingRefusesAnInvalidDrive(t *testing.T) {
  282. useTestStorageConfig(t, []fs.FileSystemOption{
  283. {Name: "Media", Uuid: "media", Path: t.TempDir(), Access: "readwrite", Hierarchy: "public", Filesystem: "ext4"},
  284. })
  285. edited := fs.FileSystemOption{Name: "Media", Uuid: "media", Path: filepath.Join(t.TempDir(), "missing"), Access: "readwrite", Hierarchy: "public", Filesystem: "ext4"}
  286. if err := setFSHConfigByGroupAndId(storagePoolSystem, "media", &edited); err == nil {
  287. t.Fatalf("a path that does not exist was saved")
  288. }
  289. }
  290. func TestParseClusterDriveRule(t *testing.T) {
  291. tests := []struct {
  292. name string
  293. raw string
  294. want access.Rule
  295. }{
  296. {"valid", `{"other":"denied","groups":{"family":"readwrite"}}`, access.Rule{Other: access.None, Groups: map[string]string{"family": access.ReadWrite}}},
  297. {"not json closes the drive", `nonsense`, access.Rule{Other: access.None}},
  298. {"unknown level closes the drive", `{"other":"everything"}`, access.Rule{Other: access.None}},
  299. }
  300. for _, tt := range tests {
  301. t.Run(tt.name, func(t *testing.T) {
  302. if got := parseClusterDriveRule([]byte(tt.raw)); !reflect.DeepEqual(got, tt.want) {
  303. t.Errorf("parseClusterDriveRule() = %+v, want %+v", got, tt.want)
  304. }
  305. })
  306. }
  307. }