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

集成测试

PolySwarm 市场由众多参与者及技术所组成:以太坊、IPFS 节点、合约、微引擎、代表、仲裁者、工件等等。 测试一个单独的组件通常需要能够获得所有其他组件。

借助 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:运行着我们的测试网的 “Homechain” 的 geth 节点。 See Chains: Home vs Side for an explanation of our split-chain design.
  5. sidechain :运行着我们测试网的“侧链”的另一个 geth 实例。
  6. ipfs:一个 IPFS 节点,负责在我们的开发测试网中托管所有工件。
  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. 我们让它运行至少5分钟,因为部署合约可能会需要点时间,这时就开始变得有趣了 :)

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. It will take several minutes for polyswarmd to become available. 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!