def test_update_port_postcommit_complete_provisioning(self, m_pc, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context._plugin_context = mock.MagicMock() mock_context.current = {'binding:profile': {'local_link_information': [ { 'switch_info': 'foo', 'port_id': 2222 } ] }, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'other'} mock_context.original = {'binding:profile': {}, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'unbound'} driver.update_port_postcommit(mock_context) self.switch_mock.plug_port_to_network.assert_not_called() m_pc.assert_called_once_with(mock_context._plugin_context, mock_context.current['id'], resources.PORT, 'GENERICSWITCH')
def test_delete_network_postcommit_failure_multiple(self, m_log, m_list): m_list.return_value = { 'foo': { 'device_type': 'bar', 'spam': 'ham', 'ip': 'ip' }, 'bar': { 'device_type': 'bar', 'spam': 'ham', 'ip': 'ip' }, } driver = gsm.GenericSwitchDriver() driver.initialize() self.switch_mock.del_network.side_effect = ValueError('boom') mock_context = mock.create_autospec(driver_context.NetworkContext) mock_context.current = { 'id': 22, 'provider:network_type': 'vlan', 'provider:segmentation_id': 22, 'provider:physical_network': 'physnet1' } self.assertRaisesRegexp(ValueError, "boom", driver.delete_network_postcommit, mock_context) self.switch_mock.del_network.assert_called_with(22, 22) self.assertEqual(2, self.switch_mock.del_network.call_count) self.assertEqual(2, m_log.error.call_count) self.assertIn('Failed to delete network', m_log.error.call_args[0][0])
def test_update_port_postcommit_unbind_not_managed_by_ngs(self, m_pc, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context._plugin_context = mock.MagicMock() mock_context.current = {'binding:profile': {}, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'unbound'} mock_context.original = {'binding:profile': {'local_link_information': [ { 'switch_info': 'ughh', 'port_id': 2222 } ] }, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'other'} driver.update_port_postcommit(mock_context) self.switch_mock.delete_port.assert_not_called() m_pc.assert_not_called()
def test_bind_port_with_physnet(self, m_apc, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context._plugin_context = mock.MagicMock() mock_context.current = {'binding:profile': {'local_link_information': [ { 'switch_info': 'foo', 'port_id': 2222 } ] }, 'binding:vnic_type': 'baremetal', 'id': '123'} mock_context.network.current = { 'provider:physical_network': 'physnet1' } mock_context.segments_to_bind = [ { 'segmentation_id': None, 'id': 123 } ] self.switch_mock._get_physical_networks.return_value = ['physnet1'] driver.bind_port(mock_context) self.switch_mock.plug_port_to_network.assert_called_once_with( 2222, 1) mock_context.set_binding.assert_called_with(123, 'other', {}) m_apc.assert_called_once_with(mock_context._plugin_context, mock_context.current['id'], resources.PORT, 'GENERICSWITCH')
def test_update_port_postcommit_unbind(self, m_pc, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context._plugin_context = mock.MagicMock() mock_context.current = {'binding:profile': {}, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'unbound'} mock_context.original = {'binding:profile': {'local_link_information': [ { 'switch_info': 'foo', 'port_id': 2222 } ] }, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'other'} mock_context.network = mock.Mock() mock_context.network.current = {'provider:segmentation_id': 123, 'id': 'aaaa-bbbb-cccc'} mock_context.segments_to_bind = [mock_context.network.current] driver.update_port_postcommit(mock_context) self.switch_mock.delete_port.assert_called_once_with(2222, 123) m_pc.assert_not_called()
def test_bind_port_with_different_physnet(self, m_apc, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context._plugin_context = mock.MagicMock() mock_context.current = {'binding:profile': {'local_link_information': [ { 'switch_info': 'bar', 'port_id': 2222 } ] }, 'binding:vnic_type': 'baremetal', 'id': '123'} mock_context.network.current = { 'provider:physical_network': 'physnet1' } mock_context.segments_to_bind = [ { 'segmentation_id': None, 'id': 123 } ] self.switch_mock._get_physical_networks.return_value = ['physnet2'] self.assertIsNone(driver.bind_port(mock_context)) self.assertFalse(mock_context.set_binding.called) self.switch_mock.plug_port_to_network.assert_not_called() self.assertFalse(m_apc.called)
def test_bind_port(self, m_list): driver = gsm.GenericSwitchDriver() mock_context = mock.create_autospec(driver_context.PortContext) mock_context.current = {'binding:profile': {'local_link_information': [ { 'switch_info': 'foo', 'port_id': 2222 } ] }, 'binding:vnic_type': 'baremetal'} mock_context.segments_to_bind = [ { 'segmentation_id': None, 'id': 123 } ] driver.bind_port(mock_context) self.switch_mock.plug_port_to_network.assert_called_once_with( 2222, '1') mock_context.set_binding.assert_called_with(123, 'other', {}, status='ACTIVE')
def test_delete_port_postcommit_failure(self, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) self.switch_mock.delete_port.side_effect = ( exceptions.GenericSwitchNetmikoMethodError(cmds='foo', args='bar')) mock_context.current = {'binding:profile': {'local_link_information': [ { 'switch_info': 'foo', 'port_id': '2222' } ] }, 'binding:vnic_type': 'baremetal', 'binding:vif_type': 'other', 'id': 'aaaa-bbbb-cccc'} mock_context.network = mock.Mock() mock_context.network.current = {'provider:segmentation_id': 123, 'id': 'aaaa-bbbb-cccc'} mock_context.segments_to_bind = [mock_context.network.current] self.assertRaises(exceptions.GenericSwitchNetmikoMethodError, driver.delete_port_postcommit, mock_context)
def test_empty_methods(self, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.NetworkContext) mock_context.current = { 'id': 22, 'provider:network_type': 'vlan', 'provider:segmentation_id': 22 } driver.initialize() driver.create_network_precommit(mock_context) driver.update_network_precommit(mock_context) driver.update_network_postcommit(mock_context) driver.delete_network_precommit(mock_context) driver.create_subnet_precommit(mock_context) driver.create_subnet_postcommit(mock_context) driver.update_subnet_precommit(mock_context) driver.update_subnet_postcommit(mock_context) driver.delete_subnet_precommit(mock_context) driver.delete_subnet_postcommit(mock_context) driver.create_port_precommit(mock_context) driver.create_port_postcommit(mock_context) driver.update_port_precommit(mock_context) driver.update_port_postcommit(mock_context) driver.delete_port_precommit(mock_context)
def test_delete_network_postcommit(self, m_list): driver = gsm.GenericSwitchDriver() mock_context = mock.create_autospec(driver_context.NetworkContext) mock_context.current = {'id': 22, 'provider:network_type': 'vlan', 'provider:segmentation_id': 22} driver.delete_network_postcommit(mock_context) self.switch_mock.del_network.assert_called_once_with(22)
def test_update_port_postcommit_no_llc(self, m_pc, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context._plugin_context = mock.MagicMock() mock_context.current = {'binding:profile': {}, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'other'} driver.update_port_postcommit(mock_context) self.assertFalse(m_pc.called)
def test_create_network_postcommit_with_different_physnet(self, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.NetworkContext) mock_context.current = {'id': 22, 'provider:network_type': 'vlan', 'provider:segmentation_id': 22, 'provider:physical_network': 'physnet1'} self.switch_mock._get_physical_networks.return_value = ['physnet2'] driver.create_network_postcommit(mock_context) self.assertFalse(self.switch_mock.add_network.called)
def test_delete_network_postcommit_failure(self, m_log, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() self.switch_mock.del_network.side_effect = Exception('boom') mock_context = mock.create_autospec(driver_context.NetworkContext) mock_context.current = {'id': 22, 'provider:network_type': 'vlan', 'provider:segmentation_id': 22} driver.delete_network_postcommit(mock_context) self.switch_mock.del_network.assert_called_once_with(22) self.assertEqual(1, m_log.error.call_count) self.assertIn('Failed to delete network', m_log.error.call_args[0][0])
def test_delete_network_postcommit_with_multiple_physnets(self, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.NetworkContext) mock_context.current = {'id': 22, 'provider:network_type': 'vlan', 'provider:segmentation_id': 22, 'provider:physical_network': 'physnet1'} self.switch_mock._get_physical_networks.return_value = ['physnet1', 'physnet2'] driver.delete_network_postcommit(mock_context) self.switch_mock.del_network.assert_called_once_with(22, 22) self.assertEqual(self.switch_mock.del_network.call_count, 1)
def test_create_network_postcommit_failure(self, m_log, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() self.switch_mock.add_network.side_effect = ValueError('boom') mock_context = mock.create_autospec(driver_context.NetworkContext) mock_context.current = {'id': 22, 'provider:network_type': 'vlan', 'provider:segmentation_id': 22, 'provider:physical_network': 'physnet1'} self.assertRaisesRegexp(ValueError, "boom", driver.create_network_postcommit, mock_context) self.switch_mock.add_network.assert_called_once_with(22, 22) self.assertEqual(1, m_log.error.call_count) self.assertIn('Failed to create network', m_log.error.call_args[0][0]) self.assertNotIn('has been added', m_log.info.call_args[0][0])
def test_bind_port_unknown_switch(self, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context.current = { 'binding:profile': { 'local_link_information': [{ 'switch_info': 'bar', 'port_id': 2222 }] }, 'binding:vnic_type': 'baremetal' } mock_context.segments_to_bind = [{'segmentation_id': None, 'id': 123}] self.assertIsNone(driver.bind_port(mock_context)) self.switch_mock.plug_port_to_network.assert_not_called()
def test_update_port_postcommit_complete_provisioning(self, _pc, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context._plugin_context = mock.MagicMock() mock_context.current = { 'binding:profile': { 'local_link_information': [{ 'switch_info': 'foo', 'port_id': 2222 }] }, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'other' } driver.update_port_postcommit(mock_context)
def test_update_port_postcommit_unbind_not_bound(self, m_pc, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context._plugin_context = mock.MagicMock() mock_context.current = {'binding:profile': {}, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'unbound'} mock_context.original = {'binding:profile': {}, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'unbound'} driver.update_port_postcommit(mock_context) self.switch_mock.delete_port.assert_not_called() m_pc.assert_not_called()
def test_update_port_postcommit_not_bound(self, m_pc, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context._plugin_context = mock.MagicMock() mock_context.current = { 'binding:profile': { 'local_link_information': [{ 'switch_info': 'foo', 'port_id': 2222 }] }, 'binding:vnic_type': 'baremetal', 'id': '123', 'binding:vif_type': 'unbound' } driver.update_port_postcommit(mock_context) self.assertFalse(m_pc.called)
def test_delete_network_postcommit_no_network_id(self, m_log, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.NetworkContext) mock_context.current = {'id': 22, 'provider:network_type': 'vlan', 'provider:segmentation_id': 22, 'provider:physical_network': 'physnet1'} self.switch_mock.del_network.side_effect = TypeError driver.delete_network_postcommit(mock_context) self.assertEqual([mock.call(22, 22), mock.call(22)], self.switch_mock.del_network.call_args_list) self.assertEqual(m_log.warn.call_count, 1) # Ensure the warning is only logged once. driver.delete_network_postcommit(mock_context) self.assertEqual(m_log.warn.call_count, 1)
def test_delete_port_postcommit(self, m_list): driver = gsm.GenericSwitchDriver() driver.initialize() mock_context = mock.create_autospec(driver_context.PortContext) mock_context.current = { 'binding:profile': { 'local_link_information': [{ 'switch_info': 'foo', 'port_id': '2222' }] }, 'binding:vnic_type': 'baremetal' } mock_context.network = mock.Mock() mock_context.network.current = {'provider:segmentation_id': 123} mock_context.segments_to_bind = [{'segmentation_id': None, 'id': 123}] driver.delete_port_postcommit(mock_context) self.switch_mock.delete_port.assert_called_once_with('2222', 123)