PolySwarmPolySwarmPolySwarmPolySwarm
Help

Testing in Linux

概覽

這些說明涵蓋如何測試以下類型的基於 Linux 的 PolySwarm 群參與者:

  • microengines
  • ambassadors
  • arbiters (coming soon)

此處列出的所有命令都假定:

  1. 您當前的工作目錄是您的參與者的根目錄,
  2. 您的參與者是在 PolySwarm 參與者模板 participant-template 上建立的。

建立參與者 Docker 映像

您需要建立參與者 Docker 映像,以進行單元測試和集成測試:

$ docker build -t ${PWD##*/} -f docker/Dockerfile .

這將生成一個標記有目錄名稱的 Docker 映像,例如 microengine-eicar


Unit Testing

participant-template 上建立的參與者將包含一組單元測試:

$ docker-compose -f docker/test-unit.yml up

Supplied unit tests are quite limited and only support microengines. Running unit tests on ambassadors and arbiters will fail unless you supply unit test functions. Unit testing logic is housed in the tests/ folder. Customize the script(s) in there to implement your unit tests.

確保 ERICAR 檢測微引擎真正對 ECAR 進行監測的示例單元測試輸出:

$ docker-compose -f docker/test-unit.yml up
Recreating docker_test_engine_eicar_1 ... done
Attaching to docker_test_engine_eicar_1
test_engine_eicar_1  | GLOB sdist-make: /usr/src/app/setup.py
test_engine_eicar_1  | py36 create: /usr/src/app/.tox/py36
test_engine_eicar_1  | py36 installdeps: -rrequirements.txt
test_engine_eicar_1  | py36 inst: /usr/src/app/.tox/dist/polyswarm_eicar-0.1.zip
test_engine_eicar_1  | py36 installed: aiodns==1.1.1,aiohttp==3.5.1,aioredis==1.2.0,aioresponses==0.6.0,aiorwlock==0.6.0,asn1crypto==0.24.0,async-timeout==3.0.1,asynctest==0.12.2,atomicwrites==1.3.0,attrdict==2.0.1,attrs==19.1.0,base58==0.2.5,certifi==2019.3.9,cffi==1.12.2,chardet==3.0.4,click==6.7,coincurve==11.0.0,coverage==4.5.1,cytoolz==0.9.0.1,eth-abi==1.3.0,eth-account==0.3.0,eth-hash==0.2.0,eth-keyfile==0.5.1,eth-keys==0.2.1,eth-rlp==0.1.2,eth-typing==2.1.0,eth-utils==1.4.1,ethereum==2.3.2,future==0.17.1,hexbytes==0.1.0,hiredis==1.0.0,hypothesis==3.82.1,idna==2.7,idna-ssl==1.1.0,lru-dict==1.1.6,malwarerepoclient==0.1,more-itertools==7.0.0,multidict==4.5.2,mypy-extensions==0.4.1,parsimonious==0.8.1,pbkdf2==1.3,pluggy==0.9.0,polyswarm-client==1.4.0,polyswarm-eicar==0.1,py==1.8.0,py-ecc==1.6.0,pycares==3.0.0,pycparser==2.19,pycryptodome==3.8.1,pyethash==0.1.27,pysha3==1.0.2,pytest==3.9.2,pytest-asyncio==0.9.0,pytest-cov==2.6.0,pytest-timeout==1.3.2,python-json-logger==0.1.9,python-magic==0.4.15,PyYAML==5.1,repoze.lru==0.7,requests==2.20.1,rlp==1.1.0,scrypt==0.8.13,six==1.12.0,toml==0.10.0,toolz==0.9.0,tox==3.4.0,typing-extensions==3.7.2,urllib3==1.24.1,virtualenv==16.4.3,web3==4.8.2,websockets==6.0,yara-python==3.7.0,yarl==1.3.0
test_engine_eicar_1  | py36 run-test-pre: PYTHONHASHSEED='1600969341'
test_engine_eicar_1  | py36 runtests: commands[0] | pytest -s
test_engine_eicar_1  | ============================= test session starts ==============================
test_engine_eicar_1  | platform linux -- Python 3.6.8, pytest-3.9.2, py-1.8.0, pluggy-0.9.0
test_engine_eicar_1  | hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/usr/src/app/.hypothesis/examples')
test_engine_eicar_1  | rootdir: /usr/src/app, inifile:
test_engine_eicar_1  | plugins: timeout-1.3.2, cov-2.6.0, asyncio-0.9.0, hypothesis-3.82.1
test_engine_eicar_1  | collected 1 item
test_engine_eicar_1  |
test_engine_eicar_1  | tests/test_eicar.py .
test_engine_eicar_1  |
test_engine_eicar_1  | =============================== warnings summary ===============================
test_engine_eicar_1  | /usr/src/app/.tox/py36/lib/python3.6/site-packages/eth_utils/applicators.py:32: DeprecationWarning: combine_argument_formatters(formatter1, formatter2)([item1, item2])has been deprecated and will be removed in a subsequent major version release of the eth-utils library. Update your calls to use apply_formatters_to_sequence([formatter1, formatter2], [item1, item2]) instead.
test_engine_eicar_1  |   "combine_argument_formatters(formatter1, formatter2)([item1, item2])"
test_engine_eicar_1  |
test_engine_eicar_1  | -- Docs: https://docs.pytest.org/en/latest/warnings.html
test_engine_eicar_1  | ===================== 1 passed, 1 warnings in 3.54 seconds =====================
test_engine_eicar_1  | ___________________________________ summary ____________________________________
test_engine_eicar_1  |   py36: commands succeeded
test_engine_eicar_1  |   congratulations :)
docker_test_engine_eicar_1 exited with code 0

