Skip to content

Commit

Permalink
Adding k6 to GHA
Browse files Browse the repository at this point in the history
  • Loading branch information
xoscar committed Apr 3, 2024
1 parent 939b96f commit 5e5e35b
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 39 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/k6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: K6 Tests
on:
workflow_dispatch:
schedule:
- cron: '0 */1 * * *' # every hour

jobs:
docker:
timeout-minutes: 10
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v1

- name: Start containers
run: docker compose -f docker-compose.yml -f docker-compose.k6.workflows.yml run k6-tracetest
env:
TRACETEST_API_TOKEN: ${{secrets.TRACETEST_TOKEN}}
POKESHOP_DEMO_URL: ${{secrets.POKESHOP_DEMO_URL}}

- name: Stop containers
if: always()
run: docker compose -f docker-compose.yml -f docker-compose.k6.workflows.yml down
4 changes: 1 addition & 3 deletions docker-compose.e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ name: pokeshop

services:
tracetest-agent:
image: kubeshop/tracetest-agent:latest
environment:
TRACETEST_DEV: ${TRACETEST_DEV}
TRACETEST_API_KEY: ${TRACETEST_AGENT_API_KEY}
TRACETEST_SERVER_URL: ${TRACETEST_SERVER_URL}
image: kubeshop/tracetest-agent:latest
networks:
default: null
15 changes: 15 additions & 0 deletions docker-compose.k6.workflows.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3'

services:
k6-tracetest:
build:
context: .
dockerfile: k6.Dockerfile
environment:
XK6_TRACETEST_API_TOKEN: ${TRACETEST_API_TOKEN}
POKESHOP_DEMO_URL: ${POKESHOP_DEMO_URL}
depends_on:
- worker
- api
volumes:
- ./test/k6/add-pokemon.js:/import-pokemon.js
22 changes: 22 additions & 0 deletions docker-compose.k6.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '3'

services:
k6-tracetest:
build:
context: .
dockerfile: k6.Dockerfile
environment:
XK6_TRACETEST_API_TOKEN: ${TRACETEST_API_TOKEN}
POKESHOP_DEMO_URL: ${POKESHOP_DEMO_URL}
depends_on:
- api
- tracetest-agent
- worker
volumes:
- ./test/k6/import-pokemon.js:/import-pokemon.js
tracetest-agent:
image: kubeshop/tracetest-agent:latest
environment:
TRACETEST_DEV: ${TRACETEST_DEV}
TRACETEST_API_KEY: ${TRACETEST_AGENT_API_KEY}
TRACETEST_SERVER_URL: ${TRACETEST_SERVER_URL}
17 changes: 17 additions & 0 deletions k6.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM golang:1.21-alpine as builder

WORKDIR /app

ENV CGO_ENABLED 0
RUN go install go.k6.io/xk6/cmd/xk6@latest

RUN xk6 build v0.50.0 --with github.com/kubeshop/xk6-tracetest

FROM alpine

COPY --from=builder /app/k6 /bin/
COPY ./test/k6/import-pokemon.js .
ENV XK6_TRACETEST_API_TOKEN=your-api-token
ENV POKESHOP_DEMO_URL=http://localhost:8081

ENTRYPOINT k6 run /import-pokemon.js -o xk6-tracetest -e POKESHOP_DEMO_URL=$POKESHOP_DEMO_URL
74 changes: 74 additions & 0 deletions test/k6/add-pokemon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Http, Tracetest } from 'k6/x/tracetest';
import { sleep } from 'k6';

export const options = {
vus: 5,
duration: '5s',
};

const POKESHOP_DEMO_URL = __ENV.POKESHOP_DEMO_URL || 'http://localhost:8081';

const http = new Http();
const tracetest = Tracetest();

export default function () {
const url = `${POKESHOP_DEMO_URL}/pokemon`;
const payload = JSON.stringify({
name: 'charizard',
type: 'flying',
imageUrl: 'https://assets.pokemon.com/assets/cms2/img/pokedex/full/006.png',
isFeatured: true,
});
const params = {
headers: {
'Content-Type': 'application/json',
},
};

const definition = `type: Test
spec:
id: k6-tracetest-pokeshop-add-pokemon
name: "K6 - Add a Pokemon"
trigger:
type: k6
specs:
- selector: span[tracetest.span.type="http" name="POST /pokemon" http.method="POST"]
name: The POST /pokemon was called correctly
assertions:
- attr:http.status_code = 201
- selector: span[tracetest.span.type="general" name="validate request"]
name: The request sent to API is valid
assertions:
- attr:validation.is_valid = "true"
- selector: span[tracetest.span.type="database" name="create pokeshop.pokemon" db.system="postgres" db.name="pokeshop" db.user="ashketchum" db.operation="create" db.sql.table="pokemon"]
name: A Pokemon was inserted on database
assertions:
- attr:db.result | json_path '$.imageUrl' = "https://assets.pokemon.com/assets/cms2/img/pokedex/full/052.png"
`;

const response = http.post(url, payload, params);

tracetest.runTest(
response.trace_id,
{
definition,
should_wait: true,
},
{
url,
method: 'GET',
}
);

sleep(1);
}

export function handleSummary() {
return {
stdout: tracetest.summary(),
};
}

export function teardown() {
tracetest.validateResult();
}
41 changes: 28 additions & 13 deletions test/k6/import-pokemon.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,59 @@
import { Http, Tracetest } from "k6/x/tracetest";
import { sleep } from "k6";
import { Http, Tracetest } from 'k6/x/tracetest';
import { sleep } from 'k6';

export const options = {
vus: 1,
duration: "5s",
duration: '5s',
};

const POKESHOP_DEMO_URL = __ENV.POKESHOP_DEMO_URL || 'http://localhost:8081';

const http = new Http();
const testId = "kc_MgKoVR";
const tracetest = Tracetest();

let pokemonId = 6; // charizard

export default function () {
const url = "http://localhost:8081/pokemon/import";
const url = `${POKESHOP_DEMO_URL}/pokemon/import`;
const payload = JSON.stringify({
id: pokemonId++,
});
const params = {
headers: {
"Content-Type": "application/json",
},
tracetest: {
testId,
'Content-Type': 'application/json',
},
};

const definition = `type: Test
spec:
id: k6-tracetest-pokeshop-import-pokemon
name: K6
description: K6
trigger:
type: k6
specs:
- selector: span[tracetest.span.type="general" name="import pokemon"]
name: Should have imported the pokemon
assertions:
- attr:tracetest.selected_spans.count = 1
- selector: |-
span[tracetest.span.type="http" net.peer.name="pokeapi.co" http.method="GET"]
name: Should trigger a request to the POKEAPI
assertions:
- attr:http.url = "https://pokeapi.co/api/v2/pokemon/${pokemonId}"
`;

const response = http.post(url, payload, params);

tracetest.runTest(
response.trace_id,
{
test_id: testId,
variable_name: "TRACE_ID",
definition,
should_wait: true,
},
{
id: "123",
url,
method: "GET",
method: 'GET',
}
);

Expand Down
23 changes: 0 additions & 23 deletions test/k6/import-pokemon.yaml

This file was deleted.

0 comments on commit 5e5e35b

Please sign in to comment.