From cf085cb6df54a6570027fae9e5c17af7c9060d1c Mon Sep 17 00:00:00 2001 From: Dane LeBlanc Date: Thu, 5 Dec 2013 17:04:55 -0500 Subject: [PATCH] Improve unit test coverage for Cisco plugin common code Closes-Bug: #1190615 This fix improves the unit test coverage for the Cisco Nexus plugin common modules (directory neutron/plugins/cisco/common) from: --- cisco_credentials_v2.py 82% --- cisco_faults.py 0% --- config.py 84% To: --- cisco_credentials_v2.py 100% --- cisco_faults.py 76% --- config.py 100% Change-Id: Id1ccfee1317309039fab83f622ced58efc3b2ae7 --- .../cisco/common/cisco_credentials_v2.py | 17 ----- neutron/tests/unit/cisco/test_config.py | 75 +++++++++++++++++++ neutron/tests/unit/cisco/test_network_db.py | 32 +++++++- 3 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 neutron/tests/unit/cisco/test_config.py diff --git a/neutron/plugins/cisco/common/cisco_credentials_v2.py b/neutron/plugins/cisco/common/cisco_credentials_v2.py index 3afe965861..5d8fc8ff50 100644 --- a/neutron/plugins/cisco/common/cisco_credentials_v2.py +++ b/neutron/plugins/cisco/common/cisco_credentials_v2.py @@ -48,11 +48,6 @@ class Store(object): # which case, the credentials are already populated pass - @staticmethod - def put_credential(cred_name, username, password): - """Set the username and password.""" - cdb.add_credential(cred_name, username, password) - @staticmethod def get_username(cred_name): """Get the username.""" @@ -64,15 +59,3 @@ class Store(object): """Get the password.""" credential = cdb.get_credential_name(cred_name) return credential[const.CREDENTIAL_PASSWORD] - - @staticmethod - def get_credential(cred_name): - """Get the username and password.""" - cdb.get_credential_name(cred_name) - return {const.USERNAME: const.CREDENTIAL_USERNAME, - const.PASSWORD: const.CREDENTIAL_PASSWORD} - - @staticmethod - def delete_credential(cred_name): - """Delete a credential.""" - cdb.remove_credential(cred_name) diff --git a/neutron/tests/unit/cisco/test_config.py b/neutron/tests/unit/cisco/test_config.py new file mode 100644 index 0000000000..620f99b86f --- /dev/null +++ b/neutron/tests/unit/cisco/test_config.py @@ -0,0 +1,75 @@ +# Copyright (c) 2013 Cisco Systems 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.config import cfg + +from neutron.common import config as neutron_config +from neutron.plugins.cisco.common import config as cisco_config +from neutron.tests import base +from neutron.tests.unit import test_api_v2 + + +class TestCiscoNexusPluginConfig(base.BaseTestCase): + + def setUp(self): + # Point neutron config file to: neutron/tests/etc/neutron.conf.test + args = ['--config-file', test_api_v2.etcdir('neutron.conf.test')] + neutron_config.parse(args=args) + + super(TestCiscoNexusPluginConfig, self).setUp() + + def test_config_parse_error(self): + """Check that config error is raised upon config parser failure.""" + with mock.patch.object(cfg, 'MultiConfigParser') as parser: + parser.return_value.read.return_value = [] + self.assertRaises(cfg.Error, cisco_config.CiscoConfigOptions) + + def test_create_device_dictionary(self): + """Test creation of the device dictionary based on nexus config.""" + test_config = { + 'NEXUS_SWITCH:1.1.1.1': { + 'username': ['admin'], + 'password': ['mySecretPassword'], + 'ssh_port': [22], + 'compute1': ['1/1'], + 'compute2': ['1/2'], + }, + 'NEXUS_SWITCH:2.2.2.2': { + 'username': ['admin'], + 'password': ['mySecretPassword'], + 'ssh_port': [22], + 'compute3': ['1/1'], + 'compute4': ['1/2'], + }, + } + expected_dev_dict = { + ('NEXUS_SWITCH', '1.1.1.1', 'username'): 'admin', + ('NEXUS_SWITCH', '1.1.1.1', 'password'): 'mySecretPassword', + ('NEXUS_SWITCH', '1.1.1.1', 'ssh_port'): 22, + ('NEXUS_SWITCH', '1.1.1.1', 'compute1'): '1/1', + ('NEXUS_SWITCH', '1.1.1.1', 'compute2'): '1/2', + ('NEXUS_SWITCH', '2.2.2.2', 'username'): 'admin', + ('NEXUS_SWITCH', '2.2.2.2', 'password'): 'mySecretPassword', + ('NEXUS_SWITCH', '2.2.2.2', 'ssh_port'): 22, + ('NEXUS_SWITCH', '2.2.2.2', 'compute3'): '1/1', + ('NEXUS_SWITCH', '2.2.2.2', 'compute4'): '1/2', + } + with mock.patch.object(cfg, 'MultiConfigParser') as parser: + parser.return_value.read.return_value = cfg.CONF.config_file + parser.return_value.parsed = [test_config] + cisco_config.CiscoConfigOptions() + self.assertEqual(cisco_config.device_dictionary, + expected_dev_dict) diff --git a/neutron/tests/unit/cisco/test_network_db.py b/neutron/tests/unit/cisco/test_network_db.py index 28ce2216b0..ef09c81c2a 100644 --- a/neutron/tests/unit/cisco/test_network_db.py +++ b/neutron/tests/unit/cisco/test_network_db.py @@ -18,7 +18,10 @@ import mock import testtools from neutron.db import api as db +from neutron.plugins.cisco.common import cisco_constants +from neutron.plugins.cisco.common import cisco_credentials_v2 from neutron.plugins.cisco.common import cisco_exceptions as c_exc +from neutron.plugins.cisco.common import config as config from neutron.plugins.cisco.db import network_db_v2 as cdb from neutron.plugins.cisco import network_plugin from neutron.tests import base @@ -31,7 +34,6 @@ class CiscoNetworkDbTest(base.BaseTestCase): def setUp(self): super(CiscoNetworkDbTest, self).setUp() db.configure_db() - self.session = db.get_session() # The Cisco network plugin includes a thin layer of QoS and # credential API methods which indirectly call Cisco QoS and @@ -259,3 +261,31 @@ class CiscoNetworkCredentialDbTest(CiscoNetworkDbTest): self.assertRaises(c_exc.CredentialNotFound, self._network_plugin.get_credential_details, "dummyCredentialId") + + +class CiscoCredentialStoreTest(base.BaseTestCase): + + """Cisco Credential Store unit tests.""" + + def setUp(self): + super(CiscoCredentialStoreTest, self).setUp() + db.configure_db() + self.addCleanup(db.clear_db) + + def test_cred_store_init_duplicate_creds_ignored(self): + """Check that with multi store instances, dup creds are ignored.""" + # Create a device dictionary containing credentials for 1 switch. + dev_dict = { + ('dev_id', '1.1.1.1', cisco_constants.USERNAME): 'user_1', + ('dev_id', '1.1.1.1', cisco_constants.PASSWORD): 'password_1', + ('dev_id', '1.1.1.1', 'host_a'): '1/1', + ('dev_id', '1.1.1.1', 'host_b'): '1/2', + ('dev_id', '1.1.1.1', 'host_c'): '1/3', + } + with mock.patch.object(config, 'get_device_dictionary', + return_value=dev_dict): + # Create and initialize 2 instances of credential store. + cisco_credentials_v2.Store().initialize() + cisco_credentials_v2.Store().initialize() + # There should be only 1 switch credential in the database. + self.assertEqual(len(cdb.get_all_credentials()), 1)