Skip to content

Commit c1aca26

Browse files
h4x3rotabclaude
andcommitted
refactor: modularize Anvil testing with separate setup and run scripts
- Create modular test scripts for better flexibility - Add setup-local-chain.sh to start Anvil and deploy contracts - Add run-tests.sh to run tests against existing chain - Add test-all.sh for complete test runs - Add cleanup.sh to stop all test processes - Update .gitignore to exclude test logs and env files - Remove old monolithic test-with-anvil.sh script - Add comprehensive documentation in scripts/README.md This allows developers to: - Set up chain once and run tests multiple times - Run different test suites independently - Better integrate with CI/CD pipelines - Debug issues more easily with separate logs 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 846870e commit c1aca26

File tree

8 files changed

+604
-161
lines changed

8 files changed

+604
-161
lines changed

kms/auth-eth/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@
22
/cache
33
/dist
44
/out
5+
6+
# Test logs
7+
anvil-test.log
8+
anvil.log
9+
deploy.log
10+
server-test.log
11+
.env.test

kms/auth-eth/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212
"test:coverage": "jest --coverage",
1313
"test:foundry": "forge test --ffi",
1414
"test:foundry:all": "forge clean && forge build && forge test --ffi",
15-
"test:full": "./scripts/test-with-anvil.sh",
16-
"test:full:keep": "./scripts/test-with-anvil.sh --keep-running"
15+
"test:setup": "./scripts/setup-local-chain.sh",
16+
"test:run": "./scripts/run-tests.sh",
17+
"test:run:foundry": "./scripts/run-tests.sh --with-foundry",
18+
"test:all": "./scripts/test-all.sh",
19+
"test:all:foundry": "./scripts/test-all.sh --with-foundry",
20+
"test:cleanup": "./scripts/cleanup.sh"
1721
},
1822
"dependencies": {
1923
"@fastify/swagger": "^8.12.0",

kms/auth-eth/scripts/README.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Test Scripts
2+
3+
This directory contains automated test scripts for the DStack KMS Ethereum backend.
4+
5+
## Scripts Overview
6+
7+
### 🚀 setup-local-chain.sh
8+
Sets up a local Anvil blockchain and deploys the DStack contracts.
9+
- Starts Anvil on port 8545
10+
- Deploys DstackKms and DstackApp contracts
11+
- Saves configuration to `.env.test`
12+
13+
```bash
14+
npm run test:setup
15+
# or
16+
./scripts/setup-local-chain.sh
17+
```
18+
19+
### 🧪 run-tests.sh
20+
Runs all tests against the deployed contracts on the local chain.
21+
- Requires local chain to be already set up
22+
- Runs Jest unit tests
23+
- Runs integration tests
24+
- Optionally runs Foundry tests
25+
26+
```bash
27+
npm run test:run # Run JS/TS tests only
28+
npm run test:run:foundry # Include Foundry tests
29+
# or
30+
./scripts/run-tests.sh
31+
./scripts/run-tests.sh --with-foundry
32+
```
33+
34+
### 🎯 test-all.sh
35+
Complete test suite - sets up chain and runs all tests.
36+
- Combines setup-local-chain.sh and run-tests.sh
37+
- Perfect for CI/CD or fresh test runs
38+
39+
```bash
40+
npm run test:all # Complete test suite
41+
npm run test:all:foundry # Include Foundry tests
42+
# or
43+
./scripts/test-all.sh
44+
./scripts/test-all.sh --with-foundry
45+
```
46+
47+
### 🧹 cleanup.sh
48+
Cleans up all test processes and temporary files.
49+
- Stops Anvil and API server
50+
- Removes temporary files and logs
51+
52+
```bash
53+
npm run test:cleanup
54+
# or
55+
./scripts/cleanup.sh
56+
```
57+
58+
## Typical Workflows
59+
60+
### One-time Setup, Multiple Test Runs
61+
```bash
62+
# Set up once
63+
npm run test:setup
64+
65+
# Run tests multiple times
66+
npm run test:run
67+
npm run test:run
68+
npm run test:run
69+
70+
# Clean up when done
71+
npm run test:cleanup
72+
```
73+
74+
### Complete Test Run
75+
```bash
76+
# Run everything in one go
77+
npm run test:all
78+
79+
# Or with Foundry tests
80+
npm run test:all:foundry
81+
```
82+
83+
### Development Workflow
84+
```bash
85+
# Set up chain
86+
npm run test:setup
87+
88+
# Keep chain running, develop and test
89+
npm run test:run
90+
# ... make changes ...
91+
npm run test:run
92+
# ... make more changes ...
93+
npm run test:run
94+
95+
# Clean up when done
96+
npm run test:cleanup
97+
```
98+
99+
## Environment Variables
100+
101+
After running `setup-local-chain.sh`, the following environment variables are saved to `.env.test`:
102+
103+
- `ANVIL_PID` - Process ID of the Anvil instance
104+
- `ETH_RPC_URL` - RPC endpoint (http://127.0.0.1:8545)
105+
- `CHAIN_ID` - Chain ID (31337)
106+
- `KMS_CONTRACT_ADDR` - Deployed KMS proxy contract address
107+
- `APP_IMPLEMENTATION` - DstackApp implementation address
108+
- `KMS_IMPLEMENTATION` - DstackKms implementation address
109+
- `DEPLOYER_ADDRESS` - Address that deployed the contracts
110+
- `DEPLOYER_PRIVATE_KEY` - Private key of the deployer
111+
112+
## Logs
113+
114+
The scripts generate the following log files:
115+
116+
- `anvil.log` - Anvil blockchain logs
117+
- `deploy.log` - Contract deployment logs
118+
- `server-test.log` - API server logs during tests
119+
120+
## Requirements
121+
122+
- Node.js and npm
123+
- Foundry (forge, anvil)
124+
- All npm dependencies installed (`npm install`)
125+

kms/auth-eth/scripts/cleanup.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
# Cleanup script to stop all test processes
3+
4+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
5+
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
6+
ENV_FILE="$PROJECT_ROOT/.env.test"
7+
8+
# Colors for output
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
YELLOW='\033[1;33m'
12+
NC='\033[0m'
13+
14+
echo "🧹 Cleaning up test environment..."
15+
16+
# Kill any running API servers
17+
echo "Stopping API servers..."
18+
pkill -f "ts-node src/main.ts" || true
19+
pkill -f "node dist/main.js" || true
20+
21+
# Load environment if exists
22+
if [ -f "$ENV_FILE" ]; then
23+
source "$ENV_FILE"
24+
25+
# Kill Anvil if PID is set
26+
if [ ! -z "$ANVIL_PID" ]; then
27+
echo "Stopping Anvil (PID: $ANVIL_PID)..."
28+
kill $ANVIL_PID 2>/dev/null || true
29+
fi
30+
fi
31+
32+
# Kill any other Anvil processes
33+
echo "Stopping any other Anvil processes..."
34+
pkill -f "anvil" || true
35+
36+
# Clean up files
37+
echo "Removing temporary files..."
38+
rm -f "$PROJECT_ROOT/.env.test"
39+
rm -f "$PROJECT_ROOT/anvil.log"
40+
rm -f "$PROJECT_ROOT/deploy.log"
41+
rm -f "$PROJECT_ROOT/server-test.log"
42+
rm -f "$PROJECT_ROOT/integration-test.js"
43+
44+
echo -e "${GREEN}✅ Cleanup complete!${NC}"

0 commit comments

Comments
 (0)