Skip to content

Commit

Permalink
Merge pull request #9 from mtlynch/fix-unit-tests
Browse files Browse the repository at this point in the history
Cleaning up unit tests
  • Loading branch information
super3 committed Aug 17, 2015
2 parents 1cda08b + 12ddf67 commit 8f90d44
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 256 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ python:
- 2.7
- 3.3
- 3.4
services:
- redis-server
install:
- pip install coverage coveralls .
after_success:
Expand All @@ -13,4 +11,4 @@ script:
- coverage run setup.py test -a "--doctest-modules --pep8 -v tests/ RandomIO/"
- coverage report -m --include=RandomIO/*,bin/*
notifications:
slack: storjcommunity:G9Q7PFrK7LQGTuomtP8h5oOH
slack: storjcommunity:G9Q7PFrK7LQGTuomtP8h5oOH
71 changes: 0 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,77 +75,6 @@ with open('path/to/file','rb') as f:
# b'\xec\xf4C\xeb\x1d\rU%\xca\xae'
```

### CLI Tools

RandomIO includes a small set of CLI tools in IOTools.py:

```
$python IOTools.py --help
usage: IOTools.py <command> [<args>]
Currently available commands include:
pairgen Outputs a series of seed-hash pairs for files generated using the
RandomIO library.
A series of command-line tools that make use of the RandomIO library.
positional arguments:
command Command to run.
optional arguments:
-h, --help show this help message and exit
```

Currently, `pairgen` is the only available tool:

```
$ IOTools.py pairgen --help
usage: IOTools.py [-h] [-l LENGTH] [-p PAIRS] [-o OUTPUT] [-v] size
Output a series of seed-hash pairs for files generated in memory using the
RandomIO library.
positional arguments:
size The target size of each file generated and hashed (in
bytes).
optional arguments:
-h, --help show this help message and exit
-l LENGTH, --length LENGTH
The length of the random seed string to use.
-p PAIRS, --pairs PAIRS
The number of seed-hash pairs to generate.
-o OUTPUT, --output OUTPUT
The name of the file you wish to write pairs to.
-r, --redis Write to file using Redis protocol.
-v, --verbose Increase output verbosity.
This tool can be used to pre-generate seed-hash pairs for the Storj uptick
service.
```

Example output of `pairgens`:

```
$ IOTools.py pairgen 100000000 -p5 -l 10 -o mypairs.txt -v
Pair 0: Generating hash for 95.4MB file with seed 6a95c93fa9ca92d249d2...
done!
Pair 1: Generating hash for 95.4MB file with seed 7b31909908ff413061ce...
done!
Pair 2: Generating hash for 95.4MB file with seed a440bcd97af94701282c...
done!
Pair 3: Generating hash for 95.4MB file with seed 0f1f9dad1d6da7e03367...
done!
Pair 4: Generating hash for 95.4MB file with seed f146dbbe9c1706e1c3d6...
done!
```

Note that files are generated and hashed in memory. In addition, seeds displayed and/or written to file are hex-encoded. Actual seeds must be decoded before generating hash.

When writing pairs to file using Redis's mass insertion format, you can use the following command to import your pairs to Redis:

`cat pairs.out | redis-cli --pipe`

### Performance

```
Expand Down
106 changes: 0 additions & 106 deletions bin/IOTools.py

This file was deleted.

3 changes: 0 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@

install_requirements = [
'pycrypto >= 2.6.1',
'redis >= 2.10.3'
]

test_requirements = [
'redis>=2.10.3',
'pytest>=2.6.4',
'pytest-pep8',
'pytest-cache',
Expand Down Expand Up @@ -80,5 +78,4 @@ def run_tests(self):
install_requires=install_requirements,
tests_require=test_requirements,
keywords=['storj', 'randomIO', 'random generator'],
scripts=['bin/IOTools.py'],
)
83 changes: 10 additions & 73 deletions tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,15 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import unittest
import io
import os
import redis
import hashlib
import subprocess
import binascii
import unittest

import RandomIO
from sys import platform as _platform

if _platform.startswith('linux') or _platform == 'darwin':
cat_cmd = 'cat'
iotools_call = ['IOTools.py']
elif _platform == 'win32':
cat_cmd = 'type'
iotools_call = ['python', os.path.join('../bin', 'IOTools.py')]


class TestRandomIO(unittest.TestCase):

def setUp(self):
pass

def tearDown(self):
pass

def test_gen(self):
s = RandomIO.RandomIO()
b = s.read(100)
Expand Down Expand Up @@ -103,26 +86,17 @@ def test_dump(self):
s1 = RandomIO.RandomIO('seed string')
s2 = RandomIO.RandomIO('seed string')

file1 = 'file1'
file2 = 'file2'
DUMP_LENGTH = 100

with open(file1, 'wb') as f:
s1.dump(f, 100)
file1 = io.BytesIO()
file2 = io.BytesIO()

with open(file2, 'wb') as f:
s2.dump(f, 100)
s1.dump(file1, DUMP_LENGTH)
s2.dump(file2, DUMP_LENGTH)

with open(file1, 'rb') as f:
contents1 = f.read()

with open(file2, 'rb') as f:
contents2 = f.read()

self.assertEqual(len(contents1), 100)
self.assertEqual(contents1, contents2)

os.remove(file1)
os.remove(file2)
self.assertEqual(file1.tell(), DUMP_LENGTH)
self.assertEqual(file2.tell(), DUMP_LENGTH)
self.assertEqual(file1.getvalue(), file2.getvalue())

def test_genfile(self):
path = RandomIO.RandomIO('seed string').genfile(100)
Expand Down Expand Up @@ -258,43 +232,6 @@ def test_seek_end_not_possible(self):
str(ex.exception),
'Cannot seek from end of stream if size is unknown.')

def test_iotools_txt(self):
output = 'txt_test.out'
size = 10485760
subprocess.call(
iotools_call + ['pairgen', str(size),
'-p', '10', '-o', output])

with open(output, 'r') as pairsfile:
for line in pairsfile:
(hexseed, hash) = line.rstrip().split(' ')
seed = binascii.unhexlify(hexseed)
testhash = hashlib.sha256(
RandomIO.RandomIO(seed).read(size)).hexdigest()
self.assertEqual(hash, testhash)
os.remove(output)

def test_iotools_redis(self):
# no redis support for windows, so just pass the test
if not _platform == 'win32':
r = redis.StrictRedis(host='localhost', port=6379, db=0)
output = 'redis_test.out'
size = 10485760

subprocess.call(
iotools_call + ['pairgen', str(size), '-p', '10', '-o', output,
'--redis'])
subprocess.call(
'{0} {1} | redis-cli --pipe'.format(cat_cmd, output),
shell=True)

for hexseed in r.scan_iter():
seed = binascii.unhexlify(hexseed)
testhash = hashlib.sha256(
RandomIO.RandomIO(seed).read(size)).hexdigest()
self.assertEqual(r.get(hexseed).decode('ascii'), testhash)
os.remove(output)
r.flushall()

if __name__ == '__main__':
unittest.main()

0 comments on commit 8f90d44

Please sign in to comment.