Integration Testing

PolySwarm 市場由眾多參與者及技術所組成:以太坊、IPFS 節點、合約、微引擎、代表、仲裁者、工件等等。 Testing a single component often demands availability of all of the other components.

借助 orchestration 項目,可簡便、順暢地建立一個完整的測試網。 顧名思義,orchestration 協調所有必要的組件,以便在本地開發計算機上搭建和拆分整個 PolySwarm 市場環境。

克隆與參與者目錄相鄰的 orchestration

$ git clone https://github.com/polyswarm/orchestration

orchestration 存儲庫中的以下 docker-compose 文件將有可幫助測試:

  • base.yml:定義測試網的基礎結構基礎知識(例如 geth 節點和 relays
  • ambassador-eicar.yml:使用 eicar.py 定義代表服務。
  • arbiter-verbatim.yml:使用 verbatim.py 定義仲裁者服務。
  • microengine-eicar.yml:使用 eicar.py 定義微引擎服務。
  • microengine-clamav.yml:使用 clamav.py + ClamAV 守護進城定義微引擎服務。
  • microengine-multi.yml:使用 multi.py 定義微引擎服務。

(可選)預覽一個完整、可用的測試網

Always ensure that you have the latest updates to core PolySwarm Docker images prior to running integration tests. docker-compose ... up commands must always be proceeded with docker-compose ... pull commands.

Create the orchestration_default network, if you haven't already:

$ docker network create orchestration_default

在 cloned 的 orchestration 目錄中:

$ docker-compose -f base.yml -f ambassador-eicar.yml -f microengine-eicar.yml -f arbiter-verbatim.yml pull
$ docker-compose -f base.yml -f ambassador-eicar.yml -f microengine-eicar.yml -f arbiter-verbatim.yml up

If you'd like to test out a working "solution" to one of the microengine tutorials, replace, e.g. microengine-eicar.yml with microengine-clamav.yml or microengine-multi.yml.

你會看到來自以下服務的輸出:

  1. contracts:負責管理及將 PolySwarm Nectar (NCT) 和 BountyRegistry 合約部署到我們的開發中測試網。
  2. polyswarmd:實現方便地訪問 homechainsidechainipfs 所提供的服務的 PolySwarm 守護進程。 所有參與者(代表、仲裁者和微引擎)都與此服務通信。
  3. consul:一個密鑰值存儲,由承包商用於將關鍵信息傳遞到polyswarmd,例如部署的BountyRegistry 合約的以太坊地址。
  4. homechain: A geth node running our testnet's "homechain". See Chains: Home vs Side for an explanation of our split-chain design.
  5. sidechain: Another geth instance, this one running our testnet's "sidechain".
  6. ipfs: An IPFS node responsible for hosting all artifacts in our development testnet.
  7. postgrescontractor 的數據存儲
  8. deposit_*balancemanager 實例,代表對應的參與者,使用relay 節點,將NCT 從homechain 移動到sidechain
  9. relay[0-2]:負責管理和選擇性地釋放區塊鏈中的保管資金的節點(例如homechainsidechain) 。 這些節點提供了一個“relay”抽象 API,參與者可以使用它們方便地在整個鏈中“中轉”資金。
  10. ambassador:一個簡單代表,它會在 EICAR 文件 和非 EICAR 的文件上面放置懸賞。
  11. Microngine-eicar:一個簡單的微型引擎,會將 EICCAR 文件作為惡意文件,並將所有其他工件識別為良性。
  12. arbiter-verbatim:一個簡單的仲​​裁者,提供腳本化標定真實數據投票,包括準確的 ERICAR 響應。

Browse through the logs to get a sense for what each of these components is doing. Let it run for at least 5 minutes - it can take time to deploy contracts - and then the fun starts :)

