From 411b1b0292bb012e58ad34912dce35e4668b54e0 Mon Sep 17 00:00:00 2001 From: Russell Teague Date: Fri, 10 Mar 2017 15:53:35 -0500 Subject: Renaming oadm_manage_node to oc_adm_manage_node --- .../lib_openshift/src/ansible/oadm_manage_node.py | 38 --- .../src/ansible/oc_adm_manage_node.py | 38 +++ roles/lib_openshift/src/class/oadm_manage_node.py | 209 ---------------- .../lib_openshift/src/class/oc_adm_manage_node.py | 209 ++++++++++++++++ roles/lib_openshift/src/doc/manage_node | 6 +- roles/lib_openshift/src/sources.yml | 6 +- .../src/test/integration/oadm_manage_node.yml | 69 ----- .../src/test/integration/oc_adm_manage_node.yml | 69 +++++ .../src/test/unit/test_oadm_manage_node.py | 277 --------------------- .../src/test/unit/test_oc_adm_manage_node.py | 277 +++++++++++++++++++++ 10 files changed, 599 insertions(+), 599 deletions(-) delete mode 100644 roles/lib_openshift/src/ansible/oadm_manage_node.py create mode 100644 roles/lib_openshift/src/ansible/oc_adm_manage_node.py delete mode 100644 roles/lib_openshift/src/class/oadm_manage_node.py create mode 100644 roles/lib_openshift/src/class/oc_adm_manage_node.py delete mode 100755 roles/lib_openshift/src/test/integration/oadm_manage_node.yml create mode 100755 roles/lib_openshift/src/test/integration/oc_adm_manage_node.yml delete mode 100755 roles/lib_openshift/src/test/unit/test_oadm_manage_node.py create mode 100755 roles/lib_openshift/src/test/unit/test_oc_adm_manage_node.py (limited to 'roles/lib_openshift/src') diff --git a/roles/lib_openshift/src/ansible/oadm_manage_node.py b/roles/lib_openshift/src/ansible/oadm_manage_node.py deleted file mode 100644 index b870c1211..000000000 --- a/roles/lib_openshift/src/ansible/oadm_manage_node.py +++ /dev/null @@ -1,38 +0,0 @@ -# pylint: skip-file -# flake8: noqa - - -def main(): - ''' - ansible oadm module for manage-node - ''' - - module = AnsibleModule( - argument_spec=dict( - debug=dict(default=False, type='bool'), - kubeconfig=dict(default='/etc/origin/master/admin.kubeconfig', type='str'), - node=dict(default=None, type='list'), - selector=dict(default=None, type='str'), - pod_selector=dict(default=None, type='str'), - schedulable=dict(default=None, type='bool'), - list_pods=dict(default=False, type='bool'), - evacuate=dict(default=False, type='bool'), - dry_run=dict(default=False, type='bool'), - force=dict(default=False, type='bool'), - grace_period=dict(default=None, type='int'), - ), - mutually_exclusive=[["selector", "node"], ['evacuate', 'list_pods'], ['list_pods', 'schedulable']], - required_one_of=[["node", "selector"]], - - supports_check_mode=True, - ) - results = ManageNode.run_ansible(module.params, module.check_mode) - - if 'failed' in results: - module.fail_json(**results) - - module.exit_json(**results) - - -if __name__ == "__main__": - main() diff --git a/roles/lib_openshift/src/ansible/oc_adm_manage_node.py b/roles/lib_openshift/src/ansible/oc_adm_manage_node.py new file mode 100644 index 000000000..b870c1211 --- /dev/null +++ b/roles/lib_openshift/src/ansible/oc_adm_manage_node.py @@ -0,0 +1,38 @@ +# pylint: skip-file +# flake8: noqa + + +def main(): + ''' + ansible oadm module for manage-node + ''' + + module = AnsibleModule( + argument_spec=dict( + debug=dict(default=False, type='bool'), + kubeconfig=dict(default='/etc/origin/master/admin.kubeconfig', type='str'), + node=dict(default=None, type='list'), + selector=dict(default=None, type='str'), + pod_selector=dict(default=None, type='str'), + schedulable=dict(default=None, type='bool'), + list_pods=dict(default=False, type='bool'), + evacuate=dict(default=False, type='bool'), + dry_run=dict(default=False, type='bool'), + force=dict(default=False, type='bool'), + grace_period=dict(default=None, type='int'), + ), + mutually_exclusive=[["selector", "node"], ['evacuate', 'list_pods'], ['list_pods', 'schedulable']], + required_one_of=[["node", "selector"]], + + supports_check_mode=True, + ) + results = ManageNode.run_ansible(module.params, module.check_mode) + + if 'failed' in results: + module.fail_json(**results) + + module.exit_json(**results) + + +if __name__ == "__main__": + main() diff --git a/roles/lib_openshift/src/class/oadm_manage_node.py b/roles/lib_openshift/src/class/oadm_manage_node.py deleted file mode 100644 index c07320477..000000000 --- a/roles/lib_openshift/src/class/oadm_manage_node.py +++ /dev/null @@ -1,209 +0,0 @@ -# pylint: skip-file -# flake8: noqa - - -class ManageNodeException(Exception): - ''' manage-node exception class ''' - pass - - -class ManageNodeConfig(OpenShiftCLIConfig): - ''' ManageNodeConfig is a DTO for the manage-node command.''' - def __init__(self, kubeconfig, node_options): - super(ManageNodeConfig, self).__init__(None, None, kubeconfig, node_options) - - -# pylint: disable=too-many-instance-attributes -class ManageNode(OpenShiftCLI): - ''' Class to wrap the oc command line tools ''' - - # pylint allows 5 - # pylint: disable=too-many-arguments - def __init__(self, - config, - verbose=False): - ''' Constructor for ManageNode ''' - super(ManageNode, self).__init__(None, kubeconfig=config.kubeconfig, verbose=verbose) - self.config = config - - def evacuate(self): - ''' formulate the params and run oadm manage-node ''' - return self._evacuate(node=self.config.config_options['node']['value'], - selector=self.config.config_options['selector']['value'], - pod_selector=self.config.config_options['pod_selector']['value'], - dry_run=self.config.config_options['dry_run']['value'], - grace_period=self.config.config_options['grace_period']['value'], - force=self.config.config_options['force']['value'], - ) - def get_nodes(self, node=None, selector=''): - '''perform oc get node''' - _node = None - _sel = None - if node: - _node = node - if selector: - _sel = selector - - results = self._get('node', rname=_node, selector=_sel) - if results['returncode'] != 0: - return results - - nodes = [] - items = None - if results['results'][0]['kind'] == 'List': - items = results['results'][0]['items'] - else: - items = results['results'] - - for node in items: - _node = {} - _node['name'] = node['metadata']['name'] - _node['schedulable'] = True - if 'unschedulable' in node['spec']: - _node['schedulable'] = False - nodes.append(_node) - - return nodes - - def get_pods_from_node(self, node, pod_selector=None): - '''return pods for a node''' - results = self._list_pods(node=[node], pod_selector=pod_selector) - - if results['returncode'] != 0: - return results - - # When a selector or node is matched it is returned along with the json. - # We are going to split the results based on the regexp and then - # load the json for each matching node. - # Before we return we are going to loop over the results and pull out the node names. - # {'node': [pod, pod], 'node': [pod, pod]} - # 3.2 includes the following lines in stdout: "Listing matched pods on node:" - all_pods = [] - if "Listing matched" in results['results']: - listing_match = re.compile('\n^Listing matched.*$\n', flags=re.MULTILINE) - pods = listing_match.split(results['results']) - for pod in pods: - if pod: - all_pods.extend(json.loads(pod)['items']) - - # 3.3 specific - else: - # this is gross but I filed a bug... - # https://bugzilla.redhat.com/show_bug.cgi?id=1381621 - # build our own json from the output. - all_pods = json.loads(results['results'])['items'] - - return all_pods - - def list_pods(self): - ''' run oadm manage-node --list-pods''' - _nodes = self.config.config_options['node']['value'] - _selector = self.config.config_options['selector']['value'] - _pod_selector = self.config.config_options['pod_selector']['value'] - - if not _nodes: - _nodes = self.get_nodes(selector=_selector) - else: - _nodes = [{'name': name} for name in _nodes] - - all_pods = {} - for node in _nodes: - results = self.get_pods_from_node(node['name'], pod_selector=_pod_selector) - if isinstance(results, dict): - return results - all_pods[node['name']] = results - - results = {} - results['nodes'] = all_pods - results['returncode'] = 0 - return results - - def schedulable(self): - '''oadm manage-node call for making nodes unschedulable''' - nodes = self.config.config_options['node']['value'] - selector = self.config.config_options['selector']['value'] - - if not nodes: - nodes = self.get_nodes(selector=selector) - else: - tmp_nodes = [] - for name in nodes: - tmp_result = self.get_nodes(name) - if isinstance(tmp_result, dict): - tmp_nodes.append(tmp_result) - continue - tmp_nodes.extend(tmp_result) - nodes = tmp_nodes - - # This is a short circuit based on the way we fetch nodes. - # If node is a dict/list then we've already fetched them. - for node in nodes: - if isinstance(node, dict) and 'returncode' in node: - return {'results': nodes, 'returncode': node['returncode']} - if isinstance(node, list) and 'returncode' in node[0]: - return {'results': nodes, 'returncode': node[0]['returncode']} - # check all the nodes that were returned and verify they are: - # node['schedulable'] == self.config.config_options['schedulable']['value'] - if any([node['schedulable'] != self.config.config_options['schedulable']['value'] for node in nodes]): - - results = self._schedulable(node=self.config.config_options['node']['value'], - selector=self.config.config_options['selector']['value'], - schedulable=self.config.config_options['schedulable']['value']) - - # 'NAME STATUS AGE\\nip-172-31-49-140.ec2.internal Ready 4h\\n' # E501 - # normalize formatting with previous return objects - if results['results'].startswith('NAME'): - nodes = [] - # removing header line and trailing new line character of node lines - for node_results in results['results'].split('\n')[1:-1]: - parts = node_results.split() - nodes.append({'name': parts[0], 'schedulable': parts[1] == 'Ready'}) - results['nodes'] = nodes - - return results - - results = {} - results['returncode'] = 0 - results['changed'] = False - results['nodes'] = nodes - - return results - - @staticmethod - def run_ansible(params, check_mode): - '''run the idempotent ansible code''' - nconfig = ManageNodeConfig(params['kubeconfig'], - {'node': {'value': params['node'], 'include': True}, - 'selector': {'value': params['selector'], 'include': True}, - 'pod_selector': {'value': params['pod_selector'], 'include': True}, - 'schedulable': {'value': params['schedulable'], 'include': True}, - 'list_pods': {'value': params['list_pods'], 'include': True}, - 'evacuate': {'value': params['evacuate'], 'include': True}, - 'dry_run': {'value': params['dry_run'], 'include': True}, - 'force': {'value': params['force'], 'include': True}, - 'grace_period': {'value': params['grace_period'], 'include': True}, - }) - - oadm_mn = ManageNode(nconfig) - # Run the oadm manage-node commands - results = None - changed = False - if params['schedulable'] != None: - if check_mode: - # schedulable returns results after the fact. - # We need to redo how this works to support check_mode completely. - return {'changed': True, 'msg': 'CHECK_MODE: would have called schedulable.'} - results = oadm_mn.schedulable() - if 'changed' not in results: - changed = True - - if params['evacuate']: - results = oadm_mn.evacuate() - changed = True - elif params['list_pods']: - results = oadm_mn.list_pods() - - if not results or results['returncode'] != 0: - return {'failed': True, 'msg': results} - - return {'changed': changed, 'results': results, 'state': "present"} diff --git a/roles/lib_openshift/src/class/oc_adm_manage_node.py b/roles/lib_openshift/src/class/oc_adm_manage_node.py new file mode 100644 index 000000000..c07320477 --- /dev/null +++ b/roles/lib_openshift/src/class/oc_adm_manage_node.py @@ -0,0 +1,209 @@ +# pylint: skip-file +# flake8: noqa + + +class ManageNodeException(Exception): + ''' manage-node exception class ''' + pass + + +class ManageNodeConfig(OpenShiftCLIConfig): + ''' ManageNodeConfig is a DTO for the manage-node command.''' + def __init__(self, kubeconfig, node_options): + super(ManageNodeConfig, self).__init__(None, None, kubeconfig, node_options) + + +# pylint: disable=too-many-instance-attributes +class ManageNode(OpenShiftCLI): + ''' Class to wrap the oc command line tools ''' + + # pylint allows 5 + # pylint: disable=too-many-arguments + def __init__(self, + config, + verbose=False): + ''' Constructor for ManageNode ''' + super(ManageNode, self).__init__(None, kubeconfig=config.kubeconfig, verbose=verbose) + self.config = config + + def evacuate(self): + ''' formulate the params and run oadm manage-node ''' + return self._evacuate(node=self.config.config_options['node']['value'], + selector=self.config.config_options['selector']['value'], + pod_selector=self.config.config_options['pod_selector']['value'], + dry_run=self.config.config_options['dry_run']['value'], + grace_period=self.config.config_options['grace_period']['value'], + force=self.config.config_options['force']['value'], + ) + def get_nodes(self, node=None, selector=''): + '''perform oc get node''' + _node = None + _sel = None + if node: + _node = node + if selector: + _sel = selector + + results = self._get('node', rname=_node, selector=_sel) + if results['returncode'] != 0: + return results + + nodes = [] + items = None + if results['results'][0]['kind'] == 'List': + items = results['results'][0]['items'] + else: + items = results['results'] + + for node in items: + _node = {} + _node['name'] = node['metadata']['name'] + _node['schedulable'] = True + if 'unschedulable' in node['spec']: + _node['schedulable'] = False + nodes.append(_node) + + return nodes + + def get_pods_from_node(self, node, pod_selector=None): + '''return pods for a node''' + results = self._list_pods(node=[node], pod_selector=pod_selector) + + if results['returncode'] != 0: + return results + + # When a selector or node is matched it is returned along with the json. + # We are going to split the results based on the regexp and then + # load the json for each matching node. + # Before we return we are going to loop over the results and pull out the node names. + # {'node': [pod, pod], 'node': [pod, pod]} + # 3.2 includes the following lines in stdout: "Listing matched pods on node:" + all_pods = [] + if "Listing matched" in results['results']: + listing_match = re.compile('\n^Listing matched.*$\n', flags=re.MULTILINE) + pods = listing_match.split(results['results']) + for pod in pods: + if pod: + all_pods.extend(json.loads(pod)['items']) + + # 3.3 specific + else: + # this is gross but I filed a bug... + # https://bugzilla.redhat.com/show_bug.cgi?id=1381621 + # build our own json from the output. + all_pods = json.loads(results['results'])['items'] + + return all_pods + + def list_pods(self): + ''' run oadm manage-node --list-pods''' + _nodes = self.config.config_options['node']['value'] + _selector = self.config.config_options['selector']['value'] + _pod_selector = self.config.config_options['pod_selector']['value'] + + if not _nodes: + _nodes = self.get_nodes(selector=_selector) + else: + _nodes = [{'name': name} for name in _nodes] + + all_pods = {} + for node in _nodes: + results = self.get_pods_from_node(node['name'], pod_selector=_pod_selector) + if isinstance(results, dict): + return results + all_pods[node['name']] = results + + results = {} + results['nodes'] = all_pods + results['returncode'] = 0 + return results + + def schedulable(self): + '''oadm manage-node call for making nodes unschedulable''' + nodes = self.config.config_options['node']['value'] + selector = self.config.config_options['selector']['value'] + + if not nodes: + nodes = self.get_nodes(selector=selector) + else: + tmp_nodes = [] + for name in nodes: + tmp_result = self.get_nodes(name) + if isinstance(tmp_result, dict): + tmp_nodes.append(tmp_result) + continue + tmp_nodes.extend(tmp_result) + nodes = tmp_nodes + + # This is a short circuit based on the way we fetch nodes. + # If node is a dict/list then we've already fetched them. + for node in nodes: + if isinstance(node, dict) and 'returncode' in node: + return {'results': nodes, 'returncode': node['returncode']} + if isinstance(node, list) and 'returncode' in node[0]: + return {'results': nodes, 'returncode': node[0]['returncode']} + # check all the nodes that were returned and verify they are: + # node['schedulable'] == self.config.config_options['schedulable']['value'] + if any([node['schedulable'] != self.config.config_options['schedulable']['value'] for node in nodes]): + + results = self._schedulable(node=self.config.config_options['node']['value'], + selector=self.config.config_options['selector']['value'], + schedulable=self.config.config_options['schedulable']['value']) + + # 'NAME STATUS AGE\\nip-172-31-49-140.ec2.internal Ready 4h\\n' # E501 + # normalize formatting with previous return objects + if results['results'].startswith('NAME'): + nodes = [] + # removing header line and trailing new line character of node lines + for node_results in results['results'].split('\n')[1:-1]: + parts = node_results.split() + nodes.append({'name': parts[0], 'schedulable': parts[1] == 'Ready'}) + results['nodes'] = nodes + + return results + + results = {} + results['returncode'] = 0 + results['changed'] = False + results['nodes'] = nodes + + return results + + @staticmethod + def run_ansible(params, check_mode): + '''run the idempotent ansible code''' + nconfig = ManageNodeConfig(params['kubeconfig'], + {'node': {'value': params['node'], 'include': True}, + 'selector': {'value': params['selector'], 'include': True}, + 'pod_selector': {'value': params['pod_selector'], 'include': True}, + 'schedulable': {'value': params['schedulable'], 'include': True}, + 'list_pods': {'value': params['list_pods'], 'include': True}, + 'evacuate': {'value': params['evacuate'], 'include': True}, + 'dry_run': {'value': params['dry_run'], 'include': True}, + 'force': {'value': params['force'], 'include': True}, + 'grace_period': {'value': params['grace_period'], 'include': True}, + }) + + oadm_mn = ManageNode(nconfig) + # Run the oadm manage-node commands + results = None + changed = False + if params['schedulable'] != None: + if check_mode: + # schedulable returns results after the fact. + # We need to redo how this works to support check_mode completely. + return {'changed': True, 'msg': 'CHECK_MODE: would have called schedulable.'} + results = oadm_mn.schedulable() + if 'changed' not in results: + changed = True + + if params['evacuate']: + results = oadm_mn.evacuate() + changed = True + elif params['list_pods']: + results = oadm_mn.list_pods() + + if not results or results['returncode'] != 0: + return {'failed': True, 'msg': results} + + return {'changed': changed, 'results': results, 'state': "present"} diff --git a/roles/lib_openshift/src/doc/manage_node b/roles/lib_openshift/src/doc/manage_node index 382377f3e..b651ea4e7 100644 --- a/roles/lib_openshift/src/doc/manage_node +++ b/roles/lib_openshift/src/doc/manage_node @@ -3,7 +3,7 @@ DOCUMENTATION = ''' --- -module: oadm_manage_node +module: oc_adm_manage_node short_description: Module to manage openshift nodes description: - Manage openshift nodes programmatically. @@ -75,13 +75,13 @@ extends_documentation_fragment: [] EXAMPLES = ''' - name: oadm manage-node --schedulable=true --selector=ops_node=new - oadm_manage_node: + oc_adm_manage_node: selector: ops_node=new schedulable: True register: schedout - name: oadm manage-node my-k8s-node-5 --evacuate - oadm_manage_node: + oc_adm_manage_node: node: my-k8s-node-5 evacuate: True force: True diff --git a/roles/lib_openshift/src/sources.yml b/roles/lib_openshift/src/sources.yml index f16b3c8de..44a1524b0 100644 --- a/roles/lib_openshift/src/sources.yml +++ b/roles/lib_openshift/src/sources.yml @@ -9,15 +9,15 @@ oc_adm_ca_server_cert.py: - class/oc_adm_ca_server_cert.py - ansible/oc_adm_ca_server_cert.py -oadm_manage_node.py: +oc_adm_manage_node.py: - doc/generated - doc/license - lib/import.py - doc/manage_node - ../../lib_utils/src/class/yedit.py - lib/base.py -- class/oadm_manage_node.py -- ansible/oadm_manage_node.py +- class/oc_adm_manage_node.py +- ansible/oc_adm_manage_node.py oc_adm_policy_user.py: - doc/generated diff --git a/roles/lib_openshift/src/test/integration/oadm_manage_node.yml b/roles/lib_openshift/src/test/integration/oadm_manage_node.yml deleted file mode 100755 index 3ee13a409..000000000 --- a/roles/lib_openshift/src/test/integration/oadm_manage_node.yml +++ /dev/null @@ -1,69 +0,0 @@ -#!/usr/bin/ansible-playbook --module-path=../../../library/ -# -# ./oadm_manage_node.yml -e "cli_master_test=$OPENSHIFT_MASTER ---- -- hosts: "{{ cli_master_test }}" - gather_facts: no - user: root - tasks: - - name: get list of nodes - oc_obj: - state: list - kind: node - register: obj_out - - - name: Set the node to work with - set_fact: - node_to_test: "{{ obj_out['results']['results'][0]['items'][0]['metadata']['name'] }}" - - - name: list pods from a node - oadm_manage_node: - list_pods: True - node: - - "{{ node_to_test }}" - register: podout - - debug: var=podout - - - assert: - that: "'{{ node_to_test }}' in podout.results.nodes" - msg: Pod data was not returned - - - name: set node to unschedulable - oadm_manage_node: - schedulable: False - node: - - "{{ node_to_test }}" - register: nodeout - - debug: var=nodeout - - - name: assert that schedulable=False - assert: - that: nodeout.results.nodes[0]['schedulable'] == False - msg: "{{ node_to_test }} schedulable set to True" - - - name: get node scheduable - oc_obj: - kind: node - state: list - name: "{{ node_to_test }}" - namespace: None - register: nodeout - - - debug: var=nodeout - - - name: assert that schedulable=False - assert: - that: nodeout.results.results[0]['spec']['unschedulable'] - - - name: set node to schedulable - oadm_manage_node: - schedulable: True - node: - - "{{ node_to_test }}" - register: nodeout - - debug: var=nodeout - - - name: assert that schedulable=False - assert: - that: nodeout.results.nodes[0]['schedulable'] - msg: "{{ node_to_test }} schedulable set to False" diff --git a/roles/lib_openshift/src/test/integration/oc_adm_manage_node.yml b/roles/lib_openshift/src/test/integration/oc_adm_manage_node.yml new file mode 100755 index 000000000..1ed2ef11b --- /dev/null +++ b/roles/lib_openshift/src/test/integration/oc_adm_manage_node.yml @@ -0,0 +1,69 @@ +#!/usr/bin/ansible-playbook --module-path=../../../library/ +# +# ./oc_adm_manage_node.yml -e "cli_master_test=$OPENSHIFT_MASTER +--- +- hosts: "{{ cli_master_test }}" + gather_facts: no + user: root + tasks: + - name: get list of nodes + oc_obj: + state: list + kind: node + register: obj_out + + - name: Set the node to work with + set_fact: + node_to_test: "{{ obj_out['results']['results'][0]['items'][0]['metadata']['name'] }}" + + - name: list pods from a node + oc_adm_manage_node: + list_pods: True + node: + - "{{ node_to_test }}" + register: podout + - debug: var=podout + + - assert: + that: "'{{ node_to_test }}' in podout.results.nodes" + msg: Pod data was not returned + + - name: set node to unschedulable + oc_adm_manage_node: + schedulable: False + node: + - "{{ node_to_test }}" + register: nodeout + - debug: var=nodeout + + - name: assert that schedulable=False + assert: + that: nodeout.results.nodes[0]['schedulable'] == False + msg: "{{ node_to_test }} schedulable set to True" + + - name: get node scheduable + oc_obj: + kind: node + state: list + name: "{{ node_to_test }}" + namespace: None + register: nodeout + + - debug: var=nodeout + + - name: assert that schedulable=False + assert: + that: nodeout.results.results[0]['spec']['unschedulable'] + + - name: set node to schedulable + oc_adm_manage_node: + schedulable: True + node: + - "{{ node_to_test }}" + register: nodeout + - debug: var=nodeout + + - name: assert that schedulable=False + assert: + that: nodeout.results.nodes[0]['schedulable'] + msg: "{{ node_to_test }} schedulable set to False" diff --git a/roles/lib_openshift/src/test/unit/test_oadm_manage_node.py b/roles/lib_openshift/src/test/unit/test_oadm_manage_node.py deleted file mode 100755 index 27d98b869..000000000 --- a/roles/lib_openshift/src/test/unit/test_oadm_manage_node.py +++ /dev/null @@ -1,277 +0,0 @@ -''' - Unit tests for oadm_manage_node -''' - -import os -import six -import sys -import unittest -import mock - -# Removing invalid variable names for tests so that I can -# keep them brief -# pylint: disable=invalid-name,no-name-in-module -# Disable import-error b/c our libraries aren't loaded in jenkins -# pylint: disable=import-error -# place class in our python path -module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501 -sys.path.insert(0, module_path) -from oadm_manage_node import ManageNode, locate_oc_binary # noqa: E402 - - -class ManageNodeTest(unittest.TestCase): - ''' - Test class for oadm_manage_node - ''' - - @mock.patch('oadm_manage_node.Utils.create_tmpfile_copy') - @mock.patch('oadm_manage_node.ManageNode.openshift_cmd') - def test_list_pods(self, mock_openshift_cmd, mock_tmpfile_copy): - ''' Testing a get ''' - params = {'node': ['ip-172-31-49-140.ec2.internal'], - 'schedulable': None, - 'selector': None, - 'pod_selector': None, - 'list_pods': True, - 'kubeconfig': '/etc/origin/master/admin.kubeconfig', - 'evacuate': False, - 'grace_period': False, - 'dry_run': False, - 'force': False} - - pod_list = '''{ - "metadata": {}, - "items": [ - { - "metadata": { - "name": "docker-registry-1-xuhik", - "generateName": "docker-registry-1-", - "namespace": "default", - "selfLink": "/api/v1/namespaces/default/pods/docker-registry-1-xuhik", - "uid": "ae2a25a2-e316-11e6-80eb-0ecdc51fcfc4", - "resourceVersion": "1501", - "creationTimestamp": "2017-01-25T15:55:23Z", - "labels": { - "deployment": "docker-registry-1", - "deploymentconfig": "docker-registry", - "docker-registry": "default" - }, - "annotations": { - "openshift.io/deployment-config.latest-version": "1", - "openshift.io/deployment-config.name": "docker-registry", - "openshift.io/deployment.name": "docker-registry-1", - "openshift.io/scc": "restricted" - } - }, - "spec": {} - }, - { - "metadata": { - "name": "router-1-kp3m3", - "generateName": "router-1-", - "namespace": "default", - "selfLink": "/api/v1/namespaces/default/pods/router-1-kp3m3", - "uid": "9e71f4a5-e316-11e6-80eb-0ecdc51fcfc4", - "resourceVersion": "1456", - "creationTimestamp": "2017-01-25T15:54:56Z", - "labels": { - "deployment": "router-1", - "deploymentconfig": "router", - "router": "router" - }, - "annotations": { - "openshift.io/deployment-config.latest-version": "1", - "openshift.io/deployment-config.name": "router", - "openshift.io/deployment.name": "router-1", - "openshift.io/scc": "hostnetwork" - } - }, - "spec": {} - }] -}''' - - mock_openshift_cmd.side_effect = [ - {"cmd": "/usr/bin/oadm manage-node ip-172-31-49-140.ec2.internal --list-pods", - "results": pod_list, - "returncode": 0} - ] - - mock_tmpfile_copy.side_effect = [ - '/tmp/mocked_kubeconfig', - ] - - results = ManageNode.run_ansible(params, False) - - # returned a single node - self.assertTrue(len(results['results']['nodes']) == 1) - # returned 2 pods - self.assertTrue(len(results['results']['nodes']['ip-172-31-49-140.ec2.internal']) == 2) - - @mock.patch('oadm_manage_node.Utils.create_tmpfile_copy') - @mock.patch('oadm_manage_node.ManageNode.openshift_cmd') - def test_schedulable_false(self, mock_openshift_cmd, mock_tmpfile_copy): - ''' Testing a get ''' - params = {'node': ['ip-172-31-49-140.ec2.internal'], - 'schedulable': False, - 'selector': None, - 'pod_selector': None, - 'list_pods': False, - 'kubeconfig': '/etc/origin/master/admin.kubeconfig', - 'evacuate': False, - 'grace_period': False, - 'dry_run': False, - 'force': False} - - node = [{ - "apiVersion": "v1", - "kind": "Node", - "metadata": { - "creationTimestamp": "2017-01-26T14:34:43Z", - "labels": { - "beta.kubernetes.io/arch": "amd64", - "beta.kubernetes.io/instance-type": "m4.large", - "beta.kubernetes.io/os": "linux", - "failure-domain.beta.kubernetes.io/region": "us-east-1", - "failure-domain.beta.kubernetes.io/zone": "us-east-1c", - "hostname": "opstest-node-compute-0daaf", - "kubernetes.io/hostname": "ip-172-31-51-111.ec2.internal", - "ops_node": "old", - "region": "us-east-1", - "type": "compute" - }, - "name": "ip-172-31-51-111.ec2.internal", - "resourceVersion": "6936", - "selfLink": "/api/v1/nodes/ip-172-31-51-111.ec2.internal", - "uid": "93d7fdfb-e3d4-11e6-a982-0e84250fc302" - }, - "spec": { - "externalID": "i-06bb330e55c699b0f", - "providerID": "aws:///us-east-1c/i-06bb330e55c699b0f", - }}] - - mock_openshift_cmd.side_effect = [ - {"cmd": "/usr/bin/oc get node -o json ip-172-31-49-140.ec2.internal", - "results": node, - "returncode": 0}, - {"cmd": "/usr/bin/oadm manage-node ip-172-31-49-140.ec2.internal --schedulable=False", - "results": "NAME STATUS AGE\n" + - "ip-172-31-49-140.ec2.internal Ready,SchedulingDisabled 5h\n", - "returncode": 0}] - - mock_tmpfile_copy.side_effect = [ - '/tmp/mocked_kubeconfig', - ] - - results = ManageNode.run_ansible(params, False) - - self.assertTrue(results['changed']) - self.assertEqual(results['results']['nodes'][0]['name'], 'ip-172-31-49-140.ec2.internal') - self.assertEqual(results['results']['nodes'][0]['schedulable'], False) - - @unittest.skipIf(six.PY3, 'py2 test only') - @mock.patch('os.path.exists') - @mock.patch('os.environ.get') - def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists): - ''' Testing binary lookup fallback ''' - - mock_env_get.side_effect = lambda _v, _d: '' - - mock_path_exists.side_effect = lambda _: False - - self.assertEqual(locate_oc_binary(), 'oc') - - @unittest.skipIf(six.PY2, 'py3 test only') - @mock.patch('shutil.which') - @mock.patch('os.environ.get') - def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which): - ''' Testing binary lookup in path ''' - - oc_bin = '/usr/bin/oc' - - mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' - - mock_shutil_which.side_effect = lambda _f, path=None: oc_bin - - self.assertEqual(locate_oc_binary(), oc_bin) - - @unittest.skipIf(six.PY3, 'py2 test only') - @mock.patch('os.path.exists') - @mock.patch('os.environ.get') - def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists): - ''' Testing binary lookup in path ''' - - oc_bin = '/usr/bin/oc' - - mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' - - mock_path_exists.side_effect = lambda f: f == oc_bin - - self.assertEqual(locate_oc_binary(), oc_bin) - - @unittest.skipIf(six.PY3, 'py2 test only') - @mock.patch('os.path.exists') - @mock.patch('os.environ.get') - def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists): - ''' Testing binary lookup in /usr/local/bin ''' - - oc_bin = '/usr/local/bin/oc' - - mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' - - mock_path_exists.side_effect = lambda f: f == oc_bin - - self.assertEqual(locate_oc_binary(), oc_bin) - - @unittest.skipIf(six.PY3, 'py2 test only') - @mock.patch('os.path.exists') - @mock.patch('os.environ.get') - def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists): - ''' Testing binary lookup in ~/bin ''' - - oc_bin = os.path.expanduser('~/bin/oc') - - mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' - - mock_path_exists.side_effect = lambda f: f == oc_bin - - self.assertEqual(locate_oc_binary(), oc_bin) - - @unittest.skipIf(six.PY2, 'py3 test only') - @mock.patch('shutil.which') - @mock.patch('os.environ.get') - def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which): - ''' Testing binary lookup fallback ''' - - mock_env_get.side_effect = lambda _v, _d: '' - - mock_shutil_which.side_effect = lambda _f, path=None: None - - self.assertEqual(locate_oc_binary(), 'oc') - - @unittest.skipIf(six.PY2, 'py3 test only') - @mock.patch('shutil.which') - @mock.patch('os.environ.get') - def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which): - ''' Testing binary lookup in /usr/local/bin ''' - - oc_bin = '/usr/local/bin/oc' - - mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' - - mock_shutil_which.side_effect = lambda _f, path=None: oc_bin - - self.assertEqual(locate_oc_binary(), oc_bin) - - @unittest.skipIf(six.PY2, 'py3 test only') - @mock.patch('shutil.which') - @mock.patch('os.environ.get') - def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which): - ''' Testing binary lookup in ~/bin ''' - - oc_bin = os.path.expanduser('~/bin/oc') - - mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' - - mock_shutil_which.side_effect = lambda _f, path=None: oc_bin - - self.assertEqual(locate_oc_binary(), oc_bin) diff --git a/roles/lib_openshift/src/test/unit/test_oc_adm_manage_node.py b/roles/lib_openshift/src/test/unit/test_oc_adm_manage_node.py new file mode 100755 index 000000000..312b1ecbb --- /dev/null +++ b/roles/lib_openshift/src/test/unit/test_oc_adm_manage_node.py @@ -0,0 +1,277 @@ +''' + Unit tests for oc_adm_manage_node +''' + +import os +import six +import sys +import unittest +import mock + +# Removing invalid variable names for tests so that I can +# keep them brief +# pylint: disable=invalid-name,no-name-in-module +# Disable import-error b/c our libraries aren't loaded in jenkins +# pylint: disable=import-error +# place class in our python path +module_path = os.path.join('/'.join(os.path.realpath(__file__).split('/')[:-4]), 'library') # noqa: E501 +sys.path.insert(0, module_path) +from oc_adm_manage_node import ManageNode, locate_oc_binary # noqa: E402 + + +class ManageNodeTest(unittest.TestCase): + ''' + Test class for oc_adm_manage_node + ''' + + @mock.patch('oc_adm_manage_node.Utils.create_tmpfile_copy') + @mock.patch('oc_adm_manage_node.ManageNode.openshift_cmd') + def test_list_pods(self, mock_openshift_cmd, mock_tmpfile_copy): + ''' Testing a get ''' + params = {'node': ['ip-172-31-49-140.ec2.internal'], + 'schedulable': None, + 'selector': None, + 'pod_selector': None, + 'list_pods': True, + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'evacuate': False, + 'grace_period': False, + 'dry_run': False, + 'force': False} + + pod_list = '''{ + "metadata": {}, + "items": [ + { + "metadata": { + "name": "docker-registry-1-xuhik", + "generateName": "docker-registry-1-", + "namespace": "default", + "selfLink": "/api/v1/namespaces/default/pods/docker-registry-1-xuhik", + "uid": "ae2a25a2-e316-11e6-80eb-0ecdc51fcfc4", + "resourceVersion": "1501", + "creationTimestamp": "2017-01-25T15:55:23Z", + "labels": { + "deployment": "docker-registry-1", + "deploymentconfig": "docker-registry", + "docker-registry": "default" + }, + "annotations": { + "openshift.io/deployment-config.latest-version": "1", + "openshift.io/deployment-config.name": "docker-registry", + "openshift.io/deployment.name": "docker-registry-1", + "openshift.io/scc": "restricted" + } + }, + "spec": {} + }, + { + "metadata": { + "name": "router-1-kp3m3", + "generateName": "router-1-", + "namespace": "default", + "selfLink": "/api/v1/namespaces/default/pods/router-1-kp3m3", + "uid": "9e71f4a5-e316-11e6-80eb-0ecdc51fcfc4", + "resourceVersion": "1456", + "creationTimestamp": "2017-01-25T15:54:56Z", + "labels": { + "deployment": "router-1", + "deploymentconfig": "router", + "router": "router" + }, + "annotations": { + "openshift.io/deployment-config.latest-version": "1", + "openshift.io/deployment-config.name": "router", + "openshift.io/deployment.name": "router-1", + "openshift.io/scc": "hostnetwork" + } + }, + "spec": {} + }] +}''' + + mock_openshift_cmd.side_effect = [ + {"cmd": "/usr/bin/oadm manage-node ip-172-31-49-140.ec2.internal --list-pods", + "results": pod_list, + "returncode": 0} + ] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mocked_kubeconfig', + ] + + results = ManageNode.run_ansible(params, False) + + # returned a single node + self.assertTrue(len(results['results']['nodes']) == 1) + # returned 2 pods + self.assertTrue(len(results['results']['nodes']['ip-172-31-49-140.ec2.internal']) == 2) + + @mock.patch('oc_adm_manage_node.Utils.create_tmpfile_copy') + @mock.patch('oc_adm_manage_node.ManageNode.openshift_cmd') + def test_schedulable_false(self, mock_openshift_cmd, mock_tmpfile_copy): + ''' Testing a get ''' + params = {'node': ['ip-172-31-49-140.ec2.internal'], + 'schedulable': False, + 'selector': None, + 'pod_selector': None, + 'list_pods': False, + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'evacuate': False, + 'grace_period': False, + 'dry_run': False, + 'force': False} + + node = [{ + "apiVersion": "v1", + "kind": "Node", + "metadata": { + "creationTimestamp": "2017-01-26T14:34:43Z", + "labels": { + "beta.kubernetes.io/arch": "amd64", + "beta.kubernetes.io/instance-type": "m4.large", + "beta.kubernetes.io/os": "linux", + "failure-domain.beta.kubernetes.io/region": "us-east-1", + "failure-domain.beta.kubernetes.io/zone": "us-east-1c", + "hostname": "opstest-node-compute-0daaf", + "kubernetes.io/hostname": "ip-172-31-51-111.ec2.internal", + "ops_node": "old", + "region": "us-east-1", + "type": "compute" + }, + "name": "ip-172-31-51-111.ec2.internal", + "resourceVersion": "6936", + "selfLink": "/api/v1/nodes/ip-172-31-51-111.ec2.internal", + "uid": "93d7fdfb-e3d4-11e6-a982-0e84250fc302" + }, + "spec": { + "externalID": "i-06bb330e55c699b0f", + "providerID": "aws:///us-east-1c/i-06bb330e55c699b0f", + }}] + + mock_openshift_cmd.side_effect = [ + {"cmd": "/usr/bin/oc get node -o json ip-172-31-49-140.ec2.internal", + "results": node, + "returncode": 0}, + {"cmd": "/usr/bin/oadm manage-node ip-172-31-49-140.ec2.internal --schedulable=False", + "results": "NAME STATUS AGE\n" + + "ip-172-31-49-140.ec2.internal Ready,SchedulingDisabled 5h\n", + "returncode": 0}] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mocked_kubeconfig', + ] + + results = ManageNode.run_ansible(params, False) + + self.assertTrue(results['changed']) + self.assertEqual(results['results']['nodes'][0]['name'], 'ip-172-31-49-140.ec2.internal') + self.assertEqual(results['results']['nodes'][0]['schedulable'], False) + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup fallback ''' + + mock_env_get.side_effect = lambda _v, _d: '' + + mock_path_exists.side_effect = lambda _: False + + self.assertEqual(locate_oc_binary(), 'oc') + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup in path ''' + + oc_bin = '/usr/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_shutil_which.side_effect = lambda _f, path=None: oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup in path ''' + + oc_bin = '/usr/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_path_exists.side_effect = lambda f: f == oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup in /usr/local/bin ''' + + oc_bin = '/usr/local/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_path_exists.side_effect = lambda f: f == oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup in ~/bin ''' + + oc_bin = os.path.expanduser('~/bin/oc') + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_path_exists.side_effect = lambda f: f == oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup fallback ''' + + mock_env_get.side_effect = lambda _v, _d: '' + + mock_shutil_which.side_effect = lambda _f, path=None: None + + self.assertEqual(locate_oc_binary(), 'oc') + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup in /usr/local/bin ''' + + oc_bin = '/usr/local/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_shutil_which.side_effect = lambda _f, path=None: oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup in ~/bin ''' + + oc_bin = os.path.expanduser('~/bin/oc') + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_shutil_which.side_effect = lambda _f, path=None: oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) -- cgit v1.2.3