Skip to content

Commit

Permalink
Bundle test-commons and host versions as assets in project releases
Browse files Browse the repository at this point in the history
  • Loading branch information
ppcano committed Jan 18, 2024
1 parent ad121b9 commit cacce56
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 11 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Release test-commons

on:
push:
tags:
- v*

jobs:
checks:
runs-on: ubuntu-latest
# if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: Check out source code
uses: actions/checkout@v4
- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: '18.x'
- name: Install dependencies
run: |
set -x
cd test-commons
npm install
- name: Build & archive
run: |
set -x
cd test-commons
rm -rf dist/*
VERSION="${GITHUB_REF##*/}"
npm run rollup
- name: Create release with assets
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -x
type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y
cd test-commons
gh release create "${VERSION}" ./dist/* --title "${VERSION}" --target "${GITHUB_SHA}" --generate-notes
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
In this example, we are using [Rollup](https://rollupjs.org/) to bundle k6 tests for demo purposes.
In this example, we are using [Rollup](https://rollupjs.org/) to bundle k6 tests and an internal library for demo purposes.

Check out the [`rollup.config.js`](./tests/rollup.config.js) file. It is set up to:
## Content

### Test project

The [tests](./tests/) folder is our testing project. It includes all tests, test dependencies, and project dependencies like `Rollup`. Its [`rollup.config.js`](./tests/rollup.config.js) is set up to:

- Bundle up test modules located in `node_modules`.
- Polyfill ES+ features that k6 doesn't natively support.

## Content
### Internal library

The [test-commons](./test-commons) folder contains a library that's imported in the test project. It's [`rollup.config.js`](./test-commons/rollup.config.js) bundles the library as ES modules.

This project includes a [GH action](./github/workflows/release.yml) that bundles and releases the library as assets in the [project release](https://github.com/grafana/k6-rollup-example/releases). Then, you can import this version from the k6 tests as remote modules, just like in [import-remote-test.js](./tests/remotes/import-remote-test.js).

```js
import { WorkloadConfig, sayHello } from 'https://github.com/grafana/k6-rollup-example/releases/download/v0.0.1/index.js';
```

Alternatively, `test-commons` could be distributed as a npm-based project and configured as a dependency like in [tests/package.json](./tests/package.json). Then, you can import it as in [import-npm-test.js](./tests/samples/import-npm-test.js).

```js
import { WorkloadConfig, sayHello } from 'test-commons';
```

The [tests](./tests/) folder is our testing project. It includes all tests, test dependencies, and project dependencies like `Rollup`.

The [test-commons](./test-commons) folder is a library imported in the testing project. It is distributed as a npm-based project and configured as a dependency in [tests/package.json](./tests/package.json).

## Installation

Expand All @@ -22,7 +38,9 @@ npm install

## Usage

Attempting to run the tests in [samples](./tests/samples/) will fail because:
To confirm the usage of remote modules, visit [import-remote-test.js](./tests/remotes/import-remote-test.js) and run the test.

However, attempting to run the tests in [samples](./tests/samples/) will fail because:

- k6 does not know how to resolve the `test-commons` module.
- k6 does not recognize the optional chaining ( `?.` ) operator.
Expand All @@ -42,7 +60,7 @@ npm run rollup -- --input samples/es-plus-test.js
You should see rollup bundling tests into the `dist` folder, like this:

```bash
samples/import-common-test.js → dist...
samples/import-npm-test.js → dist...
created dist in 76ms

samples/es-plus-test.js → dist...
Expand Down
2 changes: 2 additions & 0 deletions test-commons/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.DS_Store
19 changes: 19 additions & 0 deletions test-commons/dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const WorkloadConfig = {
average: [
{ duration: '1m', target: 100 },
{ duration: '4m', target: 100 },
{ duration: '1m', target: 0 },
],
stress: [
{ duration: '1m', target: 700 },
{ duration: '4m', target: 700 },
{ duration: '1m', target: 0 },
],
smoke: [{ duration: '10s', target: 1 }],
};

function sayHello() {
return 'Hello';
}

export { WorkloadConfig, sayHello };
2 changes: 2 additions & 0 deletions test-commons/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { WorkloadConfig } from './src/config/workload';
export { sayHello } from './src/utils/say-hello';
239 changes: 239 additions & 0 deletions test-commons/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion test-commons/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
{
"name": "test-commons",
"version": "1.0.0"
"version": "1.0.0",
"type": "module",
"engines": {
"node": ">=18 <19"
},
"devDependencies": {
"rollup": "^4.9.5"
},
"scripts": {
"rollup": "rollup --config"
}
}
9 changes: 9 additions & 0 deletions test-commons/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

export default {
input: 'index.js',
output: {
dir: 'dist',
format: 'es',
},
external: [new RegExp(/^(k6|https?\:\/\/)(\/.*)?/)],
};
File renamed without changes.
3 changes: 3 additions & 0 deletions test-commons/src/utils/say-hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function sayHello() {
return 'Hello';
}
Loading

0 comments on commit cacce56

Please sign in to comment.