minio.integration.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
  2. import { MinioService } from '@/server/modules/files/minio.service';
  3. import { Client } from 'minio';
  4. import { logger } from '@/server/utils/logger';
  5. // Mock dependencies
  6. vi.mock('minio');
  7. vi.mock('@/server/utils/logger');
  8. describe('MinIO Integration Tests', () => {
  9. let minioService: MinioService;
  10. let mockClient: Client;
  11. beforeEach(() => {
  12. mockClient = new Client({} as any);
  13. (Client as any).mockClear();
  14. // Mock environment variables for testing
  15. vi.stubEnv('MINIO_HOST', 'localhost');
  16. vi.stubEnv('MINIO_PORT', '9000');
  17. vi.stubEnv('MINIO_USE_SSL', 'false');
  18. vi.stubEnv('MINIO_BUCKET_NAME', 'test-bucket');
  19. // Create MinioService with mock client
  20. minioService = new MinioService();
  21. // Replace the internal client with our mock
  22. minioService['client'] = mockClient;
  23. });
  24. afterEach(() => {
  25. vi.clearAllMocks();
  26. });
  27. describe('Bucket Operations', () => {
  28. it('should ensure bucket exists and set policy', async () => {
  29. // Mock bucket doesn't exist
  30. mockClient.bucketExists = vi.fn().mockResolvedValue(false);
  31. mockClient.makeBucket = vi.fn().mockResolvedValue(undefined);
  32. mockClient.setBucketPolicy = vi.fn().mockResolvedValue(undefined);
  33. const result = await minioService.ensureBucketExists();
  34. expect(result).toBe(true);
  35. expect(mockClient.bucketExists).toHaveBeenCalledWith('test-bucket');
  36. expect(mockClient.makeBucket).toHaveBeenCalledWith('test-bucket');
  37. expect(mockClient.setBucketPolicy).toHaveBeenCalled();
  38. expect(logger.db).toHaveBeenCalledWith('Created new bucket: test-bucket');
  39. });
  40. it('should handle existing bucket', async () => {
  41. mockClient.bucketExists = vi.fn().mockResolvedValue(true);
  42. const result = await minioService.ensureBucketExists();
  43. expect(result).toBe(true);
  44. expect(mockClient.bucketExists).toHaveBeenCalledWith('test-bucket');
  45. expect(mockClient.makeBucket).not.toHaveBeenCalled();
  46. expect(mockClient.setBucketPolicy).not.toHaveBeenCalled();
  47. });
  48. });
  49. describe('File Operations', () => {
  50. it('should upload and download file successfully', async () => {
  51. const testContent = Buffer.from('Hello, MinIO!');
  52. const mockUrl = 'http://localhost:9000/test-bucket/test.txt';
  53. // Mock bucket operations
  54. mockClient.bucketExists = vi.fn().mockResolvedValue(true);
  55. mockClient.putObject = vi.fn().mockResolvedValue(undefined);
  56. mockClient.statObject = vi.fn().mockResolvedValue({ size: testContent.length } as any);
  57. mockClient.getObject = vi.fn().mockReturnValue({
  58. on: (event: string, callback: Function) => {
  59. if (event === 'data') callback(testContent);
  60. if (event === 'end') callback();
  61. }
  62. } as any);
  63. // Upload file
  64. const uploadUrl = await minioService.createObject('test-bucket', 'test.txt', testContent, 'text/plain');
  65. expect(uploadUrl).toBe(mockUrl);
  66. expect(mockClient.putObject).toHaveBeenCalledWith(
  67. 'test-bucket',
  68. 'test.txt',
  69. testContent,
  70. testContent.length,
  71. { 'Content-Type': 'text/plain' }
  72. );
  73. // Check file exists
  74. const exists = await minioService.objectExists('test-bucket', 'test.txt');
  75. expect(exists).toBe(true);
  76. expect(mockClient.statObject).toHaveBeenCalledWith('test-bucket', 'test.txt');
  77. });
  78. it('should handle file not found', async () => {
  79. const notFoundError = new Error('Object not found');
  80. notFoundError.message = 'not found';
  81. mockClient.statObject = vi.fn().mockRejectedValue(notFoundError);
  82. const exists = await minioService.objectExists('test-bucket', 'nonexistent.txt');
  83. expect(exists).toBe(false);
  84. });
  85. it('should delete file successfully', async () => {
  86. mockClient.removeObject = vi.fn().mockResolvedValue(undefined);
  87. await minioService.deleteObject('test-bucket', 'test.txt');
  88. expect(mockClient.removeObject).toHaveBeenCalledWith('test-bucket', 'test.txt');
  89. expect(logger.db).toHaveBeenCalledWith('Deleted object: test-bucket/test.txt');
  90. });
  91. });
  92. describe('Presigned URL Operations', () => {
  93. it('should generate presigned URLs correctly', async () => {
  94. const mockPresignedUrl = 'https://minio.example.com/presigned-url';
  95. mockClient.presignedGetObject = vi.fn().mockResolvedValue(mockPresignedUrl);
  96. // Test regular presigned URL
  97. const url = await minioService.getPresignedFileUrl('test-bucket', 'file.txt', 3600);
  98. expect(url).toBe(mockPresignedUrl);
  99. expect(mockClient.presignedGetObject).toHaveBeenCalledWith('test-bucket', 'file.txt', 3600);
  100. // Test download URL with content disposition
  101. const downloadUrl = await minioService.getPresignedFileDownloadUrl(
  102. 'test-bucket',
  103. 'file.txt',
  104. '测试文件.txt',
  105. 1800
  106. );
  107. expect(downloadUrl).toBe(mockPresignedUrl);
  108. expect(mockClient.presignedGetObject).toHaveBeenCalledWith(
  109. 'test-bucket',
  110. 'file.txt',
  111. 1800,
  112. {
  113. 'response-content-disposition': 'attachment; filename="%E6%B5%8B%E8%AF%95%E6%96%87%E4%BB%B6.txt"',
  114. 'response-content-type': 'application/octet-stream'
  115. }
  116. );
  117. });
  118. });
  119. describe('Multipart Upload Operations', () => {
  120. it('should handle multipart upload workflow', async () => {
  121. const mockUploadId = 'upload-123';
  122. const mockPartUrls = ['url1', 'url2', 'url3'];
  123. const mockStat = { size: 3072 };
  124. // Mock multipart operations
  125. mockClient.initiateNewMultipartUpload = vi.fn().mockResolvedValue(mockUploadId);
  126. mockClient.presignedUrl = vi.fn()
  127. .mockResolvedValueOnce('url1')
  128. .mockResolvedValueOnce('url2')
  129. .mockResolvedValueOnce('url3');
  130. mockClient.completeMultipartUpload = vi.fn().mockResolvedValue(undefined);
  131. mockClient.statObject = vi.fn().mockResolvedValue(mockStat as any);
  132. // Create multipart upload
  133. const uploadId = await minioService.createMultipartUpload('test-bucket', 'large-file.zip');
  134. expect(uploadId).toBe(mockUploadId);
  135. expect(mockClient.initiateNewMultipartUpload).toHaveBeenCalledWith(
  136. 'test-bucket',
  137. 'large-file.zip',
  138. {}
  139. );
  140. // Generate part URLs
  141. const partUrls = await minioService.generateMultipartUploadUrls(
  142. 'test-bucket',
  143. 'large-file.zip',
  144. mockUploadId,
  145. 3
  146. );
  147. expect(partUrls).toEqual(mockPartUrls);
  148. expect(mockClient.presignedUrl).toHaveBeenCalledTimes(3);
  149. // Complete multipart upload
  150. const parts = [
  151. { ETag: 'etag1', PartNumber: 1 },
  152. { ETag: 'etag2', PartNumber: 2 },
  153. { ETag: 'etag3', PartNumber: 3 }
  154. ];
  155. const result = await minioService.completeMultipartUpload(
  156. 'test-bucket',
  157. 'large-file.zip',
  158. mockUploadId,
  159. parts
  160. );
  161. expect(result).toEqual({ size: 3072 });
  162. expect(mockClient.completeMultipartUpload).toHaveBeenCalledWith(
  163. 'test-bucket',
  164. 'large-file.zip',
  165. mockUploadId,
  166. [{ part: 1, etag: 'etag1' }, { part: 2, etag: 'etag2' }, { part: 3, etag: 'etag3' }]
  167. );
  168. });
  169. });
  170. describe('Error Handling', () => {
  171. it('should handle MinIO connection errors', async () => {
  172. const connectionError = new Error('Connection refused');
  173. mockClient.bucketExists = vi.fn().mockRejectedValue(connectionError);
  174. await expect(minioService.ensureBucketExists()).rejects.toThrow(connectionError);
  175. expect(logger.error).toHaveBeenCalledWith(
  176. 'Failed to ensure bucket exists: test-bucket',
  177. connectionError
  178. );
  179. });
  180. it('should handle file operation errors', async () => {
  181. const operationError = new Error('Operation failed');
  182. mockClient.putObject = vi.fn().mockRejectedValue(operationError);
  183. await expect(minioService.createObject(
  184. 'test-bucket',
  185. 'test.txt',
  186. Buffer.from('test'),
  187. 'text/plain'
  188. )).rejects.toThrow(operationError);
  189. expect(logger.error).toHaveBeenCalledWith(
  190. 'Failed to create object test-bucket/test.txt:',
  191. operationError
  192. );
  193. });
  194. it('should handle permission errors gracefully', async () => {
  195. const permissionError = new Error('Permission denied');
  196. mockClient.statObject = vi.fn().mockRejectedValue(permissionError);
  197. await expect(minioService.objectExists('test-bucket', 'file.txt')).rejects.toThrow(permissionError);
  198. expect(logger.error).toHaveBeenCalledWith(
  199. 'Error checking existence of object test-bucket/file.txt:',
  200. permissionError
  201. );
  202. });
  203. });
  204. describe('Configuration Validation', () => {
  205. it('should validate MinIO configuration', () => {
  206. expect(minioService.bucketName).toBe('test-bucket');
  207. // Test URL generation with different configurations
  208. const url = minioService.getFileUrl('test-bucket', 'file.txt');
  209. expect(url).toBe('http://localhost:9000/test-bucket/file.txt');
  210. });
  211. it('should handle SSL configuration', async () => {
  212. // Create new instance with SSL
  213. vi.stubEnv('MINIO_USE_SSL', 'true');
  214. vi.stubEnv('MINIO_PORT', '443');
  215. const sslService = new MinioService();
  216. const url = sslService.getFileUrl('test-bucket', 'file.txt');
  217. expect(url).toBe('https://localhost:443/test-bucket/file.txt');
  218. });
  219. });
  220. describe('Performance Testing', () => {
  221. it('should handle concurrent operations', async () => {
  222. mockClient.presignedGetObject = vi.fn().mockResolvedValue('https://minio.example.com/file');
  223. // Test concurrent URL generation
  224. const promises = Array(10).fill(0).map((_, i) =>
  225. minioService.getPresignedFileUrl('test-bucket', `file${i}.txt`)
  226. );
  227. const results = await Promise.all(promises);
  228. expect(results).toHaveLength(10);
  229. expect(results.every(url => url === 'https://minio.example.com/file')).toBe(true);
  230. });
  231. it('should handle large file operations', async () => {
  232. const largeBuffer = Buffer.alloc(10 * 1024 * 1024); // 10MB
  233. mockClient.putObject = vi.fn().mockResolvedValue(undefined);
  234. await minioService.createObject('test-bucket', 'large-file.bin', largeBuffer, 'application/octet-stream');
  235. expect(mockClient.putObject).toHaveBeenCalledWith(
  236. 'test-bucket',
  237. 'large-file.bin',
  238. largeBuffer,
  239. largeBuffer.length,
  240. { 'Content-Type': 'application/octet-stream' }
  241. );
  242. });
  243. });
  244. });