Skip to content

Commit 46ba7b3

Browse files
authored
Merge pull request #292 from MatrixAI/qa-testing
Adding QA jobs and expanding test jobs for CI/CD.
2 parents ef6c124 + 760ae54 commit 46ba7b3

207 files changed

Lines changed: 10312 additions & 6782 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.env*
44
!.env.example
55
/result*
6+
/builds
67

78
# Logs
89
logs

.gitlab-ci.yml

Lines changed: 150 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,192 @@
11
variables:
2-
GIT_SUBMODULE_STRATEGY: recursive
2+
GIT_SUBMODULE_STRATEGY: "recursive"
3+
# Cache .npm
4+
NPM_CONFIG_CACHE: "./tmp/npm"
5+
# Prefer offline node module installation
6+
NPM_CONFIG_PREFER_OFFLINE: "true"
7+
# `ts-node` has its own cache
8+
# It must use an absolute path, otherwise ts-node calls will CWD
9+
TS_CACHED_TRANSPILE_CACHE: "${CI_PROJECT_DIR}/tmp/ts-node-cache"
10+
TS_CACHED_TRANSPILE_PORTABLE: "true"
11+
12+
# Cached directories shared between jobs & pipelines per-branch
13+
cache:
14+
key: $CI_COMMIT_REF_SLUG
15+
paths:
16+
- ./tmp/npm/
17+
- ./tmp/ts-node-cache/
18+
# `jest` cache is configured in jest.config.js
19+
- ./tmp/jest/
320

421
stages:
522
- check
23+
- test
624
- build
25+
- quality
726
- release
827

9-
image: registry.gitlab.com/matrixai/engineering/maintenance/gitlab-runner
10-
1128
lint:
29+
image: registry.gitlab.com/matrixai/engineering/maintenance/gitlab-runner
1230
stage: check
1331
interruptible: true
1432
script:
1533
- >
1634
nix-shell -I nixpkgs=./pkgs.nix --packages nodejs --run '
17-
npm install;
35+
npm ci;
1836
npm run lint;
1937
'
2038
21-
test:
22-
stage: check
23-
interruptible: true
24-
script:
25-
- >
26-
nix-shell -I nixpkgs=./pkgs.nix --packages nodejs --run '
27-
npm install;
28-
npm run test;
29-
'
30-
3139
nix-dry:
3240
stage: check
33-
interruptible: true
41+
image: registry.gitlab.com/matrixai/engineering/maintenance/gitlab-runner
3442
script:
3543
- nix-build -v -v --dry-run ./release.nix --attr application
3644
- nix-build -v -v --dry-run ./release.nix --attr docker
3745
- nix-build -v -v --dry-run ./release.nix --attr package.linux.x64.elf
3846
- nix-build -v -v --dry-run ./release.nix --attr package.windows.x64.exe
3947
- nix-build -v -v --dry-run ./release.nix --attr package.macos.x64.macho
4048

