From 119855e02b82f3eb4b17d1d7ea739adce4f650d6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adnan=20Rahi=C4=87?= <adnan@kubeshop.io>
Date: Tue, 19 Mar 2024 16:06:50 +0100
Subject: [PATCH] docs(tools+integrations): fix broken k6 guide (#39)

---
 .gitignore                               |  3 ++
 cypress/e2e/1-getting-started/home.cy.ts | 12 ++---
 test/k6/import-pokemon.js                | 56 ++++++++++++++++++++++++
 test/k6/import-pokemon.yaml              | 23 ++++++++++
 4 files changed, 88 insertions(+), 6 deletions(-)
 create mode 100644 test/k6/import-pokemon.js
 create mode 100644 test/k6/import-pokemon.yaml

diff --git a/.gitignore b/.gitignore
index a34680e..112cdea 100644
--- a/.gitignore
+++ b/.gitignore
@@ -16,3 +16,6 @@ cypress/downloads
 /blob-report/
 /playwright/.cache/
 serverless/.env
+
+# k6 executable 
+/k6
diff --git a/cypress/e2e/1-getting-started/home.cy.ts b/cypress/e2e/1-getting-started/home.cy.ts
index af5e315..d70da0d 100644
--- a/cypress/e2e/1-getting-started/home.cy.ts
+++ b/cypress/e2e/1-getting-started/home.cy.ts
@@ -7,8 +7,8 @@ let tracetest: Types.TracetestCypress | undefined = undefined;
 const definition = `
 type: Test
 spec:
-  id: aW1wb3J0cyBhIHBva2Vtb24=
-  name: imports a pokemon
+  id: aW1wb3J0cyBhIHBva2Vtb23=
+  name: "Cypress: imports a pokemon"
   trigger:
     type: cypress
   specs:
@@ -32,7 +32,7 @@ describe('Home', { defaultCommandTimeout: 80000 }, () => {
       tracetest = instance;
       tracetest
         .setOptions({
-          'imports a pokemon': {
+          'Cypress: imports a pokemon': {
             definition,
           },
         })
@@ -51,7 +51,7 @@ describe('Home', { defaultCommandTimeout: 80000 }, () => {
     tracetest.summary().then(() => done());
   });
 
-  it('create a pokemon', () => {
+  it('Cypress: create a pokemon', () => {
     cy.get('[data-cy="create-pokemon-button"]').should('be.visible').click();
     cy.get('[data-cy="create-pokemon-modal"]').should('be.visible');
     cy.get('#name').type('Pikachu');
@@ -61,7 +61,7 @@ describe('Home', { defaultCommandTimeout: 80000 }, () => {
     cy.get('button').contains('OK').click();
   });
 
-  it('imports a pokemon', () => {
+  it('Cypress: imports a pokemon', () => {
     cy.get('[data-cy="import-pokemon-button"]').click();
     cy.get('[data-cy="import-pokemon-form"]').should('be.visible');
 
@@ -71,7 +71,7 @@ describe('Home', { defaultCommandTimeout: 80000 }, () => {
     cy.get('button').contains('OK').click({ force: true });
   });
 
-  it('deletes a pokemon', () => {
+  it('Cypress: deletes a pokemon', () => {
     cy.get('[data-cy="pokemon-list"]').should('be.visible');
     cy.get('[data-cy="pokemon-card"]').first().click().get('[data-cy="delete-pokemon-button"]').first().click();
   });
diff --git a/test/k6/import-pokemon.js b/test/k6/import-pokemon.js
new file mode 100644
index 0000000..2bb927d
--- /dev/null
+++ b/test/k6/import-pokemon.js
@@ -0,0 +1,56 @@
+import { Http, Tracetest } from "k6/x/tracetest";
+import { sleep } from "k6";
+
+export const options = {
+  vus: 1,
+  duration: "5s",
+};
+
+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 payload = JSON.stringify({
+    id: pokemonId++,
+  });
+  const params = {
+    headers: {
+      "Content-Type": "application/json",
+    },
+    tracetest: {
+      testId,
+    },
+  };
+
+  const response = http.post(url, payload, params);
+
+  tracetest.runTest(
+    response.trace_id,
+    {
+      test_id: testId,
+      variable_name: "TRACE_ID",
+      should_wait: true,
+    },
+    {
+      id: "123",
+      url,
+      method: "GET",
+    }
+  );
+
+  sleep(1);
+}
+
+export function handleSummary() {
+  return {
+    stdout: tracetest.summary(),
+  };
+}
+
+export function teardown() {
+  tracetest.validateResult();
+}
diff --git a/test/k6/import-pokemon.yaml b/test/k6/import-pokemon.yaml
new file mode 100644
index 0000000..dee21f1
--- /dev/null
+++ b/test/k6/import-pokemon.yaml
@@ -0,0 +1,23 @@
+type: Test
+spec:
+  id: kc_MgKoVR
+  name: K6
+  description: K6
+  trigger:
+    type: traceid
+    traceid:
+      id: ${env:TRACE_ID}
+  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/6"
+    - selector: span[tracetest.span.type="database" name="create postgres.pokemon"]
+      name: Should insert the pokemon to the DB
+      assertions:
+        - attr:db.result | json_path '.name'  =  "charizard"
\ No newline at end of file