Skip to content

Commit 0e10ad4

Browse files
committed
Migrate integration tests to built JS files (#2750)
(cherry picked from commit 27774c9)
1 parent 2694b5d commit 0e10ad4

12 files changed

+630
-1759
lines changed

.buildkite/pipeline.yml

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
---
2+
agents:
3+
provider: "gcp"
4+
image: family/core-ubuntu-2204
5+
memory: "8G"
6+
cpu: "2"
7+
28
steps:
3-
- label: ":elasticsearch: :javascript: ES JavaScript ({{ matrix.nodejs }}) Test Suite: {{ matrix.suite }}"
4-
agents:
5-
provider: "gcp"
9+
- label: ":elasticsearch: :javascript: ES JavaScript ({{ matrix.nodejs }})"
610
env:
711
NODE_VERSION: "{{ matrix.nodejs }}"
8-
TEST_SUITE: "{{ matrix.suite }}"
9-
STACK_VERSION: 8.16.0
12+
TEST_SUITE: "platinum"
13+
STACK_VERSION: 9.0.0
14+
GITHUB_TOKEN_PATH: "secret/ci/elastic-elasticsearch-js/github-token"
15+
TEST_ES_STACK: "1"
1016
matrix:
1117
setup:
12-
suite:
13-
- "free"
14-
- "platinum"
1518
nodejs:
1619
- "18"
1720
- "20"
@@ -21,9 +24,6 @@ steps:
2124
- wait: ~
2225
continue_on_failure: true
2326
- label: ":junit: Test results"
24-
agents:
25-
provider: "gcp"
26-
image: family/core-ubuntu-2204
2727
plugins:
2828
- junit-annotate#v2.4.1:
2929
artifacts: "junit-output/junit-*.xml"

.buildkite/run-client.sh

+23-16
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,29 @@ export NODE_VERSION=${NODE_VERSION:-18}
1010

1111
echo "--- :javascript: Building Docker image"
1212
docker build \
13-
--file "$script_path/Dockerfile" \
14-
--tag elastic/elasticsearch-js \
15-
--build-arg NODE_VERSION="$NODE_VERSION" \
16-
.
13+
--file "$script_path/Dockerfile" \
14+
--tag elastic/elasticsearch-js \
15+
--build-arg NODE_VERSION="$NODE_VERSION" \
16+
.
1717

18-
echo "--- :javascript: Running $TEST_SUITE tests"
18+
GITHUB_TOKEN=$(vault read -field=token "$GITHUB_TOKEN_PATH")
19+
export GITHUB_TOKEN
20+
21+
echo "--- :javascript: Running tests"
1922
mkdir -p "$repo/junit-output"
2023
docker run \
21-
--network="${network_name}" \
22-
--env "TEST_ES_SERVER=${elasticsearch_url}" \
23-
--env "ELASTIC_PASSWORD=${elastic_password}" \
24-
--env "TEST_SUITE=${TEST_SUITE}" \
25-
--env "ELASTIC_USER=elastic" \
26-
--env "BUILDKITE=true" \
27-
--volume "$repo/junit-output:/junit-output" \
28-
--name elasticsearch-js \
29-
--rm \
30-
elastic/elasticsearch-js \
31-
bash -c "npm run test:integration; [ -f ./$TEST_SUITE-report-junit.xml ] && mv ./$TEST_SUITE-report-junit.xml /junit-output/junit-$BUILDKITE_JOB_ID.xml || echo 'No JUnit artifact found'"
24+
--network="${network_name}" \
25+
--env TEST_ES_STACK \
26+
--env STACK_VERSION \
27+
--env GITHUB_TOKEN \
28+
--env "TEST_ES_SERVER=${elasticsearch_url}" \
29+
--env "ELASTIC_PASSWORD=${elastic_password}" \
30+
--env "ELASTIC_USER=elastic" \
31+
--env "BUILDKITE=true" \
32+
--volume "/usr/src/app/node_modules" \
33+
--volume "$repo:/usr/src/app" \
34+
--volume "$repo/junit-output:/junit-output" \
35+
--name elasticsearch-js \
36+
--rm \
37+
elastic/elasticsearch-js \
38+
bash -c "npm run test:integration; [ -f ./report-junit.xml ] && mv ./report-junit.xml /junit-output/junit-$BUILDKITE_JOB_ID.xml || echo 'No JUnit artifact found'"

.dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ elasticsearch
66
lib
77
junit-output
88
.tap
9+
rest-api-spec
10+
yaml-rest-tests
11+
generated-tests

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ bun.lockb
6868
test-results
6969
processinfo
7070
.tap
71+
rest-api-spec
72+
yaml-rest-tests
73+
generated-tests
74+
schema

.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,6 @@ CONTRIBUTING.md
7474
src
7575
bun.lockb
7676
.tap
77+
rest-api-spec
78+
yaml-rest-tests
79+
generated-tests

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"test:coverage-100": "npm run build && tap --coverage --100",
1919
"test:coverage-report": "npm run build && tap --coverage && nyc report --reporter=text-lcov > coverage.lcov",
2020
"test:coverage-ui": "npm run build && tap --coverage --coverage-report=html",
21-
"test:integration": "tsc && node test/integration/index.js",
21+
"test:integration-build": "npm run build && node test/integration/index.js",
22+
"test:integration": "npm run test:integration-build && env tap run --jobs=1 --reporter=junit --reporter-file=report-junit.xml generated-tests/",
2223
"lint": "ts-standard src",
2324
"lint:fix": "ts-standard --fix src",
2425
"license-checker": "license-checker --production --onlyAllow='MIT;Apache-2.0;Apache1.1;ISC;BSD-3-Clause;BSD-2-Clause;0BSD'",
@@ -76,7 +77,7 @@
7677
"node-fetch": "2.7.0",
7778
"ora": "5.4.1",
7879
"proxy": "1.0.2",
79-
"rimraf": "3.0.2",
80+
"rimraf": "5.0.10",
8081
"semver": "7.7.1",
8182
"split2": "4.2.0",
8283
"stoppable": "1.1.0",

scripts/download-artifacts.js

+55-115
Original file line numberDiff line numberDiff line change
@@ -17,162 +17,102 @@
1717
* under the License.
1818
*/
1919

20-
'use strict'
21-
2220
const { join } = require('path')
23-
const minimist = require('minimist')
2421
const stream = require('stream')
2522
const { promisify } = require('util')
2623
const { createWriteStream, promises } = require('fs')
27-
const rimraf = require('rimraf')
24+
const { rimraf } = require('rimraf')
2825
const fetch = require('node-fetch')
2926
const crossZip = require('cross-zip')
3027
const ora = require('ora')
3128

32-
const { mkdir, writeFile } = promises
29+
const { mkdir, cp } = promises
3330
const pipeline = promisify(stream.pipeline)
3431
const unzip = promisify(crossZip.unzip)
35-
const rm = promisify(rimraf)
36-
37-
const esFolder = join(__dirname, '..', 'elasticsearch')
38-
const zipFolder = join(esFolder, 'artifacts.zip')
39-
const specFolder = join(esFolder, 'rest-api-spec', 'api')
40-
const freeTestFolder = join(esFolder, 'rest-api-spec', 'test', 'free')
41-
const xPackTestFolder = join(esFolder, 'rest-api-spec', 'test', 'platinum')
42-
const artifactInfo = join(esFolder, 'info.json')
43-
44-
async function downloadArtifacts (opts) {
45-
if (typeof opts.version !== 'string') {
46-
throw new Error('Missing version')
47-
}
4832

49-
const log = ora('Checking out spec and test').start()
33+
const testYamlFolder = join(__dirname, '..', 'yaml-rest-tests')
34+
const zipFile = join(__dirname, '..', 'elasticsearch-clients-tests.zip')
5035

51-
log.text = 'Resolving versions'
52-
let resolved
53-
try {
54-
resolved = await resolve(opts.version, opts.hash)
55-
} catch (err) {
56-
log.fail(err.message)
57-
process.exit(1)
58-
}
36+
const schemaFolder = join(__dirname, '..', 'schema')
37+
const schemaJson = join(schemaFolder, 'schema.json')
5938

60-
opts.id = opts.id || resolved.id
61-
opts.hash = opts.hash || resolved.hash
62-
opts.version = resolved.version
39+
async function downloadArtifacts (localTests, version = 'main') {
40+
const log = ora('Checking out spec and test').start()
6341

64-
const info = loadInfo()
42+
const { GITHUB_TOKEN } = process.env
6543

66-
if (info && info.version === opts.version) {
67-
if (info.hash === opts.hash && info.id === opts.id) {
68-
log.succeed('The artifact copy present locally is already up to date')
69-
return
70-
}
44+
if (version !== 'main') {
45+
version = version.split('.').slice(0, 2).join('.')
7146
}
7247

73-
log.text = 'Cleanup checkouts/elasticsearch'
74-
await rm(esFolder)
75-
await mkdir(esFolder, { recursive: true })
48+
log.text = 'Clean tests folder'
49+
await rimraf(testYamlFolder)
50+
await mkdir(testYamlFolder, { recursive: true })
7651

77-
log.text = 'Downloading artifacts'
78-
const response = await fetch(resolved.url)
79-
if (!response.ok) {
80-
log.fail(`unexpected response ${response.statusText}`)
81-
process.exit(1)
82-
}
83-
await pipeline(response.body, createWriteStream(zipFolder))
52+
log.text = `Fetch test YAML files for version ${version}`
8453

85-
log.text = 'Unzipping'
86-
await unzip(zipFolder, esFolder)
54+
if (localTests) {
55+
log.text = `Copying local tests from ${localTests}`
56+
await cp(localTests, testYamlFolder, { recursive: true })
57+
} else {
58+
if (!GITHUB_TOKEN) {
59+
log.fail("Missing required environment variable 'GITHUB_TOKEN'")
60+
process.exit(1)
61+
}
8762

88-
log.text = 'Cleanup'
89-
await rm(zipFolder)
63+
const response = await fetch(`https://api.github.com/repos/elastic/elasticsearch-clients-tests/zipball/${version}`, {
64+
headers: {
65+
Authorization: `Bearer ${GITHUB_TOKEN}`,
66+
Accept: 'application/vnd.github+json'
67+
}
68+
})
9069

91-
log.text = 'Update info'
92-
await writeFile(artifactInfo, JSON.stringify(opts), 'utf8')
70+
if (!response.ok) {
71+
log.fail(`unexpected response ${response.statusText}`)
72+
process.exit(1)
73+
}
9374

94-
log.succeed('Done')
95-
}
75+
log.text = 'Downloading tests zipball'
76+
await pipeline(response.body, createWriteStream(zipFile))
9677

97-
function loadInfo () {
98-
try {
99-
return require(artifactInfo)
100-
} catch (err) {
101-
return null
102-
}
103-
}
78+
log.text = 'Unzipping tests'
79+
await unzip(zipFile, testYamlFolder)
10480

105-
async function resolve (version, hash) {
106-
const response = await fetch(`https://artifacts-api.elastic.co/v1/versions/${version}`)
107-
if (!response.ok) {
108-
throw new Error(`unexpected response ${response.statusText}`)
81+
log.text = 'Cleanup'
82+
await rimraf(zipFile)
10983
}
11084

111-
const data = await response.json()
112-
const esBuilds = data.version.builds
113-
.filter(build => build.projects.elasticsearch != null)
114-
.map(build => {
115-
return {
116-
projects: build.projects.elasticsearch,
117-
buildId: build.build_id,
118-
date: build.start_time,
119-
version: build.version
120-
}
121-
})
122-
.sort((a, b) => {
123-
const dA = new Date(a.date)
124-
const dB = new Date(b.date)
125-
if (dA > dB) return -1
126-
if (dA < dB) return 1
127-
return 0
128-
})
85+
log.text = 'Fetching Elasticsearch specification'
86+
await rimraf(schemaFolder)
87+
await mkdir(schemaFolder, { recursive: true })
12988

130-
if (hash != null) {
131-
const build = esBuilds.find(build => build.projects.commit_hash === hash)
132-
if (!build) {
133-
throw new Error(`Can't find any build with hash '${hash}'`)
134-
}
135-
const zipKey = Object.keys(build.projects.packages).find(key => key.startsWith('rest-resources-zip-') && key.endsWith('.zip'))
136-
return {
137-
url: build.projects.packages[zipKey].url,
138-
id: build.buildId,
139-
hash: build.projects.commit_hash,
140-
version: build.version
141-
}
89+
const response = await fetch(`https://raw.githubusercontent.com/elastic/elasticsearch-specification/${version}/output/schema/schema.json`)
90+
if (!response.ok) {
91+
log.fail(`unexpected response ${response.statusText}`)
92+
process.exit(1)
14293
}
14394

144-
const lastBuild = esBuilds[0]
145-
const zipKey = Object.keys(lastBuild.projects.packages).find(key => key.startsWith('rest-resources-zip-') && key.endsWith('.zip'))
146-
return {
147-
url: lastBuild.projects.packages[zipKey].url,
148-
id: lastBuild.buildId,
149-
hash: lastBuild.projects.commit_hash,
150-
version: lastBuild.version
151-
}
95+
log.text = 'Downloading schema.json'
96+
await pipeline(response.body, createWriteStream(schemaJson))
97+
98+
log.succeed('Done')
15299
}
153100

154-
async function main (options) {
155-
delete options._
156-
await downloadArtifacts(options)
101+
async function main () {
102+
await downloadArtifacts()
157103
}
104+
158105
if (require.main === module) {
159106
process.on('unhandledRejection', function (err) {
160107
console.error(err)
161108
process.exit(1)
162109
})
163110

164-
const options = minimist(process.argv.slice(2), {
165-
string: ['id', 'version', 'hash']
166-
})
167-
main(options).catch(t => {
111+
main().catch(t => {
168112
console.log(t)
169113
process.exit(2)
170114
})
171115
}
172116

173117
module.exports = downloadArtifacts
174-
module.exports.locations = {
175-
specFolder,
176-
freeTestFolder,
177-
xPackTestFolder
178-
}
118+
module.exports.locations = { testYamlFolder, zipFile, schemaJson }

scripts/generate-docs-examples.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
const { join } = require('path')
2121
const { writeFile } = require('fs/promises')
2222
const fetch = require('node-fetch')
23-
const rimraf = require('rimraf')
23+
const { rimraf } = require('rimraf')
2424
const ora = require('ora')
2525
const { convertRequests } = require('@elastic/request-converter')
2626
const minimist = require('minimist')

0 commit comments

Comments
 (0)