Skip to content

Commit

Permalink
test(bats): complete move of public.bats (#3527)
Browse files Browse the repository at this point in the history
* test: add subscriber to top level bats

* test(bats): price sub working

* test: add realtime price to top level

* fix: PRICE_HOST in tilt

* ci: test buck-out cache

* ci: remove buck caches
  • Loading branch information
bodymindarts authored Nov 13, 2023
1 parent 8f1cfe3 commit 142e7f4
Show file tree
Hide file tree
Showing 18 changed files with 479 additions and 140 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/bats.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ jobs:
- name: Run the Magic Nix Cache
uses: DeterminateSystems/magic-nix-cache-action@v2
- name: Buck2 build
run: nix develop -c buck2 build //core/api //core/api-keys //apps/dashboard //apps/consent
run: |
nix develop -c buck2 build //core/api //core/api-ws-server \
//core/api-keys //apps/dashboard //apps/consent
- name: Run bats tests
run: |
nix develop -c bats --setup-suite-file bats/ci_setup_suite.bash -t bats/core/**
33 changes: 0 additions & 33 deletions .github/workflows/cache-buck-build.yml

This file was deleted.

4 changes: 2 additions & 2 deletions bats/ci_setup_suite.bash
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/bash

export REPO_ROOT=$(git rev-parse --show-toplevel)
source "${REPO_ROOT}/bats/helpers/setup-and-teardown.bash"
source "${REPO_ROOT}/bats/helpers/_common.bash"

TILT_PID_FILE=$REPO_ROOT/bats/.tilt_pid
TILT_PID_FILE="${BATS_ROOT_DIR}/.tilt_pid"

setup_suite() {
background buck2 run //dev:up -- --bats=True > "${REPO_ROOT}/bats/.e2e-tilt.log"
Expand Down
60 changes: 60 additions & 0 deletions bats/core/api/public.bats
Original file line number Diff line number Diff line change
@@ -1,9 +1,69 @@
#!/usr/bin/env bats

load "../../helpers/_common.bash"
load "../../helpers/subscriber.bash"

@test "public: can query globals" {
exec_graphql 'anon' 'globals'
network="$(graphql_output '.data.globals.network')"
[[ "${network}" = "regtest" ]] || exit 1
}

@test "public: can apply idempotency key to queries" {
fixed_idempotency_key=$(new_idempotency_key)
original_new_idempotency_key=$(declare -f new_idempotency_key)
new_idempotency_key() {
echo $fixed_idempotency_key
}

# Successful 1st attempt
exec_graphql 'anon' 'globals'
errors="$(graphql_output '.errors')"
[[ "$errors" == "null" ]] || exit 1

# Failed 2nd attempt with same idempotency key
exec_graphql 'anon' 'globals'
error_msg="$(graphql_output '.errors[0].message')"
[[ "$error_msg" == "HTTP fetch failed from 'public': 409: Conflict" ]] || exit 1

# Failed attempt with invalid idempotency key
new_idempotency_key() {
echo "invalid-key"
}
exec_graphql 'anon' 'globals'
error_msg="$(graphql_output '.errors[0].message')"
[[ "$error_msg" == "HTTP fetch failed from 'public': 400: Bad Request" ]] || exit 1

# Successful 3rd attempt with unique valid idempotency key
eval "$original_new_idempotency_key"
exec_graphql 'anon' 'globals'
[[ "$errors" == "null" ]] || exit 1
}

@test "public: can subscribe to price" {
subscribe_to 'anon' price-sub
retry 10 1 grep 'Data.*\bprice\b' "${SUBSCRIBER_LOG_FILE}"

num_errors=$(
grep 'Data.*\bprice\b' "${SUBSCRIBER_LOG_FILE}" \
| awk '{print $2}' \
| jq -r '.data.price.errors | length'
)
[[ "$num_errors" == "0" ]] || exit 1

stop_subscriber
}

@test "public: can subscribe to realtime price" {
subscribe_to 'anon' real-time-price-sub '{"currency": "EUR"}'
retry 10 1 grep 'Data.*\brealtimePrice\b.*EUR' "${SUBSCRIBER_LOG_FILE}"

num_errors=$(
grep 'Data.*\brealtimePrice\b.*EUR' "${SUBSCRIBER_LOG_FILE}" \
| awk '{print $2}' \
| jq -r '.data.brealtimePrice.errors | length'
)
[[ "$num_errors" == "0" ]] || exit 1

stop_subscriber
}
14 changes: 14 additions & 0 deletions bats/gql/price-sub.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
subscription {
price(
input: { amount: 100 amountCurrencyUnit: BTCSAT priceCurrencyUnit: USDCENT }
) {
errors {
message
}
price {
base
offset
currencyUnit
}
}
}
22 changes: 22 additions & 0 deletions bats/gql/real-time-price-sub.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
subscription realtimePrice($currency: DisplayCurrency!) {
realtimePrice(input: { currency: $currency }) {
errors {
message
}
realtimePrice {
id
timestamp
denominatorCurrency
btcSatPrice {
base
offset
currencyUnit
}
usdCentPrice {
base
offset
currencyUnit
}
}
}
}
14 changes: 1 addition & 13 deletions bats/helpers/_common.bash
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
export REPO_ROOT=$(git rev-parse --show-toplevel)

export BATS_ROOT_DIR="${REPO_ROOT}/bats"
CACHE_DIR=${BATS_TMPDIR:-tmp/bats}/galoy-bats-cache
mkdir -p "$CACHE_DIR"

OATHKEEPER_PROXY=${OATHKEEPER_PROXY:-localhost:4455}

SERVICES_PID_FILE=$REPO_ROOT/bats/.services_pid

start_services() {
stop_services > /dev/null 2>&1 || true
background buck2 run //dev:up -- "$@" > "${REPO_ROOT}/bats/.e2e-services.log"
echo $! > "$SERVICES_PID_FILE"
}

stop_services() {
[[ -f "$SERVICES_PID_FILE" ]] && kill -9 "$(cat "$SERVICES_PID_FILE")" > /dev/null || true
buck2 run //dev:down
}

if ! type fail &>/dev/null; then
fail() {
echo "$1"
Expand Down
3 changes: 2 additions & 1 deletion bats/helpers/setup-and-teardown.bash
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source 'bats/helpers/_common.bash'
CURRENT_FILE=${BASH_SOURCE:-bats/helpers/.}
source "$(dirname "$CURRENT_FILE")/_common.bash"

login_user() {
local token_name=$1
Expand Down
31 changes: 31 additions & 0 deletions bats/helpers/subscriber.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
CURRENT_FILE=${BASH_SOURCE:-bats/helpers/.}
source "$(dirname "$CURRENT_FILE")/_common.bash"

export SUBSCRIBER_PID_FILE="${BATS_ROOT_DIR}/.gql_subscriber_pid"
export SUBSCRIBER_LOG_FILE="${BATS_ROOT_DIR}/.e2e-subscriber.log"

subscribe_to() {
stop_subscriber > /dev/null 2>&1 || true
rm -f "$SUBSCRIBER_LOG_FILE" "$SUBSCRIBER_PID_FILE" || true

token_name=$1
if [[ -n "$token_name" && "$token_name" != 'anon' ]]; then
token="$(read_value "$token_name")"
fi
gql_filename=$2
variables=$3

background \
buck2 run //bats/helpers/subscriber:run -- \
"ws://${OATHKEEPER_PROXY}/graphqlws" \
"$(gql_file "$gql_filename")" \
"$token" \
"$variables" \
> "${SUBSCRIBER_LOG_FILE}"
echo $! > "$SUBSCRIBER_PID_FILE"
}

stop_subscriber() {
[[ -f "$SUBSCRIBER_PID_FILE" ]] && kill $(cat $SUBSCRIBER_PID_FILE) > /dev/null || true
}

9 changes: 9 additions & 0 deletions bats/helpers/subscriber/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load(
"@toolchains//workspace-pnpm:macros.bzl",
"dev_pnpm_task_binary",
)

dev_pnpm_task_binary(
name = "run",
command = "subscriber",
)
13 changes: 13 additions & 0 deletions bats/helpers/subscriber/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "subscriber",
"scripts": {
"subscriber": "ts-node src/gql-subscribe.ts"
},
"devDependencies": {
"@types/ws": "^8.5.8",
"@types/node": "^20.8.7",
"ws": "^8.14.2",
"graphql-ws": "^5.14.1",
"ts-node": "^10.9.1"
}
}
Loading

0 comments on commit 142e7f4

Please sign in to comment.