X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=functest%2Ftests%2Funit%2Futils%2Ftest_openstack_utils.py;h=15b540577f81a461253a704122e247f0cdeeccc4;hb=ac816628995c1e017f12ba23435ae07d24ceecac;hp=ef3764cc55c13ce1580894437abf833f24453861;hpb=4e0ebc01f1fa41d1b15f7d135c3769a051b1667c;p=functest.git diff --git a/functest/tests/unit/utils/test_openstack_utils.py b/functest/tests/unit/utils/test_openstack_utils.py index ef3764cc5..15b540577 100644 --- a/functest/tests/unit/utils/test_openstack_utils.py +++ b/functest/tests/unit/utils/test_openstack_utils.py @@ -17,8 +17,6 @@ from functest.utils import openstack_utils class OSUtilsTesting(unittest.TestCase): - logging.disable(logging.CRITICAL) - def _get_env_cred_dict(self, os_prefix=''): return {'OS_USERNAME': os_prefix + 'username', 'OS_PASSWORD': os_prefix + 'password', @@ -28,7 +26,8 @@ class OSUtilsTesting(unittest.TestCase): 'OS_PROJECT_DOMAIN_NAME': os_prefix + 'project_domain_name', 'OS_PROJECT_NAME': os_prefix + 'project_name', 'OS_ENDPOINT_TYPE': os_prefix + 'endpoint_type', - 'OS_REGION_NAME': os_prefix + 'region_name'} + 'OS_REGION_NAME': os_prefix + 'region_name', + 'OS_CACERT': os_prefix + 'https_cacert'} def _get_os_env_vars(self): return {'username': 'test_username', 'password': 'test_password', @@ -37,7 +36,8 @@ class OSUtilsTesting(unittest.TestCase): 'project_domain_name': 'test_project_domain_name', 'project_name': 'test_project_name', 'endpoint_type': 'test_endpoint_type', - 'region_name': 'test_region_name'} + 'region_name': 'test_region_name', + 'https_cacert': 'test_https_cacert'} def setUp(self): self.env_vars = ['OS_AUTH_URL', 'OS_USERNAME', 'OS_PASSWORD'] @@ -227,6 +227,12 @@ class OSUtilsTesting(unittest.TestCase): self.sec_group = {'id': 'sec_group_id', 'name': 'test_sec_group'} + self.sec_group_rule = {'id': 'sec_group_rule_id', + 'direction': 'direction', + 'protocol': 'protocol', + 'port_range_max': 'port_max', + 'security_group_id': self.sec_group['id'], + 'port_range_min': 'port_min'} self.neutron_floatingip = {'id': 'fip_id', 'floating_ip_address': 'test_ip'} self.neutron_client = mock.Mock() @@ -258,6 +264,9 @@ class OSUtilsTesting(unittest.TestCase): 'show_bgpvpn.return_value': self.mock_return, 'list_security_groups.return_value': {'security_groups': [self.sec_group]}, + 'list_security_group_rules.' + 'return_value': {'security_group_rules': + [self.sec_group_rule]}, 'create_security_group_rule.return_value': mock.Mock(), 'create_security_group.return_value': {'security_group': self.sec_group}, @@ -407,21 +416,45 @@ class OSUtilsTesting(unittest.TestCase): mock_logger_info.assert_called_once_with("OS_IDENTITY_API_VERSION is " "set in env as '%s'", '3') - def test_get_keystone_client(self): + @mock.patch('functest.utils.openstack_utils.get_session') + @mock.patch('functest.utils.openstack_utils.keystoneclient.Client') + @mock.patch('functest.utils.openstack_utils.get_keystone_client_version', + return_value='3') + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value='public') + def test_get_keystone_client_with_interface(self, mock_os_getenv, + mock_keystoneclient_version, + mock_key_client, + mock_get_session): mock_keystone_obj = mock.Mock() mock_session_obj = mock.Mock() - with mock.patch('functest.utils.openstack_utils' - '.get_keystone_client_version', return_value='3'), \ - mock.patch('functest.utils.openstack_utils' - '.keystoneclient.Client', - return_value=mock_keystone_obj) \ - as mock_key_client, \ - mock.patch('functest.utils.openstack_utils.get_session', - return_value=mock_session_obj): - self.assertEqual(openstack_utils.get_keystone_client(), - mock_keystone_obj) - mock_key_client.assert_called_once_with('3', - session=mock_session_obj) + mock_key_client.return_value = mock_keystone_obj + mock_get_session.return_value = mock_session_obj + self.assertEqual(openstack_utils.get_keystone_client(), + mock_keystone_obj) + mock_key_client.assert_called_once_with('3', + session=mock_session_obj, + interface='public') + + @mock.patch('functest.utils.openstack_utils.get_session') + @mock.patch('functest.utils.openstack_utils.keystoneclient.Client') + @mock.patch('functest.utils.openstack_utils.get_keystone_client_version', + return_value='3') + @mock.patch('functest.utils.openstack_utils.os.getenv', + return_value='admin') + def test_get_keystone_client_no_interface(self, mock_os_getenv, + mock_keystoneclient_version, + mock_key_client, + mock_get_session): + mock_keystone_obj = mock.Mock() + mock_session_obj = mock.Mock() + mock_key_client.return_value = mock_keystone_obj + mock_get_session.return_value = mock_session_obj + self.assertEqual(openstack_utils.get_keystone_client(), + mock_keystone_obj) + mock_key_client.assert_called_once_with('3', + session=mock_session_obj, + interface='admin') @mock.patch('functest.utils.openstack_utils.os.getenv', return_value=None) @@ -1245,6 +1278,52 @@ class OSUtilsTesting(unittest.TestCase): 'test_sec_group'), 'sec_group_id') + def test_get_security_group_rules_default(self): + self.assertEqual(openstack_utils. + get_security_group_rules(self.neutron_client, + self.sec_group['id']), + [self.sec_group_rule]) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_get_security_group_rules_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + get_security_group_rules(Exception, + 'sec_group_id'), + None) + self.assertTrue(mock_logger_error.called) + + def test_check_security_group_rules_not_exists(self): + self.assertEqual(openstack_utils. + check_security_group_rules(self.neutron_client, + 'sec_group_id_2', + 'direction', + 'protocol', + 'port_min', + 'port_max'), + True) + + def test_check_security_group_rules_exists(self): + self.assertEqual(openstack_utils. + check_security_group_rules(self.neutron_client, + self.sec_group['id'], + 'direction', + 'protocol', + 'port_min', + 'port_max'), + False) + + @mock.patch('functest.utils.openstack_utils.logger.error') + def test_check_security_group_rules_exception(self, mock_logger_error): + self.assertEqual(openstack_utils. + check_security_group_rules(Exception, + 'sec_group_id', + 'direction', + 'protocol', + 'port_max', + 'port_min'), + None) + self.assertTrue(mock_logger_error.called) + def test_create_security_group_default(self): self.assertEqual(openstack_utils. create_security_group(self.neutron_client, @@ -1758,4 +1837,5 @@ class OSUtilsTesting(unittest.TestCase): if __name__ == "__main__": + logging.disable(logging.CRITICAL) unittest.main(verbosity=2)