Skip to content

Commit

Permalink
chore: run local node in e2e tests (#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
RickyLB authored Nov 20, 2023
1 parent 1e908ce commit f105126
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ OPERATOR_ACCOUNT_ID=

# Default private key to use to sign for all transactions and queries
OPERATOR_KEY=

# Network names: `"localhost"`, `"testnet"`, `"previewnet"`, `"mainnet"`
TEST_NETWORK_NAME=

# Enables non-free transactions when testing
TEST_RUN_NONFREE=1
15 changes: 15 additions & 0 deletions .github/workflows/swift-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,20 @@ jobs:
restore-keys: |
${{ runner.os }}-${{ matrix.swift }}-spm-
- name: "Create env file"
run: |
touch .env
echo TEST_OPERATOR_KEY="302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137" >> .env
echo TEST_OPERATOR_ID="0.0.2" >> .env
echo TEST_HEDERA_NETWORK="localhost" >> .env
echo TEST_RUN_NONFREE="1" >> .env
cat .env
- name: Start the local node
run: npx @hashgraph/[email protected] start -d --network local

- name: Test
run: swift test

- name: Stop the local node
run: npx @hashgraph/[email protected] stop
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,35 @@ protoc --swift_opt=Visibility=Public --swift_opt=FileNaming=PathToUnderscores --
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
```

### Integration Tests
Before running the integration tests, an operator key, operator account id, and a network name must be set in an `.env` file.
```bash
# Account that will pay query and transaction fees
TEST_OPERATOR_ACCOUNT_ID=
# Default private key to use to sign for all transactions and queries
TEST_OPERATOR_KEY=
# Network names: `"localhost"`, `"testnet"`, `"previewnet"`, `"mainnet"`
TEST_NETWORK_NAME=
```
```bash
# Run tests
$ swift test
```

#### Local Environment Testing
Hedera offers a way to run tests through your localhost using the `hedera-local-node` service.

For instructions on how to set up and run local node, follow the steps in the git repository:
https://github.com/hashgraph/hedera-local-node

Once the local node is running in Docker, the appropriate `.env` values must be set:
```bash
TEST_OPERATOR_ACCOUNT_ID=0.0.2
TEST_OPERATOR_KEY=3030020100300706052b8104000a042204205bc004059ffa2943965d306f2c44d266255318b3775bacfec42a77ca83e998f2
TEST_NETWORK_NAME=localhost
```
Lastly, run the tests using `swift test`

### Generate SDK
```bash
# cwd: `$REPO/sdk/swift`
Expand Down
30 changes: 27 additions & 3 deletions Tests/HederaE2ETests/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ internal actor Ratelimit {
}

internal struct TestEnvironment {
private let defaultLocalNodeAddress: String = "127.0.0.1:50211"
private let defaultLocalMirrorNodeAddress: String = "127.0.0.1:5600"
internal struct Config {
private static func envBool(env: Environment?, key: String, defaultValue: Bool) -> Bool {
guard let value = env?[key]?.stringValue else {
Expand Down Expand Up @@ -178,11 +180,33 @@ internal struct TestEnvironment {
config = .init()
ratelimits = .init()

// todo: warn when error
client = (try? Client.forName(config.network)) ?? Client.forTestnet()
do {
switch config.network {
case "mainnet":
self.client = Client.forMainnet()
case "testnet":
self.client = .forTestnet()
case "previewnet":
self.client = Client.forPreviewnet()
case "localhost":
var network: [String: AccountId] = [String: AccountId]()

network[defaultLocalNodeAddress] = AccountId(num: 3)

let client = try Client.forNetwork(network)

self.client = client.setMirrorNetwork([defaultLocalMirrorNodeAddress])
default:
self.client = Client.forTestnet()
}
} catch {
print("Error creating client: \(config.network); creating one using testnet")

self.client = Client.forTestnet()
}

if let op = config.operator {
client.setOperator(op.accountId, op.privateKey)
self.client.setOperator(op.accountId, op.privateKey)
}
}

Expand Down

0 comments on commit f105126

Please sign in to comment.