Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of running setup methods to support blog post #3630

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/setup-of-tracetest-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.env
dist
9 changes: 9 additions & 0 deletions examples/setup-of-tracetest-tests/README.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading