From 06bb00e5161cda622bd6c3236d74226592272fdc Mon Sep 17 00:00:00 2001 From: seven Date: Sat, 29 Jul 2023 01:08:15 +0800 Subject: [PATCH] fix arm binary downlaod issue :q --- package-lock.json | 4 +- package.json | 2 +- src/engine.ts | 2 +- src/utils.ts | 31 +++++---- tests/utils.test.ts | 98 +++++++++++++++++++++++++++++ tests/{unitls => utils}/common.ts | 0 tests/{unitls => utils}/fixtures.ts | 0 7 files changed, 120 insertions(+), 17 deletions(-) create mode 100644 tests/utils.test.ts rename tests/{unitls => utils}/common.ts (100%) rename tests/{unitls => utils}/fixtures.ts (100%) diff --git a/package-lock.json b/package-lock.json index 78ccf60..7899e62 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@geek-fun/jest-search", - "version": "1.0.3", + "version": "1.0.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@geek-fun/jest-search", - "version": "1.0.3", + "version": "1.0.4", "license": "MIT", "dependencies": { "debug": "^4.3.4", diff --git a/package.json b/package.json index 17f69e2..963e5a1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@geek-fun/jest-search", - "version": "1.0.3", + "version": "1.0.4", "main": "dist/src/index.js", "types": "dist/src/index.d.ts", "description": "Jest preset for running tests with local search platform", diff --git a/src/engine.ts b/src/engine.ts index 545654a..b5c65bc 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -26,7 +26,7 @@ let engineOptions: ConfiguredOptions; let engineClient: EngineClient; const prepareEngine = async (engine: EngineType, version: string, binaryLocation: string) => { - const url = await getEngineBinaryURL(engine, version); + const url = getEngineBinaryURL(engine, version); return await download(url, binaryLocation, engine, version); }; diff --git a/src/utils.ts b/src/utils.ts index 1d992b6..13e0a52 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,5 @@ import { access, constants } from 'fs'; import { promisify } from 'util'; -import execa from 'execa'; import { debug } from './debug'; import { Artifacts, EngineType } from './constants'; import { extract } from 'tar-fs'; @@ -9,6 +8,7 @@ import fetch from 'node-fetch'; import { pipeline } from 'stream'; import { EngineClient } from './engineClient'; import { HttpsProxyAgent } from 'https-proxy-agent'; +import * as os from 'os'; export const waitForLocalhost = async (engineClient: EngineClient, retries = 30) => { await new Promise((resolve) => setTimeout(() => resolve(0), 2000)); @@ -34,9 +34,9 @@ export const isFileExists = async (path: string): Promise => { } }; -export const platform = async () => { - const { stdout: sysName } = await execa('uname', ['-s']); - const { stdout: arch } = await execa('uname', ['-m']); +const platform = () => { + const arch = os.arch().toString(); + const sysName = os.platform().toString(); debug(`checking platform uname: ${sysName} ${arch}`); return { sysName: sysName.toLowerCase(), arch: arch.toLowerCase() }; }; @@ -84,19 +84,24 @@ export const download = async (url: string, dir: string, engine: EngineType, ver ); }; -export const getEngineBinaryURL = async (engine: EngineType, version: string) => { - const { sysName, arch } = await platform(); +export const getEngineBinaryURL = (engine: EngineType, version: string) => { + const { sysName, arch } = platform(); const engines: { [engineType: string]: () => string; } = { - [EngineType.ELASTICSEARCH]: () => - parseInt(version.charAt(0)) >= 7 - ? `${Artifacts.ES}-${version}-${sysName}-${arch}.tar.gz` - : `${Artifacts.ES}-${version}.tar.gz`, + [EngineType.ELASTICSEARCH]: () => { + const archName = arch === 'arm64' ? 'aarch64' : 'x86_64'; + return parseInt(version.charAt(0)) >= 7 + ? `${Artifacts.ES}-${version}-${sysName}-${archName}.tar.gz` + : `${Artifacts.ES}-${version}.tar.gz`; + }, [EngineType.OPENSEARCH]: () => - `${Artifacts.OS}/${version}/opensearch-${version}-linux-${arch.replace('86_', '')}.tar.gz`, - [EngineType.ZINCSEARCH]: () => - `${Artifacts.ZINC}/v${version}/zincsearch_${version}_${sysName}_${arch}.tar.gz`, + `${Artifacts.OS}/${version}/opensearch-${version}-linux-${arch}.tar.gz`, + + [EngineType.ZINCSEARCH]: () => { + const archName = arch === 'x64' ? 'x86_64' : arch; + return `${Artifacts.ZINC}/v${version}/zincsearch_${version}_${sysName}_${archName}.tar.gz`; + }, }; return engines[engine](); diff --git a/tests/utils.test.ts b/tests/utils.test.ts new file mode 100644 index 0000000..329d028 --- /dev/null +++ b/tests/utils.test.ts @@ -0,0 +1,98 @@ +import { getEngineBinaryURL } from '../src/utils'; +import { EngineType } from '../src'; + +const mockedPlatform = jest.fn(); +const mockedArch = jest.fn(); + +jest.mock('os', () => ({ + ...jest.requireActual('os'), + platform: () => mockedPlatform(), + arch: () => mockedArch(), +})); + +const platforms = [ + { + engine: EngineType.ELASTICSEARCH, + version: '8.9.0', + platform: 'darwin', + arch: 'x64', + URL: 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.9.0-darwin-x86_64.tar.gz', + }, + { + engine: EngineType.ELASTICSEARCH, + version: '8.9.0', + platform: 'darwin', + arch: 'arm64', + URL: 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.9.0-darwin-aarch64.tar.gz', + }, + { + engine: EngineType.ELASTICSEARCH, + version: '8.9.0', + platform: 'linux', + arch: 'x64', + URL: 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.9.0-linux-x86_64.tar.gz', + }, + { + engine: EngineType.ELASTICSEARCH, + version: '8.9.0', + platform: 'linux', + arch: 'arm64', + URL: 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.9.0-linux-aarch64.tar.gz', + }, + { + engine: EngineType.ELASTICSEARCH, + version: '6.8.23', + platform: 'darwin', + arch: 'x64', + URL: 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.8.23.tar.gz', + }, + { + engine: EngineType.ELASTICSEARCH, + version: '8.9.0', + platform: 'linux', + arch: 'arm64', + URL: 'https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.9.0-linux-aarch64.tar.gz', + }, + { + engine: EngineType.OPENSEARCH, + version: '2.9.0', + platform: 'darwin', + arch: 'x64', + URL: 'https://artifacts.opensearch.org/releases/bundle/opensearch/2.9.0/opensearch-2.9.0-linux-x64.tar.gz', + }, + { + engine: EngineType.OPENSEARCH, + version: '2.9.0', + platform: 'linux', + arch: 'arm64', + URL: 'https://artifacts.opensearch.org/releases/bundle/opensearch/2.9.0/opensearch-2.9.0-linux-arm64.tar.gz', + }, + { + engine: EngineType.ZINCSEARCH, + version: '0.4.7', + platform: 'darwin', + arch: 'x64', + URL: 'https://github.com/zincsearch/zincsearch/releases/download/v0.4.7/zincsearch_0.4.7_darwin_x86_64.tar.gz', + }, + { + engine: EngineType.ZINCSEARCH, + version: '0.4.7', + platform: 'linux', + arch: 'arm64', + URL: 'https://github.com/zincsearch/zincsearch/releases/download/v0.4.7/zincsearch_0.4.7_linux_arm64.tar.gz', + }, +]; + +describe('unit test for utils', () => { + describe('unit test for getEngineBinaryURL', () => { + platforms.forEach(({ engine, version, platform, arch, URL }) => { + it(`should return ${engine} ${version} ${platform} ${arch} download location`, () => { + mockedPlatform.mockReturnValue(platform); + mockedArch.mockReturnValue(arch); + + const binaryURL = getEngineBinaryURL(engine, version); + expect(binaryURL).toEqual(URL); + }); + }); + }); +}); diff --git a/tests/unitls/common.ts b/tests/utils/common.ts similarity index 100% rename from tests/unitls/common.ts rename to tests/utils/common.ts diff --git a/tests/unitls/fixtures.ts b/tests/utils/fixtures.ts similarity index 100% rename from tests/unitls/fixtures.ts rename to tests/utils/fixtures.ts