49+
test-generate:
50+
image: registry.gitlab.com/matrixai/engineering/maintenance/gitlab-runner
51+
stage: check
52+
interruptible: true
53+
script:
54+
- mkdir -p ./tmp
55+
- >
56+
nix-shell -I nixpkgs=./pkgs.nix --packages bash --run '
57+
./scripts/test-pipelines.sh > ./tmp/test-pipelines.yml
58+
'
59+
artifacts:
60+
paths:
61+
- ./tmp/test-pipelines.yml
62+
63+
test:
64+
stage: test
65+
# Don't implicitly inherit top-level variables in child pipeline
66+
# All inherited variables should be explicitly defined here
67+
# Note that variables defined here override any variables defined in the child pipeline
68+
# This causes a bug with $CI_PROJECT_DIR, which is expanded into an empty string
69+
inherit:
70+
variables: false
71+
trigger:
72+
include:
73+
- artifact: tmp/test-pipelines.yml
74+
job: test-generate
75+
strategy: depend
76+
4177
nix:
4278
stage: build
43-
interruptible: true
79+
image: registry.gitlab.com/matrixai/engineering/maintenance/gitlab-runner
4480
script:
81+
- mkdir -p ./builds
82+
# nix-specific application target
83+
- >
84+
build_application="$(nix-build \
85+
--max-jobs "$(nproc)" --cores "$(nproc)" \
86+
./release.nix \
87+
--attr application \
88+
)"
89+
- >
90+
nix-store --export $( \
91+
nix-store --query --requisites "$build_application" \
92+
) | gzip > ./builds/js-polykey.closure.gz
93+
# non-nix targets
4594
- >
46-
nix-build ./release.nix
47-
--max-jobs $(nproc)
48-
--attr application
49-
--attr docker
95+
builds="$(nix-build \
96+
--max-jobs "$(nproc)" --cores "$(nproc)" \
97+
./release.nix \
98+
--attr docker \
99+
--attr package.linux.x64.elf \
100+
--attr package.windows.x64.exe \
101+
--attr package.macos.x64.macho)"
102+
- cp -r $builds ./builds/
103+
only:
104+
- master
105+
artifacts:
106+
paths:
107+
- ./builds/
108+
109+
application run:
110+
stage: quality
111+
image: registry.gitlab.com/matrixai/engineering/maintenance/gitlab-runner
112+
dependencies:
113+
- nix
114+
script:
115+
- >
116+
build_application="$( \
117+
gunzip -c ./builds/js-polykey.closure.gz | \
118+
nix-store --import | \
119+
tail -1 \
120+
)"
121+
- $build_application/bin/polykey
122+
only:
123+
- master
124+
125+
docker run:
126+
stage: quality
127+
image: docker:20.10.11
128+
dependencies:
129+
- nix
130+
services:
131+
- docker:20.10.11-dind
132+
variables:
133+
DOCKER_TLS_CERTDIR: "/certs"
134+
before_script:
135+
- docker info
136+
script:
137+
- image="$(docker load --input ./builds/*docker* | cut -d' ' -f3)"
138+
- docker run "$image"
139+
only:
140+
- master
141+
142+
linux run:
143+
stage: quality
144+
image: ubuntu:latest
145+
dependencies:
146+
- nix
147+
script:
148+
- for f in ./builds/*-linux-*; do "$f"; done
149+
only:
150+
- master
151+
152+
windows run:
153+
stage: quality
154+
dependencies:
155+
- nix
156+
script:
157+
- Get-ChildItem -File ./builds/*-win32-* | ForEach {& $_.FullName}
158+
tags:
159+
- windows
50160
only:
51161
- master
52162

163+
macos run:
164+
stage: quality
165+
image: macos-11-xcode-12
166+
dependencies:
167+
- nix
168+
script:
169+
- for f in ./builds/*-macos-*; do "$f"; done
170+
only:
171+
- master
172+
tags:
173+
- shared-macos-amd64
174+
53175
packages:
54176
stage: release
55-
interruptible: true
177+
image: registry.gitlab.com/matrixai/engineering/maintenance/gitlab-runner
178+
dependencies:
179+
- nix
56180
script:
57181
- >
58182
nix-shell -I nixpkgs=./pkgs.nix --packages git gitAndTools.gh --run '
59-
builds="$(nix-build \
60-
--max-jobs $(nproc) --cores $(nproc) \
61-
./release.nix \
62-
--attr package.linux.x64.elf \
63-
--attr package.windows.x64.exe \
64-
--attr package.macos.x64.macho)";
65183
commit="$(git rev-parse --short HEAD)";
66184
gh release \
67-
create "$commit" $builds \
185+
create "$commit" \
186+
builds/*.closure.gz \
187+
builds/*-linux-* \
188+
builds/*-win32-* \
189+
builds/*-macos-* \
68190
--title "Build-$(date -u +"%Y-%m-%dT%H:%M:%SZ")" \
69191
--prerelease \
70192
--notes "" \

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
/tmp
1414
/docs
1515
/benches
16+
/builds

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Once you update the `src/proto/schemas` files, run this to update the `src/proto
6767
npm run proto-generate
6868
```
6969

70-
### Executing Commands
70+
### Calling Commands
7171

7272
When calling commands in development, use this style:
7373

@@ -85,7 +85,7 @@ npm run docs
8585

8686
See the docs at: https://matrixai.github.io/Polykey/
8787

88-
### Publishing
88+
### Publishing to NPM
8989

9090
```sh
9191
# npm login
@@ -96,6 +96,18 @@ git push
9696
git push --tags
9797
```
9898

99+
### Packaging Cross-Platform Executables
100+
101+
We use `pkg` to package the source code into executables.
102+
103+
This requires a specific version of `pkg` and also `node-gyp-build`.
104+
105+
Configuration for `pkg` is done in:
106+
107+
* `package.json` - Pins `pkg` and `node-gyp-build`, and configures assets and scripts.
108+
* `utils.nix` - Pins `pkg` for Nix usage
109+
* `release.nix` - Build expressions for executables
110+
99111
## Deployment
100112

101113
### Deploying to AWS ECS:

default.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ let
1212
}
1313
''
1414
mkdir -p $out/lib/node_modules/${utils.node2nixDev.packageName}
15-
# copy only the dist
15+
# copy the package.json
16+
cp ${utils.node2nixDev}/lib/node_modules/${utils.node2nixDev.packageName}/package.json $out/lib/node_modules/${utils.node2nixDev.packageName}/
17+
# copy the dist
1618
cp -r ${utils.node2nixDev}/lib/node_modules/${utils.node2nixDev.packageName}/dist $out/lib/node_modules/${utils.node2nixDev.packageName}/
1719
# copy over the production dependencies
1820
if [ -d "${utils.node2nixProd}/lib/node_modules" ]; then

jest.config.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const os = require('os');
2+
const path = require('path');
3+
const fs = require('fs');
4+
const process = require('process');
15
const { pathsToModuleNameMapper } = require('ts-jest/utils');
26
const { compilerOptions } = require('./tsconfig');
37

@@ -10,8 +14,33 @@ const moduleNameMapper = pathsToModuleNameMapper(
1014
// https://github.com/panva/jose/discussions/105
1115
moduleNameMapper['^jose/(.*)$'] = "<rootDir>/node_modules/jose/dist/node/cjs/$1";
1216

17+
// Global variables that are shared across the jest worker pool
18+
// These variables must be static and serialisable
19+
const globals = {
20+
// Absolute directory to the project root
21+
projectDir: __dirname,
22+
// Absolute directory to the test root
23+
testDir: path.join(__dirname, 'tests'),
24+
// Default global data directory
25+
dataDir: fs.mkdtempSync(
26+
path.join(os.tmpdir(), 'polykey-test-global-'),
27+
),
28+
// Default asynchronous test timeout
29+
defaultTimeout: 20000,
30+
polykeyStartupTimeout: 30000,
31+
failedConnectionTimeout: 50000,
32+
// Timeouts rely on setTimeout which takes 32 bit numbers
33+
maxTimeout: Math.pow(2, 31) - 1,
34+
};
35+
36+
// The `globalSetup` and `globalTeardown` cannot access the `globals`
37+
// They run in their own process context
38+
// They can receive process environment
39+
process.env['GLOBAL_DATA_DIR'] = globals.dataDir;
40+
1341
module.exports = {
1442
testEnvironment: "node",
43+
cacheDirectory: '<rootDir>/tmp/jest',
1544
verbose: true,
1645
roots: [
1746
"<rootDir>/tests"
@@ -23,13 +52,21 @@ module.exports = {
2352
"^.+\\.tsx?$": "ts-jest",
2453
"^.+\\.jsx?$": "babel-jest"
2554
},
55+
globals,
56+
// Global setup script executed once before all test files
57+
globalSetup: "<rootDir>/tests/globalSetup.ts",
58+
// Global teardown script executed once after all test files
59+
globalTeardown: "<rootDir>/tests/globalTeardown.ts",
60+
// Setup files are executed before each test file
61+
// Can access globals
2662
setupFiles: [
2763
"<rootDir>/tests/setup.ts"
2864
],
65+
// Setup files after env are executed before each test file
66+
// after the jest test environment is installed
67+
// Can access globals
2968
setupFilesAfterEnv: [
3069
"<rootDir>/tests/setupAfterEnv.ts"
3170
],
32-
globalSetup: "<rootDir>/tests/globalSetup.ts",
33-
globalTeardown: "<rootDir>/tests/globalTeardown.ts",
3471
moduleNameMapper: moduleNameMapper
3572
};

0 commit comments

Comments
 (0)