Skip to content

Commit af2477e

Browse files
authored
added Sample Tests pipeline to test samples on Fabric v3 and FabricX (#46)
Signed-off-by: pco <pasquale.convertini@ibm.com> Signed-off-by: pasquale95 <pasqualeconvertini95@gmail.com>
1 parent 4bd9574 commit af2477e

File tree

10 files changed

+171
-19
lines changed

10 files changed

+171
-19
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#
2+
# Copyright IBM Corp. All Rights Reserved.
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
name: Sample Tests
8+
9+
on:
10+
push:
11+
branches: ["main"]
12+
pull_request:
13+
branches: ["main"]
14+
# Enable manual triggering
15+
workflow_dispatch:
16+
17+
jobs:
18+
test:
19+
name: Token Samples Tests
20+
runs-on: ubuntu-latest
21+
strategy:
22+
matrix:
23+
include:
24+
- platform: "fabricx"
25+
- platform: "fabric3"
26+
fail-fast: false
27+
steps:
28+
- name: Checkout code
29+
uses: actions/checkout@v4
30+
31+
- name: Setup Go
32+
uses: actions/setup-go@v5
33+
with:
34+
go-version-file: go.mod
35+
36+
- name: Verify Go installation
37+
run: |
38+
go version
39+
echo "GOPATH: $GOPATH"
40+
echo "GOROOT: $GOROOT"
41+
42+
- name: Set up Python
43+
if: ${{ matrix.platform == 'fabricx' }}
44+
uses: actions/setup-python@v4
45+
with:
46+
python-version: "3.x"
47+
48+
- name: Install Ansible
49+
if: ${{ matrix.platform == 'fabricx' }}
50+
run: |
51+
python -m pip install --upgrade pip
52+
pip install ansible ansible-core
53+
ansible --version
54+
55+
- name: Run samples tests on ${{ matrix.platform }}
56+
env:
57+
PLATFORM: ${{ matrix.platform }}
58+
run: |
59+
cd samples/tokens
60+
make install-prerequisites test

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
.vscode
55

66
# Ansible
7-
.ansible
7+
.ansible
8+
9+
# python
10+
.venv

samples/tokens/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ start: start-fabric start-app
3434
.PHONY: teardown
3535
teardown: teardown-app teardown-fabric
3636

37+
# Test Fabric and the token network.
38+
.PHONY: test
39+
test:
40+
@./scripts/test.sh
41+
3742
# Stop Fabric and the token network.
3843
.PHONY: stop
3944
stop: stop-app stop-fabric

samples/tokens/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ Use ansible scripts to deploy real distributed networks. For the sake of this sa
157157

158158
```shell
159159
make install-prerequisites
160-
python3 -m pip install -r ansible/requirements.txt
161160
```
162161

163162
### Setup Fabric-X
@@ -261,7 +260,7 @@ We begin with initializing the token namespace (commit the parameters for the ne
261260
```bash
262261
curl -X POST http://localhost:9300/endorser/init # Fabric-X only
263262

264-
curl http://localhost:9100/issuer/issue --json '{
263+
curl http://localhost:9100/issuer/issue -d '{
265264
"amount": {"code": "TOK","value": 1000},
266265
"counterparty": {"node": "owner1","account": "alice"},
267266
"message": "hello world!"
@@ -276,7 +275,7 @@ curl http://localhost:9600/owner/accounts/dan | jq
276275
Now `alice` transfers `100 TOK` to `dan`.
277276

278277
```bash
279-
curl http://localhost:9500/owner/accounts/alice/transfer --json '{
278+
curl http://localhost:9500/owner/accounts/alice/transfer -d '{
280279
"amount": {"code": "TOK","value": 100},
281280
"counterparty": {"node": "owner2","account": "dan"},
282281
"message": "hello dan!"

samples/tokens/ansible/requirements.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ collections:
1414
- name: containers.podman
1515
version: ">=1.16.2"
1616
- name: hyperledger.fabricx
17-
version: "0.5.3"
17+
version: "0.5.5"

samples/tokens/app.mk

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,28 @@ setup-app: build-app
1818
# Setup application
1919
.PHONY: build-app
2020
build-app:
21-
$(CONTAINER_CLI)-compose $(COMPOSE_ARGS) build
21+
$(CONTAINER_CLI) compose $(COMPOSE_ARGS) build
2222

2323
# Start application
2424
.PHONY: start-app
2525
start-app:
26-
$(CONTAINER_CLI)-compose $(COMPOSE_ARGS) up -d
26+
$(CONTAINER_CLI) compose $(COMPOSE_ARGS) up -d
2727

2828
# Restart application
2929
.PHONY: restart-app
3030
restart-app: build-app
31-
$(CONTAINER_CLI)-compose $(COMPOSE_ARGS) down
32-
$(CONTAINER_CLI)-compose $(COMPOSE_ARGS) up -d
31+
$(CONTAINER_CLI) compose $(COMPOSE_ARGS) down
32+
$(CONTAINER_CLI) compose $(COMPOSE_ARGS) up -d
3333

3434
# Stop application
3535
.PHONY: stop-app
3636
stop-app:
37-
PLATFORM=$(PLATFORM) $(CONTAINER_CLI)-compose $(COMPOSE_ARGS) stop
37+
PLATFORM=$(PLATFORM) $(CONTAINER_CLI) compose $(COMPOSE_ARGS) stop
3838

3939
# Teardown application
4040
.PHONY: teardown-app
4141
teardown-app:
42-
$(CONTAINER_CLI)-compose $(COMPOSE_ARGS) down
42+
$(CONTAINER_CLI) compose $(COMPOSE_ARGS) down
4343
rm -rf "$(CONF_ROOT)"/*/data
4444

4545
# Clean just the databases.

samples/tokens/fabric3.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ clean-fabric:
3535
rm -rf "$$d/keys/fabric" "$$d/data"; \
3636
done
3737

38-
3938
# Start the targeted hosts (e.g. make fabric-fabric start).
4039
.PHONY: start-fabric
4140
start-fabric:

samples/tokens/fabricx_ansible.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66

77
# Makefile vars
88
PROJECT_DIR := $(CURDIR)
9+
10+
# Ansible
911
ANSIBLE_PATH := $(PROJECT_DIR)/ansible
12+
VENV_DIR := ${ANSIBLE_PATH}/.venv
13+
ANSIBLE_PYTHON_INTERPRETER := ${VENV_DIR}/bin/python
1014
PLAYBOOK_PATH := $(ANSIBLE_PATH)/playbooks
1115
TARGET_HOSTS ?= all
1216
ANSIBLE_CONFIG ?= $(ANSIBLE_PATH)/ansible.cfg
17+
18+
# app
1319
CONF_ROOT=conf
1420

1521
# exported vars
@@ -25,6 +31,9 @@ CONTAINER_CLI ?= docker
2531
# Install the utilities needed to run the components on the targeted remote hosts (e.g. make install-prerequisites).
2632
.PHONY: install-prerequisites-fabric
2733
install-prerequisites-fabric:
34+
python3 -m venv $(VENV_DIR)
35+
$(VENV_DIR)/bin/python -m pip install --upgrade pip
36+
$(VENV_DIR)/bin/pip install -r $(ANSIBLE_PATH)/requirements.txt
2837
ansible-galaxy collection install -r $(ANSIBLE_PATH)/requirements.yml
2938
ansible-playbook "$(PLAYBOOK_PATH)/01-install-control-node-prerequisites.yaml"
3039
ansible-playbook hyperledger.fabricx.install_prerequisites --extra-vars '{"target_hosts": "$(TARGET_HOSTS)"}'

samples/tokens/scripts/gen_crypto.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@
88

99
set -e
1010
CONF_ROOT=$(realpath "${CONF_ROOT:-$(pwd)/conf}")
11+
FAB_BINS=$(realpath "${FAB_BINS:-$(pwd)/fabric-samples/bin}")
1112

12-
fabric-ca-client version || (echo "install fabric binaries and make sure they're in your \$PATH"; exit 1)
13+
"${FAB_BINS}/fabric-ca-client" version || (echo "install fabric binaries and make sure they're in your \$PATH"; exit 1)
1314

1415
# Start the fabric CA and enroll the admin user. We keep it running in the background until the script exits.
1516
setup_fabric_ca() {
1617
# start CA to issue idemix certificates for owners (idemix) and nodes (tls).
1718
export FABRIC_CA_HOME="${CONF_ROOT}/ca/keys/msp"
1819
mkdir -p "$FABRIC_CA_HOME"
19-
fabric-ca-server start -b "admin:adminpw" --idemix.curve gurvy.Bn254 & pid=$!
20+
"${FAB_BINS}/fabric-ca-server" start -b "admin:adminpw" --idemix.curve gurvy.Bn254 & pid=$!
2021
trap 'kill "$pid"' EXIT SIGINT SIGTERM SIGHUP SIGQUIT
21-
while ! fabric-ca-client getcainfo -u localhost:7054 2>/dev/null; do echo "waiting for CA to start..." && sleep 1; done
22+
while ! "${FAB_BINS}/fabric-ca-client" getcainfo -u localhost:7054 2>/dev/null; do echo "waiting for CA to start..." && sleep 1; done
2223

2324
# ca admin
24-
fabric-ca-client enroll -u "http://admin:adminpw@localhost:7054" -M "${CONF_ROOT}/ca/keys/ca-admin"
25+
"${FAB_BINS}/fabric-ca-client" enroll -u "http://admin:adminpw@localhost:7054" -M "${CONF_ROOT}/ca/keys/ca-admin"
2526
}
2627

2728
# generate an elliptic curve keypair for the issuer, and the parameters for the dlog verifications.
@@ -41,8 +42,8 @@ enroll_token_users() {
4142
node=$1; shift
4243
owners=("$@")
4344
for owner in "${owners[@]}"; do
44-
fabric-ca-client register -u http://localhost:7054 --id.name "${owner}" --id.secret password --id.type client --enrollment.type idemix --idemix.curve gurvy.Bn254
45-
fabric-ca-client enroll -u "http://${owner}:password@localhost:7054" -M "${CONF_ROOT}/${node}/keys/wallet/${owner}/msp" --enrollment.type idemix --idemix.curve gurvy.Bn254
45+
"${FAB_BINS}/fabric-ca-client" register -u http://localhost:7054 --id.name "${owner}" --id.secret password --id.type client --enrollment.type idemix --idemix.curve gurvy.Bn254
46+
"${FAB_BINS}/fabric-ca-client" enroll -u "http://${owner}:password@localhost:7054" -M "${CONF_ROOT}/${node}/keys/wallet/${owner}/msp" --enrollment.type idemix --idemix.curve gurvy.Bn254
4647
done
4748
}
4849

@@ -55,7 +56,7 @@ gen_node_crypto() {
5556
mkdir -p "$dir/node"
5657

5758
# we use the shared CA for mTLS certificates. The certificate chain is currently not verified so we could also use self-signed certificates.
58-
fabric-ca-client enroll -u "http://admin:adminpw@localhost:7054" -m "${node}.example.com" --enrollment.profile tls -M "${dir}/node"
59+
"${FAB_BINS}/fabric-ca-client" enroll -u "http://admin:adminpw@localhost:7054" -m "${node}.example.com" --enrollment.profile tls -M "${dir}/node"
5960
cp "${dir}"/node/keystore/* "${dir}/node.key"
6061
cp "${dir}/node/signcerts/cert.pem" "${dir}/node.crt"
6162

samples/tokens/scripts/test.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# Copyright IBM Corp. All Rights Reserved.
5+
#
6+
# SPDX-License-Identifier: Apache-2.0
7+
#
8+
9+
CONTAINER_CLI="${CONTAINER_CLI:-docker}"
10+
11+
## Print a section title
12+
function print_section_header() {
13+
echo "# ========================="
14+
echo "# $1"
15+
echo "# ========================="
16+
}
17+
18+
## Cleanup and stop network on abort
19+
function cleanup() {
20+
stop_network
21+
exit 1
22+
}
23+
24+
## Setup and start the network
25+
function run_network() {
26+
print_section_header "Setup and start the network..."
27+
make setup start
28+
}
29+
30+
## Stop and clean up the network
31+
function stop_network() {
32+
print_section_header "Stopping network..."
33+
make teardown clean
34+
}
35+
36+
## Initialize FabricX if needed
37+
function init_fabricx() {
38+
print_section_header "Initializing ${PLATFORM}..."
39+
curl -f -X POST http://localhost:9300/endorser/init
40+
}
41+
42+
## Run tests to verify the network
43+
function run_test() {
44+
# test application
45+
print_section_header "Run tests"
46+
47+
curl -f -X POST http://localhost:9100/issuer/issue -d '{
48+
"amount": {"code": "TOK","value": 1000},
49+
"counterparty": {"node": "owner1","account": "alice"},
50+
"message": "hello world!"
51+
}'
52+
curl -f -X GET http://localhost:9500/owner/accounts/alice | jq
53+
curl -f -X GET http://localhost:9600/owner/accounts/dan | jq
54+
curl -f -X POST http://localhost:9500/owner/accounts/alice/transfer -d '{
55+
"amount": {"code": "TOK","value": 100},
56+
"counterparty": {"node": "owner2","account": "dan"},
57+
"message": "hello dan!"
58+
}'
59+
curl -f -X GET http://localhost:9600/owner/accounts/dan/transactions | jq
60+
curl -f -X GET http://localhost:9500/owner/accounts/alice/transactions | jq
61+
}
62+
63+
# Script Start
64+
set +e
65+
set -o pipefail
66+
trap exit 1 INT
67+
68+
run_network
69+
# # currently we wait manually with a sleep.
70+
# # TODO: add an healthcheck within the `docker-compose`
71+
sleep 10
72+
if [[ "$PLATFORM" == "fabricx" ]]; then
73+
init_fabricx
74+
fi
75+
run_test
76+
stop_network

0 commit comments

Comments
 (0)