Skip to content
This repository has been archived by the owner on Feb 2, 2018. It is now read-only.

Commit

Permalink
containermgr: Added remove_node to kubernetes handler.
Browse files Browse the repository at this point in the history
Also added _delete which is used by remove_node.
  • Loading branch information
ashcrow committed Dec 7, 2016
1 parent 32ff47c commit 1886cb0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
41 changes: 41 additions & 0 deletions src/commissaire/containermgr/kubernetes/__init__.py
Expand Up @@ -130,6 +130,28 @@ def _get(self, part, *args, **kwargs):
part, resp.status_code))
return resp

def _delete(self, part, *args, **kwargs):
"""
Delete data from the Kubernetes apiserver.
:param part: The URI part. EG: /nodes
:type part: str
::param payload: Data to send with the DELETE.
:type payload: dict
:param args: All other non-keyword arguments.
:type args: tuple
:param kwargs: All other keyword arguments.
:type kwargs: dict
:returns: requests.Response
"""
part = self._fix_part(part)
self.logger.debug('Executing DELETE for {}.'.format(part))
resp = self.con.delete(
'{}{}'.format(self.base_uri, part), *args, **kwargs)
self.logger.debug('Response for {}. Status: {}'.format(
part, resp.status_code))
return resp

def _put(self, part, payload, *args, **kwargs):
"""
Put data to the Kubernetes apiserver.
Expand Down Expand Up @@ -205,6 +227,25 @@ def register_node(self, name):
'Status: {}, Data: {}'.format(name, resp.status_code, resp.text))
return False

def remove_node(self, name):
"""
Removes a node from the Kubernetes Container Manager.
:param name: The name of the node.
:type name: str
:returns: True if removed, otherwise False
:rtype: bool
"""
part = '/nodes/{}'.format(name)

resp = self._delete(part)
if resp.status_code == 200:
return True
self.logger.error(
'Unexpected response when trying to remove the node {}.'
'Status: {}, Data: {}'.format(name, resp.status_code, resp.text))
return False

def node_registered(self, name):
"""
Checks is a node was registered.
Expand Down
27 changes: 21 additions & 6 deletions test/test_containermgr_kubernetes.py
Expand Up @@ -64,14 +64,17 @@ def test_initialization_with_bad_config(self):
kubernetes.KubeContainerManager,
config)

def test__get(self):
def test__get_and_delete(self):
"""
Verify _get makes proper HTTP requests.
Verify _get and _delete makes proper HTTP requests.
"""
self.instance.con = mock.MagicMock()
self.instance._get('test')
self.instance.con.get.assert_called_once_with(
CONTAINER_MGR_CONFIG['server_url'] + 'api/v1/test')
for method in ('get', 'delete'):
self.instance.con = mock.MagicMock()
method_callable = getattr(self.instance, '_' + method)
method_callable('test')
assertable_callable = getattr(self.instance.con, method)
assertable_callable.assert_called_once_with(
CONTAINER_MGR_CONFIG['server_url'] + 'api/v1/test')

def test__put_and__post(self):
"""
Expand Down Expand Up @@ -111,6 +114,18 @@ def test_register_node(self):
CONTAINER_MGR_CONFIG['server_url'] + 'api/v1/nodes/test',
data=mock.ANY)

def test_remove_node(self):
"""
Verify remove_node makes the proper remote call and returns the proper result.
"""
for code, result in ((200, True), (404, False)):
self.instance.con = mock.MagicMock()
self.instance.con.delete.return_value = mock.MagicMock(
status_code=code)
self.assertEquals(result, self.instance.remove_node('test'))
self.instance.con.delete.assert_called_once_with(
CONTAINER_MGR_CONFIG['server_url'] + 'api/v1/nodes/test')

def test_get_host_status(self):
"""
Verify get_host_status makes the proper remote call.
Expand Down

0 comments on commit 1886cb0

Please sign in to comment.