From f105126cb618461c67b3848b0e7aae174b260cb3 Mon Sep 17 00:00:00 2001 From: Ricky Saechao <76449893+RickyLB@users.noreply.github.com> Date: Mon, 20 Nov 2023 12:59:53 -0800 Subject: [PATCH] chore: run local node in e2e tests (#318) --- .env.example | 6 ++++++ .github/workflows/swift-ci.yml | 15 +++++++++++++++ README.md | 29 +++++++++++++++++++++++++++++ Tests/HederaE2ETests/Config.swift | 30 +++++++++++++++++++++++++++--- 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 0a25c01e..7c6fd8a6 100644 --- a/.env.example +++ b/.env.example @@ -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 diff --git a/.github/workflows/swift-ci.yml b/.github/workflows/swift-ci.yml index 67584ce4..3ebcf925 100644 --- a/.github/workflows/swift-ci.yml +++ b/.github/workflows/swift-ci.yml @@ -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/hedera-local@2.13.0 start -d --network local + - name: Test run: swift test + + - name: Stop the local node + run: npx @hashgraph/hedera-local@2.13.0 stop diff --git a/README.md b/README.md index 68041e35..bca80a43 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/Tests/HederaE2ETests/Config.swift b/Tests/HederaE2ETests/Config.swift index 79bbdda0..aed22e15 100644 --- a/Tests/HederaE2ETests/Config.swift +++ b/Tests/HederaE2ETests/Config.swift @@ -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 { @@ -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) } }