diff --git a/test-requirements.txt b/test-requirements.txt index 49bbf52c..4d5f5b8c 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -26,3 +26,4 @@ ddt>=0.7.0 sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 oslosphinx>=2.5.0 # Apache-2.0 openstack-doc-tools>=0.23 +python-openstackclient>=2.0.0 diff --git a/tests/unit/cli/__init__.py b/tests/unit/cli/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/cli/fakes.py b/tests/unit/cli/fakes.py new file mode 100644 index 00000000..ff44ae6b --- /dev/null +++ b/tests/unit/cli/fakes.py @@ -0,0 +1,31 @@ +# Copyright (c) 2015 Catalyst IT Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import mock +from openstackclient.tests import utils + + +class TestMessaging(utils.TestCommand): + + def setUp(self): + super(TestMessaging, self).setUp() + + self.messaging_client = mock.MagicMock() + # TODO(flwang): It would be nice if we can figure out a better way to + # get the mocked request and transport. + req_trans = (mock.MagicMock(), mock.MagicMock()) + self.messaging_client._request_and_transport.return_value = req_trans + self.app.client_manager.messaging = self.messaging_client diff --git a/tests/unit/cli/v1/__init__.py b/tests/unit/cli/v1/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/cli/v1/test_queues.py b/tests/unit/cli/v1/test_queues.py new file mode 100644 index 00000000..0878f261 --- /dev/null +++ b/tests/unit/cli/v1/test_queues.py @@ -0,0 +1,69 @@ +# Copyright (c) 2015 Catalyst IT Ltd. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from tests.unit.cli import fakes + +from zaqarclient.queues.v1 import cli as v1_cli +from zaqarclient.queues.v1 import iterator +from zaqarclient.queues.v1 import queues as v1_api_queues + + +class TestQueues(fakes.TestMessaging): + def setUp(self): + super(TestQueues, self).setUp() + + +class TestV1ListQueues(TestQueues): + def setUp(self): + super(TestV1ListQueues, self).setUp() + queues_list = iterator._Iterator(self, [{'name': 'fake_queue'}], + 'queues', + v1_api_queues.create_object(self)) + self.app.client_manager.messaging.queues.return_value = queues_list + + # Command to test + self.cmd = v1_cli.ListQueues(self.app, None) + + def test_queues_list(self): + arglist = [] + verifylist = [] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + + # Check that columns are correct + expected_columns = ('Name',) + self.assertEqual(expected_columns, columns) + # Check that data is correct + expected_data = [('fake_queue',)] + self.assertEqual(expected_data, list(data)) + + +class TestV1CreateQueue(TestQueues): + + def setUp(self): + super(TestV1CreateQueue, self).setUp() + + # Command to test + self.cmd = v1_cli.CreateQueue(self.app, None) + + def test_queue_create(self): + arglist = ['fake_queue'] + verifylist = [] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + columns, data = self.cmd.take_action(parsed_args) + self.messaging_client.queue.assert_called_with('fake_queue', + force_create=True) diff --git a/zaqarclient/queues/v1/cli.py b/zaqarclient/queues/v1/cli.py index 8e7fd5a9..3fded9cb 100644 --- a/zaqarclient/queues/v1/cli.py +++ b/zaqarclient/queues/v1/cli.py @@ -43,7 +43,7 @@ class CreateQueue(show.ShowOne): def take_action(self, parsed_args): client = _get_client(self, parsed_args) queue_name = parsed_args.queue_name - data = client.queue(queue_name) + data = client.queue(queue_name, force_create=True) columns = ('Name',) return columns, utils.get_item_properties(data, columns) diff --git a/zaqarclient/queues/v1/queues.py b/zaqarclient/queues/v1/queues.py index 56f5459e..63f0c933 100644 --- a/zaqarclient/queues/v1/queues.py +++ b/zaqarclient/queues/v1/queues.py @@ -23,7 +23,20 @@ from zaqarclient.queues.v1 import message class Queue(object): - def __init__(self, client, name, auto_create=True): + def __init__(self, client, name, auto_create=True, force_create=False): + """Initialize queue object + + :param client: The client object of Zaqar. + :type client: `object` + :param name: Name of the queue. + :type name: `six.string_type` + :param auto_create: If create the queue automatically in database. + :type auto_create: `boolean` + :param force_create: If create the queue and skip the API version + check, which is useful for command line interface. + :type force_create: `boolean` + :returns: The queue object. + """ self.client = client if name == "": @@ -34,7 +47,7 @@ class Queue(object): self._metadata = None if auto_create: - self.ensure_exists() + self.ensure_exists(force_create=force_create) @property def name(self): @@ -48,7 +61,7 @@ class Queue(object): else: return core.queue_exists(trans, req, self._name) - def ensure_exists(self): + def ensure_exists(self, force_create=False): """Ensures a queue exists This method is not race safe, @@ -56,7 +69,7 @@ class Queue(object): right after it was called. """ req, trans = self.client._request_and_transport() - if req.api.is_supported('queue_set_metadata'): + if force_create or req.api.is_supported('queue_set_metadata'): core.queue_create(trans, req, self._name) def metadata(self, new_meta=None, force_reload=False):