Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 8cd8316

Browse files
committed
Run the guessing game in CI
1 parent 634e9c2 commit 8cd8316

File tree

4 files changed

+116
-4
lines changed

4 files changed

+116
-4
lines changed

.github/workflows/test.yml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,25 @@ jobs:
1212
runs-on: ${{ matrix.os }}
1313
steps:
1414
- uses: actions/checkout@v2.3.4
15-
- uses: nixbuild/nix-quick-install-action@v5
16-
- run: nix-build release.nix --restrict-eval -I . --allowed-uris 'https://github.com/NixOS/nixpkgs https://github.com/input-output-hk https://github.com/NixOS/nixpkgs-channels' --option trusted-public-keys "hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= iohk.cachix.org-1:DpRUyj7h7V830dp/i6Nti+NEO2/nhblbov/8MW7Rqoo= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" --option substituters "https://hydra.iohk.io https://iohk.cachix.org https://cache.nixos.org/" -A required --no-out-link
15+
- uses: nixbuild/nix-quick-install-action@v9
16+
with:
17+
nix_conf: |
18+
allowed-uris = https://github.com/NixOS/nixpkgs https://github.com/input-output-hk https://github.com/NixOS/nixpkgs-channels
19+
trusted-public-keys = hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= iohk.cachix.org-1:DpRUyj7h7V830dp/i6Nti+NEO2/nhblbov/8MW7Rqoo= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
20+
substituters = https://hydra.iohk.io https://iohk.cachix.org https://cache.nixos.org/
21+
- run: nix-build release.nix --restrict-eval -I . -A required --no-out-link
22+
23+
guessing-game:
24+
strategy:
25+
matrix:
26+
os: [ubuntu-latest]
27+
runs-on: ${{ matrix.os }}
28+
steps:
29+
- uses: actions/checkout@v2.3.4
30+
- uses: nixbuild/nix-quick-install-action@v9
31+
with:
32+
nix_conf: |
33+
allowed-uris = https://github.com/NixOS/nixpkgs https://github.com/input-output-hk https://github.com/NixOS/nixpkgs-channels
34+
trusted-public-keys = hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ= iohk.cachix.org-1:DpRUyj7h7V830dp/i6Nti+NEO2/nhblbov/8MW7Rqoo= cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=
35+
substituters = https://hydra.iohk.io https://iohk.cachix.org https://cache.nixos.org/
36+
- run: ./scripts/test

release.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ let
4343
dimension "Haskell component" attrs select;
4444

4545
ciJobsets = stripAttrsForHydra (filterDerivations {
46-
shell = (import ./shell.nix);
46+
shell = (import ./shell.nix {});
47+
48+
pureShell = (import ./shell.nix { pure = true; });
4749

4850
build = pkgs.recurseIntoAttrs (mkHaskellDimension pkgs projectPackages);
4951
});

scripts/test

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#! /usr/bin/env nix-shell
2+
# -*-Shell-script-*-
3+
#! nix-shell -i bash ../shell.nix --pure --arg pure true
4+
5+
set -eEuo pipefail
6+
7+
cabal update
8+
9+
cabal build plutus-starter-pab
10+
11+
# We run plutus-starter-pab under a coproc to enable backgrounding when in an interactive shell
12+
coproc cabal exec -- plutus-starter-pab
13+
kill_pab() {
14+
echo >&${COPROC[1]}
15+
16+
wait
17+
}
18+
trap kill_pab EXIT
19+
20+
# Start a background loop to print out the output from plutus-starter-pab
21+
exec 3<&${COPROC[0]}
22+
(while IFS= read -u 3 -r line; do echo $line; done) &
23+
24+
wait_with_backoff() {
25+
cmd="$1"
26+
msg="$2"
27+
final_cmd="${3:-$cmd}"
28+
up=0
29+
for ((i=0;i<=6;i++))
30+
do
31+
if eval "$cmd"
32+
then
33+
up=1
34+
break
35+
fi
36+
let delay=2**i
37+
echo "$msg, sleeping for ${delay} seconds then trying again" >&2
38+
sleep "${delay}"
39+
done
40+
if [[ "$up" == "0" ]]
41+
then
42+
eval "$final_cmd"
43+
fi
44+
}
45+
46+
wait_with_backoff "curl --fail http://localhost:9080/api/contract/definitions &>/dev/null" "Connecting to PAB failed" "curl --fail http://localhost:9080/api/contract/definitions"
47+
48+
play() {
49+
secret="$1"
50+
guess="$2"
51+
52+
WALLET_ID=$(curl --silent --fail --data '' http://localhost:9080/wallet/create | jq -r '.wiWallet.getWalletId')
53+
wait_with_backoff '[[ $(curl --fail --silent http://localhost:9080/wallet/$WALLET_ID/total-funds | jq '\''.getValue|length'\'') != "0" ]]' "Wallet not populated"
54+
55+
CONTRACT_ID=$(curl --fail --silent \
56+
--header "Content-Type: application/json" \
57+
--data '{"caID": "GameContract", "caWallet":{"getWalletId": "'$WALLET_ID'"}}' \
58+
http://localhost:9080/api/contract/activate | jq -r '.unContractInstanceId')
59+
60+
log_count=0
61+
wait_for_contract() {
62+
wait_with_backoff 'curl --fail --silent http://localhost:9080/api/contract/instance/'$CONTRACT_ID'/status > status.json && [[ "$(jq -r '\''.cicCurrentState.logs | length'\'' < status.json)" -gt "'$log_count'" && ("$(jq -r .cicCurrentState.lastLogs[0]._logMessageContent < status.json)" == "Waiting for guess or lock endpoint..." || "$(jq -r .cicStatus < status.json)" == "Done") ]]' "Contract not ready"
63+
log_count="$(jq -r '.cicCUrrentState.logs | length' <status.json)"
64+
}
65+
66+
wait_for_contract
67+
68+
curl --fail --silent --header "Content-Type: application/json" \
69+
--data '{"amount":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},90]]]]},"secretWord":"'"$secret"'"}' \
70+
http://localhost:9080/api/contract/instance/$CONTRACT_ID/endpoint/lock
71+
wait_for_contract
72+
73+
curl --fail -s -H "Content-Type: application/json" \
74+
--request POST \
75+
--data '{"guessWord": "'"$guess"'"}' \
76+
http://localhost:9080/api/contract/instance/$CONTRACT_ID/endpoint/guess
77+
wait_for_contract
78+
79+
jq -r < status.json
80+
}
81+
82+
play "eagle" "duck"
83+
84+
play "lion" "lion"

shell.nix

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{ pure ? false }:
12
let
23
packages = import ./.;
34
inherit (packages) pkgs plutus-starter;
@@ -17,5 +18,10 @@ in
1718
pkgs.ghcid
1819
# HACK: This shouldn't need to be here.
1920
pkgs.lzma.dev
20-
];
21+
] ++ (pkgs.lib.optionals pure [
22+
pkgs.git
23+
pkgs.cacert
24+
pkgs.curl
25+
pkgs.jq
26+
]);
2127
}

0 commit comments

Comments
 (0)