Merge "Add validator for fixed_network."

This commit is contained in:
Zuul 2023-10-24 22:48:18 +00:00 committed by Gerrit Code Review
commit 1b2261de26
2 changed files with 55 additions and 3 deletions

View File

@ -99,9 +99,23 @@ def validate_external_network(cli, external_network):
def validate_fixed_network(cli, fixed_network):
"""Validate fixed network"""
# TODO(houming):this method implement will be added after this
# first pathch for Cluster's OpenStack resources validation is merged.
pass
count = 0
network_id = None
networks = cli.neutron().list_networks()
for net in networks.get('networks'):
if fixed_network in [net.get('name'), net.get('id')]:
count += 1
network_id = net.get('id')
if count == 0:
# Unable to find the configured fixed_network.
raise exception.FixedNetworkNotFound(network=fixed_network)
elif count > 1:
msg = _("Multiple networks exist with same name '%s'. "
"Please use the network ID instead.")
raise exception.Conflict(msg % fixed_network)
return network_id
def validate_labels(labels):

View File

@ -94,6 +94,44 @@ class TestAttrValidator(base.BaseTestCase):
attr_validator.validate_external_network,
mock_os_cli, 'test_ext_net')
def test_validate_fixed_network_with_valid_network(self):
mock_networks = {'networks': [{'name': 'test_net',
'id': 'test_net_id'}]}
mock_neutron = mock.MagicMock()
mock_neutron.list_networks.return_value = mock_networks
mock_os_cli = mock.MagicMock()
mock_os_cli.neutron.return_value = mock_neutron
self.assertEqual('test_net_id',
attr_validator.validate_fixed_network(mock_os_cli,
'test_net'))
self.assertTrue(mock_neutron.list_networks.called)
def test_validate_fixed_network_with_multiple_valid_network(self):
mock_networks = {
'networks': [{'name': 'test_net',
'id': 'test_net_id1'},
{'name': 'test_net',
'id': 'test_net_id2'}],
}
mock_neutron = mock.MagicMock()
mock_neutron.list_networks.return_value = mock_networks
mock_os_cli = mock.MagicMock()
mock_os_cli.neutron.return_value = mock_neutron
self.assertRaises(exception.Conflict,
attr_validator.validate_fixed_network,
mock_os_cli, 'test_net')
def test_validate_fixed_network_with_invalid_network(self):
mock_networks = {'networks': [{'name': 'test_net_not_equal',
'id': 'test_net_id_not_equal'}]}
mock_neutron = mock.MagicMock()
mock_neutron.list_networks.return_value = mock_networks
mock_os_cli = mock.MagicMock()
mock_os_cli.neutron.return_value = mock_neutron
self.assertRaises(exception.FixedNetworkNotFound,
attr_validator.validate_fixed_network,
mock_os_cli, 'test_net')
def test_validate_keypair_with_no_keypair(self):
mock_keypair = mock.MagicMock()
mock_keypair.id = None