Skip to content

Commit f105126

Browse files
authored
chore: run local node in e2e tests (#318)
1 parent 1e908ce commit f105126

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ OPERATOR_ACCOUNT_ID=
33

44
# Default private key to use to sign for all transactions and queries
55
OPERATOR_KEY=
6+
7+
# Network names: `"localhost"`, `"testnet"`, `"previewnet"`, `"mainnet"`
8+
TEST_NETWORK_NAME=
9+
10+
# Enables non-free transactions when testing
11+
TEST_RUN_NONFREE=1

.github/workflows/swift-ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,20 @@ jobs:
5959
restore-keys: |
6060
${{ runner.os }}-${{ matrix.swift }}-spm-
6161
62+
- name: "Create env file"
63+
run: |
64+
touch .env
65+
echo TEST_OPERATOR_KEY="302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137" >> .env
66+
echo TEST_OPERATOR_ID="0.0.2" >> .env
67+
echo TEST_HEDERA_NETWORK="localhost" >> .env
68+
echo TEST_RUN_NONFREE="1" >> .env
69+
cat .env
70+
71+
- name: Start the local node
72+
run: npx @hashgraph/hedera-local@2.13.0 start -d --network local
73+
6274
- name: Test
6375
run: swift test
76+
77+
- name: Stop the local node
78+
run: npx @hashgraph/hedera-local@2.13.0 stop

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,35 @@ protoc --swift_opt=Visibility=Public --swift_opt=FileNaming=PathToUnderscores --
9292
protoc --grpc-swift_opt=Visibility=Public,FileNaming=PathToUnderscores,Server=false --grpc-swift_out=./Sources/HederaProtobufs/Mirror -I=protobufs/mirror -I=protobufs/services protobufs/mirror/**.proto
9393
```
9494

95+
### Integration Tests
96+
Before running the integration tests, an operator key, operator account id, and a network name must be set in an `.env` file.
97+
```bash
98+
# Account that will pay query and transaction fees
99+
TEST_OPERATOR_ACCOUNT_ID=
100+
# Default private key to use to sign for all transactions and queries
101+
TEST_OPERATOR_KEY=
102+
# Network names: `"localhost"`, `"testnet"`, `"previewnet"`, `"mainnet"`
103+
TEST_NETWORK_NAME=
104+
```
105+
```bash
106+
# Run tests
107+
$ swift test
108+
```
109+
110+
#### Local Environment Testing
111+
Hedera offers a way to run tests through your localhost using the `hedera-local-node` service.
112+
113+
For instructions on how to set up and run local node, follow the steps in the git repository:
114+
https://github.com/hashgraph/hedera-local-node
115+
116+
Once the local node is running in Docker, the appropriate `.env` values must be set:
117+
```bash
118+
TEST_OPERATOR_ACCOUNT_ID=0.0.2
119+
TEST_OPERATOR_KEY=3030020100300706052b8104000a042204205bc004059ffa2943965d306f2c44d266255318b3775bacfec42a77ca83e998f2
120+
TEST_NETWORK_NAME=localhost
121+
```
122+
Lastly, run the tests using `swift test`
123+
95124
### Generate SDK
96125
```bash
97126
# cwd: `$REPO/sdk/swift`

Tests/HederaE2ETests/Config.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ internal actor Ratelimit {
8787
}
8888

8989
internal struct TestEnvironment {
90+
private let defaultLocalNodeAddress: String = "127.0.0.1:50211"
91+
private let defaultLocalMirrorNodeAddress: String = "127.0.0.1:5600"
9092
internal struct Config {
9193
private static func envBool(env: Environment?, key: String, defaultValue: Bool) -> Bool {
9294
guard let value = env?[key]?.stringValue else {
@@ -178,11 +180,33 @@ internal struct TestEnvironment {
178180
config = .init()
179181
ratelimits = .init()
180182

181-
// todo: warn when error
182-
client = (try? Client.forName(config.network)) ?? Client.forTestnet()
183+
do {
184+
switch config.network {
185+
case "mainnet":
186+
self.client = Client.forMainnet()
187+
case "testnet":
188+
self.client = .forTestnet()
189+
case "previewnet":
190+
self.client = Client.forPreviewnet()
191+
case "localhost":
192+
var network: [String: AccountId] = [String: AccountId]()
193+
194+
network[defaultLocalNodeAddress] = AccountId(num: 3)
195+
196+
let client = try Client.forNetwork(network)
197+
198+
self.client = client.setMirrorNetwork([defaultLocalMirrorNodeAddress])
199+
default:
200+
self.client = Client.forTestnet()
201+
}
202+
} catch {
203+
print("Error creating client: \(config.network); creating one using testnet")
204+
205+
self.client = Client.forTestnet()
206+
}
183207

184208
if let op = config.operator {
185-
client.setOperator(op.accountId, op.privateKey)
209+
self.client.setOperator(op.accountId, op.privateKey)
186210
}
187211
}
188212

0 commit comments

Comments
 (0)