When you've seen enough log output, press Ctrl-C to halt the development testnet gracefully.

測試您的參與者

Always ensure that you have the latest updates to core PolySwarm Docker images prior to running integration tests. docker-compose ... up commands must always be proceeded with docker-compose ... pull commands.

We'll use orchestration to start the entire testnet - with a few exceptions:

  1. 我們不會推出我們正在測試的參與者的競爭版本。 例如,如果我們正在開發一個定制仲裁者,我們不希望已有仲裁者為我們添麻煩。
  2. polyswarmd 做好準備好,我們準備好觀察網絡後,我們才會推出代表(定製或庫存)。

測試定制引擎

Step 0: Create the orchestration_default network, if you haven't already:

$ docker network create orchestration_default

Step 1: launch the base network + an arbiter (from the orchestration directory):

$ docker-compose -f base.yml -f arbiter-verbatim.yml pull
$ docker-compose -f base.yml -f arbiter-verbatim.yml up

Step 2: wait for polyswarmd to become available. polyswarmd 可能需要過幾分鐘才會完整運行。 During this time, you will see many messages like Problem with dial... dial tcp connection refused. and chain for config not available in consul yet. These errors are normal while the testnet is initializing, so have patience.

View polyswarmd logs in a new shell session (from the orchestration directory):

docker-compose -f base.yml logs --follow polyswarmd

In this new shell, wait for polyswarmd:

Attaching to orchestration_polyswarmd_1_c1d732ac5355
polyswarmd_1_c1d732ac5355 | [2019-05-13 20:52:36 +0000] [1] [INFO] Starting gunicorn 19.9.0
polyswarmd_1_c1d732ac5355 | [2019-05-13 20:52:36 +0000] [1] [INFO] Listening at: http://0.0.0.0:31337 (1)
...
polyswarmd_1_c1d732ac5355 | CRITICAL:polyswarmd.wsgi:polyswarmd is ready!
polyswarmd_1_c1d732ac5355 | CRITICAL:polyswarmd.wsgi:polyswarmd is ready!
polyswarmd_1_c1d732ac5355 | CRITICAL:polyswarmd.wsgi:polyswarmd is ready!
polyswarmd_1_c1d732ac5355 | CRITICAL:polyswarmd.wsgi:polyswarmd is ready!

When ready, polyswarmd will print polyswarmd is ready! once for every polyswarmd thread (4 by default).

Step 3: launch your custom microengine (from your microengine's root directory):

$ docker-compose -f docker/test-integration.yml up

Step 4: launch an ambassador that will place bounties on the testnet (in orchestration directory):

$ docker-compose -f ambassador-eicar.yml pull
$ docker-compose -f ambassador-eicar.yml up

Step 5: observe your microengine's output to ensure that your microengine is seeing and responding to the incoming bounties.

測試定制代表

Step 1: launch the base network + an arbiter + a microengine (from the orchestration directory):

$ docker-compose -f base.yml -f arbiter-verbatim.yml -f microengine-eicar.yml pull
$ docker-compose -f base.yml -f arbiter-verbatim.yml -f microengine-eicar.yml up

Step 2: wait for polyswarmd to become available. After several minutes, you should start seeing successful query messages (HTTP status 200) messages to polyswarmd:

INFO:polyswarmd:2018-12-06 05:42:08.396534 GET 200 /nonce 0x05328f171b8c1463eaFDACCA478D9EE6a1d923F8
INFO:geventwebsocket.handler:::ffff:172.19.0.12 - - [2018-12-06 05:42:08] "GET /nonce?account=0x05328f171b8c1463eaFDACCA478D9EE6a1d923F8&chain=home HTTP/1.1" 200 135 0.048543

Step 3: launch your custom ambassador (from your ambassador's root directory):

$ docker-compose -f docker/test-integration.yml up

Step 4: observe your ambassador's terminal output to ensure it's placing bounties correctly.

測試定制仲裁者

Arbiter testing instructions coming soon!