Skip to content

Commit 85f9680

Browse files
committed
Add tests
1 parent 194807e commit 85f9680

11 files changed

+143
-3
lines changed

.circleci/config.yml

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
defaults: &defaults
2+
docker:
3+
- image: circleci/python:2.7.15
4+
working_directory: ~/bitnodes
5+
6+
version: 2
7+
jobs:
8+
build:
9+
<<: *defaults
10+
steps:
11+
- checkout
12+
- restore_cache:
13+
keys:
14+
- dependencies-{{ checksum "requirements.txt" }}
15+
- run:
16+
name: install dependencies
17+
command: |
18+
pip install virtualenv
19+
virtualenv -p python2 venv
20+
. venv/bin/activate
21+
pip install -r requirements.txt
22+
- save_cache:
23+
paths:
24+
- ./venv
25+
key: dependencies-{{ checksum "requirements.txt" }}
26+
27+
test:
28+
<<: *defaults
29+
steps:
30+
- checkout
31+
- restore_cache:
32+
keys:
33+
- dependencies-{{ checksum "requirements.txt" }}
34+
- run:
35+
name: run tests
36+
command: |
37+
. venv/bin/activate
38+
pytest --junitxml=test-reports/pytest/junit.xml
39+
- store_test_results:
40+
path: test-reports
41+
- store_artifacts:
42+
path: test-reports
43+
44+
workflows:
45+
version: 2
46+
build_and_test:
47+
jobs:
48+
- build
49+
- test:
50+
requires:
51+
- build
52+
filters:
53+
branches:
54+
only: master

.flake8

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[flake8]
2+
ignore = E402
3+
exclude =
4+
.git
5+
conf
6+
data
7+
geoip
8+
internal-tests
9+
log
10+
venv

.gitignore

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
*.pyc
55
.DS_Store
66
/.idea/
7+
/.pytest_cache/
78
/data/
8-
/tests/
9-
clean.sh
10-
geoip/*.dat
9+
/internal-tests/
10+
/test-reports/
1111
geoip/*.mmdb
1212
geoip/*.txt
1313
rsync.sh

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ Bitnodes is currently being developed to estimate the size of the Bitcoin networ
1515
* [Leaderboard](https://bitnodes.earn.com/nodes/leaderboard/)
1616

1717
See [Provisioning Bitcoin Network Crawler](https://github.com/ayeowch/bitnodes/wiki/Provisioning-Bitcoin-Network-Crawler) for steps on setting up a machine to run Bitnodes. The [Redis Data](https://github.com/ayeowch/bitnodes/wiki/Redis-Data) contains the list of keys and their associated values that are written by the scripts in this project. If you wish to access the data, e.g. network snapshots, collected using this project, see [Bitnodes API v1.0](https://bitnodes.earn.com/api/).
18+
19+
[![CircleCI](https://circleci.com/gh/ayeowch/bitnodes.svg?style=svg)](https://circleci.com/gh/ayeowch/bitnodes)

requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
dpkt==1.9.1
2+
flake8==3.5.0
23
geoip2==2.9.0
34
gevent==1.3.4
45
ipaddress==1.0.22
56
PySocks==1.6.8
7+
pytest==3.7.3
68
redis==2.10.6
79
requests==2.19.1

tests/data/bip144.pcap

379 Bytes
Binary file not shown.

tests/pcap.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../pcap.py

tests/protocol.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../protocol.py

tests/test_bip144.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
import os
4+
from binascii import hexlify, unhexlify
5+
from collections import defaultdict
6+
from Queue import PriorityQueue
7+
8+
from pcap import Stream, Cache
9+
from protocol import Serializer
10+
11+
12+
class Reader(Cache):
13+
def __init__(self, filepath):
14+
self.filepath = filepath
15+
self.serializer = Serializer(magic_number=unhexlify('f9beb4d9'))
16+
self.streams = defaultdict(PriorityQueue)
17+
self.stream = Stream()
18+
19+
20+
def test_bip144():
21+
filepath = os.path.join(
22+
os.path.dirname(os.path.realpath(__file__)), 'data', 'bip144.pcap')
23+
24+
reader = Reader(filepath=filepath)
25+
reader.extract_streams()
26+
msgs = []
27+
28+
for stream_id, reader.stream.segments in reader.streams.iteritems():
29+
assert stream_id == ('1.1.1.1', 56691, '2.2.2.2', 8333)
30+
msg, _ = reader.serializer.deserialize_msg(reader.stream.data().next())
31+
msgs.append(msg)
32+
33+
assert len(msgs) == 1
34+
35+
keys = sorted(msgs[0].keys())
36+
assert keys == [
37+
'checksum',
38+
'command',
39+
'length',
40+
'lock_time',
41+
'magic_number',
42+
'tx_hash',
43+
'tx_in',
44+
'tx_in_count',
45+
'tx_out',
46+
'tx_out_count',
47+
'version',
48+
]
49+
50+
sig = hexlify(msgs[0]['tx_in'][0]['wits'][0]).decode()
51+
pub = hexlify(msgs[0]['tx_in'][0]['wits'][1]).decode()
52+
53+
assert (
54+
sig ==
55+
'30440220643f9527d1d226bb25321e85fa4c2e13afcc2ccfb14bc069642ec51efd5c7'
56+
'b430220752a1d43350d5014559eb8d57fb5d48f1e759d120fc5e0dd6f26769b1d7951'
57+
'c701')
58+
59+
assert (
60+
pub ==
61+
'03bf91e659ba26545fe7b04b48ca3b77c624993caed10e6fdb177fba33b25230d2')

tests/test_flake8.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
from subprocess import call
4+
5+
6+
def test_flake8():
7+
return_code = call(['flake8'])
8+
assert return_code == 0

tests/utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../utils.py

0 commit comments

Comments
 (0)