Skip to content

Commit

Permalink
Fix port disable for turned off VMs
Browse files Browse the repository at this point in the history
Add port enable/disable logic to VRouterPortService
Add disable port method to VRouterAPIClient

Closes-Bug: #1778533
Change-Id: Iecbd523f512f98aa4f0e9152e117d506aaff90f7
  • Loading branch information
aszc-dev committed Jun 25, 2018
1 parent 193f076 commit 472b884
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions cvm/clients.py
Expand Up @@ -464,6 +464,12 @@ def enable_port(self, vmi_uuid):
except Exception, e:
logger.error('There was a problem with vRouter API Client: %s', e)

def disable_port(self, vmi_uuid):
try:
self.vrouter_api.disable_port(vmi_uuid)
except Exception, e:
logger.error('There was a problem with vRouter API Client: %s', e)

def read_port(self, vmi_uuid):
request_url = '{host}:{port}/port/{uuid}'.format(host=self.vrouter_host,
port=self.vrouter_port,
Expand Down
8 changes: 7 additions & 1 deletion cvm/services.py
Expand Up @@ -280,6 +280,7 @@ def _update_ports(self):
for vmi_model in ports:
if self._port_needs_an_update(vmi_model):
self._update_port(vmi_model)
self._set_port_state(vmi_model)
self._database.ports_to_update.remove(vmi_model)

def _port_needs_an_update(self, vmi_model):
Expand All @@ -295,4 +296,9 @@ def _port_needs_an_update(self, vmi_model):
def _update_port(self, vmi_model):
self._vrouter_api_client.delete_port(vmi_model.uuid)
self._vrouter_api_client.add_port(vmi_model)
self._vrouter_api_client.enable_port(vmi_model.uuid)

def _set_port_state(self, vmi_model):
if vmi_model.vm_model.is_powered_on:
self._vrouter_api_client.enable_port(vmi_model.uuid)
else:
self._vrouter_api_client.disable_port(vmi_model.uuid)
29 changes: 26 additions & 3 deletions tests/test_services.py
Expand Up @@ -614,9 +614,8 @@ def test_no_update(self):
port_check.return_value = False
self.port_service.sync_ports()

self.assertEqual(0, self.vrouter_api_client.delete_port.call_count)
self.assertEqual(0, self.vrouter_api_client.add_port.call_count)
self.assertEqual(0, self.vrouter_api_client.enable_port.call_count)
self.vrouter_api_client.delete_port.assert_not_called()
self.vrouter_api_client.add_port.assert_not_called()
self.assertEqual([], self.database.ports_to_update)

def test_delete_port(self):
Expand All @@ -625,3 +624,27 @@ def test_delete_port(self):
self.port_service.sync_ports()

self.vrouter_api_client.delete_port.assert_called_once_with('fe71b44d-0654-36aa-9841-ab9b78d628c5')

def test_enable_port(self):
vmi_model = construct_vmi_model()
vmi_model.vm_model.is_powered_on = True
self.database.ports_to_update.append(vmi_model)

with patch('cvm.services.VRouterPortService._port_needs_an_update') as port_check:
port_check.return_value = False
self.port_service.sync_ports()

self.vrouter_api_client.enable_port.assert_called_once_with('fe71b44d-0654-36aa-9841-ab9b78d628c5')
self.vrouter_api_client.disable_port.assert_not_called()

def test_disable_port(self):
vmi_model = construct_vmi_model()
vmi_model.vm_model.is_powered_on = False
self.database.ports_to_update.append(vmi_model)

with patch('cvm.services.VRouterPortService._port_needs_an_update') as port_check:
port_check.return_value = False
self.port_service.sync_ports()

self.vrouter_api_client.disable_port.assert_called_once_with('fe71b44d-0654-36aa-9841-ab9b78d628c5')
self.vrouter_api_client.enable_port.assert_not_called()

0 comments on commit 472b884

Please sign in to comment.