Skip to content

Commit

Permalink
Add examples to run k6 tests in CI using github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
DefCon-007 committed Jun 10, 2024
1 parent a193c69 commit f846d1f
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 9 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/example_basic_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Basic Test Workflow
on: [push]

jobs:
basic_k6_test:
name: k6 test run - basic example
runs-on: ubuntu-latest

services:
quickpizza:
image: ghcr.io/grafana/quickpizza-local:latest
ports:
- 3333:3333

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup k6
uses: grafana/setup-k6-action@v1
- name: Run k6 test
uses: grafana/run-k6-action@v1
with:
path: |
./k6/foundations/01.basic.js
flags: --duration 5s
26 changes: 26 additions & 0 deletions .github/workflows/example_browser_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Browser Test Workflow
on: [push]

jobs:
basic_k6_test:
name: k6 test run - browser test example
runs-on: ubuntu-latest

services:
quickpizza:
image: ghcr.io/grafana/quickpizza-local:latest
ports:
- 3333:3333

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup k6
uses: grafana/setup-k6-action@v1
with:
browser: true
- name: Run k6 test
uses: grafana/run-k6-action@v1
with:
path: |
./k6/browser/01.basic.js
25 changes: 25 additions & 0 deletions .github/workflows/example_cli_flags_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: CLI Flags Test Workflow
on: [push]

jobs:
basic_k6_test:
name: k6 test run - CLI flags example
runs-on: ubuntu-latest

services:
quickpizza:
image: ghcr.io/grafana/quickpizza-local:latest
ports:
- 3333:3333

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup k6
uses: grafana/setup-k6-action@v1
- name: Run k6 test
uses: grafana/run-k6-action@v1
with:
path: |
./k6/foundations/01.basic.js
flags: --vus 10 --duration 5s
26 changes: 26 additions & 0 deletions .github/workflows/example_env_var.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Environment Variables Test Workflow
on: [push]

jobs:
basic_k6_test:
name: k6 test run - environment variable example
runs-on: ubuntu-latest

services:
quickpizza:
image: ghcr.io/grafana/quickpizza-local:latest
ports:
- 3355:3333

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup k6
uses: grafana/setup-k6-action@v1
- name: Run k6 test
uses: grafana/run-k6-action@v1
env:
BASE_URL: "http://localhost:3355"
with:
path: |
./k6/foundations/01.basic.js
24 changes: 24 additions & 0 deletions .github/workflows/example_multiple_scripts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Multiple Test Scripts Workflow
on: [push]

jobs:
basic_k6_test:
name: k6 test run - running multiple scripts
runs-on: ubuntu-latest

