diff options
Diffstat (limited to 'barbicanclient/tests')
-rw-r--r-- | barbicanclient/tests/test_barbican.py | 89 | ||||
-rw-r--r-- | barbicanclient/tests/test_client.py | 76 |
2 files changed, 112 insertions, 53 deletions
diff --git a/barbicanclient/tests/test_barbican.py b/barbicanclient/tests/test_barbican.py index 71ce182..ded8ca1 100644 --- a/barbicanclient/tests/test_barbican.py +++ b/barbicanclient/tests/test_barbican.py @@ -14,18 +14,59 @@ # limitations under the License. import io +from requests_mock.contrib import fixture +import testtools + from barbicanclient import barbican as barb from barbicanclient.barbican import Barbican from barbicanclient import client from barbicanclient import exceptions from barbicanclient.tests import keystone_client_fixtures -from barbicanclient.tests import test_client -class WhenTestingBarbicanCLI(test_client.BaseEntityResource): +class WhenTestingBarbicanCLI(testtools.TestCase): def setUp(self): - self._setUp('barbican') + super().setUp() + self.endpoint = 'http://localhost:9311/' + self.project_id = '1234567890abcdef1234567890abcdef' + self.responses = self.useFixture(fixture.Fixture()) + self.responses.get( + 'http://localhost:9311/v1/', + json={ + 'version': { + 'id': 'v1', + 'status': 'CURRENT', + 'min_version': '1.0', + 'max_version': '1.1', + 'links': [{ + 'rel': 'self', + 'href': 'http://192.168.122.110/key-manager/v1/' + }, { + 'rel': 'describedby', + 'type': 'text/html', + 'href': 'https://docs.openstack.org/'}]}}) + self.responses.get( + 'http://localhost:9311/', + json={ + "versions": { + "values": [{ + "id": "v1", + "status": "stable", + "links": [{ + "rel": "self", + "href": "http://localhost:9311/v1/" + }, { + "rel": "describedby", + "type": "text/html", + "href": "https://docs.openstack.org/" + }], + "media-types": [{ + "type": "application/vnd.openstack.key-manager-v1" + "+json", + "base": "application/json", + }]}]}} + ) self.captured_stdout = io.StringIO() self.captured_stderr = io.StringIO() self.barbican = Barbican( @@ -86,12 +127,15 @@ class WhenTestingBarbicanCLI(test_client.BaseEntityResource): '--no-auth --endpoint {0} --os-tenant-id {1}' 'secret list'.format(self.endpoint, self.project_id) ) - list_secrets_url = '{0}/v1/secrets'.format(self.endpoint) + list_secrets_url = '{0}/v1/secrets'.format(self.endpoint.rstrip('/')) self.responses.get(list_secrets_url, json={"secrets": [], "total": 0}) client = self.create_and_assert_client(args) secret_list = client.secrets.list() self.assertTrue(self.responses._adapter.called) - self.assertEqual(1, self.responses._adapter.call_count) + # there should be two requests + # 1. requests sent by microversions check + # 2. the request under test. + self.assertEqual(2, self.responses._adapter.call_count) self.assertEqual([], secret_list) def test_should_error_if_required_keystone_auth_arguments_are_missing( @@ -158,7 +202,7 @@ class WhenTestingBarbicanCLI(test_client.BaseEntityResource): self.assertEqual(1, response) def test_default_endpoint_filter_kwargs_set_correctly(self): - auth_args = ('--no-auth --endpoint http://barbican_endpoint:9311/v1 ' + auth_args = ('--no-auth --endpoint http://localhost:9311/ ' '--os-project-id project1') argv, remainder = self.parser.parse_known_args(auth_args.split()) barbican_client = self.barbican.create_client(argv) @@ -171,7 +215,7 @@ class WhenTestingBarbicanCLI(test_client.BaseEntityResource): self.assertIsNone(httpclient.service_name) def test_endpoint_filter_kwargs_set_correctly(self): - auth_args = ('--no-auth --endpoint http://barbican_endpoint:9311 ' + auth_args = ('--no-auth --endpoint http://localhost:9311/ ' '--os-project-id project1') endpoint_filter_args = ('--interface private ' '--service-type custom-type ' @@ -190,7 +234,7 @@ class WhenTestingBarbicanCLI(test_client.BaseEntityResource): self.assertEqual('v1', httpclient.version) def test_should_fail_if_provide_unsupported_api_version(self): - auth_args = ('--no-auth --endpoint http://barbican_endpoint:9311/v1 ' + auth_args = ('--no-auth --endpoint http://localhost:9311/ ' '--os-project-id project1') endpoint_filter_args = ('--interface private ' '--service-type custom-type ' @@ -210,16 +254,16 @@ class WhenTestingBarbicanCLI(test_client.BaseEntityResource): '--file foo --payload' 'secret get'.format(self.endpoint, self.project_id) ) - list_secrets_url = '{0}/v1/secrets'.format(self.endpoint) + list_secrets_url = '{0}/v1/secrets'.format(self.endpoint.rstrip('/')) self.responses.get(list_secrets_url, json={"secrets": [], "total": 0}) client = self.create_and_assert_client(args) secret_list = client.secrets.list() self.assertTrue(self.responses._adapter.called) - self.assertEqual(1, self.responses._adapter.call_count) + self.assertEqual(2, self.responses._adapter.call_count) self.assertEqual([], secret_list) def test_insecure_true_kwargs_set_correctly(self): - auth_args = ('--no-auth --endpoint https://barbican_endpoint:9311/v1 ' + auth_args = ('--no-auth --endpoint http://localhost:9311/ ' '--os-project-id project1') endpoint_filter_args = ('--interface public ' '--service-type custom-type ' @@ -235,29 +279,6 @@ class WhenTestingBarbicanCLI(test_client.BaseEntityResource): httpclient = barbican_client.secrets._api self.assertFalse(httpclient.session.verify) - def test_cafile_certfile_keyfile_kwargs_set_correctly(self): - auth_args = ('no_auth ' - '--os-auth-url https://keystone_endpoint:5000/v2 ' - '--os-auth-token f554ccb5-e157-4824-b67b-d139c87bc555 ' - '--os-project-id project1') - endpoint_filter_args = ('--interface public ' - '--service-type custom-type ' - '--service-name Burrbican ' - '--region-name RegionTwo ' - '--barbican-api-version v1') - args = auth_args + ' ' + endpoint_filter_args - argv, remainder = self.parser.parse_known_args(args.split()) - argv.os_cacert = 'ca.pem' - argv.os_cert = 'cert.pem' - argv.os_key = 'key.pem' - argv.os_identity_api_version = '2.0' - argv.os_tenant_name = 'my_tenant_name' - barbican_client = self.barbican.create_client(argv) - httpclient = barbican_client.secrets._api - self.assertEqual('ca.pem', httpclient.session.verify) - self.assertEqual('cert.pem', httpclient.session.cert[0]) - self.assertEqual('key.pem', httpclient.session.cert[1]) - class TestBarbicanWithKeystonePasswordAuth( keystone_client_fixtures.KeystoneClientFixture): diff --git a/barbicanclient/tests/test_client.py b/barbicanclient/tests/test_client.py index 87c7296..6548f0a 100644 --- a/barbicanclient/tests/test_client.py +++ b/barbicanclient/tests/test_client.py @@ -27,8 +27,45 @@ class TestClient(testtools.TestCase): def setUp(self): super(TestClient, self).setUp() - self.responses = self.useFixture(fixture.Fixture()) self.endpoint = 'http://localhost:9311' + self.responses = self.useFixture(fixture.Fixture()) + self.responses.get( + 'http://localhost:9311/v1/', + json={ + 'version': { + 'id': 'v1', + 'status': 'CURRENT', + 'min_version': '1.0', + 'max_version': '1.1', + 'links': [{ + 'rel': 'self', + 'href': 'http://192.168.122.110/key-manager/v1/' + }, { + 'rel': 'describedby', + 'type': 'text/html', + 'href': 'https://docs.openstack.org/'}]}}) + self.responses.get( + 'http://localhost:9311/', + json={ + "versions": { + "values": [{ + "id": "v1", + "status": "stable", + "links": [{ + "rel": "self", + "href": "http://localhost:9311/v1/" + }, { + "rel": "describedby", + "type": "text/html", + "href": "https://docs.openstack.org/" + }], + "media-types": [{ + "type": "application/vnd.openstack.key-manager-v1" + "+json", + "base": "application/json", + }]}]}} + ) + self.project_id = 'project_id' self.session = session.Session() self.httpclient = client._HTTPClient(session=self.session, @@ -39,10 +76,11 @@ class TestClient(testtools.TestCase): class WhenTestingClientInit(TestClient): def test_api_version_is_appended_to_endpoint(self): - c = client._HTTPClient(session=self.session, - endpoint=self.endpoint, - project_id=self.project_id) - self.assertEqual('http://localhost:9311/v1', c.endpoint_override) + c = client.Client(session=self.session, + endpoint=self.endpoint, + project_id=self.project_id) + self.assertEqual('http://localhost:9311/v1/', + c.client.endpoint_override) def test_default_headers_are_empty(self): c = client._HTTPClient(session=self.session, endpoint=self.endpoint) @@ -65,17 +103,18 @@ class WhenTestingClientInit(TestClient): **{"endpoint": self.endpoint}) def test_endpoint_override_starts_with_endpoint_url(self): - c = client._HTTPClient(session=self.session, - endpoint=self.endpoint, - project_id=self.project_id) - self.assertTrue(c.endpoint_override.startswith(self.endpoint)) + c = client.Client(session=self.session, + endpoint=self.endpoint, + project_id=self.project_id) + self.assertTrue(c.client.endpoint_override.startswith(self.endpoint)) def test_endpoint_override_ends_with_default_api_version(self): - c = client._HTTPClient(session=self.session, - endpoint=self.endpoint, - project_id=self.project_id) - self.assertTrue( - c.endpoint_override.endswith(client._DEFAULT_API_VERSION)) + c = client.Client(session=self.session, + endpoint=self.endpoint, + project_id=self.project_id) + self.assertTrue(c.client.endpoint_override.rstrip('/').endswith( + client._DEFAULT_API_VERSION + )) class WhenTestingClientPost(TestClient): @@ -83,7 +122,8 @@ class WhenTestingClientPost(TestClient): def setUp(self): super(WhenTestingClientPost, self).setUp() self.httpclient = client._HTTPClient(session=self.session, - endpoint=self.endpoint) + endpoint=self.endpoint, + version='v1') self.href = self.endpoint + '/v1/secrets/' self.post_mock = self.responses.post(self.href, json={}) @@ -274,13 +314,11 @@ class WhenTestingGetErrorMessage(TestClient): self.assertEqual('test_text: oopsie', msg) -class BaseEntityResource(testtools.TestCase): +class BaseEntityResource(TestClient): def _setUp(self, entity, entity_id='abcd1234-eabc-5678-9abc-abcdef012345'): + # TODO(dmendiza) Why are we calling super().setUp() from _setUp()? super(BaseEntityResource, self).setUp() - self.responses = self.useFixture(fixture.Fixture()) - self.endpoint = 'http://localhost:9311' - self.project_id = '1234567' self.entity = entity self.entity_id = entity_id |