Skip to content

Commit 447a94c

Browse files
committed
[Tests] updated artifact script
Updating artifact to enable running integration tests and functional tests to pull an artifact from the current hosted distributions. At the time of this commit, there is not manifest hosted but there are static links which can have an unknown amount of RC versions if the GA snapshot does not exist for that version. But the assumption is that it will not be too high. Deprecating the previous implementation but we can remove that in a future iteration. Wanted to leave that available incase others use that for custom manifests. Enable tests that depended on snapshots as well. Issues resolved: opensearch-project#242 opensearch-project#19 Signed-off-by: Kawika Avilla <kavilla414@gmail.com>
1 parent b102ed9 commit 447a94c

File tree

5 files changed

+164
-102
lines changed

5 files changed

+164
-102
lines changed

packages/osd-opensearch-archiver/src/lib/docs/__tests__/generate_doc_records_stream.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('opensearchArchiver: createGenerateDocRecordsStream()', () => {
104104
await delay(200);
105105
return {
106106
_scroll_id: 'index1ScrollId',
107-
hits: { total: 2, hits: [{ _id: 1, _index: '.opensearch_dashboards_foo' }] },
107+
hits: { total: 2, hits: [{ _id: 1, _index: '.opensearch_dashboards_1' }] },
108108
};
109109
},
110110
async (name, params) => {

packages/osd-opensearch/src/artifact.js

+90-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
* Modifications Copyright OpenSearch Contributors. See
3030
* GitHub history for details.
3131
*/
32-
3332
const fetch = require('node-fetch');
3433
const AbortController = require('abort-controller');
3534
const fs = require('fs');
@@ -40,8 +39,11 @@ const { createHash } = require('crypto');
4039
const path = require('path');
4140

4241
const asyncPipeline = promisify(pipeline);
43-
const DAILY_SNAPSHOTS_BASE_URL = '';
42+
const DAILY_SNAPSHOTS_BASE_URL = 'https://artifacts.opensearch.org/snapshots/core/opensearch';
43+
// TODO: [RENAMEME] currently do not have an existing replacement
4444
const PERMANENT_SNAPSHOTS_BASE_URL = '';
45+
// Since we do not have a manifest URL, limiting how many RC checks to 5 should be more than enough
46+
const MAX_RC_CHECK = 5;
4547

4648
const { cache } = require('./utils');
4749
const { resolveCustomSnapshotUrl } = require('./custom_snapshots');
@@ -96,6 +98,21 @@ async function fetchSnapshotManifest(url, log) {
9698
return { abc, resp, json };
9799
}
98100

101+
async function verifySnapshotUrl(url, log) {
102+
log.info('Verifying snapshot URL at %s', chalk.bold(url));
103+
104+
const abc = new AbortController();
105+
const resp = await retry(
106+
log,
107+
async () => await fetch(url, { signal: abc.signal }, { method: 'HEAD' })
108+
);
109+
110+
return { abc, resp };
111+
}
112+
113+
/**
114+
* @deprecated This method should not be used, uses logic for resources we do not have access to.
115+
*/
99116
async function getArtifactSpecForSnapshot(urlVersion, license, log) {
100117
const desiredVersion = urlVersion.replace('-SNAPSHOT', '');
101118
const desiredLicense = license === 'oss' ? 'oss' : 'default';
@@ -153,6 +170,76 @@ async function getArtifactSpecForSnapshot(urlVersion, license, log) {
153170
};
154171
}
155172

173+
async function getArtifactSpecForSnapshotFromUrl(urlVersion, log) {
174+
const desiredVersion = urlVersion.replace('-SNAPSHOT', '');
175+
176+
if (process.env.OPENSEARCH_SNAPSHOT_MANIFEST) {
177+
return await getArtifactSpecForSnapshot(urlVersion, 'oss', log);
178+
}
179+
180+
// [RENAMEME] Need replacement for other platforms.
181+
const platform = process.platform === 'win32' ? 'windows' : process.platform;
182+
const arch = process.arch === 'arm64' ? 'arm64' : 'x64';
183+
if (platform !== 'linux' || arch !== 'x64') {
184+
throw createCliError(`Snapshots are only available for Linux x64`);
185+
}
186+
187+
const latestUrl = `${DAILY_SNAPSHOTS_BASE_URL}/${desiredVersion}`;
188+
const latestFile = `opensearch-${desiredVersion}-SNAPSHOT-${platform}-${arch}-latest.tar.gz`;
189+
const completeLatestUrl = `${latestUrl}/${latestFile}`;
190+
191+
let { abc, resp } = await verifySnapshotUrl(completeLatestUrl, log);
192+
193+
if (resp.ok) {
194+
return {
195+
url: completeLatestUrl,
196+
checksumUrl: completeLatestUrl + '.sha512',
197+
checksumType: 'sha512',
198+
filename: latestFile,
199+
};
200+
}
201+
202+
log.info(
203+
'Daily general-availability snapshot URL not found for current version, falling back to release-candidate snapshot URL.'
204+
);
205+
206+
let completeUrl = null;
207+
let snapshotFile = null;
208+
209+
// This checks and uses an RC if a RC exists at a higher increment than RC1 or it tries to use RC1
210+
// This is in replacement of having a manifest URL, so the exact RC number is unknown but expect it not to be a large number
211+
let rcCheck = MAX_RC_CHECK;
212+
do {
213+
const secondaryLatestUrl = `${DAILY_SNAPSHOTS_BASE_URL}/${desiredVersion}-rc${rcCheck}`;
214+
const secondaryLatestFile = `opensearch-${desiredVersion}-rc${rcCheck}-SNAPSHOT-${platform}-${arch}-latest.tar.gz`;
215+
const completeSecondaryLatestUrl = `${secondaryLatestUrl}/${secondaryLatestFile}`;
216+
({ abc, resp } = await verifySnapshotUrl(completeSecondaryLatestUrl, log));
217+
218+
if (resp.ok) {
219+
completeUrl = completeSecondaryLatestUrl;
220+
snapshotFile = secondaryLatestFile;
221+
break;
222+
}
223+
} while (rcCheck-- >= 1);
224+
225+
if (resp.status === 404 || !completeUrl || !snapshotFile) {
226+
abc.abort();
227+
throw createCliError(`Snapshots for ${desiredVersion} are not available`);
228+
}
229+
230+
if (!resp.ok) {
231+
abc.abort();
232+
throw new Error(`Unable to read snapshot url: ${resp.statusText}`);
233+
}
234+
235+
return {
236+
url: completeUrl,
237+
checksumUrl: completeUrl + '.sha512',
238+
checksumType: 'sha512',
239+
filename: snapshotFile,
240+
};
241+
}
242+
156243
exports.Artifact = class Artifact {
157244
/**
158245
* Fetch an Artifact from the Artifact API for a license level and version
@@ -168,7 +255,7 @@ exports.Artifact = class Artifact {
168255
return new Artifact(customSnapshotArtifactSpec, log);
169256
}
170257

171-
const artifactSpec = await getArtifactSpecForSnapshot(urlVersion, license, log);
258+
const artifactSpec = await getArtifactSpecForSnapshotFromUrl(urlVersion, log);
172259
return new Artifact(artifactSpec, log);
173260
}
174261

packages/osd-opensearch/src/artifact.test.js

+71-94
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,33 @@ import { Artifact } from './artifact';
4040
const log = new ToolingLog();
4141
let MOCKS;
4242

43+
const DAILY_SNAPSHOT_BASE_URL = 'https://artifacts.opensearch.org/snapshots/core/opensearch';
44+
const PERMANENT_SNAPSHOT_BASE_URL = '';
45+
46+
const ORIGINAL_PLATFROM = process.platform;
47+
const ORIGINAL_ARCHITECTURE = process.arch;
4348
const PLATFORM = process.platform === 'win32' ? 'windows' : process.platform;
4449
const ARCHITECTURE = process.arch === 'arm64' ? 'arm64' : 'x64';
4550
const MOCK_VERSION = 'test-version';
46-
const MOCK_URL = 'http://127.0.0.1:12345';
47-
const MOCK_FILENAME = 'test-filename';
51+
const MOCK_RC_VERSION = `test-version-rc4`;
52+
const MOCK_FILENAME = 'opensearch-test-version-SNAPSHOT-linux-x64-latest.tar.gz';
53+
const MOCK_RC_FILENAME = `opensearch-test-version-rc4-SNAPSHOT-linux-x64-latest.tar.gz`;
54+
const MOCK_URL = `${DAILY_SNAPSHOT_BASE_URL}/${MOCK_VERSION}/${MOCK_FILENAME}`;
55+
const MOCK_RC_URL = `${DAILY_SNAPSHOT_BASE_URL}/${MOCK_RC_VERSION}/${MOCK_RC_FILENAME}`;
4856

49-
const DAILY_SNAPSHOT_BASE_URL = '';
50-
const PERMANENT_SNAPSHOT_BASE_URL = '';
57+
const itif = process.platfom !== 'linux' || process.arch !== 'x64' ? it : it.skip;
5158

5259
const createArchive = (params = {}) => {
53-
const license = params.license || 'default';
5460
const architecture = params.architecture || ARCHITECTURE;
61+
const useRCVersion = params.useRCVersion || false;
5562

5663
return {
57-
license: 'default',
64+
license: 'oss',
5865
architecture,
59-
version: MOCK_VERSION,
60-
url: MOCK_URL + `/${license}`,
66+
version: !useRCVersion ? MOCK_VERSION : MOCK_RC_VERSION,
67+
url: !useRCVersion ? MOCK_URL : MOCK_RC_URL,
6168
platform: PLATFORM,
62-
filename: MOCK_FILENAME + `-${architecture}.${license}`,
69+
filename: !useRCVersion ? MOCK_FILENAME : MOCK_RC_FILENAME,
6370
...params,
6471
};
6572
};
@@ -92,99 +99,87 @@ beforeEach(() => {
9299
jest.resetAllMocks();
93100

94101
MOCKS = {
95-
valid: {
96-
archives: [createArchive({ license: 'oss' }), createArchive({ license: 'default' })],
102+
GA: {
103+
archives: [createArchive({ useRCVersion: false })],
104+
},
105+
RC: {
106+
archives: [createArchive({ useRCVersion: true })],
97107
},
98108
multipleArch: {
99109
archives: [
100-
createArchive({ architecture: 'fake_arch', license: 'oss' }),
101-
createArchive({ architecture: ARCHITECTURE, license: 'oss' }),
110+
createArchive({ architecture: 'fake_arch', useRCVersion: false }),
111+
createArchive({ architecture: ARCHITECTURE, useRCVersion: false }),
102112
],
103113
},
104114
};
105115
});
106116

107-
const artifactTest = (requestedLicense, expectedLicense, fetchTimesCalled = 1) => {
117+
const artifactTest = (fetchTimesCalled = 1) => {
108118
return async () => {
109-
const artifact = await Artifact.getSnapshot(requestedLicense, MOCK_VERSION, log);
119+
const artifact = await Artifact.getSnapshot('oss', MOCK_VERSION, log);
120+
const expectedUrl = fetchTimesCalled === 1 ? MOCK_URL : MOCK_RC_URL;
121+
const expectedFilename = fetchTimesCalled === 1 ? MOCK_FILENAME : MOCK_RC_FILENAME;
110122
expect(fetch).toHaveBeenCalledTimes(fetchTimesCalled);
111-
expect(fetch.mock.calls[0][0]).toEqual(
112-
`${DAILY_SNAPSHOT_BASE_URL}/${MOCK_VERSION}/manifest-latest-verified.json`
113-
);
114-
if (fetchTimesCalled === 2) {
115-
expect(fetch.mock.calls[1][0]).toEqual(
116-
`${PERMANENT_SNAPSHOT_BASE_URL}/${MOCK_VERSION}/manifest.json`
117-
);
123+
expect(fetch.mock.calls[0][0]).toEqual(MOCK_URL);
124+
if (fetchTimesCalled !== 1) {
125+
expect(fetch.mock.calls[fetchTimesCalled - 1][0]).toEqual(MOCK_RC_URL);
118126
}
119-
expect(artifact.getUrl()).toEqual(MOCK_URL + `/${expectedLicense}`);
120-
expect(artifact.getChecksumUrl()).toEqual(MOCK_URL + `/${expectedLicense}.sha512`);
127+
expect(artifact.getUrl()).toEqual(expectedUrl);
128+
expect(artifact.getChecksumUrl()).toEqual(expectedUrl + '.sha512');
121129
expect(artifact.getChecksumType()).toEqual('sha512');
122-
expect(artifact.getFilename()).toEqual(MOCK_FILENAME + `-${ARCHITECTURE}.${expectedLicense}`);
130+
expect(artifact.getFilename()).toEqual(expectedFilename);
123131
};
124132
};
125133

126134
describe('Artifact', () => {
127135
describe('getSnapshot()', () => {
128-
describe('with default snapshot', () => {
129-
beforeEach(() => {
130-
mockFetch(MOCKS.valid);
131-
});
132-
133-
it('should return artifact metadata for a daily oss artifact', artifactTest('oss', 'oss'));
136+
itif('should return artifact metadata for a daily GA artifact', () => {
137+
mockFetch(MOCKS.GA);
138+
artifactTest();
139+
});
134140

135-
it(
136-
'should return artifact metadata for a daily default artifact',
137-
artifactTest('default', 'default')
138-
);
141+
itif('should return artifact metadata for a RC artifact', () => {
142+
fetch.mockReturnValueOnce(Promise.resolve(new Response('', { status: 404 })));
143+
fetch.mockReturnValueOnce(Promise.resolve(new Response('', { status: 404 })));
144+
mockFetch(MOCKS.RC);
145+
artifactTest(3);
146+
});
139147

140-
it(
141-
'should default to default license with anything other than "oss"',
142-
artifactTest('INVALID_LICENSE', 'default')
148+
itif('should throw when an artifact cannot be found for the specified parameters', async () => {
149+
fetch.mockReturnValue(Promise.resolve(new Response('', { status: 404 })));
150+
await expect(Artifact.getSnapshot('default', 'INVALID_VERSION', log)).rejects.toThrow(
151+
'Snapshots for INVALID_VERSION are not available'
143152
);
144-
145-
it('should throw when an artifact cannot be found in the manifest for the specified parameters', async () => {
146-
await expect(Artifact.getSnapshot('default', 'INVALID_VERSION', log)).rejects.toThrow(
147-
"couldn't find an artifact"
148-
);
149-
});
150153
});
151154

152-
describe('with missing default snapshot', () => {
153-
beforeEach(() => {
154-
fetch.mockReturnValueOnce(Promise.resolve(new Response('', { status: 404 })));
155-
mockFetch(MOCKS.valid);
155+
describe('with snapshots for multiple architectures', () => {
156+
afterAll(() => {
157+
Object.defineProperties(process, {
158+
platform: {
159+
value: ORIGINAL_PLATFROM,
160+
},
161+
arch: {
162+
value: ORIGINAL_ARCHITECTURE,
163+
},
164+
});
156165
});
157166

158-
it(
159-
'should return artifact metadata for a permanent oss artifact',
160-
artifactTest('oss', 'oss', 2)
161-
);
162-
163-
it(
164-
'should return artifact metadata for a permanent default artifact',
165-
artifactTest('default', 'default', 2)
166-
);
167-
168-
it(
169-
'should default to default license with anything other than "oss"',
170-
artifactTest('INVALID_LICENSE', 'default', 2)
171-
);
172-
173-
it('should throw when an artifact cannot be found in the manifest for the specified parameters', async () => {
174-
await expect(Artifact.getSnapshot('default', 'INVALID_VERSION', log)).rejects.toThrow(
175-
"couldn't find an artifact"
167+
it('should throw when on a non-Linux platform', async () => {
168+
Object.defineProperty(process, 'platform', {
169+
value: 'win32',
170+
});
171+
await expect(Artifact.getSnapshot('default', 'INVALID_PLATFORM', log)).rejects.toThrow(
172+
'Snapshots are only available for Linux x64'
176173
);
177174
});
178-
});
179-
180-
describe('with snapshots for multiple architectures', () => {
181-
beforeEach(() => {
182-
mockFetch(MOCKS.multipleArch);
183-
});
184175

185-
it('should return artifact metadata for the correct architecture', async () => {
186-
const artifact = await Artifact.getSnapshot('oss', MOCK_VERSION, log);
187-
expect(artifact.getFilename()).toEqual(MOCK_FILENAME + `-${ARCHITECTURE}.oss`);
176+
it('should throw when on a non-x64 arch', async () => {
177+
Object.defineProperty(process, 'arch', {
178+
value: 'arm64',
179+
});
180+
await expect(Artifact.getSnapshot('default', 'INVALID_ARCH', log)).rejects.toThrow(
181+
'Snapshots are only available for Linux x64'
182+
);
188183
});
189184
});
190185

@@ -193,7 +188,7 @@ describe('Artifact', () => {
193188

194189
beforeEach(() => {
195190
process.env.OPENSEARCH_SNAPSHOT_MANIFEST = CUSTOM_URL;
196-
mockFetch(MOCKS.valid);
191+
mockFetch(MOCKS.GA);
197192
});
198193

199194
it('should use the custom URL when looking for a snapshot', async () => {
@@ -205,23 +200,5 @@ describe('Artifact', () => {
205200
delete process.env.OPENSEARCH_SNAPSHOT_MANIFEST;
206201
});
207202
});
208-
209-
describe('with latest unverified snapshot', () => {
210-
beforeEach(() => {
211-
process.env.OSD_OPENSEARCH_SNAPSHOT_USE_UNVERIFIED = 1;
212-
mockFetch(MOCKS.valid);
213-
});
214-
215-
it('should use the daily unverified URL when looking for a snapshot', async () => {
216-
await Artifact.getSnapshot('oss', MOCK_VERSION, log);
217-
expect(fetch.mock.calls[0][0]).toEqual(
218-
`${DAILY_SNAPSHOT_BASE_URL}/${MOCK_VERSION}/manifest-latest.json`
219-
);
220-
});
221-
222-
afterEach(() => {
223-
delete process.env.OSD_OPENSEARCH_SNAPSHOT_USE_UNVERIFIED;
224-
});
225-
});
226203
});
227204
});

src/core/server/ui_settings/create_or_upgrade_saved_config/integration_tests/create_or_upgrade.test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ describe('createOrUpgradeSavedConfig()', () => {
9595
await osd.stop();
9696
}, 30000);
9797

98-
// TODO: [RENAMEME] Test can be enabled once there is a valid snapshot URL
99-
xit('upgrades the previous version on each increment', async function () {
98+
it('upgrades the previous version on each increment', async function () {
10099
// ------------------------------------
101100
// upgrade to 5.4.0
102101
await createOrUpgradeSavedConfig({

src/core/server/ui_settings/integration_tests/index.test.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ import { docExistsSuite } from './doc_exists';
3636
import { docMissingSuite } from './doc_missing';
3737
import { docMissingAndIndexReadOnlySuite } from './doc_missing_and_index_read_only';
3838

39-
// TODO: [RENAMEME] Test can be enabled once there is a valid snapshot URL
40-
xdescribe('uiSettings/routes', function () {
39+
describe('uiSettings/routes', function () {
4140
jest.setTimeout(10000);
4241

4342
beforeAll(startServers);

0 commit comments

Comments
 (0)