Skip to content

Commit

Permalink
Change test runner from nose to py.test
Browse files Browse the repository at this point in the history
  • Loading branch information
arcimboldo authored and riccardomurri committed Mar 8, 2017
1 parent c22809b commit 94e2a04
Show file tree
Hide file tree
Showing 38 changed files with 1,207 additions and 1,306 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ addons:
- time
install:
- "pip install --upgrade setuptools pip"
- "pip install nose mock"
- "pip install 'pytest>=3.0.6' mock"
- "pip install -e .[openstack,ec2,daemon]"
script: nosetests -v
script: pytest -v
1 change: 1 addition & 0 deletions gc3libs/backends/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def __init__(self, name,

# "Connect" to the cloud (connection is actually performed
# only when needed by the `Client` class.

self.client = self._new_client()

# Set up the VMPool persistent class. This has been delayed
Expand Down
102 changes: 56 additions & 46 deletions gc3libs/backends/tests/test_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import tempfile

# 3rd party imports
from nose.tools import assert_equal, assert_false, assert_true, raises
from nose.plugins.skip import SkipTest
import pytest

# local imports
boto = pytest.importorskip("boto")
from gc3libs.backends.ec2 import VMPool
import gc3libs.exceptions

Expand All @@ -47,6 +47,7 @@ class TestVMPool(object):
# XXX: the `get*` methods are not tested here (yet) as they
# require mocking the `boto` library!

@pytest.fixture(autouse=True)
def setup(self):
# for persistence tests
self.tmpdir = tempfile.mkdtemp()
Expand All @@ -63,30 +64,37 @@ def setup(self):
self.pool2.add_vm(self.vm1)
self.pool2.add_vm(self.vm2)

def teardown(self):
yield

shutil.rmtree(self.tmpdir)

def test_empty_vmpool(self):
assert_equal(self.pool0._vm_ids, set([]))
assert_equal(self.pool0._vm_cache, {})
assert self.pool0._vm_ids == set([])
assert self.pool0._vm_cache == {}

def test_vmpool_one_vm(self):
assert_equal(self.pool1._vm_ids, set(['a']))
assert_equal(self.pool1._vm_cache, {'a': self.vm1})
assert self.pool1._vm_ids == set(['a'])
assert self.pool1._vm_cache == {'a': self.vm1}

def test_vmpool_two_vms(self):
assert_equal(self.pool2._vm_ids, set(['a', 'b']))
assert_equal(self.pool2._vm_cache, {'a': self.vm1, 'b': self.vm2})
assert self.pool2._vm_ids == set(['a', 'b'])
assert self.pool2._vm_cache == {'a': self.vm1, 'b': self.vm2}

def test_repr(self):
assert_equal(repr(self.pool0), "set([])")
assert_equal(repr(self.pool1), "set(['a'])")
assert_equal(repr(self.pool2), "set(['a', 'b'])")
assert repr(self.pool0) == "set([])"
assert repr(self.pool1) == "set(['a'])"
# Note: set do not have predictable representation
assert repr(self.pool2) in ("set(['a', 'b'])", "set(['b', 'a'])")


def test_str(self):
assert_equal(str(self.pool0), "VMPool('pool0') : set([])")
assert_equal(str(self.pool1), "VMPool('pool1') : set(['a'])")
assert_equal(str(self.pool2), "VMPool('pool2') : set(['a', 'b'])")
assert str(self.pool0) == "VMPool('pool0') : set([])"
assert str(self.pool1) == "VMPool('pool1') : set(['a'])"
# Note: set do not have predictable represenetation, and neither VMPool
assert str(self.pool2) in (
"VMPool('pool2') : set(['a', 'b'])",
"VMPool('pool2') : set(['b', 'a'])"
)

def test_add_remove(self):
VM_ID = 'x'
Expand All @@ -95,12 +103,12 @@ def test_add_remove(self):
L = len(pool)
# test add
pool.add_vm(vm)
assert_true(VM_ID in pool)
assert_equal(len(pool), L + 1)
assert VM_ID in pool
assert len(pool) == L + 1
# test remove
pool.remove_vm(VM_ID)
assert_false(VM_ID in pool)
assert_equal(len(pool), L)
assert not VM_ID in pool
assert len(pool) == L

def test_add_delete(self):
VM_ID = 'x'
Expand All @@ -109,34 +117,37 @@ def test_add_delete(self):
L = len(pool)
# test add
pool.add_vm(vm)
assert_true(VM_ID in pool)
assert_equal(len(pool), L + 1)
assert VM_ID in pool
assert len(pool) == L + 1
# test delete
del pool[VM_ID]
assert_false(VM_ID in pool)
assert_equal(len(pool), L)
assert not VM_ID in pool
assert len(pool) == L

def test_len(self):
for l, pool in enumerate([self.pool0, self.pool1, self.pool2]):
assert_equal(len(pool), l)
assert len(pool) == l

def test_iter(self):
"""Check that `VMPool.__iter__` iterates over VM IDs."""
for n, pool in enumerate([self.pool0, self.pool1, self.pool2]):
assert_equal(list(iter(pool)), list('ab'[:n]))
assert list(iter(pool)) in (
list('ab'[:n]),
list('ba'[:n]),
)

def test_get_vm_in_cache(self):
assert_equal(self.vm1, self.pool1['a'])
assert self.vm1 == self.pool1['a']

assert_equal(self.vm1, self.pool2['a'])
assert_equal(self.vm2, self.pool2['b'])
assert self.vm1 == self.pool2['a']
assert self.vm2 == self.pool2['b']

@raises(gc3libs.exceptions.UnrecoverableError)
@pytest.mark.xfail(raises=gc3libs.exceptions.UnrecoverableError)
def test_get_vm_not_in_cache_and_no_connection(self):
# clone pool2 from disk copy
pool = VMPool(self.pool2.path, None)
assert_equal(self.vm1, pool['a'])
assert_equal(self.vm2, pool['b'])
assert self.vm1 == pool['a']
assert self.vm2 == pool['b']

def test_get_all_vms(self):
all_vms1 = self.pool1.get_all_vms()
Expand All @@ -147,10 +158,10 @@ def test_get_all_vms(self):
assert self.vm2 in all_vms2

def test_lookup_is_get_vm(self):
assert_equal(self.pool1.get_vm('a'), self.pool1['a'])
assert self.pool1.get_vm('a') == self.pool1['a']

assert_equal(self.pool2.get_vm('a'), self.pool2['a'])
assert_equal(self.pool2.get_vm('b'), self.pool2['b'])
assert self.pool2.get_vm('a') == self.pool2['a']
assert self.pool2.get_vm('b') == self.pool2['b']

def test_reload(self):
# simulate stopping program by deleting object and cloning a
Expand All @@ -160,32 +171,32 @@ def test_reload(self):
path = pool.path
del pool
pool = VMPool(path, None)
assert_equal(pool._vm_ids, ids)
assert pool._vm_ids == ids
assert pool._vm_ids is not ids

def test_save_then_load_empty_vmpool(self):
self.pool0.save()

pool = VMPool(self.pool0.path, None)
assert_equal(pool.name, self.pool0.name)
assert_equal(pool.conn, None)
assert_equal(pool._vm_cache, {})
assert_equal(pool._vm_ids, self.pool0._vm_ids)
assert pool.name == self.pool0.name
assert pool.conn == None
assert pool._vm_cache == {}
assert pool._vm_ids == self.pool0._vm_ids

def test_save_then_load_nonempty_vmpool(self):
self.pool2.save()

pool = VMPool(self.pool2.path, None)
assert_equal(pool.name, self.pool2.name)
assert pool.name == self.pool2.name
# the list of VM ids should have been persisted ...
assert_equal(pool._vm_ids, self.pool2._vm_ids)
assert pool._vm_ids == self.pool2._vm_ids
# ... but the VM cache is now empty
assert_equal(pool._vm_cache, {})
assert pool._vm_cache == {}
# ...and so is the connection
assert_equal(pool.conn, None)
assert pool.conn == None

@pytest.mark.skip("This test must be rewritten")
def test_update(self):
raise SkipTest("This test must be rewritten")
# clone pool2
pool = VMPool(self.pool2.path, None)
vm3 = _MockVM('c')
Expand All @@ -205,5 +216,4 @@ def test_update_remove(self):


if "__main__" == __name__:
import nose
nose.runmodule()
pytest.main(["-v", __file__])
4 changes: 2 additions & 2 deletions gc3libs/backends/tests/test_generic_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ def test_sge_backends():


if "__main__" == __name__:
import nose
nose.runmodule()
import pytest
pytest.main(["-v", __file__])
Loading

0 comments on commit 94e2a04

Please sign in to comment.