port tests to testtools, add branch coverage, omit openstack common

Change-Id: I364dcaac1db1922cc815ece455a69734aa6c51f6
This commit is contained in:
Paul Kehrer 2014-03-20 09:06:11 -04:00
parent 026db28334
commit cc78b2a2a8
11 changed files with 71 additions and 96 deletions

3
.coveragerc Normal file
View File

@ -0,0 +1,3 @@
[run]
branch = True
omit = barbicanclient/openstack/*

4
.gitignore vendored
View File

@ -24,9 +24,9 @@ pip-log.txt
# Unit test / coverage reports
.coverage
.tox
nosetests.xml
coverage.xml
.testrepository
flake8.log
cover
# pyenv
.python-version

5
.testr.conf Normal file
View File

@ -0,0 +1,5 @@
[DEFAULT]
test_command=${PYTHON:-python} -m subunit.run discover -t . $LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list

View File

@ -16,14 +16,15 @@ import json
import mock
import requests
import unittest2 as unittest
import testtools
from barbicanclient.common import auth
class WhenTestingKeystoneAuthentication(unittest.TestCase):
class WhenTestingKeystoneAuthentication(testtools.TestCase):
def setUp(self):
super(WhenTestingKeystoneAuthentication, self).setUp()
self.keystone_client = mock.MagicMock()
self.auth_url = 'https://www.yada.com'
@ -41,8 +42,8 @@ class WhenTestingKeystoneAuthentication(unittest.TestCase):
self.keystone_client)
def test_endpoint_username_password_tenant_are_required(self):
with self.assertRaises(ValueError):
keystone = auth.KeystoneAuthV2()
with testtools.ExpectedException(ValueError):
auth.KeystoneAuthV2()
def test_nothing_is_required_if_keystone_is_present(self):
fake_keystone = mock.Mock(tenant_name='foo', tenant_id='bar')
@ -56,9 +57,10 @@ class WhenTestingKeystoneAuthentication(unittest.TestCase):
self.assertEquals(barbican_url, self.keystone_auth.barbican_url)
class WhenTestingRackspaceAuthentication(unittest.TestCase):
class WhenTestingRackspaceAuthentication(testtools.TestCase):
def setUp(self):
super(WhenTestingRackspaceAuthentication, self).setUp()
self._auth_url = 'https://auth.url.com'
self._username = 'username'
self._api_key = 'api_key'
@ -85,16 +87,16 @@ class WhenTestingRackspaceAuthentication(unittest.TestCase):
self.addCleanup(patcher.stop)
def test_auth_url_username_and_api_key_are_required(self):
with self.assertRaises(ValueError):
identity = auth.RackspaceAuthV2()
with self.assertRaises(ValueError):
identity = auth.RackspaceAuthV2(self._auth_url)
with self.assertRaises(ValueError):
identity = auth.RackspaceAuthV2(self._auth_url,
self._username)
with self.assertRaises(ValueError):
identity = auth.RackspaceAuthV2(self._auth_url,
api_key=self._api_key)
with testtools.ExpectedException(ValueError):
auth.RackspaceAuthV2()
with testtools.ExpectedException(ValueError):
auth.RackspaceAuthV2(self._auth_url)
with testtools.ExpectedException(ValueError):
auth.RackspaceAuthV2(self._auth_url,
self._username)
with testtools.ExpectedException(ValueError):
auth.RackspaceAuthV2(self._auth_url,
api_key=self._api_key)
def test_tokens_is_appended_to_auth_url(self):
identity = auth.RackspaceAuthV2(self._auth_url,
@ -129,17 +131,17 @@ class WhenTestingRackspaceAuthentication(unittest.TestCase):
def test_auth_exception_thrown_for_bad_status(self):
self._response.status_code = 400
with self.assertRaises(auth.AuthException):
identity = auth.RackspaceAuthV2(self._auth_url,
self._username,
api_key=self._api_key)
with testtools.ExpectedException(auth.AuthException):
auth.RackspaceAuthV2(self._auth_url,
self._username,
api_key=self._api_key)
def test_error_raised_for_bad_response_from_server(self):
self._response._content = 'Not JSON'
with self.assertRaises(auth.AuthException):
identity = auth.RackspaceAuthV2(self._auth_url,
self._username,
api_key=self._api_key)
with testtools.ExpectedException(auth.AuthException):
auth.RackspaceAuthV2(self._auth_url,
self._username,
api_key=self._api_key)
def test_auth_token_is_set(self):
identity = auth.RackspaceAuthV2(self._auth_url,

View File

@ -16,20 +16,12 @@
import cStringIO
import os
import sys
import unittest2 as unittest
import testtools
import barbicanclient.barbican
def suite():
suite = unittest.TestSuite()
suite.addTest(TestBarbican())
return suite
class TestBarbican(unittest.TestCase):
class TestBarbican(testtools.TestCase):
def barbican(self, argstr):
"""Source: Keystone client's shell method in test_shell.py"""
orig = sys.stdout
@ -49,12 +41,6 @@ class TestBarbican(unittest.TestCase):
os.environ = _old_env
return out
def setUp(self):
pass
def test_help(self):
args = "-h"
self.assertIn('usage: ', self.barbican(args))
if __name__ == '__main__':
unittest.main()

View File

@ -15,7 +15,7 @@
import mock
import requests
import unittest2 as unittest
import testtools
from barbicanclient import client
from barbicanclient.openstack.common import timeutils
@ -47,8 +47,9 @@ class FakeResp(object):
return self.content
class WhenTestingClientInit(unittest.TestCase):
class WhenTestingClientInit(testtools.TestCase):
def setUp(self):
super(WhenTestingClientInit, self).setUp()
self.auth_endpoint = 'https://localhost:5000/v2.0/'
self.auth_token = 'fake_auth_token'
self.user = 'user'
@ -79,12 +80,12 @@ class WhenTestingClientInit(unittest.TestCase):
self.auth_token)
def test_error_thrown_when_no_auth_and_no_endpoint(self):
with self.assertRaises(ValueError):
c = client.Client(tenant_id=self.tenant_id)
self.assertRaises(ValueError, client.Client,
**{"tenant_id": self.tenant_id})
def test_error_thrown_when_no_auth_and_no_tenant_id(self):
with self.assertRaises(ValueError):
c = client.Client(endpoint=self.endpoint)
self.assertRaises(ValueError, client.Client,
**{"endpoint": self.endpoint})
def test_client_strips_trailing_slash_from_endpoint(self):
c = client.Client(endpoint=self.endpoint, tenant_id=self.tenant_id)
@ -97,24 +98,22 @@ class WhenTestingClientInit(unittest.TestCase):
def test_should_raise_for_unauthorized_response(self):
resp = self._mock_response(status_code=401)
c = client.Client(auth_plugin=self.fake_auth)
with self.assertRaises(client.HTTPAuthError):
c._check_status_code(resp)
self.assertRaises(client.HTTPAuthError, c._check_status_code, resp)
def test_should_raise_for_server_error(self):
resp = self._mock_response(status_code=500)
c = client.Client(auth_plugin=self.fake_auth)
with self.assertRaises(client.HTTPServerError):
c._check_status_code(resp)
self.assertRaises(client.HTTPServerError, c._check_status_code, resp)
def test_should_raise_for_client_errors(self):
resp = self._mock_response(status_code=400)
c = client.Client(auth_plugin=self.fake_auth)
with self.assertRaises(client.HTTPClientError):
c._check_status_code(resp)
self.assertRaises(client.HTTPClientError, c._check_status_code, resp)
class WhenTestingClientWithSession(unittest.TestCase):
class WhenTestingClientWithSession(testtools.TestCase):
def setUp(self):
super(WhenTestingClientWithSession, self).setUp()
self.endpoint = 'https://localhost:9311/v1/'
self.tenant_id = '1234567'
@ -195,8 +194,9 @@ class WhenTestingClientWithSession(unittest.TestCase):
self.assertEqual(self.entity_href, url)
class BaseEntityResource(unittest.TestCase):
class BaseEntityResource(testtools.TestCase):
def _setUp(self, entity):
super(BaseEntityResource, self).setUp()
self.endpoint = 'https://localhost:9311/v1/'
self.tenant_id = '1234567'

View File

@ -123,9 +123,7 @@ class WhenTestingOrders(test_client.BaseEntityResource):
self.assertEqual(5, params['offset'])
def test_should_fail_get_no_href(self):
with self.assertRaises(ValueError):
self.manager.get(None)
self.assertRaises(ValueError, self.manager.get, None)
def test_should_fail_delete_no_href(self):
with self.assertRaises(ValueError):
self.manager.delete(None)
self.assertRaises(ValueError, self.manager.delete, None)

View File

@ -163,27 +163,24 @@ class WhenTestingSecrets(test_client.BaseEntityResource):
self.assertEqual(5, params['offset'])
def test_should_fail_get_invalid_secret(self):
with self.assertRaises(ValueError):
self.manager.get('12345')
self.assertRaises(ValueError, self.manager.get, '12345')
def test_should_fail_get_no_href(self):
with self.assertRaises(ValueError):
self.manager.get(None)
self.assertRaises(ValueError, self.manager.get, None)
def test_should_fail_decrypt_no_content_types(self):
self.api.get.return_value = self.secret.get_dict(self.entity_href)
with self.assertRaises(ValueError):
self.manager.decrypt(secret_ref=self.entity_href)
self.assertRaises(ValueError, self.manager.decrypt,
**{"secret_ref": self.entity_href})
def test_should_fail_decrypt_no_default_content_type(self):
content_types_dict = {'no-default': 'application/octet-stream'}
self.api.get.return_value = self.secret.get_dict(self.entity_href,
content_types_dict)
with self.assertRaises(ValueError):
self.manager.decrypt(secret_ref=self.entity_href)
self.assertRaises(ValueError, self.manager.decrypt,
**{"secret_ref": self.entity_href})
def test_should_fail_delete_no_href(self):
with self.assertRaises(ValueError):
self.manager.delete(None)
self.assertRaises(ValueError, self.manager.get, None)

View File

@ -127,9 +127,7 @@ class WhenTestingVerifications(test_client.BaseEntityResource):
self.assertEqual(5, params['offset'])
def test_should_fail_get_no_href(self):
with self.assertRaises(ValueError):
self.manager.get(None)
self.assertRaises(ValueError, self.manager.get, None)
def test_should_fail_delete_no_href(self):
with self.assertRaises(ValueError):
self.manager.delete(None)
self.assertRaises(ValueError, self.manager.delete, None)

View File

@ -1,6 +1,7 @@
coverage>=3.6
discover
hacking>=0.7.0
mock>=1.0.1
nose>=1.3.0
nosexcover>=1.0.8
testrepository>=0.0.17
testtools>=0.9.32,<0.9.35
tox>=1.6.0
unittest2>=0.5.1

25
tox.ini
View File

@ -7,24 +7,15 @@
envlist = py26, py27, pep8
[testenv]
setenv = VIRTUAL_ENV={envdir}
LANG=en_US.UTF-8
LANGUAGE=en_US:en
LC_ALL=C
OS_STDOUT_NOCAPTURE=False
OS_STDERR_NOCAPTURE=False
deps = -r{toxinidir}/requirements.txt
-r{toxinidir}/test-requirements.txt
commands =
python setup.py testr --coverage
coverage combine
coverage report -m
[testenv:pep8]
commands = {toxinidir}/tools/hacking.sh
[testenv:venv]
commands = {posargs}
[testenv:cover]
commands = nosetests --with-coverage --cover-package=barbicanclient
commands = {toxinidir}/tools/hacking.sh {posargs}
[tox:jenkins]
downloadcache = ~/cache/pip
@ -33,9 +24,3 @@ downloadcache = ~/cache/pip
ignore = F,H
show-source = True
exclude = .venv,.tox,dist,doc,*egg
[testenv:py26]
commands = nosetests {posargs:--with-xcoverage --all-modules --cover-inclusive --traverse-namespace --with-xunit --cover-package=barbicanclient}
[testenv:py27]
commands = nosetests {posargs:--with-xcoverage --all-modules --cover-inclusive --traverse-namespace --with-xunit --cover-package=barbicanclient}