Skip to content

Commit

Permalink
updating tyk example (#3968)
Browse files Browse the repository at this point in the history
* updating tyk example

* updating tyk recipe
  • Loading branch information
xoscar authored Aug 14, 2024
1 parent 7f55ed1 commit 2e10f27
Show file tree
Hide file tree
Showing 16 changed files with 321 additions and 840 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ cd tracetest/examples/quick-start-tyk

**Tracetest Account**:

- Copy the `.env.template` file to `.env`.
- Log into **the** [Tracetest app](https://app.tracetest.io/).
- This example is configured to use the OpenTelemetry Collector. Ensure the environment you will be utilizing to run this example is also configured to use the OpenTelemetry Tracing Backend by clicking on Settings, Tracing Backend, OpenTelemetry, and Save.
- Configure your environment to use [the agent](https://docs.tracetest.io/concepts/agent), then click the Settings link, and from the Agent tab select the "Run Agent Locally" option.
- Fill out the [token](https://docs.tracetest.io/concepts/environment-tokens) and [agent api key](https://docs.tracetest.io/concepts/agent) details by editing your .env file. You can find these values in the Settings area for your environment.
- Sign up to [`app.tracetest.io`](https://app.tracetest.io) or follow the [get started](/getting-started/installation) docs.
- Have access to the environment's [agent API key](https://app.tracetest.io/retrieve-token).

**Docker**: Have [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) installed on your machine.

## Project Structure

Expand Down Expand Up @@ -111,30 +110,49 @@ You can find the Tyk Gateway apps and configuration in the `deployments/tyk-gate
The tracetest setup is composed by the `tracetest-e2e` and the `tracetest-agent` services under the `docker-compose.yml` file.

```yaml title=docker-compose.yml
# Tracetest x Playwright
tracetest-e2e:
build: .
command: npm test
# Cloud-based Managed Tracetest
tracetest-agent:
image: kubeshop/tracetest-agent:latest
environment:
- POKESHOP_DEMO_URL=${POKESHOP_DEMO_URL}
- TRACETEST_API_TOKEN=${TRACETEST_API_TOKEN}
- TYK_AUTH_KEY=${TYK_AUTH_KEY}
# Get the required information here: https://app.tracetest.io/retrieve-token
- TRACETEST_API_KEY=${TRACETEST_TOKEN}
- TRACETEST_ENVIRONMENT_ID=${TRACETEST_ENVIRONMENT_ID}
tracetest-apply:
build:
dockerfile: Dockerfile.tracetest
volumes:
- ./resources:/resources
environment:
TRACETEST_TOKEN: ${TRACETEST_TOKEN}
TRACETEST_ENVIRONMENT_ID: ${TRACETEST_ENVIRONMENT_ID}
entrypoint:
- bash
- /resources/apply.sh
networks:
default: null
depends_on:
tracetest-agent:
condition: service_started
api:
condition: service_healthy
worker:
condition: service_started
tyk-gateway:
tracetest-agent:
condition: service_started
tracetest-agent:
tracetest-run:
build:
dockerfile: Dockerfile.tracetest
volumes:
- ./resources:/resources
environment:
TRACETEST_DEV: ${TRACETEST_DEV}
TRACETEST_API_KEY: ${TRACETEST_AGENT_API_KEY}
TRACETEST_SERVER_URL: ${TRACETEST_SERVER_URL}
image: kubeshop/tracetest-agent:latest
TRACETEST_TOKEN: ${TRACETEST_TOKEN}
TRACETEST_ENVIRONMENT_ID: ${TRACETEST_ENVIRONMENT_ID}
entrypoint:
- bash
- /resources/run.sh
networks:
default: null
depends_on:
tracetest-apply:
condition: service_completed_successfully
```

### 3. Services under Test
Expand Down Expand Up @@ -223,137 +241,121 @@ worker:
Copy the `.env.template` and create a `.env` file in the same directory. Add token and Cloud Agent endpoint.

```txt title=.env
TRACETEST_API_TOKEN=<my_token_with_engineer_access>
TRACETEST_AGENT_API_KEY=<my_agent_api_key>
# Get the required information here: https://app.tracetest.io/retrieve-token
TRACETEST_TOKEN="<YOUR_TRACETEST_TOKEN>"
TRACETEST_ENVIRONMENT_ID="<YOUR_ENV_ID>"
POKESHOP_DEMO_URL=http://tyk-gateway:8080
TYK_AUTH_KEY=28d220fd77974a4facfb07dc1e49c2aa
```

## The Tracetest End To End Script

The `playwright/home.spec.ts` file contains the script that will execute the trace-based tests against the Pokeshop App proxied by a Tyk endpoint.
The `resources/script.js` file contains the script that will execute the trace-based tests against the Pokeshop App proxied by a Tyk endpoint.

### Steps Executed by the Script

1. Create a new key in the Tyk Gateway.
2. Create a new test definition in Tracetest (using the async import service from the Pokeshop Demo).
3. Run the Playwright Test.
4. Report back the results.

```typescript title=playwright/home.spec.ts
import { test, expect } from "@playwright/test";
import { getKey } from "./auth";
import Tracetest, { Types } from "@tracetest/playwright";
const { TRACETEST_API_TOKEN = "" } = process.env;
let tracetest: Types.TracetestPlaywright | undefined = undefined;
test.describe.configure({ mode: "serial" });
const definition = `
type: Test
spec:
id: UGxheXdyaWdodDogaW1wb3J0cyBhIHBva2Vtb24=
name: "Playwright: imports a pokemon"
trigger:
type: playwright
specs:
- selector: span[tracetest.span.type="http"] span[tracetest.span.type="http"]
name: "All HTTP Spans: Status code is 200"
assertions:
- attr:http.status_code = 200
- selector: span[tracetest.span.type="database"]
name: "All Database Spans: Processing time is less than 100ms"
assertions:
- attr:tracetest.span.duration < 2s
outputs:
- name: MY_OUTPUT
selector: span[tracetest.span.type="general" name="Tracetest trigger"]
value: attr:name
`;
test.beforeAll(async () => {
tracetest = await Tracetest({ apiToken: TRACETEST_API_TOKEN });
await tracetest.setOptions({
"Playwright: imports a pokemon": {
definition,
3. Import a Pokemon.

```typescript title=resources/script.js
const { expect } = require('@playwright/test');
const URL = 'http://tyk-gateway:8080';
const API_KEY = '28d220fd77974a4facfb07dc1e49c2aa';
const getKey = async () => {
const params = {
headers: {
'Content-Type': 'application/json',
'x-tyk-authorization': API_KEY,
'Response-Type': 'application/json',
},
};
const data = {
alias: 'website',
expires: -1,
access_rights: {
1: {
api_id: '1',
api_name: 'pokeshop',
versions: ['Default'],
},
},
};
const res = await fetch(`${URL}/tyk/keys/create`, {
...params,
method: 'POST',
body: JSON.stringify(data),
});
});

test.beforeEach(async ({ page, context }, { title }) => {
// sets the auth headers for the page requests
const { key } = await res.json();

return key;
};

async function importPokemon(page) {
const key = await getKey();
await context.setExtraHTTPHeaders({

await page.setExtraHTTPHeaders({
Authorization: `Bearer ${key}`,
});

await page.goto("/");
await tracetest?.capture(title, page);
});
await page.goto(URL);

// optional step to break the playwright script in case a Tracetest test fails
test.afterAll(async ({}, testInfo) => {
testInfo.setTimeout(80000);
await tracetest?.summary();
});
expect(await page.getByText('Pokeshop')).toBeTruthy();

test("Playwright: imports a pokemon", async ({ page }) => {
expect(await page.getByText("Pokeshop")).toBeTruthy();
await page.click('text=Import');
await page.getByLabel('ID').fill('143');

await page.click("text=Import");
await Promise.all([
page.waitForResponse((resp) => resp.url().includes('/pokemon/import') && resp.status() === 200),
page.getByRole('button', { name: 'OK', exact: true }).click(),
]);
}

await page.getByLabel("ID").fill(Math.floor(Math.random() * 101).toString());
await page.getByRole("button", { name: "OK", exact: true }).click();
});
module.exports = { importPokemon };
```

The output from the Playwright tests will show the test results with links to the Tracetest App.

```bash title=Terminal
WARN[0000] The "TRACETEST_SERVER_URL" variable is not set. Defaulting to a blank string.
[+] Running 2/2
✔ worker Pulled 1.1s
✔ api Pulled 1.1s
[+] Creating 9/0
✔ Container quick-start-tyk-tyk-redis-1 Running 0.0s
✔ Container quick-start-tyk-otel-collector-1 Running 0.0s
✔ Container quick-start-tyk-cache-1 Running 0.0s
✔ Container quick-start-tyk-queue-1 Running 0.0s
✔ Container quick-start-tyk-tracetest-agent-1 Running 0.0s
✔ Container quick-start-tyk-postgres-1 Running 0.0s
✔ Container quick-start-tyk-tyk-gateway-1 Running 0.0s
✔ Container quick-start-tyk-worker-1 Running 0.0s
✔ Container quick-start-tyk-api-1 Running 0.0s
[+] Running 3/3
✔ Container quick-start-tyk-postgres-1 Healthy 0.5s
✔ Container quick-start-tyk-cache-1 Healthy 0.5s
✔ Container quick-start-tyk-queue-1 Healthy 0.5s
✔ worker Pulled 0.9s
✔ api Pulled 0.8s
[+] Creating 8/0
✔ Container quick-start-tyk-tracetest-agent-1 Running 0.0s
✔ Container quick-start-tyk-postgres-1 Running 0.0s
✔ Container quick-start-tyk-jaeger-1 Running 0.0s
✔ Container quick-start-tyk-tyk-redis-1 Running 0.0s
✔ Container quick-start-tyk-cache-1 Running 0.0s
✔ Container quick-start-tyk-otel-collector-1 Running 0.0s
✔ Container quick-start-tyk-queue-1 Running 0.0s
✔ Container quick-start-tyk-tyk-gateway-1 Running 0.0s
[+] Running 7/7
✔ Container quick-start-tyk-jaeger-1 Healthy 0.5s
✔ Container quick-start-tyk-postgres-1 Healthy 3.2s
✔ Container quick-start-tyk-cache-1 Healthy 3.2s
✔ Container quick-start-tyk-queue-1 Healthy 3.2s
✔ Container quick-start-tyk-worker-1 Started 0.2s
✔ Container quick-start-tyk-api-1 Started 0.1s
✔ Container quick-start-tyk-tracetest-apply-1 Started 0.2s
[+] Running 2/2
✔ worker Pulled 1.2s
✔ api Pulled 1.1s

> [email protected] test
> playwright test


Running 1 test using 1 worker
[chromium] › home.spec.ts:59:5 › Playwright: imports a pokemon

Successful: 1
Failed: 0

[✔️ Playwright: imports a pokemon] #20 - https://app.tracetest.io/organizations/<YOUR_ORG>/environments/<YOUR_ENV>/test/UGxheXdyaWdodDogaW1wb3J0cyBhIHBva2Vtb24=/run/20

1 passed (14.1s)

To open last HTML report run:

npx playwright show-report

✔ api Pulled 0.8s
✔ worker Pulled 0.8s
Configuring Tracetest
SUCCESS Successfully configured Tracetest CLI
Running Trace-Based Tests...
✔ RunGroup: #QMjWZ5jIR (https://app.tracetest.io/organizations/ttorg_ced62e34638d965e/environments/ttenv_b42fa137465c6e04/run/QMjWZ5jIR)
Summary: 1 passed, 0 failed, 0 pending
✔ Import Pokemon (https://app.tracetest.io/organizations/ttorg_ced62e34638d965e/environments/ttenv_b42fa137465c6e04/test/4_IKfPlIg/run/13/test) - trace id: 1554d5d5904daa17f2a6854a7d7608e5
✔ Import should return 200
✔ All Database Spans: Processing time is less than 1s
✔ Import Pokemon Span Should be Present
✔ Redis should return in 20ms
```

### Tracetest App Results
Expand All @@ -365,7 +367,7 @@ To open last HTML report run:
Spin up the deployment and test execution.

```bash title=Terminal
docker-compose run tracetest-e2e
docker-compose run tracetest-run
```

This will trigger the Docker Compose setup and immediately run the [trace-based tests using the Tracetest Playwright integration](../../tools-and-integrations/playwright.mdx) as part of the `tracetest-e2e` service.
Expand Down
7 changes: 5 additions & 2 deletions examples/quick-start-tyk/.env.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
TRACETEST_API_TOKEN=
TRACETEST_AGENT_API_KEY=
# Get the required information here: https://app.tracetest.io/retrieve-token

TRACETEST_TOKEN="<YOUR_TRACETEST_TOKEN>"
TRACETEST_ENVIRONMENT_ID="<YOUR_ENV_ID>"

POKESHOP_DEMO_URL=http://tyk-gateway:8080
TYK_AUTH_KEY=28d220fd77974a4facfb07dc1e49c2aa
11 changes: 11 additions & 0 deletions examples/quick-start-tyk/Dockerfile.tracetest
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM alpine

WORKDIR /app
ARG TRACETEST_IMAGE_VERSION=v1.4.0

RUN apk --update add bash jq curl
RUN curl -L https://raw.githubusercontent.com/kubeshop/tracetest/main/install-cli.sh | bash -s -- $TRACETEST_IMAGE_VERSION

WORKDIR /resources

ENTRYPOINT ["echo", "Tracetest CLI installed"]
Loading

0 comments on commit 2e10f27

Please sign in to comment.