services:
quickpizza:
image: ghcr.io/grafana/quickpizza-local:latest
ports:
- 3333:3333

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup k6
uses: grafana/setup-k6-action@v1
- name: Run k6 test
uses: grafana/run-k6-action@v1
with:
path: |
./k6/foundations/*basic*.js
27 changes: 27 additions & 0 deletions .github/workflows/example_specific_k6_version.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Specific k6 version Workflow
on: [push]

jobs:
basic_k6_test:
name: k6 test run - specific k6 version example
runs-on: ubuntu-latest

services:
quickpizza:
image: ghcr.io/grafana/quickpizza-local:latest
ports:
- 3333:3333

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup k6
uses: grafana/setup-k6-action@v1
with:
k6-version: '0.49.0'
- name: Run k6 test
uses: grafana/run-k6-action@v1
with:
cloud-run-locally: false
path: |
./k6/foundations/01.basic.js
25 changes: 25 additions & 0 deletions .github/workflows/example_verify_scripts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Verify scripts Workflow
on: [push]

jobs:
basic_k6_test:
name: k6 test run - verify scripts example
runs-on: ubuntu-latest

services:
quickpizza:
image: ghcr.io/grafana/quickpizza-local:latest
ports:
- 3333:3333

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup k6
uses: grafana/setup-k6-action@v1
- name: Verify test script
uses: grafana/run-k6-action@v1
with:
path: |
./k6/foundations/01.basic.js
only-verify-scripts: true
90 changes: 81 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,31 @@

![QuickPizza screenshot](./docs/images/quickpizza-screenshot.png)

- [QuickPizza](#quickpizza)
- [What is QuickPizza? 🍕🍕🍕](#what-is-quickpizza-)
- [Requirements](#requirements)
- [Run locally with Docker](#run-locally-with-docker)
- [Use k6 to test QuickPizza](#use-k6-to-test-quickpizza)
- [Run k6 tests using GitHub actions](#run-k6-tests-using-github-actions)
- [Run a basic test](#run-a-basic-test)
- [Use environment variables](#use-environment-variables)
- [Use k6 CLI flags](#use-k6-cli-flags)
- [Run multiple test files](#run-multiple-test-files)
- [Run a browser test](#run-a-browser-test)
- [Only verify scripts without execution](#only-verify-scripts-without-execution)
- [Use a custom k6 version](#use-a-custom-k6-version)
- [Collect telemetry (Docker Compose)](#collect-telemetry-docker-compose)
- [Enabling debug logging](#enabling-debug-logging)
- [Running a Prometheus instance](#running-a-prometheus-instance)
- [Local Setup](#local-setup)
- [Grafana Cloud](#grafana-cloud)
- [Deploy QuickPizza Docker image](#deploy-quickpizza-docker-image)
- [Use an external database](#use-an-external-database)
- [Deploy application to Kubernetes](#deploy-application-to-kubernetes)

## What is QuickPizza? 🍕🍕🍕

`QuickPizza` is a web application, used for demonstrations and workshops, that generates new and exciting pizza combinations!
`QuickPizza` is a web application, used for demonstrations and workshops, that generates new and exciting pizza combinations!

The app is built using [SvelteKit](https://kit.svelte.dev/) for the frontend and [Go](https://go.dev/) for the backend.

Expand Down Expand Up @@ -83,7 +105,7 @@ k6 run -e BASE_URL=https://acmecorp.dev:3333 01.basic.js

<details>
<summary>Using k6 Docker image</summary>

If you want to use the [k6 Docker image](https://hub.docker.com/r/grafana/k6) to run k6, you need to run the Quickpizza and k6 containers within the same network.

First, create a Docker network. Then, run Quickpizza, assigning a hostname and connecting to the created network.
Expand All @@ -101,6 +123,56 @@ k6 run -e BASE_URL=https://acmecorp.dev:3333 01.basic.js
</details>


## Run k6 tests using GitHub actions

You can use [Setup k6 action](https://github.com/marketplace/actions/setup-grafana-k6) to set up k6 in your workflow and then use [Run k6 action](https://github.com/marketplace/actions/run-grafana-k6-tests) to execute tests.

Following are some examples of running k6 tests in CI pipeline using GitHub actions.

### Run a basic test

This examples runs a basic test using the latest available k6 version.

[.github/workflows/example_basic_test.yaml](.github/workflows/example_basic_test.yaml)

### Use environment variables

This example uses environment variables to configure aspects of the test script during the execution.

[.github/workflows/example_env_var.yaml](.github/workflows/example_env_var.yaml)

### Use k6 CLI flags

This example shows how to supply additional CLI flags when executing a test

[.github/workflows/example_cli_flags_test.yaml](.github/workflows/example_cli_flags_test.yaml)

### Run multiple test files

This example shows how to use the glob pattern to execute multiple test scripts.

[.github/workflows/example_multiple_scripts.yaml](.github/workflows/example_multiple_scripts.yaml)

### Run a browser test

This example shows how to run a browser test

[.github/workflows/example_browser_test.yaml](.github/workflows/example_browser_test.yaml)


### Only verify scripts without execution

This example shows how you verify that test scripts are valid without starting execution.

[.github/workflows/example_verify_scripts.yaml](.github/workflows/example_verify_scripts.yaml)


### Use a custom k6 version

This example shows how to use a specific k6 version

[.github/workflows/example_specific_k6_version.yaml](.github/workflows/example_specific_k6_version.yaml)

## Collect telemetry (Docker Compose)

Testing something you can't observe is only half the fun. QuickPizza is instrumented using best practices to record logs, emit metrics, traces and allow profiling. You can either collect and [store this data locally](#local-setup) or send it to [Grafana Cloud](#grafana-cloud).
Expand All @@ -119,7 +191,7 @@ export QUICKPIZZA_LOG_LEVEL=debug

The [docker-compose-local.yaml](./docker-compose-local.yaml) file is set up to run and orchestrate the QuickPizza, Grafana, Tempo, Loki, Prometheus, Pyroscope, and Grafana Agent containers.

The Grafana Agent collects traces, metrics, logs and profiling data from the QuickPizza app, forwarding them to the Tempo, Prometheus, Loki and Pyroscope services. Finally, you can visualize and correlate data stored in these containers with the locally running Grafana instance.
The Grafana Agent collects traces, metrics, logs and profiling data from the QuickPizza app, forwarding them to the Tempo, Prometheus, Loki and Pyroscope services. Finally, you can visualize and correlate data stored in these containers with the locally running Grafana instance.

To start the local environment, use the following command:

Expand Down Expand Up @@ -152,12 +224,12 @@ For detailed instructions about the different options of the k6 Prometheus outpu

### Grafana Cloud

The [docker-compose-cloud.yaml](./docker-compose-cloud.yaml) file is set up to run the QuickPizza and Grafana Agent containers.
The [docker-compose-cloud.yaml](./docker-compose-cloud.yaml) file is set up to run the QuickPizza and Grafana Agent containers.

In this setup, the Grafana Agent collects observability data from the QuickPizza app and forwards it to [Grafana Cloud](https://grafana.com/products/cloud/).

You will need the following settings:
1. The name of the [Grafana Cloud Stack](https://grafana.com/docs/grafana-cloud/account-management/cloud-portal/#your-grafana-cloud-stack) where the telemetry data will be stored.
1. The name of the [Grafana Cloud Stack](https://grafana.com/docs/grafana-cloud/account-management/cloud-portal/#your-grafana-cloud-stack) where the telemetry data will be stored.
2. An [Access Policy Token](https://grafana.com/docs/grafana-cloud/account-management/authentication-and-permissions/access-policies/) that includes the following scopes for the selected Grafana Cloud Stack: `stacks:read`, `metrics:write`, `logs:write`, `traces:write`, and `profiles:write`.

Then, create an `.env` file with the following environment variables and the values of the previous settings:
Expand Down Expand Up @@ -191,7 +263,7 @@ Restart the `docker-compose-cloud.yaml` environment.

**Send k6 results to Grafana Cloud Prometheus and visualize them with prebuilt dashboards**

Just like in the local setup, we can output k6 result metrics to a Prometheus instance; in this case, it is provided by our Grafana Cloud Stack.
Just like in the local setup, we can output k6 result metrics to a Prometheus instance; in this case, it is provided by our Grafana Cloud Stack.

```bash
K6_PROMETHEUS_RW_USERNAME=USERNAME \
Expand All @@ -204,11 +276,11 @@ For detailed instructions, refer to the [k6 output guide for Grafana Cloud Prome

## Deploy QuickPizza Docker image

The [Dockerfile](./Dockerfile) contains the setup for running QuickPizza without collecting data with the Grafana agent.
The [Dockerfile](./Dockerfile) contains the setup for running QuickPizza without collecting data with the Grafana agent.

You can use the Dockerfile or build a Docker image to deploy the QuickPizza app on any cloud provider that supports Docker deployments. For simplicity, here are the `Fly.io` instructions:

1. [Authenticate using the fly CLI](https://fly.io/docs/speedrun/).
1. [Authenticate using the fly CLI](https://fly.io/docs/speedrun/).
2. Then, run the CLI to deploy the application and set up the internal port `3333` that the server listens to.
```bash
fly launch --internal-port 3333 --now
Expand All @@ -234,6 +306,6 @@ export QUICKPIZZA_DB="quickpizza.db"

## Deploy application to Kubernetes

If you want to run a test that uses [xk6-disruptor](https://k6.io/docs/javascript-api/xk6-disruptor/), or want to experiment with distributed tracing, you will need to deploy QuickPizza to Kubernetes.
If you want to run a test that uses [xk6-disruptor](https://k6.io/docs/javascript-api/xk6-disruptor/), or want to experiment with distributed tracing, you will need to deploy QuickPizza to Kubernetes.

For a detailed setup instructions, see the [QuickPizza Kubernetes guide](./docs/kubernetes-setup.md).

0 comments on commit f846d1f

Please sign in to comment.