randomized-test-ci

 1#!/usr/bin/env node --redirect-warnings=/dev/null
 2
 3const fs = require('fs')
 4const {randomBytes} = require('crypto')
 5const {execFileSync} = require('child_process')
 6const {minimizeTestPlan, buildTests, runTests} = require('./randomized-test-minimize');
 7
 8const {ZED_SERVER_URL, ZED_CLIENT_SECRET_TOKEN} = process.env
 9if (!ZED_SERVER_URL) throw new Error('Missing env var `ZED_SERVER_URL`')
10if (!ZED_CLIENT_SECRET_TOKEN) throw new Error('Missing env var `ZED_CLIENT_SECRET_TOKEN`')
11
12main()
13
14async function main() {
15  buildTests()
16
17  const seed = randomU64();
18  const commit = execFileSync(
19    'git',
20    ['rev-parse', 'HEAD'],
21    {encoding: 'utf8'}
22  ).trim()
23
24  console.log("commit:", commit)
25  console.log("starting seed:", seed)
26
27  const planPath = 'target/test-plan.json'
28  const minPlanPath = 'target/test-plan.min.json'
29  const failingSeed = runTests({
30    SEED: seed,
31    SAVE_PLAN: planPath,
32    ITERATIONS: 50000,
33    OPERATIONS: 200,
34  })
35
36  if (!failingSeed) {
37    console.log("tests passed")
38    return
39  }
40
41  console.log("found failure at seed", failingSeed)
42  const minimizedSeed = minimizeTestPlan(planPath, minPlanPath)
43  const minimizedPlan = fs.readFileSync(minPlanPath, 'utf8')
44
45  console.log("minimized plan:\n", minimizedPlan)
46
47  const url = `${ZED_SERVER_URL}/api/randomized_test_failure`
48  const body = {
49    seed: minimizedSeed,
50    token: ZED_CLIENT_SECRET_TOKEN,
51    plan: JSON.parse(minimizedPlan),
52    commit: commit,
53  }
54  await fetch(url, {
55    method: 'POST',
56    headers: {"Content-Type": "application/json"},
57    body: JSON.stringify(body)
58  })
59
60  process.exit(1)
61}
62
63function randomU64() {
64  const bytes = randomBytes(8)
65  const hexString = bytes.reduce(((string, byte) => string + byte.toString(16)), '')
66  return BigInt('0x' + hexString).toString(10)
67}