Skip to content

Commit

Permalink
fix arm binary downlaod issue
Browse files Browse the repository at this point in the history
:q
  • Loading branch information
Blankll committed Jul 28, 2023
1 parent ac29b53 commit 06bb00e
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 17 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
31 changes: 18 additions & 13 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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));
Expand All @@ -34,9 +34,9 @@ export const isFileExists = async (path: string): Promise<boolean> => {
}
};

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() };
};
Expand Down Expand Up @@ -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]();
Expand Down
98 changes: 98 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
});
File renamed without changes.
File renamed without changes.

0 comments on commit 06bb00e

Please sign in to comment.