-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: integrate runtime-tests as a helper library (#549)
test: integrate runtime-tests as a helper library (#549)
- Loading branch information
Showing
27 changed files
with
344 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
**/build/* | ||
calc | ||
docs | ||
runtime-tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**/build/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('@substrate/dev/config/eslint'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
## Summary | ||
|
||
This is a helper library for Sidecar to run runtime tests against specific chains, at certain blocks. | ||
|
||
## Testing | ||
|
||
The below instructions are specific to runtime-tests against one chain. | ||
If you are looking to run the runtime-tests against all 3 chains (Polkadot, Kusama, Westend) then run `yarn test:init-runtime-tests` in | ||
the root directory of sidecar. | ||
|
||
### Polkadot | ||
|
||
Lets first get sidecar ready in a seperate terminal. | ||
|
||
``` | ||
$ cd substrate-api-sidecar | ||
$ git checkout <your_branch> | ||
$ export SAS_SUBSTRATE_WS_URL=<network archive node> | ||
$ yarn | ||
$ yarn build && yarn start | ||
``` | ||
|
||
Sidecar should now be connected to the node and running successfully. If you find a bug file an issue [here](https://github.com/paritytech/substrate-api-sidecar/issues). | ||
|
||
Now lets run our runtime tests against polkadot. Go to a separate terminal and run: | ||
|
||
`yarn test:runtime-tests --chain polkadot` | ||
|
||
Thats it! | ||
All the tests should come back with green checkmarks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ChainSpec } from '../types'; | ||
import { kusamaEndpoints } from './kusamaEndpoints'; | ||
import { polkadotEndpoints } from './polkadotEndpoints'; | ||
import { westendEndpoints } from './westendEndpoints'; | ||
|
||
export const endpoints: Record<ChainSpec, string[][]> = { | ||
kusama: kusamaEndpoints, | ||
polkadot: polkadotEndpoints, | ||
westend: westendEndpoints, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export const kusamaEndpoints = [ | ||
['/blocks/2350438', '2350438'], //v1062 | ||
['/blocks/2684767', '2684767'], //v2005 | ||
['/blocks/2713513', '2713513'], //v2007 | ||
['/blocks/2786879', '2786879'], //v2008 | ||
['/blocks/2863649', '2863649'], //v2011 | ||
['/blocks/3102680', '3102680'], //v2012 | ||
['/blocks/3305067', '3305067'], //v2015 | ||
['/blocks/3492347', '3492347'], //v2019 | ||
['/blocks/3573193', '3573193'], //v2022 | ||
['/blocks/4086065', '4086065'], //v2023 | ||
['/blocks/4178606', '4178606'], //v2024 | ||
['/blocks/4542697', '4542697'], //v2025 | ||
['/blocks/5265416', '5265416'], //v2026 | ||
['/blocks/6065416', '6065416'], //v2027 | ||
['/blocks/6396694', '6396694'], //v2028 | ||
['/blocks/6566865', '6566865'], //v2029 | ||
['/blocks/6819725', '6819725'], //v2029 | ||
['/blocks/7354817', '7354817'], //v2030 | ||
['/blocks/7519631', '7519631'], //v9010 | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const polkadotEndpoints = [ | ||
['/blocks/943438', '943438'], //v17 | ||
['/blocks/1603025', '1603025'], //v18 | ||
['/blocks/1662525', '1662525'], //v23 | ||
['/blocks/1993625', '1993625'], //v24 | ||
['/blocks/2392625', '2392625'], //v25 | ||
['/blocks/3592619', '3592619'], //v26 | ||
['/blocks/3892620', '3892620'], //v27 | ||
['/blocks/4092619', '4092619'], //v28 | ||
['/blocks/4392619', '4392619'], //v29 | ||
['/blocks/4947391', '4947391'], //v30 | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const westendEndpoints = [ | ||
['/blocks/3813629', '3813629'], | ||
['/blocks/3032259', '3032259'], | ||
['/blocks/3891809', '3891809'], | ||
['/blocks/4781573', '4781573'], | ||
['/blocks/5277929', '5277929'], | ||
['/blocks/5480769', '5480769'], //v9000 | ||
['/blocks/5493461', '5493461'], //v9000 | ||
['/blocks/5495855', '5495855'], //v9000 | ||
['/blocks/5657482', '5657482'], //v9010 | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Port to Substrate-api-sidecar | ||
*/ | ||
export const PORT = 8080; | ||
|
||
/** | ||
* Path to Substrate-api-sidecar | ||
*/ | ||
export const HOST = '127.0.0.1'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import http from 'http'; | ||
|
||
export const request = ( | ||
path: string, | ||
hostname: string, | ||
port: number | ||
): Promise<string> => { | ||
return new Promise((resolve) => { | ||
http.get({ path, hostname, port }, (response) => { | ||
let data = ''; | ||
response.on('data', (_data) => (data += _data)); | ||
response.on('end', () => resolve(data)); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { ArgumentParser } from 'argparse'; | ||
|
||
import { IParser } from './types'; | ||
|
||
const config = { | ||
chain: 'polkadot', | ||
}; | ||
|
||
const argv = process.argv.slice(0, 2); | ||
|
||
/** | ||
* The arg parser takes in two commands. | ||
* @arg --chain The chain to be passed into the jest test | ||
* @arg --config The path to the correct jest config. This is important as the | ||
* jest config inside of /runtime-tests ignores all the other tests. | ||
*/ | ||
const parser = new ArgumentParser(); | ||
|
||
parser.add_argument('--chain', { | ||
choices: ['polkadot', 'kusama', 'westend'], | ||
default: 'polkadot', | ||
}); | ||
parser.add_argument('--config', { default: './runtime-tests/jest.config.js' }); | ||
|
||
const args = parser.parse_args() as IParser; | ||
|
||
// Set the chain property for the jest test | ||
config['chain'] = args.chain; | ||
|
||
// Pass in the Jest CLI path option to the correct config | ||
argv.push('--config='.concat(args.config)); | ||
|
||
// Store configuration on env | ||
process.env.__SAS_RUNTIME_TEST_CONFIGURATION = JSON.stringify(config); | ||
|
||
// Setting real ARGV | ||
process.argv = argv; | ||
|
||
// Calling jest runner | ||
require('jest-cli/bin/jest'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const base = require('@substrate/dev/config/jest') | ||
|
||
module.exports = { | ||
...base, | ||
testPathIgnorePatterns: ['/build/', '/node_modules/', '/src/'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { endpoints } from './endpoints'; | ||
import { HOST, PORT } from './helpers/consts'; | ||
import { request } from './helpers/request'; | ||
import { BlockResponse, ChainConfig, ChainSpec } from './types'; | ||
|
||
const config = JSON.parse( | ||
process.env.__SAS_RUNTIME_TEST_CONFIGURATION as string | ||
) as ChainConfig; | ||
const chain = config.chain as ChainSpec; | ||
|
||
const polkadotEndpoints: string[][] = endpoints[chain]; | ||
|
||
describe('Runtime Tests for blocks', () => { | ||
jest.setTimeout(15000); | ||
|
||
/** | ||
* Test runtimes for `/blocks` | ||
*/ | ||
test.each(polkadotEndpoints)( | ||
'Given path %p, it should return block height %p', | ||
async (blockPath, blockHeight) => { | ||
const res = await request(blockPath, HOST, PORT); | ||
const responseJson = JSON.parse(res) as BlockResponse; | ||
|
||
expect(responseJson['number']).toBe(blockHeight); | ||
} | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "@substrate/dev/config/tsconfig.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"outDir": "build", | ||
"suppressImplicitAnyIndexErrors": true, | ||
"resolveJsonModule": true, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* String literal for specific chains that are being tested for | ||
*/ | ||
export type ChainSpec = 'polkadot' | 'kusama' | 'westend'; | ||
|
||
/** | ||
* | ||
*/ | ||
export interface ChainConfig { | ||
chain: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './chainSpec'; | ||
export * from './parser'; | ||
export * from './responses'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Interface type for args for the arg parser | ||
*/ | ||
export interface IParser { | ||
chain: string; | ||
config: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Block Responses | ||
*/ | ||
export interface BlockResponse { | ||
number: string; | ||
hash: string; | ||
parentHash: string; | ||
stateRoot: string; | ||
extrinsicsRoot: string; | ||
authorId: string | undefined; | ||
logs: Array<object>; | ||
onInitialize: object; | ||
extrinsics: Array<object>; | ||
onFinalize: object; | ||
finalized: boolean | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<div style="text-align:center"> | ||
<h2>Sidecar Scripts</h2> | ||
<div> | ||
A set of notes and instructions for scripts used in Substrate-api-sidecar | ||
</div> | ||
</div> | ||
<br></br> | ||
|
||
|
||
## Script `run_chain_tests.py` | ||
|
||
### Summary | ||
|
||
This script calls the local runtime-tests helper library in order to test the current branch or development enviornment against | ||
a collection of different blocks, across different runtimes. It does this for Polkadot, Kusama, and Westend. | ||
|
||
### Requirements | ||
|
||
`python3` - required to run the script | ||
|
||
`psutil` - package needed to run the script | ||
|
||
Run: `pip install -r requirements.txt` from this directory in order to install necessary dependencies. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
psutil==5.8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
import psutil | ||
import signal | ||
import subprocess | ||
import time | ||
|
||
def run_process(args): | ||
return subprocess.check_output(args, stderr=subprocess.STDOUT, | ||
encoding="utf-8") | ||
|
||
def run_chain_test(chain): | ||
run_process(["yarn"]) | ||
|
||
if chain == "polkadot": | ||
url = "wss://rpc.polkadot.io" | ||
chain = "polkadot" | ||
elif chain == "kusama": | ||
url = "wss://kusama-rpc.polkadot.io" | ||
chain = "kusama" | ||
elif chain == "westend": | ||
url = "wss://westend-rpc.polkadot.io" | ||
chain = "westend" | ||
else: | ||
return -1 | ||
|
||
os.environ["SAS_SUBSTRATE_WS_URL"] = url | ||
proc = subprocess.Popen(["yarn", "dev"], stdout=subprocess.PIPE, | ||
stderr=subprocess.STDOUT) | ||
|
||
print('Sidecar is loading in development mode...') | ||
time.sleep(30) | ||
|
||
print('Running `yarn`, and checking/loading cache...') | ||
run_process(["yarn"]) | ||
|
||
print('Running runtime tests for {}'.format(chain)) | ||
res = run_process(["yarn", "test:runtime-tests", "--chain", chain]) | ||
|
||
print('Killing all processes...') | ||
for child in psutil.Process(proc.pid).children(recursive=True): | ||
child.kill() | ||
proc.kill() | ||
|
||
print(res) | ||
return res.find("PASS") | ||
|
||
|
||
def main(): | ||
polka_test = run_chain_test("polkadot") | ||
kusama_test = run_chain_test("kusama") | ||
westend_test = run_chain_test("westend") | ||
|
||
if polka_test == 0 and kusama_test == 0 and westend_test == 0: | ||
return 0 | ||
else: | ||
return -1 | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.