Skip to content

Commit

Permalink
Move basic.py to where ci-cd will pick it up from (#610)
Browse files Browse the repository at this point in the history
* Move basic.py to where ci-cd will pick it up from

* Update README

* Update test to pass in CI
  • Loading branch information
pouneh authored Dec 2, 2022
1 parent 851108c commit 590892e
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 151 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/dev-cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ jobs:
rancher_url: ${{ secrets.DEV_RANCHER_URL }}
rancher_token: ${{ secrets.DEV_RANCHER_TOKEN }}

# TODO: Add integration tests.

# This will need to run on our self-hosted so it can connect to the privately deployed full-service.
test:
runs-on: [self-hosted, Linux, small]
Expand Down Expand Up @@ -213,7 +211,7 @@ jobs:
"${POETRY_HOME}/bin/poetry" install --without=dev
# Run test
"${POETRY_HOME}/bin/poetry" run python3 ./basic.py
"${POETRY_HOME}/bin/poetry" run python3 ./basic.py "${CONFIG_JSON}"
popd || exit 0
# remove the testing environment after all tests are run successfully when this
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ __pycache__

*sgx_linux_x64_sdk_2.9.101.2.bin

integ-tests/entropy_config
.internal-ci/test/fs-integration/test_config.json

# mob prompt and build tmp
.mob/
Expand Down
26 changes: 25 additions & 1 deletion .internal-ci/test/fs-integration/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# fs-integration

These scrips use Python Poetry Env/Package manager.
These scrips use Python Poetry Env/Package manager

## To run locally

1. Setup poetry (see sections below for more details)

```
curl -sSL https://install.python-poetry.org | python3 -
poetry install
```

2. Add mnemonics to `test_config.json` (or create another configuration file and pass as another parameter when running the test)
- Don't worry, `.gitignore` will prevent you from accidentally checking in your secrets

3. Run the test

```
poetry run python3 basic.py
```

## Install Dependencies

Expand Down Expand Up @@ -47,6 +65,12 @@ Remove the default directory project directory in the `pyproject.toml`, we're no
{include = "fs_integration"}
```

## Use existing poetry configuration
To read and install dependencies from current project's `pyproject.toml`:
```
poetry install
```

## VSCode hints.

**Activate Poetry**
Expand Down
171 changes: 165 additions & 6 deletions .internal-ci/test/fs-integration/basic.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,176 @@

import sys
import argparse
import asyncio
import json
import subprocess
import sys

# Path to "python-library" modules.
repo_root_dir = subprocess.check_output("git rev-parse --show-toplevel", shell=True).decode("utf8").strip()
sys.path.append("{}/python-library".format(repo_root_dir))

from fullservice import FullServiceAPIv2 as v2
from dataobjects import Response, Account # TODO rename as FSDataObjects
from FSDataObjects import Response, Account

sleepy_time = 15
default_config_path = "./test_config.json"
config = []
account_ids = []

fs = v2()

# TODO: this is very uggly and needs to be refactored
async def wait_for_account_to_sync(id):
account_status = await fs.get_account_status(id)
while ((account_status.get("result").get("account").get("next_block_index")
!= account_status.get("result").get("local_block_height")) or (account_status.get("result").get("balance_per_token").get("0").get("pending") != "0")):
await asyncio.sleep(sleepy_time)
account_status = await fs.get_account_status(id)


async def test_cleanup():
global account_ids
for id in account_ids:
await wait_for_account_to_sync(id)
await fs.remove_account(id)
accounts = await fs.get_accounts()
for id in account_ids:
assert id not in accounts.get('result').get('account_ids'),"Failed to clear out accounts"
account_ids = []


# If this test fails before reaching the last cleanup step, we have leftover
# artifacts in the FS instance. We clean up those residual artifacts here.
# Note: Using a testing framework like pytest would allow us to bundle this in
# a test fixture
async def preclean_this_test():
await get_account(0, "alice", True)
await get_account(1, "bob", True)
await test_cleanup()


def get_mnemonics(n=2):
if n > len(config["Account Mnemonics"]):
raise ValueError("Not enough account available in config")
return config["Account Mnemonics"][:n]


async def get_account(i, name="", okay_if_already_imported=False):
global account_ids

mnemonic = config["Account Mnemonics"][i]["mnemonic"]
account = await fs.import_account(
mnemonic,
"2", # This parameter indicates that we are using the 2nd key derivations method (mnemonics)
name=name
)

if not okay_if_already_imported:
assert "error" not in account.keys(), "Failed to import account"

# Newly imported
if "error" not in account.keys():
result = Account(account["result"]["account"])
account_ids.append(result.id)
# Previously imported
else:
error_msg = account.get("error").get("data").get("details")
assert error_msg.startswith("Error interacting& with the database: Account already exists:"), "Unknown import failure"
id = error_msg.split()[-1]
result = Response(await fs.get_account_status(id)).account
account_ids.append(result.id)
return result


async def wait_for_network_sync():
network_status = await fs.get_network_status()
while network_status.get('result').get('network_block_height') != network_status.get('result').get('local_block_height'):
print("Sleep")
await asyncio.sleep(sleepy_time)
network_status = await fs.get_network_status()


async def main():
print("Placeholder script")
while (await fs.get_wallet_status())['result']['wallet_status']['is_synced_all'] != True:
await asyncio.sleep(sleepy_time)
await does_it_go()


async def does_it_go(amount_pmob: int = 600000000) -> bool:
network_status = await fs.get_network_status()
assert "error" not in network_status.keys(), "Failed to get network status"
fee = int(network_status.get("result")
.get("network_status")
.get("fees")
.get("0") # zero is the fee key for mob
)

"""Test Setup """
pmob_to_send = amount_pmob

# await preclean_this_test()

alice = await get_account(0, "alice", True)
bob = await get_account(1, "bob", True)

await wait_for_account_to_sync(alice.id)
await wait_for_account_to_sync(bob.id)

alice_balance_0 = int(
(await fs.get_account_status(alice.id))
.get("result")
.get("balance_per_token")
.get("0")
.get("unspent")
)

assert alice_balance_0 >= pmob_to_send + fee, "Insufficient funds in first account."

bob_balance_0 = int(
(await fs.get_account_status(bob.id))
.get("result")
.get("balance_per_token")
.get("0")
.get("unspent")
)

first_transaction = await fs.build_and_submit_transaction(
alice.id,
recipient_public_address=bob.main_address,
amount={"value": str(pmob_to_send), "token_id": str(0)},
)

# TODO: replace this with a poll loop that waits a block or two
await wait_for_network_sync()
await wait_for_account_to_sync(alice.id)
await wait_for_account_to_sync(bob.id)

""" Check Results """
alice_balance_1 = int(
(await fs.get_account_status(alice.id))
.get("result")
.get("balance_per_token")
.get("0")
.get("unspent")
)

bob_balance_1 = int(
(await fs.get_account_status(bob.id))
.get("result")
.get("balance_per_token")
.get("0")
.get("unspent")
)

assert alice_balance_0 == alice_balance_1 + fee + pmob_to_send, "Alice doesn't end with the expected amount"
assert bob_balance_1 == bob_balance_0 + pmob_to_send, "Bob doesn't end with the expected amount"

# await test_cleanup()

if __name__ == "__main__":
main()
parser = argparse.ArgumentParser(description="Basic test")
parser.add_argument("config_path", nargs='?', type=str, default=default_config_path)
args = parser.parse_args()

with open(args.config_path) as json_file:
config = json.load(json_file)

asyncio.run(main())
10 changes: 10 additions & 0 deletions .internal-ci/test/fs-integration/test_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Account Mnemonics" : [
{
"mnemonic":""
},
{
"mnemonic":""
}
]
}
134 changes: 0 additions & 134 deletions integ-tests/basic.py

This file was deleted.

Loading

0 comments on commit 590892e

Please sign in to comment.