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.

281 lines
5.9 KiB

  1. /*
  2. * Minio Cloud Storage, (C) 2016 Minio, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package cmd
  17. import (
  18. "fmt"
  19. "net/http"
  20. "reflect"
  21. "runtime"
  22. "testing"
  23. )
  24. // Tests http.Header clone.
  25. func TestCloneHeader(t *testing.T) {
  26. headers := []http.Header{
  27. {
  28. "Content-Type": {"text/html; charset=UTF-8"},
  29. "Content-Length": {"0"},
  30. },
  31. {
  32. "Content-Length": {"0", "1", "2"},
  33. },
  34. {
  35. "Expires": {"-1"},
  36. "Content-Length": {"0"},
  37. "Content-Encoding": {"gzip"},
  38. },
  39. }
  40. for i, header := range headers {
  41. clonedHeader := cloneHeader(header)
  42. if !reflect.DeepEqual(header, clonedHeader) {
  43. t.Errorf("Test %d failed", i+1)
  44. }
  45. }
  46. }
  47. // Tests check duplicates function.
  48. func TestCheckDuplicates(t *testing.T) {
  49. tests := []struct {
  50. list []string
  51. err error
  52. shouldPass bool
  53. }{
  54. // Test 1 - for '/tmp/1' repeated twice.
  55. {
  56. list: []string{"/tmp/1", "/tmp/1", "/tmp/2", "/tmp/3"},
  57. err: fmt.Errorf("Duplicate key: \"/tmp/1\" found of count: \"2\""),
  58. shouldPass: false,
  59. },
  60. // Test 2 - for '/tmp/1' repeated thrice.
  61. {
  62. list: []string{"/tmp/1", "/tmp/1", "/tmp/1", "/tmp/3"},
  63. err: fmt.Errorf("Duplicate key: \"/tmp/1\" found of count: \"3\""),
  64. shouldPass: false,
  65. },
  66. // Test 3 - empty string.
  67. {
  68. list: []string{""},
  69. err: errInvalidArgument,
  70. shouldPass: false,
  71. },
  72. // Test 4 - empty string.
  73. {
  74. list: nil,
  75. err: errInvalidArgument,
  76. shouldPass: false,
  77. },
  78. // Test 5 - non repeated strings.
  79. {
  80. list: []string{"/tmp/1", "/tmp/2", "/tmp/3"},
  81. err: nil,
  82. shouldPass: true,
  83. },
  84. }
  85. // Validate if function runs as expected.
  86. for i, test := range tests {
  87. err := checkDuplicates(test.list)
  88. if test.shouldPass && err != test.err {
  89. t.Errorf("Test: %d, Expected %s got %s", i+1, test.err, err)
  90. }
  91. if !test.shouldPass && err.Error() != test.err.Error() {
  92. t.Errorf("Test: %d, Expected %s got %s", i+1, test.err, err)
  93. }
  94. }
  95. }
  96. // Tests maximum object size.
  97. func TestMaxObjectSize(t *testing.T) {
  98. sizes := []struct {
  99. isMax bool
  100. size int64
  101. }{
  102. // Test - 1 - maximum object size.
  103. {
  104. true,
  105. maxObjectSize + 1,
  106. },
  107. // Test - 2 - not maximum object size.
  108. {
  109. false,
  110. maxObjectSize - 1,
  111. },
  112. }
  113. for i, s := range sizes {
  114. isMax := isMaxObjectSize(s.size)
  115. if isMax != s.isMax {
  116. t.Errorf("Test %d: Expected %t, got %t", i+1, s.isMax, isMax)
  117. }
  118. }
  119. }
  120. // Test urlPathSplit.
  121. func TestURLPathSplit(t *testing.T) {
  122. type test struct {
  123. urlPath string
  124. bucketName string
  125. prefixName string
  126. }
  127. testCases := []test{
  128. {
  129. urlPath: "/b/c/",
  130. bucketName: "b",
  131. prefixName: "c/",
  132. },
  133. {
  134. urlPath: "c/aa",
  135. bucketName: "c",
  136. prefixName: "aa",
  137. },
  138. {
  139. urlPath: "",
  140. bucketName: "",
  141. prefixName: "",
  142. },
  143. {
  144. urlPath: "/b",
  145. bucketName: "b",
  146. prefixName: "",
  147. },
  148. }
  149. for i, testCase := range testCases {
  150. bucketName, prefixName := urlPathSplit(testCase.urlPath)
  151. if bucketName != testCase.bucketName {
  152. t.Errorf("Tets %d: Expected %s, %s", i+1, testCase.bucketName, bucketName)
  153. }
  154. if prefixName != testCase.prefixName {
  155. t.Errorf("Tets %d: Expected %s, %s", i+1, testCase.bucketName, bucketName)
  156. }
  157. }
  158. }
  159. // Tests minimum allowed part size.
  160. func TestMinAllowedPartSize(t *testing.T) {
  161. sizes := []struct {
  162. isMin bool
  163. size int64
  164. }{
  165. // Test - 1 - within minimum part size.
  166. {
  167. true,
  168. minPartSize + 1,
  169. },
  170. // Test - 2 - smaller than minimum part size.
  171. {
  172. false,
  173. minPartSize - 1,
  174. },
  175. }
  176. for i, s := range sizes {
  177. isMin := isMinAllowedPartSize(s.size)
  178. if isMin != s.isMin {
  179. t.Errorf("Test %d: Expected %t, got %t", i+1, s.isMin, isMin)
  180. }
  181. }
  182. }
  183. // Tests maximum allowed part number.
  184. func TestMaxPartID(t *testing.T) {
  185. sizes := []struct {
  186. isMax bool
  187. partN int
  188. }{
  189. // Test - 1 part number within max part number.
  190. {
  191. false,
  192. maxPartID - 1,
  193. },
  194. // Test - 2 part number bigger than max part number.
  195. {
  196. true,
  197. maxPartID + 1,
  198. },
  199. }
  200. for i, s := range sizes {
  201. isMax := isMaxPartID(s.partN)
  202. if isMax != s.isMax {
  203. t.Errorf("Test %d: Expected %t, got %t", i+1, s.isMax, isMax)
  204. }
  205. }
  206. }
  207. // Tests fetch local address.
  208. func TestLocalAddress(t *testing.T) {
  209. if runtime.GOOS == "windows" {
  210. return
  211. }
  212. // need to set this to avoid stale values from other tests.
  213. globalMinioPort = 9000
  214. testCases := []struct {
  215. srvCmdConfig serverCmdConfig
  216. localAddr string
  217. }{
  218. // Test 1 - local address is found.
  219. {
  220. srvCmdConfig: serverCmdConfig{
  221. isDistXL: true,
  222. disks: []string{
  223. "localhost:/mnt/disk1",
  224. "1.1.1.2:/mnt/disk2",
  225. "1.1.2.1:/mnt/disk3",
  226. "1.1.2.2:/mnt/disk4",
  227. },
  228. },
  229. localAddr: fmt.Sprintf("localhost:%d", globalMinioPort),
  230. },
  231. // Test 2 - local address is everything.
  232. {
  233. srvCmdConfig: serverCmdConfig{
  234. isDistXL: false,
  235. disks: []string{
  236. "/mnt/disk1",
  237. "/mnt/disk2",
  238. "/mnt/disk3",
  239. "/mnt/disk4",
  240. },
  241. },
  242. localAddr: fmt.Sprintf(":%d", globalMinioPort),
  243. },
  244. // Test 3 - local address is not found.
  245. {
  246. srvCmdConfig: serverCmdConfig{
  247. isDistXL: true,
  248. disks: []string{
  249. "1.1.1.1:/mnt/disk1",
  250. "1.1.1.2:/mnt/disk2",
  251. "1.1.2.1:/mnt/disk3",
  252. "1.1.2.2:/mnt/disk4",
  253. },
  254. },
  255. localAddr: "",
  256. },
  257. }
  258. // Validates fetching local address.
  259. for i, testCase := range testCases {
  260. localAddr := getLocalAddress(testCase.srvCmdConfig)
  261. if localAddr != testCase.localAddr {
  262. t.Fatalf("Test %d: Expected %s, got %s", i+1, testCase.localAddr, localAddr)
  263. }
  264. }
  265. }