You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
6.8 KiB

  1. // Copyright (c) 2015-2021 MinIO, Inc.
  2. //
  3. // This file is part of MinIO Object Storage stack
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. package cmd
  18. import (
  19. "testing"
  20. "time"
  21. )
  22. var metaCacheTestsetTimestamp = time.Now()
  23. var metaCacheTestset = []metacache{
  24. 0: {
  25. id: "case-1-normal",
  26. bucket: "bucket",
  27. root: "folder/prefix",
  28. recursive: false,
  29. status: scanStateSuccess,
  30. fileNotFound: false,
  31. error: "",
  32. started: metaCacheTestsetTimestamp,
  33. ended: metaCacheTestsetTimestamp.Add(time.Minute),
  34. lastUpdate: metaCacheTestsetTimestamp.Add(time.Minute),
  35. lastHandout: metaCacheTestsetTimestamp,
  36. dataVersion: metacacheStreamVersion,
  37. },
  38. 1: {
  39. id: "case-2-recursive",
  40. bucket: "bucket",
  41. root: "folder/prefix",
  42. recursive: true,
  43. status: scanStateSuccess,
  44. fileNotFound: false,
  45. error: "",
  46. started: metaCacheTestsetTimestamp,
  47. ended: metaCacheTestsetTimestamp.Add(time.Minute),
  48. lastUpdate: metaCacheTestsetTimestamp.Add(time.Minute),
  49. lastHandout: metaCacheTestsetTimestamp,
  50. dataVersion: metacacheStreamVersion,
  51. },
  52. 2: {
  53. id: "case-3-older",
  54. bucket: "bucket",
  55. root: "folder/prefix",
  56. recursive: false,
  57. status: scanStateSuccess,
  58. fileNotFound: true,
  59. error: "",
  60. started: metaCacheTestsetTimestamp.Add(-time.Minute),
  61. ended: metaCacheTestsetTimestamp,
  62. lastUpdate: metaCacheTestsetTimestamp,
  63. lastHandout: metaCacheTestsetTimestamp,
  64. dataVersion: metacacheStreamVersion,
  65. },
  66. 3: {
  67. id: "case-4-error",
  68. bucket: "bucket",
  69. root: "folder/prefix",
  70. recursive: false,
  71. status: scanStateError,
  72. fileNotFound: false,
  73. error: "an error lol",
  74. started: metaCacheTestsetTimestamp.Add(-20 * time.Minute),
  75. ended: metaCacheTestsetTimestamp.Add(-20 * time.Minute),
  76. lastUpdate: metaCacheTestsetTimestamp.Add(-20 * time.Minute),
  77. lastHandout: metaCacheTestsetTimestamp.Add(-20 * time.Minute),
  78. dataVersion: metacacheStreamVersion,
  79. },
  80. 4: {
  81. id: "case-5-noupdate",
  82. bucket: "bucket",
  83. root: "folder/prefix",
  84. recursive: false,
  85. status: scanStateStarted,
  86. fileNotFound: false,
  87. error: "",
  88. started: metaCacheTestsetTimestamp.Add(-time.Minute),
  89. ended: time.Time{},
  90. lastUpdate: metaCacheTestsetTimestamp.Add(-time.Minute),
  91. lastHandout: metaCacheTestsetTimestamp,
  92. dataVersion: metacacheStreamVersion,
  93. },
  94. 5: {
  95. id: "case-6-404notfound",
  96. bucket: "bucket",
  97. root: "folder/notfound",
  98. recursive: true,
  99. status: scanStateSuccess,
  100. fileNotFound: true,
  101. error: "",
  102. started: metaCacheTestsetTimestamp,
  103. ended: metaCacheTestsetTimestamp.Add(time.Minute),
  104. lastUpdate: metaCacheTestsetTimestamp.Add(time.Minute),
  105. lastHandout: metaCacheTestsetTimestamp,
  106. dataVersion: metacacheStreamVersion,
  107. },
  108. 6: {
  109. id: "case-7-oldcycle",
  110. bucket: "bucket",
  111. root: "folder/prefix",
  112. recursive: true,
  113. status: scanStateSuccess,
  114. fileNotFound: false,
  115. error: "",
  116. started: metaCacheTestsetTimestamp.Add(-10 * time.Minute),
  117. ended: metaCacheTestsetTimestamp.Add(-8 * time.Minute),
  118. lastUpdate: metaCacheTestsetTimestamp.Add(-8 * time.Minute),
  119. lastHandout: metaCacheTestsetTimestamp,
  120. dataVersion: metacacheStreamVersion,
  121. },
  122. 7: {
  123. id: "case-8-running",
  124. bucket: "bucket",
  125. root: "folder/running",
  126. recursive: false,
  127. status: scanStateStarted,
  128. fileNotFound: false,
  129. error: "",
  130. started: metaCacheTestsetTimestamp.Add(-1 * time.Minute),
  131. ended: time.Time{},
  132. lastUpdate: metaCacheTestsetTimestamp.Add(-1 * time.Minute),
  133. lastHandout: metaCacheTestsetTimestamp,
  134. dataVersion: metacacheStreamVersion,
  135. },
  136. 8: {
  137. id: "case-8-finished-a-week-ago",
  138. bucket: "bucket",
  139. root: "folder/finished",
  140. recursive: false,
  141. status: scanStateSuccess,
  142. fileNotFound: false,
  143. error: "",
  144. started: metaCacheTestsetTimestamp.Add(-7 * 24 * time.Hour),
  145. ended: metaCacheTestsetTimestamp.Add(-7 * 24 * time.Hour),
  146. lastUpdate: metaCacheTestsetTimestamp.Add(-7 * 24 * time.Hour),
  147. lastHandout: metaCacheTestsetTimestamp.Add(-7 * 24 * time.Hour),
  148. dataVersion: metacacheStreamVersion,
  149. },
  150. }
  151. func Test_baseDirFromPrefix(t *testing.T) {
  152. tests := []struct {
  153. name string
  154. prefix string
  155. want string
  156. }{
  157. {
  158. name: "root",
  159. prefix: "object.ext",
  160. want: "",
  161. },
  162. {
  163. name: "rootdotslash",
  164. prefix: "./object.ext",
  165. want: "",
  166. },
  167. {
  168. name: "rootslash",
  169. prefix: "/",
  170. want: "",
  171. },
  172. {
  173. name: "folder",
  174. prefix: "prefix/",
  175. want: "prefix/",
  176. },
  177. {
  178. name: "folderobj",
  179. prefix: "prefix/obj.ext",
  180. want: "prefix/",
  181. },
  182. {
  183. name: "folderfolderobj",
  184. prefix: "prefix/prefix2/obj.ext",
  185. want: "prefix/prefix2/",
  186. },
  187. {
  188. name: "folderfolder",
  189. prefix: "prefix/prefix2/",
  190. want: "prefix/prefix2/",
  191. },
  192. }
  193. for _, tt := range tests {
  194. t.Run(tt.name, func(t *testing.T) {
  195. if got := baseDirFromPrefix(tt.prefix); got != tt.want {
  196. t.Errorf("baseDirFromPrefix() = %v, want %v", got, tt.want)
  197. }
  198. })
  199. }
  200. }
  201. func Test_metacache_finished(t *testing.T) {
  202. wantResults := []bool{0: true, 1: true, 2: true, 3: true, 4: false, 5: true, 6: true, 7: false, 8: true}
  203. for i, tt := range metaCacheTestset {
  204. t.Run(tt.id, func(t *testing.T) {
  205. var want bool
  206. if i >= len(wantResults) {
  207. t.Logf("no expected result for test #%d", i)
  208. } else {
  209. want = wantResults[i]
  210. }
  211. got := tt.finished()
  212. if got != want {
  213. t.Errorf("#%d: want %v, got %v", i, want, got)
  214. }
  215. })
  216. }
  217. }
  218. func Test_metacache_worthKeeping(t *testing.T) {
  219. // TODO: Update...
  220. wantResults := []bool{0: true, 1: true, 2: true, 3: false, 4: false, 5: true, 6: true, 7: false, 8: false}
  221. for i, tt := range metaCacheTestset {
  222. t.Run(tt.id, func(t *testing.T) {
  223. var want bool
  224. if i >= len(wantResults) {
  225. t.Logf("no expected result for test #%d", i)
  226. } else {
  227. want = wantResults[i]
  228. }
  229. got := tt.worthKeeping()
  230. if got != want {
  231. t.Errorf("#%d: want %v, got %v", i, want, got)
  232. }
  233. })
  234. }
  235. }