diff options
author | Zuul <zuul@review.openstack.org> | 2019-01-11 01:27:12 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2019-01-11 01:27:12 +0000 |
commit | 9ebd05065d759434b6f341421fb6cce23f7b8249 (patch) | |
tree | 7b6216b4445b79f1d43c3531744cb7745b045511 /saharaclient/tests/unit | |
parent | f733b1007f5c1320453d4d823881bf511d0d1efe (diff) | |
parent | 3f6f2d1128314e541f5a9036b1560a0f8e79ac32 (diff) | |
download | python-saharaclient-9ebd05065d759434b6f341421fb6cce23f7b8249.tar.gz |
Merge "Preparing OSC for APIv2"2.1.0
Diffstat (limited to 'saharaclient/tests/unit')
19 files changed, 2933 insertions, 1 deletions
diff --git a/saharaclient/tests/unit/osc/v1/test_cluster_templates.py b/saharaclient/tests/unit/osc/v1/test_cluster_templates.py index 78e67a3..bfa2605 100644 --- a/saharaclient/tests/unit/osc/v1/test_cluster_templates.py +++ b/saharaclient/tests/unit/osc/v1/test_cluster_templates.py @@ -53,6 +53,7 @@ class TestClusterTemplates(fakes.TestDataProcessing): self.app.client_manager.data_processing.node_group_templates) self.ct_mock.reset_mock() self.ngt_mock.reset_mock() + self.app.api_version['data_processing'] = '1' class TestCreateClusterTemplate(TestClusterTemplates): @@ -63,6 +64,7 @@ class TestCreateClusterTemplate(TestClusterTemplates): None, CT_INFO) self.ngt_mock.find_unique.return_value = api_ngt.NodeGroupTemplate( None, CT_INFO['node_groups'][0]) + self.app.api_version['data_processing'] = '1.1' # Command to test self.cmd = osc_ct.CreateClusterTemplate(self.app, None) diff --git a/saharaclient/tests/unit/osc/v1/test_clusters.py b/saharaclient/tests/unit/osc/v1/test_clusters.py index 25197dc..7695ad1 100644 --- a/saharaclient/tests/unit/osc/v1/test_clusters.py +++ b/saharaclient/tests/unit/osc/v1/test_clusters.py @@ -91,6 +91,7 @@ class TestClusters(fakes.TestDataProcessing): self.ngt_mock.reset_mock() self.ct_mock.reset_mock() self.img_mock.reset_mock() + self.app.api_version['data_processing'] = '1' class TestCreateCluster(TestClusters): diff --git a/saharaclient/tests/unit/osc/v1/test_data_sources.py b/saharaclient/tests/unit/osc/v1/test_data_sources.py index 8753206..c37d8ab 100644 --- a/saharaclient/tests/unit/osc/v1/test_data_sources.py +++ b/saharaclient/tests/unit/osc/v1/test_data_sources.py @@ -33,6 +33,7 @@ class TestDataSources(fakes.TestDataProcessing): self.ds_mock = ( self.app.client_manager.data_processing.data_sources) self.ds_mock.reset_mock() + self.app.api_version['data_processing'] = '1' class TestCreateDataSource(TestDataSources): diff --git a/saharaclient/tests/unit/osc/v1/test_images.py b/saharaclient/tests/unit/osc/v1/test_images.py index d116c04..457b3f0 100644 --- a/saharaclient/tests/unit/osc/v1/test_images.py +++ b/saharaclient/tests/unit/osc/v1/test_images.py @@ -32,6 +32,7 @@ class TestImages(fakes.TestDataProcessing): self.image_mock = ( self.app.client_manager.data_processing.images) self.image_mock.reset_mock() + self.app.api_version['data_processing'] = '1' class TestListImages(TestImages): diff --git a/saharaclient/tests/unit/osc/v1/test_job_binaries.py b/saharaclient/tests/unit/osc/v1/test_job_binaries.py index b4d18e0..0b4b66e 100644 --- a/saharaclient/tests/unit/osc/v1/test_job_binaries.py +++ b/saharaclient/tests/unit/osc/v1/test_job_binaries.py @@ -37,6 +37,7 @@ class TestJobBinaries(fakes.TestDataProcessing): super(TestJobBinaries, self).setUp() self.jb_mock = self.app.client_manager.data_processing.job_binaries self.jb_mock.reset_mock() + self.app.api_version['data_processing'] = '1' class TestCreateJobBinary(TestJobBinaries): diff --git a/saharaclient/tests/unit/osc/v1/test_job_templates.py b/saharaclient/tests/unit/osc/v1/test_job_templates.py index 6f88260..9b6b711 100644 --- a/saharaclient/tests/unit/osc/v1/test_job_templates.py +++ b/saharaclient/tests/unit/osc/v1/test_job_templates.py @@ -48,6 +48,7 @@ class TestJobTemplates(fakes.TestDataProcessing): super(TestJobTemplates, self).setUp() self.job_mock = self.app.client_manager.data_processing.jobs self.job_mock.reset_mock() + self.app.api_version['data_processing'] = '1' class TestCreateJobTemplate(TestJobTemplates): diff --git a/saharaclient/tests/unit/osc/v1/test_job_types.py b/saharaclient/tests/unit/osc/v1/test_job_types.py index 8a1a471..ccc7be3 100644 --- a/saharaclient/tests/unit/osc/v1/test_job_types.py +++ b/saharaclient/tests/unit/osc/v1/test_job_types.py @@ -47,6 +47,7 @@ class TestJobTypes(fakes.TestDataProcessing): self.jt_mock = self.app.client_manager.data_processing.job_types self.jt_mock.reset_mock() self.job_mock.reset_mock() + self.app.api_version['data_processing'] = '1' class TestListJobTemplates(TestJobTypes): diff --git a/saharaclient/tests/unit/osc/v1/test_jobs.py b/saharaclient/tests/unit/osc/v1/test_jobs.py index 82ce3d8..fc325f2 100644 --- a/saharaclient/tests/unit/osc/v1/test_jobs.py +++ b/saharaclient/tests/unit/osc/v1/test_jobs.py @@ -57,6 +57,7 @@ class TestJobs(fakes.TestDataProcessing): super(TestJobs, self).setUp() self.je_mock = self.app.client_manager.data_processing.job_executions self.je_mock.reset_mock() + self.app.api_version['data_processing'] = '1' class TestExecuteJob(TestJobs): diff --git a/saharaclient/tests/unit/osc/v1/test_plugins.py b/saharaclient/tests/unit/osc/v1/test_plugins.py index 2c7c3f6..fad361d 100644 --- a/saharaclient/tests/unit/osc/v1/test_plugins.py +++ b/saharaclient/tests/unit/osc/v1/test_plugins.py @@ -38,6 +38,7 @@ class TestPlugins(fakes.TestDataProcessing): super(TestPlugins, self).setUp() self.plugins_mock = self.app.client_manager.data_processing.plugins self.plugins_mock.reset_mock() + self.app.api_version['data_processing'] = '1' class TestListPlugins(TestPlugins): diff --git a/saharaclient/tests/unit/osc/v2/test_cluster_templates.py b/saharaclient/tests/unit/osc/v2/test_cluster_templates.py new file mode 100644 index 0000000..dc4088f --- /dev/null +++ b/saharaclient/tests/unit/osc/v2/test_cluster_templates.py @@ -0,0 +1,336 @@ +# 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 osc_lib.tests import utils as osc_utils + +from saharaclient.api import cluster_templates as api_ct +from saharaclient.api import node_group_templates as api_ngt +from saharaclient.osc.v2 import cluster_templates as osc_ct +from saharaclient.tests.unit.osc.v1 import test_cluster_templates as tct_v1 + +CT_INFO = { + "description": "Cluster template for tests", + "use_autoconfig": True, + "is_default": False, + "node_groups": [ + { + "count": 2, + "id": "d29631fc-0fad-434b-80aa-7a3e9526f57c", + "name": "fakeng", + "plugin_name": 'fake', + "plugin_version": '0.1' + } + ], + "plugin_version": "0.1", + "is_public": False, + "plugin_name": "fake", + "id": "0647061f-ab98-4c89-84e0-30738ea55750", + "anti_affinity": [], + "name": "template", + "is_protected": False, + "domain_name": 'domain.org.' +} + + +class TestClusterTemplates(tct_v1.TestClusterTemplates): + def setUp(self): + super(TestClusterTemplates, self).setUp() + self.app.api_version['data_processing'] = '2' + self.ct_mock = ( + self.app.client_manager.data_processing.cluster_templates) + self.ngt_mock = ( + self.app.client_manager.data_processing.node_group_templates) + self.ct_mock.reset_mock() + self.ngt_mock.reset_mock() + + +class TestCreateClusterTemplate(TestClusterTemplates): + # TODO(apavlov): check for creation with --json + def setUp(self): + super(TestCreateClusterTemplate, self).setUp() + self.ct_mock.create.return_value = api_ct.ClusterTemplate( + None, CT_INFO) + self.ngt_mock.find_unique.return_value = api_ngt.NodeGroupTemplate( + None, CT_INFO['node_groups'][0]) + + # Command to test + self.cmd = osc_ct.CreateClusterTemplate(self.app, None) + + def test_ct_create_minimum_options(self): + arglist = ['--name', 'template', '--node-groups', 'fakeng:2'] + verifylist = [('name', 'template'), + ('node_groups', ['fakeng:2'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.ct_mock.create.assert_called_once_with( + description=None, plugin_version='0.1', is_protected=False, + is_public=False, name='template', node_groups=[ + {'count': 2, 'name': 'fakeng', + 'node_group_template_id': + 'd29631fc-0fad-434b-80aa-7a3e9526f57c'}], + plugin_name='fake', use_autoconfig=False, shares=None, + cluster_configs=None, domain_name=None) + + def test_ct_create_all_options(self): + arglist = ['--name', 'template', '--node-groups', 'fakeng:2', + '--anti-affinity', 'datanode', + '--description', 'descr', + '--autoconfig', '--public', '--protected', + '--domain-name', 'domain.org.'] + + verifylist = [('name', 'template'), + ('node_groups', ['fakeng:2']), + ('description', 'descr'), ('autoconfig', True), + ('public', True), ('protected', True), + ('domain_name', 'domain.org.')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.ct_mock.create.assert_called_once_with( + description='descr', plugin_version='0.1', is_protected=True, + is_public=True, name='template', node_groups=[ + {'count': 2, 'name': 'fakeng', + 'node_group_template_id': + 'd29631fc-0fad-434b-80aa-7a3e9526f57c'}], + plugin_name='fake', use_autoconfig=True, shares=None, + cluster_configs=None, domain_name='domain.org.') + + # Check that columns are correct + expected_columns = ('Anti affinity', 'Description', + 'Domain name', 'Id', 'Is default', + 'Is protected', 'Is public', 'Name', 'Node groups', + 'Plugin name', 'Plugin version', 'Use autoconfig') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('', 'Cluster template for tests', 'domain.org.', + '0647061f-ab98-4c89-84e0-30738ea55750', False, False, + False, 'template', 'fakeng:2', 'fake', '0.1', True) + self.assertEqual(expected_data, data) + + +class TestListClusterTemplates(TestClusterTemplates): + def setUp(self): + super(TestListClusterTemplates, self).setUp() + self.ct_mock.list.return_value = [api_ct.ClusterTemplate( + None, CT_INFO)] + + # Command to test + self.cmd = osc_ct.ListClusterTemplates(self.app, None) + + def test_ct_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', 'Id', 'Plugin name', 'Plugin version'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('template', '0647061f-ab98-4c89-84e0-30738ea55750', + 'fake', '0.1')] + self.assertEqual(expected_data, list(data)) + + def test_ct_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', 'Id', 'Plugin name', 'Plugin version', + 'Node groups', 'Description'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('template', '0647061f-ab98-4c89-84e0-30738ea55750', + 'fake', '0.1', 'fakeng:2', + 'Cluster template for tests')] + self.assertEqual(expected_data, list(data)) + + def test_ct_list_extra_search_opts(self): + arglist = ['--plugin', 'fake', '--plugin-version', '0.1', '--name', + 'templ'] + verifylist = [('plugin', 'fake'), ('plugin_version', '0.1'), + ('name', 'templ')] + + 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', 'Id', 'Plugin name', 'Plugin version'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('template', '0647061f-ab98-4c89-84e0-30738ea55750', + 'fake', '0.1')] + self.assertEqual(expected_data, list(data)) + + +class TestShowClusterTemplate(TestClusterTemplates): + def setUp(self): + super(TestShowClusterTemplate, self).setUp() + self.ct_mock.find_unique.return_value = api_ct.ClusterTemplate( + None, CT_INFO) + + # Command to test + self.cmd = osc_ct.ShowClusterTemplate(self.app, None) + + def test_ct_show(self): + arglist = ['template'] + verifylist = [('cluster_template', 'template')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.ct_mock.find_unique.assert_called_once_with(name='template') + + # Check that columns are correct + expected_columns = ('Anti affinity', 'Description', + 'Domain name', 'Id', 'Is default', + 'Is protected', 'Is public', 'Name', 'Node groups', + 'Plugin name', 'Plugin version', 'Use autoconfig') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ( + '', 'Cluster template for tests', 'domain.org.', + '0647061f-ab98-4c89-84e0-30738ea55750', False, False, False, + 'template', 'fakeng:2', 'fake', '0.1', True) + self.assertEqual(expected_data, data) + + +class TestDeleteClusterTemplate(TestClusterTemplates): + def setUp(self): + super(TestDeleteClusterTemplate, self).setUp() + self.ct_mock.find_unique.return_value = api_ct.ClusterTemplate( + None, CT_INFO) + + # Command to test + self.cmd = osc_ct.DeleteClusterTemplate(self.app, None) + + def test_ct_delete(self): + arglist = ['template'] + verifylist = [('cluster_template', ['template'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.ct_mock.delete.assert_called_once_with( + '0647061f-ab98-4c89-84e0-30738ea55750') + + +class TestUpdateClusterTemplate(TestClusterTemplates): + # TODO(apavlov): check for update with --json + def setUp(self): + super(TestUpdateClusterTemplate, self).setUp() + self.ct_mock.update.return_value = api_ct.ClusterTemplate( + None, CT_INFO) + self.ct_mock.find_unique.return_value = api_ct.ClusterTemplate( + None, CT_INFO) + self.ngt_mock.find_unique.return_value = api_ngt.NodeGroupTemplate( + None, CT_INFO['node_groups'][0]) + + # Command to test + self.cmd = osc_ct.UpdateClusterTemplate(self.app, None) + + def test_ct_update_no_options(self): + arglist = [] + verifylist = [] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_ct_update_nothing_updated(self): + arglist = ['template'] + verifylist = [('cluster_template', 'template')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + self.ct_mock.update.assert_called_once_with( + '0647061f-ab98-4c89-84e0-30738ea55750') + + def test_ct_update_all_options(self): + arglist = ['template', '--name', 'template', '--node-groups', + 'fakeng:2', '--anti-affinity', 'datanode', + '--description', 'descr', '--autoconfig-enable', + '--public', '--protected', '--domain-name', 'domain.org.'] + + verifylist = [('cluster_template', 'template'), ('name', 'template'), + ('node_groups', ['fakeng:2']), + ('description', 'descr'), ('use_autoconfig', True), + ('is_public', True), ('is_protected', True), + ('domain_name', 'domain.org.')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.ct_mock.update.assert_called_once_with( + '0647061f-ab98-4c89-84e0-30738ea55750', description='descr', + plugin_version='0.1', is_protected=True, is_public=True, + name='template', + node_groups=[ + {'count': 2, 'name': 'fakeng', + 'node_group_template_id': + 'd29631fc-0fad-434b-80aa-7a3e9526f57c'}], + plugin_name='fake', use_autoconfig=True, domain_name='domain.org.') + + # Check that columns are correct + expected_columns = ('Anti affinity', 'Description', + 'Domain name', 'Id', 'Is default', + 'Is protected', 'Is public', 'Name', 'Node groups', + 'Plugin name', 'Plugin version', 'Use autoconfig') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('', 'Cluster template for tests', 'domain.org.', + '0647061f-ab98-4c89-84e0-30738ea55750', False, False, + False, 'template', 'fakeng:2', 'fake', '0.1', True) + self.assertEqual(expected_data, data) + + def test_ct_update_private_unprotected(self): + arglist = ['template', '--private', '--unprotected'] + verifylist = [('cluster_template', 'template'), + ('is_protected', False), ('is_public', False)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + self.ct_mock.update.assert_called_once_with( + '0647061f-ab98-4c89-84e0-30738ea55750', is_protected=False, + is_public=False) diff --git a/saharaclient/tests/unit/osc/v2/test_clusters.py b/saharaclient/tests/unit/osc/v2/test_clusters.py new file mode 100644 index 0000000..6bbb725 --- /dev/null +++ b/saharaclient/tests/unit/osc/v2/test_clusters.py @@ -0,0 +1,549 @@ +# 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 osc_lib.tests import utils as osc_utils + +from saharaclient.api import cluster_templates as api_ct +from saharaclient.api import clusters as api_cl +from saharaclient.api import images as api_img +from saharaclient.api import node_group_templates as api_ngt +from saharaclient.osc.v2 import clusters as osc_cl +from saharaclient.tests.unit.osc.v1 import test_clusters as tc_v1 + +CLUSTER_INFO = { + "description": "Cluster template for tests", + + "use_autoconfig": True, + "is_default": False, + "node_groups": [ + { + "count": 2, + "id": "ng_id", + "name": "fakeng", + "plugin_name": 'fake', + "plugin_version": '0.1', + "node_group_template_id": 'ngt_id' + } + ], + "plugin_version": "0.1", + "is_public": False, + "plugin_name": "fake", + "id": "cluster_id", + "anti_affinity": [], + "name": "fake", + "is_protected": False, + "cluster_template_id": "ct_id", + "neutron_management_network": "net_id", + "user_keypair_id": "test", + "status": 'Active', + "default_image_id": "img_id", + 'verification': { + 'status': 'GREEN', + 'id': 'ver_id', + 'cluster_id': 'cluster_id', + 'checks': [ + { + 'status': 'GREEN', + 'name': 'Some check' + } + ] + } +} + +CT_INFO = { + "plugin_name": "fake", + "plugin_version": "0.1", + "name": '"template', + "id": "ct_id" +} + +NGT_INFO = { + 'id': 'ngt_id', + 'name': 'fakeng' +} + + +class TestClusters(tc_v1.TestClusters): + def setUp(self): + super(TestClusters, self).setUp() + self.app.api_version['data_processing'] = '2' + self.cl_mock = ( + self.app.client_manager.data_processing.clusters) + self.ngt_mock = ( + self.app.client_manager.data_processing.node_group_templates) + self.ct_mock = ( + self.app.client_manager.data_processing.cluster_templates) + self.img_mock = ( + self.app.client_manager.data_processing.images) + self.cl_mock.reset_mock() + self.ngt_mock.reset_mock() + self.ct_mock.reset_mock() + self.img_mock.reset_mock() + + +class TestCreateCluster(TestClusters): + # TODO(apavlov): check for creation with --json + def setUp(self): + super(TestCreateCluster, self).setUp() + self.cl_mock.create.return_value = api_cl.Cluster( + None, CLUSTER_INFO) + self.cl_mock.find_unique.return_value = api_cl.Cluster( + None, CLUSTER_INFO) + self.ct_mock.find_unique.return_value = api_ct.ClusterTemplate( + None, CT_INFO) + self.img_mock.find_unique.return_value = api_img.Image( + None, {'id': 'img_id'}) + self.net_mock = self.app.client_manager.network + self.net_mock.find_network.return_value = mock.Mock(id='net_id') + self.net_mock.reset_mock() + + # Command to test + self.cmd = osc_cl.CreateCluster(self.app, None) + + def test_cluster_create_minimum_options(self): + arglist = ['--name', 'fake', '--cluster-template', 'template', + '--image', 'ubuntu'] + verifylist = [('name', 'fake'), ('cluster_template', 'template'), + ('image', 'ubuntu')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.create.assert_called_once_with( + cluster_template_id='ct_id', count=None, default_image_id='img_id', + description=None, plugin_version='0.1', is_protected=False, + is_public=False, is_transient=False, name='fake', net_id=None, + plugin_name='fake', user_keypair_id=None) + + def test_cluster_create_all_options(self): + arglist = ['--name', 'fake', '--cluster-template', 'template', + '--image', 'ubuntu', '--user-keypair', 'test', + '--neutron-network', 'net', '--description', 'descr', + '--transient', '--public', '--protected'] + + verifylist = [('name', 'fake'), ('cluster_template', 'template'), + ('image', 'ubuntu'), ('user_keypair', 'test'), + ('neutron_network', 'net'), ('description', 'descr'), + ('transient', True), ('public', True), + ('protected', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.create.assert_called_once_with( + cluster_template_id='ct_id', count=None, default_image_id='img_id', + description='descr', plugin_version='0.1', is_protected=True, + is_public=True, is_transient=True, name='fake', net_id='net_id', + plugin_name='fake', user_keypair_id='test') + + # Check that columns are correct + expected_columns = ('Anti affinity', 'Cluster template id', + 'Description', 'Id', 'Image', + 'Is protected', 'Is public', 'Name', + 'Neutron management network', 'Node groups', + 'Plugin name', 'Plugin version', 'Status', + 'Use autoconfig', 'User keypair id') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('', 'ct_id', 'Cluster template for tests', + 'cluster_id', 'img_id', False, False, 'fake', + 'net_id', 'fakeng:2', 'fake', '0.1', 'Active', True, + 'test') + self.assertEqual(expected_data, data) + + def test_cluster_create_with_count(self): + clusters_mock = mock.Mock() + clusters_mock.to_dict.return_value = { + 'clusters': [{'cluster': {'id': 'cluster1_id'}}, + {'cluster': {'id': 'cluster2_id'}}] + } + self.cl_mock.create.return_value = clusters_mock + + arglist = ['--name', 'fake', '--cluster-template', 'template', + '--image', 'ubuntu', '--count', '2'] + verifylist = [('name', 'fake'), ('cluster_template', 'template'), + ('image', 'ubuntu'), ('count', 2)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.create.assert_called_once_with( + cluster_template_id='ct_id', count=2, default_image_id='img_id', + description=None, plugin_version='0.1', is_protected=False, + is_public=False, is_transient=False, name='fake', net_id=None, + plugin_name='fake', user_keypair_id=None) + + # Check that columns are correct + expected_columns = ('fake',) + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('cluster_id',) + self.assertEqual(expected_data, data) + + +class TestListClusters(TestClusters): + def setUp(self): + super(TestListClusters, self).setUp() + self.cl_mock.list.return_value = [api_cl.Cluster( + None, CLUSTER_INFO)] + + # Command to test + self.cmd = osc_cl.ListClusters(self.app, None) + + def test_clusters_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', 'Id', 'Plugin name', 'Plugin version', + 'Status'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('fake', 'cluster_id', 'fake', '0.1', 'Active')] + self.assertEqual(expected_data, list(data)) + + def test_clusters_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', 'Id', 'Plugin name', 'Plugin version', + 'Status', 'Description', 'Image'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('fake', 'cluster_id', 'fake', '0.1', 'Active', + 'Cluster template for tests', 'img_id')] + self.assertEqual(expected_data, list(data)) + + def test_clusters_list_extra_search_opts(self): + arglist = ['--plugin', 'fake', '--plugin-version', '0.1', '--name', + 'fake'] + verifylist = [('plugin', 'fake'), ('plugin_version', '0.1'), + ('name', 'fake')] + + 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', 'Id', 'Plugin name', 'Plugin version', + 'Status'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('fake', 'cluster_id', 'fake', '0.1', 'Active')] + self.assertEqual(expected_data, list(data)) + + +class TestShowCluster(TestClusters): + def setUp(self): + super(TestShowCluster, self).setUp() + self.cl_mock.find_unique.return_value = api_cl.Cluster( + None, CLUSTER_INFO) + + # Command to test + self.cmd = osc_cl.ShowCluster(self.app, None) + + def test_cluster_show(self): + arglist = ['fake'] + verifylist = [('cluster', 'fake')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.find_unique.assert_called_once_with(name='fake') + + # Check that columns are correct + expected_columns = ('Anti affinity', 'Cluster template id', + 'Description', 'Id', 'Image', + 'Is protected', 'Is public', 'Name', + 'Neutron management network', 'Node groups', + 'Plugin name', 'Plugin version', 'Status', + 'Use autoconfig', 'User keypair id') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('', 'ct_id', 'Cluster template for tests', + 'cluster_id', 'img_id', False, False, 'fake', + 'net_id', 'fakeng:2', 'fake', '0.1', 'Active', True, + 'test') + self.assertEqual(expected_data, data) + + def test_cluster_show_verification(self): + arglist = ['fake', '--verification'] + verifylist = [('cluster', 'fake'), ('verification', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.find_unique.assert_called_once_with(name='fake') + + # Check that columns are correct + expected_columns = ('Anti affinity', 'Cluster template id', + 'Description', 'Health check (some check)', 'Id', + 'Image', 'Is protected', 'Is public', 'Name', + 'Neutron management network', 'Node groups', + 'Plugin name', 'Plugin version', 'Status', + 'Use autoconfig', 'User keypair id', + 'Verification status') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('', 'ct_id', 'Cluster template for tests', 'GREEN', + 'cluster_id', 'img_id', False, False, 'fake', + 'net_id', 'fakeng:2', 'fake', '0.1', 'Active', True, + 'test', 'GREEN') + self.assertEqual(expected_data, data) + + +class TestDeleteCluster(TestClusters): + def setUp(self): + super(TestDeleteCluster, self).setUp() + self.cl_mock.find_unique.return_value = api_cl.Cluster( + None, CLUSTER_INFO) + + # Command to test + self.cmd = osc_cl.DeleteCluster(self.app, None) + + def test_cluster_delete(self): + arglist = ['fake'] + verifylist = [('cluster', ['fake'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.delete.assert_called_once_with('cluster_id') + + +class TestUpdateCluster(TestClusters): + def setUp(self): + super(TestUpdateCluster, self).setUp() + self.cl_mock.update.return_value = mock.Mock( + cluster=CLUSTER_INFO.copy()) + self.cl_mock.find_unique.return_value = api_cl.Cluster( + None, CLUSTER_INFO) + + # Command to test + self.cmd = osc_cl.UpdateCluster(self.app, None) + + def test_cluster_update_no_options(self): + arglist = [] + verifylist = [] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_cluster_update_nothing_updated(self): + arglist = ['fake'] + + verifylist = [('cluster', 'fake')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.update.assert_called_once_with('cluster_id') + + def test_cluster_update_all_options(self): + arglist = ['fake', '--name', 'fake', '--description', 'descr', + '--public', '--protected'] + + verifylist = [('cluster', 'fake'), ('name', 'fake'), + ('description', 'descr'), ('is_public', True), + ('is_protected', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.update.assert_called_once_with( + 'cluster_id', description='descr', is_protected=True, + is_public=True, name='fake') + + # Check that columns are correct + expected_columns = ('Anti affinity', 'Cluster template id', + 'Description', 'Id', 'Image', + 'Is protected', 'Is public', 'Name', + 'Neutron management network', 'Node groups', + 'Plugin name', 'Plugin version', 'Status', + 'Use autoconfig', 'User keypair id') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('', 'ct_id', 'Cluster template for tests', + 'cluster_id', 'img_id', False, False, 'fake', + 'net_id', 'fakeng:2', 'fake', '0.1', 'Active', True, + 'test') + self.assertEqual(expected_data, data) + + def test_cluster_update_private_unprotected(self): + arglist = ['fake', '--private', '--unprotected'] + + verifylist = [('cluster', 'fake'), ('is_public', False), + ('is_protected', False)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.update.assert_called_once_with( + 'cluster_id', is_protected=False, is_public=False) + + +class TestScaleCluster(TestClusters): + def setUp(self): + super(TestScaleCluster, self).setUp() + self.cl_mock.scale.return_value = mock.Mock( + cluster=CLUSTER_INFO.copy()) + self.cl_mock.find_unique.return_value = api_cl.Cluster( + None, CLUSTER_INFO) + + # Command to test + self.cmd = osc_cl.ScaleCluster(self.app, None) + + def test_cluster_scale_no_options(self): + arglist = [] + verifylist = [] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_cluster_scale_resize(self): + self.ngt_mock.find_unique.return_value = api_ngt.NodeGroupTemplate( + None, NGT_INFO) + arglist = ['fake', '--instances', 'fakeng:1'] + + verifylist = [('cluster', 'fake'), + ('instances', ['fakeng:1'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.scale.assert_called_once_with( + 'cluster_id', + {'resize_node_groups': [ + {'count': 1, + 'name': 'fakeng'}]} + ) + + # Check that columns are correct + expected_columns = ('Anti affinity', 'Cluster template id', + 'Description', 'Id', 'Image', + 'Is protected', 'Is public', 'Name', + 'Neutron management network', 'Node groups', + 'Plugin name', 'Plugin version', 'Status', + 'Use autoconfig', 'User keypair id') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('', 'ct_id', 'Cluster template for tests', + 'cluster_id', 'img_id', False, False, 'fake', + 'net_id', 'fakeng:2', 'fake', '0.1', 'Active', True, + 'test') + self.assertEqual(expected_data, data) + + def test_cluster_scale_add_ng(self): + new_ng = {'name': 'new', 'id': 'new_id'} + self.ngt_mock.find_unique.return_value = api_ngt.NodeGroupTemplate( + None, new_ng) + arglist = ['fake', '--instances', 'new:1'] + + verifylist = [('cluster', 'fake'), ('instances', ['new:1'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.scale.assert_called_once_with( + 'cluster_id', + {'add_node_groups': [ + {'count': 1, + 'node_group_template_id': 'new_id', + 'name': 'new'} + ]}) + + +class TestVerificationUpdateCluster(TestClusters): + def setUp(self): + super(TestVerificationUpdateCluster, self).setUp() + self.cl_mock.find_unique.return_value = api_cl.Cluster( + None, CLUSTER_INFO) + self.cl_mock.verification_update.return_value = api_cl.Cluster( + None, CLUSTER_INFO) + + # Command to test + self.cmd = osc_cl.VerificationUpdateCluster(self.app, None) + + def test_verification_show(self): + arglist = ['fake', '--show'] + verifylist = [('cluster', 'fake'), ('show', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.find_unique.assert_called_once_with(name='fake') + + # Check that columns are correct + expected_columns = ('Health check (some check)', 'Verification status') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('GREEN', 'GREEN') + self.assertEqual(expected_data, data) + + def test_verification_start(self): + arglist = ['fake', '--start'] + verifylist = [('cluster', 'fake'), ('status', 'START')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.cl_mock.verification_update.assert_called_once_with( + 'cluster_id', 'START') diff --git a/saharaclient/tests/unit/osc/v2/test_data_sources.py b/saharaclient/tests/unit/osc/v2/test_data_sources.py new file mode 100644 index 0000000..27af0b5 --- /dev/null +++ b/saharaclient/tests/unit/osc/v2/test_data_sources.py @@ -0,0 +1,309 @@ +# 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 osc_lib.tests import utils as osc_utils + +from saharaclient.api import data_sources as api_ds +from saharaclient.osc.v1 import data_sources as osc_ds +from saharaclient.tests.unit.osc.v1 import test_data_sources as tds_v1 + +DS_INFO = {'id': 'id', 'name': 'source', 'type': 'swift', + 'url': 'swift://container.sahara/object', + 'description': 'Data Source for tests', + 'is_public': True, 'is_protected': True} + + +class TestDataSources(tds_v1.TestDataSources): + def setUp(self): + super(TestDataSources, self).setUp() + self.app.api_version['data_processing'] = '2' + self.ds_mock = ( + self.app.client_manager.data_processing.data_sources) + self.ds_mock.reset_mock() + + +class TestCreateDataSource(TestDataSources): + def setUp(self): + super(TestCreateDataSource, self).setUp() + self.ds_mock.create.return_value = api_ds.DataSources( + None, DS_INFO) + + # Command to test + self.cmd = osc_ds.CreateDataSource(self.app, None) + + def test_data_sources_create_no_options(self): + arglist = [] + verifylist = [] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_data_sources_create_required_options(self): + arglist = ['source', '--type', 'swift', '--url', + 'swift://container.sahara/object'] + verifylist = [('name', 'source'), ('type', 'swift'), + ('url', 'swift://container.sahara/object')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that data source was created with correct arguments + called_args = {'credential_pass': None, 'credential_user': None, + 'data_source_type': 'swift', 'name': 'source', + 'description': '', + 'url': 'swift://container.sahara/object', + 'is_public': False, 'is_protected': False, + 's3_credentials': None} + self.ds_mock.create.assert_called_once_with(**called_args) + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Name', 'Type', 'Url') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('Data Source for tests', 'id', True, True, 'source', + 'swift', 'swift://container.sahara/object') + self.assertEqual(expected_data, data) + + def test_data_sources_create_all_options(self): + arglist = ['source', '--type', 'swift', '--url', + 'swift://container.sahara/object', '--username', 'user', + '--password', 'pass', '--description', + 'Data Source for tests', '--public', '--protected'] + verifylist = [('name', 'source'), ('type', 'swift'), + ('url', 'swift://container.sahara/object'), + ('username', 'user'), ('password', 'pass'), + ('description', 'Data Source for tests'), + ('public', True), ('protected', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that data source was created with correct arguments + called_args = {'credential_pass': 'pass', 'credential_user': 'user', + 'data_source_type': 'swift', 'name': 'source', + 'description': 'Data Source for tests', + 'url': 'swift://container.sahara/object', + 'is_protected': True, 'is_public': True, + 's3_credentials': None} + self.ds_mock.create.assert_called_once_with(**called_args) + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Name', 'Type', 'Url') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('Data Source for tests', 'id', True, True, 'source', + 'swift', 'swift://container.sahara/object') + self.assertEqual(expected_data, data) + + +class TestListDataSources(TestDataSources): + def setUp(self): + super(TestListDataSources, self).setUp() + self.ds_mock.list.return_value = [api_ds.DataSources( + None, DS_INFO)] + + # Command to test + self.cmd = osc_ds.ListDataSources(self.app, None) + + def test_data_sources_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', 'Id', 'Type'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('source', 'id', 'swift')] + self.assertEqual(expected_data, list(data)) + + def test_data_sources_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', 'Id', 'Type', 'Url', 'Description', + 'Is public', 'Is protected'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('source', 'id', 'swift', + 'swift://container.sahara/object', + 'Data Source for tests', True, True)] + self.assertEqual(expected_data, list(data)) + + +class TestShowDataSource(TestDataSources): + def setUp(self): + super(TestShowDataSource, self).setUp() + self.ds_mock.find_unique.return_value = api_ds.DataSources( + None, DS_INFO) + + # Command to test + self.cmd = osc_ds.ShowDataSource(self.app, None) + + def test_data_sources_show(self): + arglist = ['source'] + verifylist = [('data_source', 'source')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments was passed + self.ds_mock.find_unique.assert_called_once_with(name='source') + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Name', 'Type', 'Url') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ['Data Source for tests', 'id', True, True, 'source', + 'swift', 'swift://container.sahara/object'] + self.assertEqual(expected_data, list(data)) + + +class TestDeleteDataSource(TestDataSources): + def setUp(self): + super(TestDeleteDataSource, self).setUp() + self.ds_mock.find_unique.return_value = api_ds.DataSources( + None, DS_INFO) + + # Command to test + self.cmd = osc_ds.DeleteDataSource(self.app, None) + + def test_data_sources_delete(self): + arglist = ['source'] + verifylist = [('data_source', ['source'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments was passed + self.ds_mock.delete.assert_called_once_with('id') + + +class TestUpdateDataSource(TestDataSources): + def setUp(self): + super(TestUpdateDataSource, self).setUp() + self.ds_mock.find_unique.return_value = api_ds.DataSources( + None, DS_INFO) + self.ds_mock.update.return_value = mock.Mock( + data_source=DS_INFO) + + # Command to test + self.cmd = osc_ds.UpdateDataSource(self.app, None) + + def test_data_sources_update_no_options(self): + arglist = [] + verifylist = [] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_data_sources_update_nothing_updated(self): + arglist = ['source'] + verifylist = [('data_source', 'source')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + self.ds_mock.update.assert_called_once_with('id', {}) + + def test_data_sources_update_required_options(self): + arglist = ['source'] + verifylist = [('data_source', 'source')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that data source was created with correct arguments + self.ds_mock.update.assert_called_once_with('id', {}) + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Name', 'Type', 'Url') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('Data Source for tests', 'id', True, True, 'source', + 'swift', 'swift://container.sahara/object') + self.assertEqual(expected_data, data) + + def test_data_sources_update_all_options(self): + arglist = ['source', '--name', 'source', '--type', 'swift', '--url', + 'swift://container.sahara/object', '--username', 'user', + '--password', 'pass', '--description', + 'Data Source for tests', '--public', '--protected'] + verifylist = [('data_source', 'source'), ('name', 'source'), + ('type', 'swift'), + ('url', 'swift://container.sahara/object'), + ('username', 'user'), ('password', 'pass'), + ('description', 'Data Source for tests'), + ('is_public', True), ('is_protected', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that data source was created with correct arguments + self.ds_mock.update.assert_called_once_with( + 'id', {'name': 'source', 'url': 'swift://container.sahara/object', + 'is_protected': True, + 'credentials': {'password': 'pass', 'user': 'user'}, + 'is_public': True, 'type': 'swift', + 'description': 'Data Source for tests'}) + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Name', 'Type', 'Url') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('Data Source for tests', 'id', True, True, 'source', + 'swift', 'swift://container.sahara/object') + self.assertEqual(expected_data, data) + + def test_data_sources_update_private_unprotected(self): + arglist = ['source', '--private', '--unprotected'] + verifylist = [('data_source', 'source'), ('is_public', False), + ('is_protected', False)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that data source was created with correct arguments + self.ds_mock.update.assert_called_once_with( + 'id', {'is_public': False, 'is_protected': False}) diff --git a/saharaclient/tests/unit/osc/v2/test_images.py b/saharaclient/tests/unit/osc/v2/test_images.py new file mode 100644 index 0000000..0a1669d --- /dev/null +++ b/saharaclient/tests/unit/osc/v2/test_images.py @@ -0,0 +1,367 @@ +# 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 osc_lib.tests import utils as osc_utils + +from saharaclient.api import images as api_images +from saharaclient.osc.v1 import images as osc_images +from saharaclient.tests.unit.osc.v1 import test_images as images_v1 + + +IMAGE_INFO = {'id': 'id', 'name': 'image', 'username': 'ubuntu', + 'status': "Active", 'tags': ['fake', '0.1'], + 'description': 'Image for tests'} + + +class TestImages(images_v1.TestImages): + def setUp(self): + super(TestImages, self).setUp() + self.app.api_version['data_processing'] = '2' + self.image_mock = ( + self.app.client_manager.data_processing.images) + self.image_mock.reset_mock() + + +class TestListImages(TestImages): + def setUp(self): + super(TestListImages, self).setUp() + self.image_mock.list.return_value = [api_images.Image( + None, IMAGE_INFO)] + + # Command to test + self.cmd = osc_images.ListImages(self.app, None) + + def test_images_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', 'Id', 'Username', 'Tags'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('image', 'id', 'ubuntu', '0.1, fake')] + self.assertEqual(expected_data, list(data)) + + def test_images_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', 'Id', 'Username', 'Tags', 'Status', + 'Description'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('image', 'id', 'ubuntu', '0.1, fake', 'Active', + 'Image for tests')] + self.assertEqual(expected_data, list(data)) + + def test_images_list_successful_selection(self): + arglist = ['--name', 'image', '--tags', 'fake', '--username', 'ubuntu'] + verifylist = [('name', 'image'), ('tags', ['fake']), + ('username', 'ubuntu')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.image_mock.list.assert_called_once_with( + search_opts={'tags': ['fake']}) + + # Check that columns are correct + expected_columns = ['Name', 'Id', 'Username', 'Tags'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('image', 'id', 'ubuntu', '0.1, fake')] + self.assertEqual(expected_data, list(data)) + + def test_images_list_with_name_nothing_selected(self): + arglist = ['--name', 'img'] + verifylist = [('name', 'img')] + + 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', 'Id', 'Username', 'Tags'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [] + self.assertEqual(expected_data, list(data)) + + def test_images_list_with_username_nothing_selected(self): + arglist = ['--username', 'fedora'] + verifylist = [('username', 'fedora')] + + 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', 'Id', 'Username', 'Tags'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [] + self.assertEqual(expected_data, list(data)) + + +class TestShowImage(TestImages): + def setUp(self): + super(TestShowImage, self).setUp() + self.image_mock.find_unique.return_value = api_images.Image( + None, IMAGE_INFO) + + # Command to test + self.cmd = osc_images.ShowImage(self.app, None) + + def test_image_show_no_options(self): + arglist = [] + verifylist = [] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_image_show(self): + arglist = ['image'] + verifylist = [('image', 'image')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.image_mock.find_unique.assert_called_once_with(name='image') + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Name', 'Status', 'Tags', + 'Username') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ['Image for tests', 'id', 'image', 'Active', + '0.1, fake', 'ubuntu'] + self.assertEqual(expected_data, list(data)) + + +class TestRegisterImage(TestImages): + def setUp(self): + super(TestRegisterImage, self).setUp() + self.image_mock.update_image.return_value = mock.Mock( + image=IMAGE_INFO.copy()) + self.app.client_manager.image = mock.Mock() + self.image_client = self.app.client_manager.image.images + self.image_client.get.return_value = mock.Mock(id='id') + + # Command to test + self.cmd = osc_images.RegisterImage(self.app, None) + + def test_image_register_without_username(self): + arglist = ['id'] + verifylist = [('image', 'id')] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_image_register_required_options(self): + arglist = ['id', '--username', 'ubuntu'] + verifylist = [('image', 'id'), ('username', 'ubuntu')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.image_mock.update_image.assert_called_once_with( + 'id', desc=None, user_name='ubuntu') + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Name', 'Status', 'Tags', + 'Username') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ['Image for tests', 'id', 'image', 'Active', + '0.1, fake', 'ubuntu'] + self.assertEqual(expected_data, list(data)) + + def test_image_register_all_options(self): + arglist = ['id', '--username', 'ubuntu', '--description', 'descr'] + verifylist = [('image', 'id'), ('username', 'ubuntu'), + ('description', 'descr')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.image_mock.update_image.assert_called_once_with( + 'id', desc='descr', user_name='ubuntu') + + +class TestUnregisterImage(TestImages): + def setUp(self): + super(TestUnregisterImage, self).setUp() + self.image_mock.find_unique.return_value = api_images.Image( + None, IMAGE_INFO) + + # Command to test + self.cmd = osc_images.UnregisterImage(self.app, None) + + def test_image_unregister_no_options(self): + arglist = [] + verifylist = [] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_image_unregister(self): + arglist = ['image'] + verifylist = [('image', ['image'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.image_mock.find_unique.assert_called_once_with(name='image') + self.image_mock.unregister_image.assert_called_once_with('id') + + +class TestSetImageTags(TestImages): + def setUp(self): + super(TestSetImageTags, self).setUp() + image_info = IMAGE_INFO.copy() + image_info['tags'] = [] + self.image_mock.find_unique.return_value = api_images.Image( + None, image_info) + self.image_mock.update_tags.return_value = api_images.Image( + None, image_info) + + # Command to test + self.cmd = osc_images.SetImageTags(self.app, None) + + def test_image_tags_set_without_tags(self): + arglist = ['id'] + verifylist = [('image', 'id')] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_image_tags_set(self): + arglist = ['image', '--tags', 'fake', '0.1'] + verifylist = [('image', 'image'), ('tags', ['fake', '0.1'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.image_mock.find_unique.assert_called_with(name='image') + self.image_mock.update_tags.assert_called_once_with( + 'id', ['fake', '0.1']) + + +class TestAddImageTags(TestImages): + def setUp(self): + super(TestAddImageTags, self).setUp() + image_info = IMAGE_INFO.copy() + image_info['tags'] = [] + self.image_mock.update_tags.return_value = api_images.Image( + None, image_info) + self.image_mock.find_unique.return_value = api_images.Image( + None, image_info) + + # Command to test + self.cmd = osc_images.AddImageTags(self.app, None) + + def test_image_tags_add_without_tags(self): + arglist = ['id'] + verifylist = [('image', 'id')] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_image_tags_add(self): + arglist = ['image', '--tags', 'fake'] + verifylist = [('image', 'image'), ('tags', ['fake'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.image_mock.find_unique.assert_called_with(name='image') + self.image_mock.update_tags.assert_called_once_with( + 'id', ['fake']) + + +class TestRemoveImageTags(TestImages): + def setUp(self): + super(TestRemoveImageTags, self).setUp() + self.image_mock.update_tags.return_value = api_images.Image( + None, IMAGE_INFO) + self.image_mock.find_unique.return_value = api_images.Image( + None, IMAGE_INFO) + + # Command to test + self.cmd = osc_images.RemoveImageTags(self.app, None) + + def test_image_tags_remove_both_options(self): + arglist = ['id', '--all', '--tags', 'fake'] + verifylist = [('image', 'id'), ('all', True), ('tags', ['fake'])] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_image_tags_remove(self): + arglist = ['image', '--tags', 'fake'] + verifylist = [('image', 'image'), ('tags', ['fake'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.image_mock.find_unique.assert_called_with(name='image') + self.image_mock.update_tags.assert_called_once_with( + 'id', ['0.1']) + + def test_image_tags_remove_all(self): + arglist = ['image', '--all'] + verifylist = [('image', 'image'), ('all', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.image_mock.find_unique.assert_called_with(name='image') + self.image_mock.update_tags.assert_called_once_with( + 'id', []) diff --git a/saharaclient/tests/unit/osc/v2/test_job_binaries.py b/saharaclient/tests/unit/osc/v2/test_job_binaries.py new file mode 100644 index 0000000..ee94e83 --- /dev/null +++ b/saharaclient/tests/unit/osc/v2/test_job_binaries.py @@ -0,0 +1,326 @@ +# 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 osc_lib.tests import utils as osc_u +import testtools + +from saharaclient.api import job_binaries as api_jb +from saharaclient.osc.v1 import job_binaries as osc_jb +from saharaclient.tests.unit.osc.v1 import test_job_binaries as tjb_v1 + + +JOB_BINARY_INFO = { + "name": 'job-binary', + "description": 'descr', + "id": 'jb_id', + "is_protected": False, + "is_public": False, + "url": 'swift://cont/test' +} + + +class TestJobBinaries(tjb_v1.TestJobBinaries): + def setUp(self): + super(TestJobBinaries, self).setUp() + self.app.api_version['data_processing'] = '2' + self.jb_mock = self.app.client_manager.data_processing.job_binaries + self.jb_mock.reset_mock() + + +class TestCreateJobBinary(TestJobBinaries): + # TODO(apavlov): check for creation with --json + def setUp(self): + super(TestCreateJobBinary, self).setUp() + self.jb_mock.create.return_value = api_jb.JobBinaries( + None, JOB_BINARY_INFO) + self.jbi_mock = (self.app.client_manager. + data_processing.job_binary_internals) + self.jbi_mock.create.return_value = mock.Mock(id='jbi_id') + self.jbi_mock.reset_mock() + + # Command to test + self.cmd = osc_jb.CreateJobBinary(self.app, None) + + def test_job_binary_create_swift_public_protected(self): + arglist = ['--name', 'job-binary', '--url', 'swift://cont/test', + '--username', 'user', '--password', 'pass', '--public', + '--protected'] + verifylist = [('name', 'job-binary'), ('url', 'swift://cont/test'), + ('username', 'user'), ('password', 'pass'), + ('public', True), ('protected', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.jb_mock.create.assert_called_once_with( + description=None, extra={'password': 'pass', 'user': 'user'}, + is_protected=True, is_public=True, name='job-binary', + url='swift://cont/test') + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Name', 'Url') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('descr', 'jb_id', False, False, 'job-binary', + 'swift://cont/test') + self.assertEqual(expected_data, data) + + def test_job_binary_create_mutual_exclusion(self): + arglist = ['job-binary', '--name', 'job-binary', '--access-key', 'ak', + '--secret-key', 'sk', '--url', 's3://abc/def', + '--password', 'pw'] + + with testtools.ExpectedException(osc_u.ParserException): + self.check_parser(self.cmd, arglist, mock.Mock()) + + +class TestListJobBinaries(TestJobBinaries): + def setUp(self): + super(TestListJobBinaries, self).setUp() + self.jb_mock.list.return_value = [api_jb.JobBinaries( + None, JOB_BINARY_INFO)] + + # Command to test + self.cmd = osc_jb.ListJobBinaries(self.app, None) + + def test_job_binary_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', 'Id', 'Url'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('job-binary', 'jb_id', 'swift://cont/test')] + self.assertEqual(expected_data, list(data)) + + def test_job_binary_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', 'Id', 'Url', 'Description', 'Is public', + 'Is protected'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('job-binary', 'jb_id', 'swift://cont/test', 'descr', + False, False)] + self.assertEqual(expected_data, list(data)) + + def test_job_binary_list_extra_search_opts(self): + arglist = ['--name', 'bin'] + verifylist = [('name', 'bin')] + + 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', 'Id', 'Url'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('job-binary', 'jb_id', 'swift://cont/test')] + self.assertEqual(expected_data, list(data)) + + +class TestShowJobBinary(TestJobBinaries): + def setUp(self): + super(TestShowJobBinary, self).setUp() + self.jb_mock.find_unique.return_value = api_jb.JobBinaries( + None, JOB_BINARY_INFO) + + # Command to test + self.cmd = osc_jb.ShowJobBinary(self.app, None) + + def test_job_binary_show(self): + arglist = ['job-binary'] + verifylist = [('job_binary', 'job-binary')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.jb_mock.find_unique.assert_called_once_with(name='job-binary') + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Name', 'Url') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('descr', 'jb_id', False, False, 'job-binary', + 'swift://cont/test') + self.assertEqual(expected_data, data) + + +class TestDeleteJobBinary(TestJobBinaries): + def setUp(self): + super(TestDeleteJobBinary, self).setUp() + self.jb_mock.find_unique.return_value = api_jb.JobBinaries( + None, JOB_BINARY_INFO) + + # Command to test + self.cmd = osc_jb.DeleteJobBinary(self.app, None) + + def test_job_binary_delete(self): + arglist = ['job-binary'] + verifylist = [('job_binary', ['job-binary'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.jb_mock.delete.assert_called_once_with('jb_id') + + +class TestUpdateJobBinary(TestJobBinaries): + def setUp(self): + super(TestUpdateJobBinary, self).setUp() + self.jb_mock.find_unique.return_value = api_jb.JobBinaries( + None, JOB_BINARY_INFO) + self.jb_mock.update.return_value = api_jb.JobBinaries( + None, JOB_BINARY_INFO) + + # Command to test + self.cmd = osc_jb.UpdateJobBinary(self.app, None) + + def test_job_binary_update_all_options(self): + arglist = ['job-binary', '--name', 'job-binary', '--description', + 'descr', '--username', 'user', '--password', 'pass', + '--public', '--protected'] + + verifylist = [('job_binary', 'job-binary'), ('name', 'job-binary'), + ('description', 'descr'), ('username', 'user'), + ('password', 'pass'), ('is_public', True), + ('is_protected', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.jb_mock.update.assert_called_once_with( + 'jb_id', + {'is_public': True, 'description': 'descr', 'is_protected': True, + 'name': 'job-binary', + 'extra': {'password': 'pass', 'user': 'user'}}) + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Name', 'Url') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('descr', 'jb_id', False, False, 'job-binary', + 'swift://cont/test') + self.assertEqual(expected_data, data) + + def test_job_binary_update_private_unprotected(self): + arglist = ['job-binary', '--private', '--unprotected'] + + verifylist = [('job_binary', 'job-binary'), ('is_public', False), + ('is_protected', False)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.jb_mock.update.assert_called_once_with( + 'jb_id', {'is_public': False, 'is_protected': False}) + + def test_job_binary_update_nothing_updated(self): + arglist = ['job-binary'] + + verifylist = [('job_binary', 'job-binary')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.jb_mock.update.assert_called_once_with( + 'jb_id', {}) + + def test_job_binary_update_mutual_exclusion(self): + arglist = ['job-binary', '--name', 'job-binary', '--access-key', 'ak', + '--secret-key', 'sk', '--url', 's3://abc/def', + '--password', 'pw'] + + with testtools.ExpectedException(osc_u.ParserException): + self.check_parser(self.cmd, arglist, mock.Mock()) + + +class TestDownloadJobBinary(TestJobBinaries): + def setUp(self): + super(TestDownloadJobBinary, self).setUp() + self.jb_mock.get_file.return_value = 'data' + self.jb_mock.find_unique.return_value = api_jb.JobBinaries( + None, JOB_BINARY_INFO) + + # Command to test + self.cmd = osc_jb.DownloadJobBinary(self.app, None) + + def test_download_job_binary_default_file(self): + m_open = mock.mock_open() + with mock.patch('six.moves.builtins.open', m_open, create=True): + arglist = ['job-binary'] + verifylist = [('job_binary', 'job-binary')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments was passed + self.jb_mock.get_file.assert_called_once_with( + 'jb_id') + + # Check that data will be saved to the right file + self.assertEqual('job-binary', m_open.call_args[0][0]) + + def test_download_job_binary_specified_file(self): + m_open = mock.mock_open() + with mock.patch('six.moves.builtins.open', m_open, create=True): + arglist = ['job-binary', '--file', 'test'] + verifylist = [('job_binary', 'job-binary'), ('file', 'test')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments was passed + self.jb_mock.get_file.assert_called_once_with( + 'jb_id') + + # Check that data will be saved to the right file + self.assertEqual('test', m_open.call_args[0][0]) diff --git a/saharaclient/tests/unit/osc/v2/test_job_templates.py b/saharaclient/tests/unit/osc/v2/test_job_templates.py new file mode 100644 index 0000000..4060fb3 --- /dev/null +++ b/saharaclient/tests/unit/osc/v2/test_job_templates.py @@ -0,0 +1,292 @@ +# 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 osc_lib.tests import utils as osc_utils + +from saharaclient.api.v2 import job_templates as api_j +from saharaclient.osc.v2 import job_templates as osc_j +from saharaclient.tests.unit.osc.v1 import test_job_templates as tjt_v1 + +JOB_TEMPLATE_INFO = { + "is_public": False, + "id": "job_id", + "name": "pig-job", + "description": "Job for test", + "interface": [], + "libs": [ + { + "id": "lib_id", + "name": "lib" + } + ], + "type": "Pig", + "is_protected": False, + "mains": [ + { + "id": "main_id", + "name": "main" + } + ] +} + + +class TestJobTemplates(tjt_v1.TestJobTemplates): + def setUp(self): + super(TestJobTemplates, self).setUp() + self.app.api_version['data_processing'] = '2' + self.job_mock = self.app.client_manager.data_processing.job_templates + self.job_mock.reset_mock() + + +class TestCreateJobTemplate(TestJobTemplates): + # TODO(apavlov): check for creation with --interface + def setUp(self): + super(TestCreateJobTemplate, self).setUp() + self.job_mock.create.return_value = api_j.JobTemplate( + None, JOB_TEMPLATE_INFO) + self.jb_mock = self.app.client_manager.data_processing.job_binaries + self.jb_mock.find_unique.return_value = mock.Mock(id='jb_id') + self.jb_mock.reset_mock() + + # Command to test + self.cmd = osc_j.CreateJobTemplate(self.app, None) + + def test_job_template_create_minimum_options(self): + arglist = ['--name', 'pig-job', '--type', 'Pig'] + verifylist = [('name', 'pig-job'), ('type', 'Pig')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.job_mock.create.assert_called_once_with( + description=None, interface=None, is_protected=False, + is_public=False, libs=None, mains=None, name='pig-job', type='Pig') + + def test_job_template_create_all_options(self): + arglist = ['--name', 'pig-job', '--type', 'Pig', '--mains', 'main', + '--libs', 'lib', '--description', 'descr', '--public', + '--protected'] + + verifylist = [('name', 'pig-job'), ('type', 'Pig'), + ('mains', ['main']), ('libs', ['lib']), + ('description', 'descr'), ('public', True), + ('protected', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.job_mock.create.assert_called_once_with( + description='descr', interface=None, is_protected=True, + is_public=True, libs=['jb_id'], mains=['jb_id'], name='pig-job', + type='Pig') + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Libs', 'Mains', 'Name', 'Type') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('Job for test', 'job_id', False, False, 'lib:lib_id', + 'main:main_id', 'pig-job', 'Pig') + self.assertEqual(expected_data, data) + + +class TestListJobTemplates(TestJobTemplates): + def setUp(self): + super(TestListJobTemplates, self).setUp() + self.job_mock.list.return_value = [api_j.JobTemplate( + None, JOB_TEMPLATE_INFO)] + + # Command to test + self.cmd = osc_j.ListJobTemplates(self.app, None) + + def test_job_templates_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', 'Id', 'Type'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('pig-job', 'job_id', 'Pig')] + self.assertEqual(expected_data, list(data)) + + def test_job_template_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', 'Id', 'Type', 'Description', 'Is public', + 'Is protected'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('pig-job', 'job_id', 'Pig', 'Job for test', + False, False)] + self.assertEqual(expected_data, list(data)) + + def test_job_template_list_extra_search_opts(self): + arglist = ['--type', 'Pig', '--name', 'pig'] + verifylist = [('type', 'Pig'), ('name', 'pig')] + + 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', 'Id', 'Type'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('pig-job', 'job_id', 'Pig')] + self.assertEqual(expected_data, list(data)) + + +class TestShowJobTemplate(TestJobTemplates): + def setUp(self): + super(TestShowJobTemplate, self).setUp() + self.job_mock.find_unique.return_value = api_j.JobTemplate( + None, JOB_TEMPLATE_INFO) + + # Command to test + self.cmd = osc_j.ShowJobTemplate(self.app, None) + + def test_job_template_show(self): + arglist = ['pig-job'] + verifylist = [('job_template', 'pig-job')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.job_mock.find_unique.assert_called_once_with(name='pig-job') + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Libs', 'Mains', 'Name', 'Type') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('Job for test', 'job_id', False, False, 'lib:lib_id', + 'main:main_id', 'pig-job', 'Pig') + self.assertEqual(expected_data, data) + + +class TestDeleteJobTemplate(TestJobTemplates): + def setUp(self): + super(TestDeleteJobTemplate, self).setUp() + self.job_mock.find_unique.return_value = api_j.JobTemplate( + None, JOB_TEMPLATE_INFO) + + # Command to test + self.cmd = osc_j.DeleteJobTemplate(self.app, None) + + def test_job_template_delete(self): + arglist = ['pig-job'] + verifylist = [('job_template', ['pig-job'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.job_mock.delete.assert_called_once_with('job_id') + + +class TestUpdateJobTemplate(TestJobTemplates): + def setUp(self): + super(TestUpdateJobTemplate, self).setUp() + self.job_mock.find_unique.return_value = api_j.JobTemplate( + None, JOB_TEMPLATE_INFO) + self.job_mock.update.return_value = mock.Mock( + job_template=JOB_TEMPLATE_INFO.copy()) + + # Command to test + self.cmd = osc_j.UpdateJobTemplate(self.app, None) + + def test_job_template_update_no_options(self): + arglist = [] + verifylist = [] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_job_template_update_nothing_updated(self): + arglist = ['pig-job'] + + verifylist = [('job_template', 'pig-job')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.job_mock.update.assert_called_once_with('job_id') + + def test_job_template_update_all_options(self): + arglist = ['pig-job', '--name', 'pig-job', '--description', 'descr', + '--public', '--protected'] + + verifylist = [('job_template', 'pig-job'), ('name', 'pig-job'), + ('description', 'descr'), ('is_public', True), + ('is_protected', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.job_mock.update.assert_called_once_with( + 'job_id', description='descr', is_protected=True, is_public=True, + name='pig-job') + + # Check that columns are correct + expected_columns = ('Description', 'Id', 'Is protected', 'Is public', + 'Libs', 'Mains', 'Name', 'Type') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('Job for test', 'job_id', False, False, 'lib:lib_id', + 'main:main_id', 'pig-job', 'Pig') + self.assertEqual(expected_data, data) + + def test_job_template_update_private_unprotected(self): + arglist = ['pig-job', '--private', '--unprotected'] + + verifylist = [('job_template', 'pig-job'), ('is_public', False), + ('is_protected', False)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.job_mock.update.assert_called_once_with( + 'job_id', is_protected=False, is_public=False) diff --git a/saharaclient/tests/unit/osc/v2/test_job_types.py b/saharaclient/tests/unit/osc/v2/test_job_types.py new file mode 100644 index 0000000..130f218 --- /dev/null +++ b/saharaclient/tests/unit/osc/v2/test_job_types.py @@ -0,0 +1,148 @@ +# 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 job_types as api_jt +from saharaclient.api.v2 import job_templates as api_job_templates +from saharaclient.osc.v2 import job_types as osc_jt +from saharaclient.tests.unit.osc.v1 import test_job_types as tjt_v1 + +JOB_TYPE_INFO = { + "name": 'Pig', + "plugins": [ + { + 'versions': { + '0.1': {}, + '0.2': {} + }, + 'name': 'fake' + }, + { + 'versions': { + '6.2.2': {} + }, + 'name': 'wod' + } + ] +} + + +class TestJobTypes(tjt_v1.TestJobTypes): + def setUp(self): + super(TestJobTypes, self).setUp() + self.app.api_version['data_processing'] = '2' + self.job_template_mock = ( + self.app.client_manager.data_processing.job_templates) + self.jt_mock = self.app.client_manager.data_processing.job_types + self.jt_mock.reset_mock() + self.job_template_mock.reset_mock() + + +class TestListJobTemplates(TestJobTypes): + def setUp(self): + super(TestListJobTemplates, self).setUp() + self.jt_mock.list.return_value = [api_jt.JobType(None, JOB_TYPE_INFO)] + + # Command to test + self.cmd = osc_jt.ListJobTypes(self.app, None) + + def test_job_types_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', 'Plugins'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('Pig', 'fake(0.1, 0.2), wod(6.2.2)')] + self.assertEqual(expected_data, list(data)) + + def test_job_types_list_extra_search_opts(self): + arglist = ['--type', 'Pig', '--plugin', 'fake', '--plugin-version', + '0.1'] + verifylist = [('type', 'Pig'), ('plugin', 'fake'), + ('plugin_version', '0.1')] + + 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', 'Plugins'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('Pig', 'fake(0.1, 0.2), wod(6.2.2)')] + self.assertEqual(expected_data, list(data)) + + +class TestGetJobTypeConfigs(TestJobTypes): + def setUp(self): + super(TestGetJobTypeConfigs, self).setUp() + self.job_template_mock.get_configs.return_value = ( + api_job_templates.JobTemplate(None, JOB_TYPE_INFO)) + + # Command to test + self.cmd = osc_jt.GetJobTypeConfigs(self.app, None) + + @mock.patch('oslo_serialization.jsonutils.dump') + def test_get_job_type_configs_default_file(self, p_dump): + m_open = mock.mock_open() + with mock.patch('six.moves.builtins.open', m_open, create=True): + arglist = ['Pig'] + verifylist = [('job_type', 'Pig')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments was passed + self.job_template_mock.get_configs.assert_called_once_with( + 'Pig') + + args_to_dump = p_dump.call_args[0] + # Check that the right data will be saved + + self.assertEqual(JOB_TYPE_INFO, args_to_dump[0]) + # Check that data will be saved to the right file + self.assertEqual('Pig', m_open.call_args[0][0]) + + @mock.patch('oslo_serialization.jsonutils.dump') + def test_get_job_type_configs_specified_file(self, p_dump): + m_open = mock.mock_open() + with mock.patch('six.moves.builtins.open', m_open): + arglist = ['Pig', '--file', 'testfile'] + verifylist = [('job_type', 'Pig'), ('file', 'testfile')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments was passed + self.job_template_mock.get_configs.assert_called_once_with( + 'Pig') + + args_to_dump = p_dump.call_args[0] + # Check that the right data will be saved + + self.assertEqual(JOB_TYPE_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/v2/test_jobs.py b/saharaclient/tests/unit/osc/v2/test_jobs.py new file mode 100644 index 0000000..5eb0e3b --- /dev/null +++ b/saharaclient/tests/unit/osc/v2/test_jobs.py @@ -0,0 +1,362 @@ +# Copyright (c) 2018 Red Hat 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 osc_lib.tests import utils as osc_utils + +from saharaclient.api.v2 import jobs as api_j +from saharaclient.osc.v2 import jobs as osc_j +from saharaclient.tests.unit.osc.v1 import test_jobs as tj_v1 + +JOB_INFO = { + "is_public": False, + "id": "j_id", + "interface": [], + "is_protected": False, + "input_id": 'input_id', + "output_id": 'output_id', + "job_template_id": "job_template_id", + "cluster_id": 'cluster_id', + "start_time": "start", + "end_time": "end", + "engine_job_id": "engine_job_id", + "info": { + "status": 'SUCCEEDED' + }, + "job_configs": { + "configs": { + "config1": "1", + "config2": "2" + }, + "args": [ + "arg1", + "arg2" + ], + "params": { + "param2": "value2", + "param1": "value1" + } + } +} + + +class TestJobs(tj_v1.TestJobs): + def setUp(self): + super(TestJobs, self).setUp() + self.app.api_version['data_processing'] = '2' + self.j_mock = self.app.client_manager.data_processing.jobs + self.j_mock.reset_mock() + + +class TestExecuteJob(TestJobs): + # TODO(apavlov): check for execution with --interface, --configs, --json + def setUp(self): + super(TestExecuteJob, self).setUp() + self.j_mock.create.return_value = api_j.Job(None, JOB_INFO) + self.ds_mock = self.app.client_manager.data_processing.data_sources + self.ds_mock.find_unique.return_value = mock.Mock(id='ds_id') + self.c_mock = self.app.client_manager.data_processing.clusters + self.c_mock.find_unique.return_value = mock.Mock(id='cluster_id') + self.jt_mock = self.app.client_manager.data_processing.job_templates + self.jt_mock.find_unique.return_value = mock.Mock(id='job_template_id') + self.ds_mock.reset_mock() + self.c_mock.reset_mock() + self.jt_mock.reset_mock() + + # Command to test + self.cmd = osc_j.ExecuteJob(self.app, None) + + def test_job_execute_minimum_options(self): + arglist = ['--job-template', 'job-template', '--cluster', 'cluster'] + verifylist = [('job_template', 'job-template'), ('cluster', 'cluster')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.j_mock.create.assert_called_once_with( + cluster_id='cluster_id', configs={}, input_id=None, + interface=None, is_protected=False, is_public=False, + job_template_id='job_template_id', output_id=None) + + def test_job_execute_with_input_output_option(self): + arglist = ['--job-template', 'job-template', '--cluster', 'cluster', + '--input', 'input', '--output', 'output'] + verifylist = [('job_template', 'job-template'), ('cluster', 'cluster'), + ('input', 'input'), ('output', 'output')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + self.j_mock.create.assert_called_once_with( + cluster_id='cluster_id', configs={}, input_id='ds_id', + interface=None, is_protected=False, is_public=False, + job_template_id='job_template_id', output_id='ds_id') + + # without option --output + arglist = ['--job-template', 'job-template', '--cluster', 'cluster', + '--input', 'input'] + verifylist = [('job_template', 'job-template'), ('cluster', 'cluster'), + ('input', 'input')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + self.j_mock.create.assert_called_with( + cluster_id='cluster_id', configs={}, input_id='ds_id', + interface=None, is_protected=False, is_public=False, + job_template_id='job_template_id', output_id=None) + + # without options --output and --input + arglist = ['--job-template', 'job-template', '--cluster', 'cluster'] + verifylist = [('job_template', 'job-template'), ('cluster', 'cluster')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + self.j_mock.create.assert_called_with( + cluster_id='cluster_id', configs={}, input_id=None, + interface=None, is_protected=False, is_public=False, + job_template_id='job_template_id', output_id=None) + + def test_job_execute_all_options(self): + arglist = ['--job-template', 'job-template', '--cluster', 'cluster', + '--input', 'input', '--output', 'output', '--params', + 'param1:value1', 'param2:value2', '--args', 'arg1', 'arg2', + '--configs', 'config1:1', 'config2:2', '--public', + '--protected'] + + verifylist = [('job_template', 'job-template'), ('cluster', 'cluster'), + ('input', 'input'), ('output', 'output'), + ('params', ['param1:value1', 'param2:value2']), + ('args', ['arg1', 'arg2']), + ('configs', ['config1:1', 'config2:2']), + ('public', True), + ('protected', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.j_mock.create.assert_called_once_with( + cluster_id='cluster_id', + configs={'configs': {'config1': '1', 'config2': '2'}, + 'args': ['arg1', 'arg2'], + 'params': {'param2': 'value2', 'param1': 'value1'}}, + input_id='ds_id', interface=None, is_protected=True, + is_public=True, job_template_id='job_template_id', + output_id='ds_id') + + # Check that columns are correct + expected_columns = ('Cluster id', 'End time', 'Engine job id', 'Id', + 'Input id', 'Is protected', 'Is public', + 'Job template id', 'Output id', 'Start time', + 'Status') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('cluster_id', 'end', 'engine_job_id', 'j_id', + 'input_id', False, False, 'job_template_id', + 'output_id', 'start', 'SUCCEEDED') + self.assertEqual(expected_data, data) + + +class TestListJobs(TestJobs): + def setUp(self): + super(TestListJobs, self).setUp() + self.j_mock.list.return_value = [api_j.Job(None, JOB_INFO)] + + # Command to test + self.cmd = osc_j.ListJobs(self.app, None) + + def test_jobs_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 = ['Id', 'Cluster id', 'Job template id', 'Status'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('j_id', 'cluster_id', 'job_template_id', + 'SUCCEEDED')] + self.assertEqual(expected_data, list(data)) + + def test_jobs_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 = ['Id', 'Cluster id', 'Job template id', 'Status', + 'Start time', 'End time'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('j_id', 'cluster_id', 'job_template_id', 'SUCCEEDED', + 'start', 'end')] + self.assertEqual(expected_data, list(data)) + + def test_jobs_list_extra_search_opts(self): + arglist = ['--status', 'succeeded'] + verifylist = [('status', 'succeeded')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that columns are correct + expected_columns = ['Id', 'Cluster id', 'Job template id', 'Status'] + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = [('j_id', 'cluster_id', 'job_template_id', + 'SUCCEEDED')] + self.assertEqual(expected_data, list(data)) + + +class TestShowJob(TestJobs): + def setUp(self): + super(TestShowJob, self).setUp() + self.j_mock.get.return_value = api_j.Job(None, JOB_INFO) + + # Command to test + self.cmd = osc_j.ShowJob(self.app, None) + + def test_job_show(self): + arglist = ['job_id'] + verifylist = [('job', 'job_id')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.j_mock.get.assert_called_once_with('job_id') + + # Check that columns are correct + expected_columns = ('Cluster id', 'End time', 'Engine job id', 'Id', + 'Input id', 'Is protected', 'Is public', + 'Job template id', 'Output id', 'Start time', + 'Status') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('cluster_id', 'end', 'engine_job_id', 'j_id', + 'input_id', False, False, 'job_template_id', + 'output_id', 'start', 'SUCCEEDED') + self.assertEqual(expected_data, data) + + +class TestDeleteJob(TestJobs): + def setUp(self): + super(TestDeleteJob, self).setUp() + self.j_mock.get.return_value = api_j.Job(None, JOB_INFO) + + # Command to test + self.cmd = osc_j.DeleteJob(self.app, None) + + def test_job_delete(self): + arglist = ['job_id'] + verifylist = [('job', ['job_id'])] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.j_mock.delete.assert_called_once_with('job_id') + + +class TestUpdateJob(TestJobs): + def setUp(self): + super(TestUpdateJob, self).setUp() + self.j_mock.get.return_value = api_j.Job(None, JOB_INFO) + self.j_mock.update.return_value = mock.Mock(job=JOB_INFO.copy()) + + # Command to test + self.cmd = osc_j.UpdateJob(self.app, None) + + def test_job_update_no_options(self): + arglist = [] + verifylist = [] + + self.assertRaises(osc_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_job_update_nothing_updated(self): + arglist = ['job_id'] + + verifylist = [('job', 'job_id')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.j_mock.update.assert_called_once_with('job_id') + + def test_job_update_public_protected(self): + arglist = ['job_id', '--public', '--protected'] + + verifylist = [('job', 'job_id'), ('is_public', True), + ('is_protected', True)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.j_mock.update.assert_called_once_with( + 'job_id', is_protected=True, is_public=True) + + # Check that columns are correct + expected_columns = ('Cluster id', 'End time', 'Engine job id', 'Id', + 'Input id', 'Is protected', 'Is public', + 'Job template id', 'Output id', 'Start time', + 'Status') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('cluster_id', 'end', 'engine_job_id', 'j_id', + 'input_id', False, False, 'job_template_id', + 'output_id', 'start', 'SUCCEEDED') + self.assertEqual(expected_data, data) + + def test_job_update_private_unprotected(self): + arglist = ['job_id', '--private', '--unprotected'] + + verifylist = [('job', 'job_id'), ('is_public', False), + ('is_protected', False)] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.j_mock.update.assert_called_once_with( + 'job_id', is_protected=False, is_public=False) diff --git a/saharaclient/tests/unit/osc/v2/test_node_group_templates.py b/saharaclient/tests/unit/osc/v2/test_node_group_templates.py index df174dd..8a70b1a 100644 --- a/saharaclient/tests/unit/osc/v2/test_node_group_templates.py +++ b/saharaclient/tests/unit/osc/v2/test_node_group_templates.py @@ -57,10 +57,10 @@ NGT_INFO = { class TestNodeGroupTemplates(fakes.TestDataProcessing): def setUp(self): super(TestNodeGroupTemplates, self).setUp() + self.app.api_version['data_processing'] = '2' self.ngt_mock = ( self.app.client_manager.data_processing.node_group_templates) self.ngt_mock.reset_mock() - self.app.api_version['data_processing'] = '2' class TestCreateNodeGroupTemplate(TestNodeGroupTemplates): diff --git a/saharaclient/tests/unit/osc/v2/test_plugins.py b/saharaclient/tests/unit/osc/v2/test_plugins.py new file mode 100644 index 0000000..6fcf972 --- /dev/null +++ b/saharaclient/tests/unit/osc/v2/test_plugins.py @@ -0,0 +1,233 @@ +# 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 oslo_serialization import jsonutils as json + +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', + 'required_image_tags': ['fake', '0.1'], + 'node_processes': { + 'HDFS': ['datanode', 'namenode'], + 'MapReduce': ['jobtracker', 'tasktracker'] + }, 'plugin_labels': {'enabled': {'status': True}}, + 'version_labels': {'0.1': {'enabled': {'status': True}}}} + + +class TestPlugins(fakes.TestDataProcessing): + def setUp(self): + super(TestPlugins, self).setUp() + self.app.api_version['data_processing'] = '2' + 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) + self.plugins_mock.get_version_details.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 = [('plugin', 'fake')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.plugins_mock.get.assert_called_once_with('fake') + + # Check that columns are correct + expected_columns = ('Description', 'Name', 'Title', 'Versions', '', + 'Plugin version 0.1: enabled', 'Plugin: enabled') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('Plugin for tests', 'fake', 'Fake Plugin', + '0.1, 0.2', '', True, True) + self.assertEqual(expected_data, data) + + def test_plugin_version_show(self): + arglist = ['fake', '--plugin-version', '0.1'] + verifylist = [('plugin', 'fake'), ('plugin_version', '0.1')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.plugins_mock.get_version_details.assert_called_once_with( + 'fake', '0.1') + + # Check that columns are correct + expected_columns = ('Description', 'Name', 'Required image tags', + 'Title', '', 'Plugin version 0.1: enabled', + 'Plugin: enabled', '', 'Service:', '', 'HDFS', + 'MapReduce') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('Plugin for tests', 'fake', '0.1, fake', + 'Fake Plugin', '', True, True, '', + 'Available processes:', '', + 'datanode, namenode', 'jobtracker, tasktracker') + 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'), ('plugin_version', '0.1')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were 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-0.1', 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'), ('plugin_version', '0.1'), + ('file', 'testfile')] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + self.cmd.take_action(parsed_args) + + # Check that correct arguments were 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]) + + +class TestUpdatePlugin(TestPlugins): + def setUp(self): + super(TestUpdatePlugin, self).setUp() + self.plugins_mock.update.return_value = api_plugins.Plugin( + None, PLUGIN_INFO) + + # Command to test + self.cmd = osc_plugins.UpdatePlugin(self.app, None) + + @mock.patch('osc_lib.utils.read_blob_file_contents') + def test_plugin_update(self, read): + arglist = ['fake', 'update.json'] + verifylist = [('plugin', 'fake'), ('json', 'update.json')] + value = {'plugin_labels': {'enabled': {'status': True}}} + value = json.dumps(value) + read.return_value = value + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + # Check that correct arguments were passed + self.plugins_mock.update.assert_called_once_with( + 'fake', {'plugin_labels': {'enabled': {'status': True}}}) + + # Check that columns are correct + expected_columns = ('Description', 'Name', 'Title', 'Versions', '', + 'Plugin version 0.1: enabled', 'Plugin: enabled') + self.assertEqual(expected_columns, columns) + + # Check that data is correct + expected_data = ('Plugin for tests', 'fake', 'Fake Plugin', + '0.1, 0.2', '', True, True) + self.assertEqual(expected_data, data) |