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 = JSON.parse(fs.readFileSync(minPlanPath, 'utf8'))
44
45  const url = `${ZED_SERVER_URL}/api/randomized_test_failure`
46  const body = {
47    seed: minimizedSeed,
48    token: ZED_CLIENT_SECRET_TOKEN,
49    plan: minimizedPlan,
50    commit: commit,
51  }
52  await fetch(url, {
53    method: 'POST',
54    headers: {"Content-Type": "application/json"},
55    body: JSON.stringify(body)
56  })
57}
58
59function randomU64() {
60  const bytes = randomBytes(8)
61  const hexString = bytes.reduce(((string, byte) => string + byte.toString(16)), '')
62  return BigInt('0x' + hexString).toString(10)
63}