Skip to content

Commit

Permalink
Add trace based testing examples (#877)
Browse files Browse the repository at this point in the history
* Adding a user purchasing product trace-based test case

* Adding more tests

* Adding run script

* Fixed yaml lint issues

* Adding license header to the files

* Adding trace-based tests for more services

* Updating tests and adding them on the same format as the integration tests

* Fixed payment tests

* Fixing e2e web tests

* Fixing details found by yamllint

* Updating trace-based tests to refer a protobuf file instead of embedding it

* Fixed data types for email test and improved test time

* Structured tests per service

* Added tests for frontend service following endpoints used on loadgenerator

* fixing yaml lint issues

* Fixing small issues on tests
  • Loading branch information
danielbdias committed Jul 10, 2023
1 parent 5a05737 commit c7588d4
Show file tree
Hide file tree
Showing 45 changed files with 1,292 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ build-env-file:
run-tests:
docker compose run frontendTests
docker compose run integrationTests
docker compose run traceBasedTests

run-tracetesting:
docker compose run traceBasedTests

.PHONY: generate-protobuf
generate-protobuf:
Expand Down
96 changes: 96 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -740,3 +740,99 @@ services:
- recommendationservice
- shippingservice
- quoteservice

# Tracebased Tests
traceBasedTests:
image: ${IMAGE_NAME}:${IMAGE_VERSION}-traceBasedTests
container_name: traceBasedTests
profiles:
- tests
build:
context: ./
dockerfile: ./test/tracetesting/Dockerfile
environment:
- AD_SERVICE_ADDR
- CART_SERVICE_ADDR
- CHECKOUT_SERVICE_ADDR
- CURRENCY_SERVICE_ADDR
- EMAIL_SERVICE_ADDR
- FRONTEND_ADDR
- PAYMENT_SERVICE_ADDR
- PRODUCT_CATALOG_SERVICE_ADDR
- RECOMMENDATION_SERVICE_ADDR
- SHIPPING_SERVICE_ADDR
extra_hosts:
- "host.docker.internal:host-gateway"
depends_on:
tracetest-server:
condition: service_healthy
# adding demo services as dependencies
frontend:
condition: service_started
adservice:
condition: service_started
cartservice:
condition: service_started
checkoutservice:
condition: service_started
currencyservice:
condition: service_started
emailservice:
condition: service_started
paymentservice:
condition: service_started
productcatalogservice:
condition: service_started
recommendationservice:
condition: service_started
shippingservice:
condition: service_started
quoteservice:
condition: service_started

tracetest-server:
image: kubeshop/tracetest:latest
platform: linux/amd64
container_name: tracetest-server
profiles:
- tests
volumes:
- type: bind
source: ./test/tracetesting/tracetest-config.yaml
target: /app/tracetest.yaml
- type: bind
source: ./test/tracetesting/tracetest-provision.yaml
target: /app/provision.yaml
command: --provisioning-file /app/provision.yaml
ports:
- 11633:11633
extra_hosts:
- "host.docker.internal:host-gateway"
depends_on:
tracetest-postgres:
condition: service_healthy
otelcol:
condition: service_started
healthcheck:
test: [ "CMD", "wget", "--spider", "localhost:11633" ]
interval: 1s
timeout: 3s
retries: 60
environment:
TRACETEST_DEV: ${TRACETEST_DEV}

tracetest-postgres:
image: postgres:14
container_name: tracetest-postgres
profiles:
- tests
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
healthcheck:
test: pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"
interval: 1s
timeout: 5s
retries: 60
ports:
- 5432
4 changes: 2 additions & 2 deletions src/frontend/cypress/e2e/Home.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ describe('Home Page', () => {

it('should validate the home page', () => {
getElementByField(CypressFields.HomePage).should('exist');
getElementByField(CypressFields.ProductCard, getElementByField(CypressFields.ProductList)).should('have.length', 9);
getElementByField(CypressFields.ProductCard, getElementByField(CypressFields.ProductList)).should('have.length', 10);

getElementByField(CypressFields.SessionId).should('contain', SessionGateway.getSession().userId);
});

it('should change currency', () => {
getElementByField(CypressFields.CurrencySwitcher).select('EUR');
getElementByField(CypressFields.ProductCard, getElementByField(CypressFields.ProductList)).should('have.length', 9);
getElementByField(CypressFields.ProductCard, getElementByField(CypressFields.ProductList)).should('have.length', 10);

getElementByField(CypressFields.CurrencySwitcher).should('have.value', 'EUR');

Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ test("payment: expired credit card", (t) => {

test("product: list", async (t) => {
const res = await productList({});
t.is(res.products.length, 9);
t.is(res.products.length, 10);
});

test("product: get", async (t) => {
Expand Down Expand Up @@ -283,7 +283,7 @@ test("recommendation: list products", async (t) => {
const req = deepCopy(data.recommend);

const res = await recommend(req);
t.is(res.productIds.length, 4);
t.is(res.productIds.length, 5);
t.is(arrayIntersection(res.productIds, req.productIds).length, 0);
});

Expand Down
17 changes: 17 additions & 0 deletions test/tracetesting/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0


FROM alpine

WORKDIR /app

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

COPY ./test/tracetesting ./test/tracetesting
COPY ./pb ./pb

WORKDIR /app/test/tracetesting

CMD ["/bin/sh", "/app/test/tracetesting/run.bash"]
10 changes: 10 additions & 0 deletions test/tracetesting/ad-service/all.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

type: Transaction
spec:
id: ad-service-all
name: 'Ad Service'
description: Run all Ad Service tests enabled in sequence
steps:
- ./get.yaml
28 changes: 28 additions & 0 deletions test/tracetesting/ad-service/get.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

type: Test
spec:
id: ad-get-ads
name: 'Ad: get'
description: Get Ads from API
trigger:
type: grpc
grpc:
protobufFile: ../../../pb/demo.proto
address: ${env:AD_SERVICE_ADDR}
method: oteldemo.AdService.GetAds
request: |-
{
"contextKeys": [ "galaxy", "telescope" ]
}
specs:
- name: It returns two ads
selector: span[tracetest.span.type="rpc" name="oteldemo.AdService/GetAds" rpc.system="grpc" rpc.method="GetAds" rpc.service="oteldemo.AdService"]
assertions:
- attr:app.ads.count = 2
- name: It returns a valid redirectUrl for each ads
selector: span[tracetest.span.type="general" name="Tracetest trigger"]
assertions:
- attr:tracetest.response.body | json_path '$.ads[0].redirectUrl' contains "/product/"
- attr:tracetest.response.body | json_path '$.ads[1].redirectUrl' contains "/product/"
35 changes: 35 additions & 0 deletions test/tracetesting/cart-service/add-item-to-cart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

type: Test
spec:
id: cart-add-item-to-cart
name: 'Cart: add item to cart'
description: Add one item to the shopping cart
trigger:
type: grpc
grpc:
protobufFile: ../../../pb/demo.proto
address: ${env:CART_SERVICE_ADDR}
method: oteldemo.CartService.AddItem
request: |-
{
"userId": "1234",
"item": {
"productId": "OLJCESPC7Z",
"quantity": 1
}
}
specs:
- name: It added an item correctly into the shopping cart
selector: span[name="oteldemo.CartService/AddItem"]
assertions:
- attr:rpc.grpc.status_code = 0
- name: It set the cart item correctly on the database
selector: span[tracetest.span.type="database" name="HMSET" db.system="redis" db.redis.database_index="0"]
assertions:
- attr:db.statement = "HMSET 1234"
- name: It returned an empty cart
selector: span[tracetest.span.type="general" name="Tracetest trigger"]
assertions:
- "attr:tracetest.response.body = '{\n \n}'"
14 changes: 14 additions & 0 deletions test/tracetesting/cart-service/all.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

type: Transaction
spec:
id: cart-all
name: 'Cart Service'
description: Run all Cart tests enabled in sequence
steps:
- ./empty-cart.yaml
- ./add-item-to-cart.yaml
- ./check-if-cart-is-populated.yaml
- ./empty-cart.yaml
- ./check-if-cart-is-empty.yaml
27 changes: 27 additions & 0 deletions test/tracetesting/cart-service/check-if-cart-is-empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

type: Test
spec:
id: cart-check-if-cart-is-empty
name: 'Cart: check if cart is empty'
description: Check if the shopping cart has no items
trigger:
type: grpc
grpc:
protobufFile: ../../../pb/demo.proto
address: ${env:CART_SERVICE_ADDR}
method: oteldemo.CartService.GetCart
request: |-
{
"userId": "1234"
}
specs:
- name: It retrieved the cart items correctly
selector: span[name="oteldemo.CartService/GetCart"]
assertions:
- attr:rpc.grpc.status_code = 0
- name: It returned no items
selector: span[tracetest.span.type="general" name="Tracetest trigger"]
assertions:
- attr:tracetest.response.body | json_path '$.items' = "[]"
28 changes: 28 additions & 0 deletions test/tracetesting/cart-service/check-if-cart-is-populated.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

type: Test
spec:
id: cart-check-if-cart-is-populated
name: 'Cart: check if cart is populated'
description: Check if the shopping cart has one item
trigger:
type: grpc
grpc:
protobufFile: ../../../pb/demo.proto
address: ${env:CART_SERVICE_ADDR}
method: oteldemo.CartService.GetCart
request: |-
{
"userId": "1234"
}
specs:
- name: It retrieved the cart items correctly
selector: span[name="oteldemo.CartService/GetCart"]
assertions:
- attr:rpc.grpc.status_code = 0
- name: It returned the first item with correct attributes
selector: span[tracetest.span.type="general" name="Tracetest trigger"]
assertions:
- attr:tracetest.response.body | json_path '$.items[0].quantity' = 1
- attr:tracetest.response.body | json_path '$.items[0].productId' = "OLJCESPC7Z"
27 changes: 27 additions & 0 deletions test/tracetesting/cart-service/empty-cart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

type: Test
spec:
id: cart-empty-cart
name: 'Cart: empty cart'
description: Clean shopping cart
trigger:
type: grpc
grpc:
protobufFile: ../../../pb/demo.proto
address: ${env:CART_SERVICE_ADDR}
method: oteldemo.CartService.EmptyCart
request: |-
{
"userId": "1234"
}
specs:
- name: It emptied the shopping cart with success
selector: span[name="oteldemo.CartService/EmptyCart"]
assertions:
- attr:rpc.grpc.status_code = 0
- name: It sent cleaning message to the database
selector: span[tracetest.span.type="database" name="EXPIRE" db.system="redis" db.redis.database_index="0"]
assertions:
- attr:db.statement = "EXPIRE 1234"
10 changes: 10 additions & 0 deletions test/tracetesting/checkout-service/all.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

type: Transaction
spec:
id: checkout-service-all
name: 'Checkout Service'
description: Run all Checkout Service tests enabled in sequence
steps:
- ./place-order.yaml
46 changes: 46 additions & 0 deletions test/tracetesting/checkout-service/place-order.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

type: Test
spec:
id: checkout-place-order
name: 'Checkout: place order'
description: Place one order on the system
trigger:
type: grpc
grpc:
protobufFile: ../../../pb/demo.proto
address: ${env:CHECKOUT_SERVICE_ADDR}
method: oteldemo.CheckoutService.PlaceOrder
request: |-
{
"userId": "1997",
"userCurrency": "USD",
"address": {
"streetAddress": "410 Terry Ave. North",
"city": "Seattle",
"state": "Washington",
"country": "United States",
"zipCode": "98109"
},
"email": "amazon@example.com",
"creditCard": {
"creditCardNumber": "4117-7059-6121-5486",
"creditCardCvv": 346,
"creditCardExpirationYear": 2025,
"creditCardExpirationMonth": 3
}
}
specs:
- name: It returns a valid order
selector: span[tracetest.span.type="general" name="Tracetest trigger"]
assertions:
- attr:tracetest.response.body | json_path '$.order.orderId' != ""
- attr:tracetest.response.body | json_path '$.order.shippingTrackingId' != ""
- attr:tracetest.response.body | json_path '$.order.shippingAddress' != "{}"
- attr:tracetest.response.body | json_path '$.order.shippingCost.currencyCode' = "USD"
- name: It calls the PlaceOrder method successfuly
selector: span[tracetest.span.type="rpc" name="oteldemo.CheckoutService/PlaceOrder"
rpc.system="grpc" rpc.method="PlaceOrder" rpc.service="oteldemo.CheckoutService"]
assertions:
- attr:rpc.grpc.status_code = 0
Loading

0 comments on commit c7588d4

Please sign in to comment.