|
| 1 | +import { Client as MinioClient } from 'minio'; |
| 2 | +import { execSync } from 'child_process'; |
| 3 | +import { ErrorCode, MilvusClient } from '../../milvus'; |
| 4 | +import { GENERATE_NAME, IP } from '../tools'; |
| 5 | + |
| 6 | +const milvusClient = new MilvusClient({ address: IP, logLevel: 'info' }); |
| 7 | +let minioClient: MinioClient; |
| 8 | + |
| 9 | +const BUCKET_NAME = 'a-bucket'; |
| 10 | +const RESOURCE_NAME = GENERATE_NAME('file_resource'); |
| 11 | +const MISSING_RESOURCE_NAME = GENERATE_NAME('missing_file_resource'); |
| 12 | +const OBJECT_NAME = `node-sdk-file-resource/${RESOURCE_NAME}.txt`; |
| 13 | +const MISSING_OBJECT_NAME = `node-sdk-file-resource/${MISSING_RESOURCE_NAME}.txt`; |
| 14 | + |
| 15 | +const createMinioClient = (endPoint: string) => |
| 16 | + new MinioClient({ |
| 17 | + endPoint, |
| 18 | + port: 9000, |
| 19 | + useSSL: false, |
| 20 | + accessKey: 'minioadmin', |
| 21 | + secretKey: 'minioadmin', |
| 22 | + }); |
| 23 | + |
| 24 | +const getMilvusMinioContainerIP = () => |
| 25 | + execSync( |
| 26 | + `docker inspect milvus-minio --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'`, |
| 27 | + { encoding: 'utf8' } |
| 28 | + ).trim(); |
| 29 | + |
| 30 | +const putTestObject = async () => { |
| 31 | + const candidates = [ |
| 32 | + () => process.env.FILE_RESOURCE_MINIO_ENDPOINT || '127.0.0.1', |
| 33 | + getMilvusMinioContainerIP, |
| 34 | + ]; |
| 35 | + |
| 36 | + let lastError: unknown; |
| 37 | + for (const getEndPoint of candidates) { |
| 38 | + try { |
| 39 | + const endPoint = getEndPoint(); |
| 40 | + minioClient = createMinioClient(endPoint); |
| 41 | + await minioClient.putObject( |
| 42 | + BUCKET_NAME, |
| 43 | + OBJECT_NAME, |
| 44 | + Buffer.from('node sdk file resource test\n') |
| 45 | + ); |
| 46 | + return; |
| 47 | + } catch (error) { |
| 48 | + lastError = error; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + throw lastError; |
| 53 | +}; |
| 54 | + |
| 55 | +describe('FileResource API', () => { |
| 56 | + beforeAll(async () => { |
| 57 | + await putTestObject(); |
| 58 | + }); |
| 59 | + |
| 60 | + afterAll(async () => { |
| 61 | + try { |
| 62 | + await milvusClient.removeFileResource({ name: RESOURCE_NAME }); |
| 63 | + } catch { |
| 64 | + // best-effort cleanup |
| 65 | + } |
| 66 | + |
| 67 | + try { |
| 68 | + await minioClient.removeObject(BUCKET_NAME, OBJECT_NAME); |
| 69 | + } catch { |
| 70 | + // best-effort cleanup |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + it('should list file resources', async () => { |
| 75 | + const res = await milvusClient.listFileResources(); |
| 76 | + |
| 77 | + expect(res.status.error_code).toEqual(ErrorCode.SUCCESS); |
| 78 | + expect(Array.isArray(res.resources)).toEqual(true); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should reject add file resource with missing object path', async () => { |
| 82 | + const res = await milvusClient.addFileResource({ |
| 83 | + name: MISSING_RESOURCE_NAME, |
| 84 | + path: MISSING_OBJECT_NAME, |
| 85 | + }); |
| 86 | + |
| 87 | + expect(res.error_code).not.toEqual(ErrorCode.SUCCESS); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should add, list, and remove file resource', async () => { |
| 91 | + const add = await milvusClient.addFileResource({ |
| 92 | + name: RESOURCE_NAME, |
| 93 | + path: OBJECT_NAME, |
| 94 | + timeout: 10000, |
| 95 | + }); |
| 96 | + expect(add.error_code).toEqual(ErrorCode.SUCCESS); |
| 97 | + |
| 98 | + const list = await milvusClient.listFileResources({ timeout: 10000 }); |
| 99 | + expect(list.status.error_code).toEqual(ErrorCode.SUCCESS); |
| 100 | + |
| 101 | + const resource = list.resources.find(item => item.name === RESOURCE_NAME); |
| 102 | + expect(resource).toBeDefined(); |
| 103 | + expect(resource!.path).toEqual(OBJECT_NAME); |
| 104 | + |
| 105 | + const remove = await milvusClient.removeFileResource({ |
| 106 | + name: RESOURCE_NAME, |
| 107 | + timeout: 10000, |
| 108 | + }); |
| 109 | + expect(remove.error_code).toEqual(ErrorCode.SUCCESS); |
| 110 | + |
| 111 | + const listAfterRemove = await milvusClient.listFileResources(); |
| 112 | + expect( |
| 113 | + listAfterRemove.resources.some(item => item.name === RESOURCE_NAME) |
| 114 | + ).toEqual(false); |
| 115 | + }); |
| 116 | +}); |
0 commit comments