Skip to content

Commit

Permalink
Example of running setup methods to support blog post (#3630)
Browse files Browse the repository at this point in the history
* example to support blog post

Example to support https://www.notion.so/kubeshop/Running-Tracetest-from-Javascript-or-Typescript-682062bd702c4f5eb7c3164a09ad62bd blog post

* examples(ts-integration): add gitignore and formatting

* Update delete_test.ts

Removed an unused line.

* examples(ts): add await

---------

Co-authored-by: Adnan Rahic <ado.raha198@gmail.com>
  • Loading branch information
kdhamric and adnanrahic committed Feb 15, 2024
1 parent 15f5e6c commit 51c3f16
Show file tree
Hide file tree
Showing 10 changed files with 498 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/setup-of-tracetest-tests/.gitignore
@@ -0,0 +1,3 @@
node_modules
.env
dist
9 changes: 9 additions & 0 deletions examples/setup-of-tracetest-tests/README.md
@@ -0,0 +1,9 @@
# Setup and Tear Down of Tracetest Tests

> Read the detailed blog post [Enabling Programatic Setup and Tear Down of Tracetest Tests](https://tracetest.io/blog/enabling-programatic-setup-and-tear-down-of-tracetest-tests) that describes two methods to run setup steps before executing a test:
- via a test suite
- via the @tracetest/client NPM package

The article contains the complete instructions on how to run this example.

Feel free to check out the [docs](https://docs.tracetest.io/), and join our [Slack Community](https://dub.sh/tracetest-community) for more info!
29 changes: 29 additions & 0 deletions examples/setup-of-tracetest-tests/delete_pokemon.yaml
@@ -0,0 +1,29 @@
type: Test
spec:
id: delete-pokemon
name: Delete Pokemon
trigger:
type: http
httpRequest:
method: DELETE
url: https://demo-pokeshop.tracetest.io/pokemon/${env:pokemon_id}
headers:
- key: Content-Type
value: application/json
specs:
- selector: span[tracetest.span.type="general" name="Tracetest trigger"]
name: Delete returns a 200 status code
assertions:
- attr:tracetest.response.status = 200
- selector: span[tracetest.span.type="database" db.system="redis" db.operation="del" db.redis.database_index="0"]
name: Ensure we are deleting from the redis cache also
assertions:
- attr:tracetest.selected_spans.count = 1
- selector: span[tracetest.span.type="database"]
name: "All Database Spans: Processing time is less than 10ms"
assertions:
- attr:tracetest.span.duration < 10ms
- selector: span[tracetest.span.type="database" name="delete pokeshop.pokemon" db.system="postgres" db.name="pokeshop" db.user="ashketchum" db.operation="delete" db.sql.table="pokemon"]
name: Check that number of deleted rows from Postgres is one
assertions:
- attr:db.result = 1
69 changes: 69 additions & 0 deletions examples/setup-of-tracetest-tests/delete_test.ts
@@ -0,0 +1,69 @@

import fs from 'fs';
import { Pokemon } from './types';
import Tracetest from '@tracetest/client';

// To use the @tracetest/client, you must have a token for the environment. This is created on the Settings
// page under Tokens by the administrator for the environment. The token below has been given the 'engineer'
// role in the pokeshop-demo env in the tracetest-demo org so you can create and run tests in this environment.
// Want to read more about setting up tokens? https://docs.tracetest.io/concepts/environment-tokens

const TRACETEST_API_TOKEN = 'tttoken_4fea89f6e7fa1500';
const baseUrl = 'https://demo-pokeshop.tracetest.io/pokemon';

// json for the body of the POST to create a pokemon
const setupPokemon = `{
"name": "fearow",
"imageUrl": "https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/22.png",
"isFeatured": false,
"type": "normal,flying"
}`

const main = async () => {
console.log('Lets use the TRACETEST_API_TOKEN to authenticate the @tracetest/client module...')
const tracetest = await Tracetest(TRACETEST_API_TOKEN);


//execute setup by adding a Pokemon with a REST POST api call directly
const createPokemon = async (): Promise<Pokemon> => {
const response = await fetch(baseUrl,{
method: 'POST',
body: setupPokemon,
headers: {'Content-Type': 'application/json'}
});
return await response.json() as Pokemon;
};

console.log('Adding the Pokemon - this is the setup action we need before running a Tracetest test')
const pokemon = await createPokemon();

// Get the id of the pokemon that we created in the setup step
let pokemonId = pokemon.id;
console.log('The Pokemon id we created was ', pokemonId);


// Lets pull in the delete-pokemon test from a file
let deleteTest = fs.readFileSync('delete_pokemon.yaml', 'utf-8');

// Lets setup the variables we will be passing into the test (ie the pokemon_id)
const getVariables = (id: string) => [
{ key: 'pokemon_id', value: id }
];

const deletePokemon = async () => {
console.log('Creating the delete-pokemon test based on the test in delete_pokemon.yaml...');
const test = await tracetest.newTest(deleteTest);


// run deletes pokemon test
console.log('Running the delete-pokemon test...');
const run = await tracetest.runTest(test, { variables: getVariables(String(pokemonId)) });
await run.wait();
};

await deletePokemon();

console.log(await tracetest.getSummary());
};

main();
8 changes: 8 additions & 0 deletions examples/setup-of-tracetest-tests/delete_test_suite.yaml
@@ -0,0 +1,8 @@
type: TestSuite
spec:
id: bS4cix2IR
name: Delete with Setup
description: A test of the deletion end point with setup test which adds the pokemon as a setup step.
steps:
- setup-pokemon
- delete-pokemon

0 comments on commit 51c3f16

Please sign in to comment.