summaryrefslogtreecommitdiff
path: root/saharaclient/tests/unit/osc
diff options
context:
space:
mode:
authorAndrey Pavlov <apavlov@mirantis.com>2015-07-24 16:36:05 +0300
committerAndrey Pavlov <apavlov@mirantis.com>2015-08-18 11:56:40 +0300
commitf0a3abeecf2a27e1de118969c00249580686d02b (patch)
treef35834c0853c023cb373ba82bc995c854d17f0e6 /saharaclient/tests/unit/osc
parentafff5823f7bd3b4a0e63f5a0e54212975c16346d (diff)
downloadpython-saharaclient-f0a3abeecf2a27e1de118969c00249580686d02b.tar.gz
Add initial commit for integration with Openstackclient
This patch adds support of Sahara CLI to Openstackclient by setting entry points and implementing interface functions. Also it adds Plugins functionality and unit tests to cover it. Co-Authored-By: Sergey Reshetnyak <sreshetniak@mirantis.com> Partially implements: blueprint cli-as-openstackclient-plugin Change-Id: If5c33f8446d64385a71e02a0ae7bf23d7b40f862
Diffstat (limited to 'saharaclient/tests/unit/osc')
-rw-r--r--saharaclient/tests/unit/osc/__init__.py0
-rw-r--r--saharaclient/tests/unit/osc/test_plugin.py38
-rw-r--r--saharaclient/tests/unit/osc/v1/__init__.py0
-rw-r--r--saharaclient/tests/unit/osc/v1/fakes.py26
-rw-r--r--saharaclient/tests/unit/osc/v1/test_plugins.py160
-rw-r--r--saharaclient/tests/unit/osc/v1/test_utils.py76
6 files changed, 300 insertions, 0 deletions
diff --git a/saharaclient/tests/unit/osc/__init__.py b/saharaclient/tests/unit/osc/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/saharaclient/tests/unit/osc/__init__.py
diff --git a/saharaclient/tests/unit/osc/test_plugin.py b/saharaclient/tests/unit/osc/test_plugin.py
new file mode 100644
index 0000000..7d057f4
--- /dev/null
+++ b/saharaclient/tests/unit/osc/test_plugin.py
@@ -0,0 +1,38 @@
+# Copyright (c) 2015 Mirantis Inc.
+#
+# 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 saharaclient.osc import plugin
+from saharaclient.tests.unit import base
+
+
+class TestDataProcessingPlugin(base.BaseTestCase):
+
+ @mock.patch("saharaclient.api.client.Client")
+ def test_make_client(self, p_client):
+
+ instance = mock.Mock()
+ instance._api_version = {"data_processing": '1.1'}
+ instance.session = 'session'
+ instance._region_name = 'region_name'
+ instance._cacert = 'cacert'
+ instance._insecure = 'insecure'
+
+ plugin.make_client(instance)
+ p_client.assert_called_with(session='session',
+ region_name='region_name',
+ cacert='cacert',
+ insecure='insecure')
diff --git a/saharaclient/tests/unit/osc/v1/__init__.py b/saharaclient/tests/unit/osc/v1/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/saharaclient/tests/unit/osc/v1/__init__.py
diff --git a/saharaclient/tests/unit/osc/v1/fakes.py b/saharaclient/tests/unit/osc/v1/fakes.py
new file mode 100644
index 0000000..d8062a3
--- /dev/null
+++ b/saharaclient/tests/unit/osc/v1/fakes.py
@@ -0,0 +1,26 @@
+# Copyright (c) 2015 Mirantis Inc.
+#
+# 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 TestDataProcessing(utils.TestCommand):
+
+ def setUp(self):
+ super(TestDataProcessing, self).setUp()
+
+ self.app.client_manager.data_processing = mock.Mock()
diff --git a/saharaclient/tests/unit/osc/v1/test_plugins.py b/saharaclient/tests/unit/osc/v1/test_plugins.py
new file mode 100644
index 0000000..1f222eb
--- /dev/null
+++ b/saharaclient/tests/unit/osc/v1/test_plugins.py
@@ -0,0 +1,160 @@
+# Copyright (c) 2015 Mirantis Inc.
+#
+# 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 saharaclient.api import plugins as api_plugins
+from saharaclient.osc.v1 import plugins as osc_plugins
+from saharaclient.tests.unit.osc.v1 import fakes
+
+
+PLUGIN_INFO = {'name': 'fake',
+ 'title': 'Fake Plugin',
+ 'versions': ['0.1', '0.2'],
+ 'description': 'Plugin for tests'}
+
+
+class TestPlugins(fakes.TestDataProcessing):
+ def setUp(self):
+ super(TestPlugins, self).setUp()
+ self.plugins_mock = self.app.client_manager.data_processing.plugins
+ self.plugins_mock.reset_mock()
+
+
+class TestListPlugins(TestPlugins):
+ def setUp(self):
+ super(TestListPlugins, self).setUp()
+ self.plugins_mock.list.return_value = [api_plugins.Plugin(
+ None, PLUGIN_INFO)]
+
+ # Command to test
+ self.cmd = osc_plugins.ListPlugins(self.app, None)
+
+ def test_plugins_list_no_options(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', 'Versions']
+ self.assertEqual(expected_columns, columns)
+
+ # Check that data is correct
+ expected_data = [('fake', '0.1, 0.2')]
+ self.assertEqual(expected_data, list(data))
+
+ def test_plugins_list_long(self):
+ arglist = ['--long']
+ verifylist = [('long', True)]
+
+ 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', 'Title', 'Versions', 'Description']
+ self.assertEqual(expected_columns, columns)
+
+ # Check that data is correct
+ expected_data = [('fake', 'Fake Plugin', '0.1, 0.2',
+ 'Plugin for tests')]
+ self.assertEqual(expected_data, list(data))
+
+
+class TestShowPlugin(TestPlugins):
+ def setUp(self):
+ super(TestShowPlugin, self).setUp()
+ self.plugins_mock.get.return_value = api_plugins.Plugin(
+ None, PLUGIN_INFO)
+
+ # Command to test
+ self.cmd = osc_plugins.ShowPlugin(self.app, None)
+
+ def test_plugin_show(self):
+ arglist = ['fake']
+ verifylist = []
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ columns, data = self.cmd.take_action(parsed_args)
+
+ # Check that correct arguments was passed
+ self.plugins_mock.get.assert_called_once_with('fake')
+
+ # Check that columns are correct
+ expected_columns = ('description', 'name', 'title', 'versions')
+ self.assertEqual(expected_columns, columns)
+
+ # Check that data is correct
+ expected_data = ('Plugin for tests', 'fake', 'Fake Plugin', '0.1, 0.2')
+ self.assertEqual(expected_data, data)
+
+
+class TestGetPluginConfigs(TestPlugins):
+ def setUp(self):
+ super(TestGetPluginConfigs, self).setUp()
+ self.plugins_mock.get_version_details.return_value = (
+ api_plugins.Plugin(None, PLUGIN_INFO))
+
+ # Command to test
+ self.cmd = osc_plugins.GetPluginConfigs(self.app, None)
+
+ @mock.patch('oslo_serialization.jsonutils.dump')
+ def test_get_plugin_configs_default_file(self, p_dump):
+ m_open = mock.mock_open()
+ with mock.patch('six.moves.builtins.open', m_open, create=True):
+ arglist = ['fake', '0.1']
+ verifylist = [('plugin', 'fake'), ('version', '0.1')]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ self.cmd.take_action(parsed_args)
+
+ # Check that correct arguments was passed
+ self.plugins_mock.get_version_details.assert_called_once_with(
+ 'fake', '0.1')
+
+ args_to_dump = p_dump.call_args[0]
+ # Check that the right data will be saved
+
+ self.assertEqual(PLUGIN_INFO, args_to_dump[0])
+ # Check that data will be saved to the right file
+ self.assertEqual('fake', m_open.call_args[0][0])
+
+ @mock.patch('oslo_serialization.jsonutils.dump')
+ def test_get_plugin_configs_specified_file(self, p_dump):
+ m_open = mock.mock_open()
+ with mock.patch('six.moves.builtins.open', m_open):
+ arglist = ['fake', '0.1', '--file', 'testfile']
+ verifylist = [('plugin', 'fake'), ('version', '0.1'),
+ ('file', 'testfile')]
+
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ self.cmd.take_action(parsed_args)
+
+ # Check that correct arguments was passed
+ self.plugins_mock.get_version_details.assert_called_once_with(
+ 'fake', '0.1')
+
+ args_to_dump = p_dump.call_args[0]
+ # Check that the right data will be saved
+
+ self.assertEqual(PLUGIN_INFO, args_to_dump[0])
+ # Check that data will be saved to the right file
+ self.assertEqual('testfile', m_open.call_args[0][0])
diff --git a/saharaclient/tests/unit/osc/v1/test_utils.py b/saharaclient/tests/unit/osc/v1/test_utils.py
new file mode 100644
index 0000000..f7d3cf0
--- /dev/null
+++ b/saharaclient/tests/unit/osc/v1/test_utils.py
@@ -0,0 +1,76 @@
+# Copyright (c) 2015 Mirantis Inc.
+#
+# 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 openstackclient.common import exceptions
+
+from saharaclient.osc.v1 import utils
+from saharaclient.tests.unit import base
+
+
+class TestUtils(base.BaseTestCase):
+ def test_prepare_data(self):
+ data = {'id': '123', 'name_of_res': 'name', 'description': 'descr'}
+
+ fields = ['id', 'name_of_res', 'description']
+ expected_data = {'Description': 'descr', 'Id': '123',
+ 'Name of res': 'name'}
+ self.assertEqual(expected_data, utils.prepare_data(data, fields))
+
+ fields = ['id', 'name_of_res']
+ expected_data = {'Id': '123', 'Name of res': 'name'}
+ self.assertEqual(expected_data, utils.prepare_data(data, fields))
+
+ fields = ['name_of_res']
+ expected_data = {'Name of res': 'name'}
+ self.assertEqual(expected_data, utils.prepare_data(data, fields))
+
+ def test_get_resource_id(self):
+ class TestResource(object):
+ def __init__(self, id):
+ self.id = id
+
+ class TestManager(object):
+
+ resource_class = TestResource
+
+ def get(self, id):
+ if id == 'id':
+ return TestResource('from_id')
+ else:
+ raise
+
+ def find(self, name):
+ if name == 'name':
+ return [TestResource('from_name')]
+ if name == 'null':
+ return []
+ if name == 'mult':
+ return [TestResource('1'), TestResource('2')]
+
+ # check case when resource id is passed
+ self.assertEqual('from_id', utils.get_resource(
+ TestManager(), 'id').id)
+
+ # check case when resource name is passed
+ self.assertEqual('from_name', utils.get_resource(
+ TestManager(), 'name').id)
+
+ # check that error is raised when resource doesn't exists
+ self.assertRaises(exceptions.CommandError, utils.get_resource,
+ TestManager(), 'null')
+
+ # check that error is raised when multiple resources choice
+ self.assertRaises(exceptions.CommandError, utils.get_resource,
+ TestManager(), 'mult')