From 94fd71fa8fba70205519e18625975b83373c1535 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Tue, 31 Jan 2017 11:09:57 -0500 Subject: Adding unit test. Fixed redudant calls to get. --- roles/lib_openshift/src/class/oc_label.py | 48 +++---- roles/lib_openshift/src/doc/label | 4 +- roles/lib_openshift/src/test/unit/oc_label.py | 188 ++++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 26 deletions(-) create mode 100755 roles/lib_openshift/src/test/unit/oc_label.py (limited to 'roles/lib_openshift/src') diff --git a/roles/lib_openshift/src/class/oc_label.py b/roles/lib_openshift/src/class/oc_label.py index 9c65b1760..4fc2ffc13 100644 --- a/roles/lib_openshift/src/class/oc_label.py +++ b/roles/lib_openshift/src/class/oc_label.py @@ -1,6 +1,7 @@ # pylint: skip-file # flake8: noqa + # pylint: disable=too-many-instance-attributes class OCLabel(OpenShiftCLI): ''' Class to wrap the oc command line tools ''' @@ -22,12 +23,22 @@ class OCLabel(OpenShiftCLI): self.kind = kind self.kubeconfig = kubeconfig self.labels = labels + self._curr_labels = None self.selector = selector - def get_current_labels(self): - ''' get the current labels on object ''' + @property + def current_labels(self): + '''property for the current labels''' + if self._curr_labels is None: + results = self.get() + self._curr_labels = results['labels'] + + return self._curr_labels - return self.get()['results']['labels'] + @current_labels.setter + def current_labels(self, data): + '''property setter for current labels''' + self._curr_labels = data def compare_labels(self, host_labels): ''' compare incoming labels against current labels''' @@ -41,9 +52,7 @@ class OCLabel(OpenShiftCLI): def all_user_labels_exist(self): ''' return whether all the labels already exist ''' - current_labels = self.get_current_labels() - - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: rbool = self.compare_labels(current_host_labels) if rbool == False: return False @@ -51,9 +60,8 @@ class OCLabel(OpenShiftCLI): def any_label_exists(self): ''' return whether any single label already exists ''' - current_labels = self.get_current_labels() - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: for label in self.labels: if label['key'] in current_host_labels: return True @@ -72,8 +80,7 @@ class OCLabel(OpenShiftCLI): ''' collect all the current label keys ''' current_label_keys = [] - current_labels = self.get_current_labels() - for current_host_labels in current_labels: + for current_host_labels in self.current_labels: for key in current_host_labels.keys(): current_label_keys.append(key) @@ -83,7 +90,6 @@ class OCLabel(OpenShiftCLI): ''' return list of labels that are currently stored, but aren't in user-provided list ''' - current_labels = self.get_current_labels() extra_labels = [] user_label_keys = self.get_user_keys() current_label_keys = self.get_current_label_keys() @@ -100,7 +106,7 @@ class OCLabel(OpenShiftCLI): extra_labels = self.get_extra_current_labels() if len(extra_labels) > 0: - return True + return True else: return False @@ -146,8 +152,9 @@ class OCLabel(OpenShiftCLI): else: label_list.append({}) - result_dict['labels'] = label_list - result_dict['item_count'] = len(label_list) + self.current_labels = label_list + result_dict['labels'] = self.current_labels + result_dict['item_count'] = len(self.current_labels) result['results'] = result_dict return result @@ -155,17 +162,12 @@ class OCLabel(OpenShiftCLI): def cmd_template(self): ''' boilerplate oc command for modifying lables on this object ''' # let's build the cmd with what we have passed in - cmd = [] - if self.namespace: - cmd = cmd + ["-n", self.namespace] + cmd = ["label", self.kind] if self.selector: - cmd = cmd + ["--selector", self.selector] - - cmd = cmd + ["--config", self.kubeconfig, "label", self.kind] - - if self.name: - cmd = cmd + [self.name] + cmd.extend(["--selector", self.selector]) + elif self.name: + cmd.extend([self.name]) return cmd diff --git a/roles/lib_openshift/src/doc/label b/roles/lib_openshift/src/doc/label index 1274d58c1..e206d0d06 100644 --- a/roles/lib_openshift/src/doc/label +++ b/roles/lib_openshift/src/doc/label @@ -11,7 +11,6 @@ options: state: description: - State represents whether to create, modify, delete, or list - required: true default: present choices: ["present", "absent", "list", "add"] aliases: [] @@ -30,8 +29,7 @@ options: kind: description: - The kind of object that can be managed. - required: True - default: None + default: node choices: - node - pod diff --git a/roles/lib_openshift/src/test/unit/oc_label.py b/roles/lib_openshift/src/test/unit/oc_label.py new file mode 100755 index 000000000..875b97c42 --- /dev/null +++ b/roles/lib_openshift/src/test/unit/oc_label.py @@ -0,0 +1,188 @@ +#!/usr/bin/env python2 +''' + Unit tests for oc label +''' +# To run +# python -m unittest version +# +# . +# Ran 1 test in 0.597s +# +# OK + +import os +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_label import OCLabel # noqa: E402 + + +class OCLabelTest(unittest.TestCase): + ''' + Test class for OCLabel + ''' + + def setUp(self): + ''' setup method will create a file and set to known configuration ''' + pass + + @mock.patch('oc_label.OCLabel._run') + def test_state_list(self, mock_cmd): + ''' Testing a label list ''' + params = {'name': 'default', + 'namespace': 'default', + 'labels': None, + 'state': 'list', + 'kind': 'namespace', + 'selector': None, + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False} + + ns = '''{ + "kind": "Namespace", + "apiVersion": "v1", + "metadata": { + "name": "default", + "selfLink": "/api/v1/namespaces/default", + "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22", + "resourceVersion": "403024", + "creationTimestamp": "2017-01-26T14:28:55Z", + "labels": { + "storage_pv_quota": "False" + }, + "annotations": { + "openshift.io/node-selector": "", + "openshift.io/sa.initialized-roles": "true", + "openshift.io/sa.scc.mcs": "s0:c1,c0", + "openshift.io/sa.scc.supplemental-groups": "1000000000/10000", + "openshift.io/sa.scc.uid-range": "1000000000/10000" + } + }, + "spec": { + "finalizers": [ + "kubernetes", + "openshift.io/origin" + ] + }, + "status": { + "phase": "Active" + } + }''' + + + mock_cmd.side_effect = [ + (0, ns, ''), + ] + + results = OCLabel.run_ansible(params, False) + + self.assertFalse(results['changed']) + self.assertTrue(results['results']['labels'] == [{'storage_pv_quota': 'False'}]) + + @mock.patch('oc_label.OCLabel._run') + def test_state_present(self, mock_cmd): + ''' Testing a label list ''' + params = {'name': 'default', + 'namespace': 'default', + 'labels': [ + {'key': 'awesomens', 'value': 'testinglabel'}, + {'key': 'storage_pv_quota', 'value': 'False'} + ], + 'state': 'present', + 'kind': 'namespace', + 'selector': None, + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False} + + ns = '''{ + "kind": "Namespace", + "apiVersion": "v1", + "metadata": { + "name": "default", + "selfLink": "/api/v1/namespaces/default", + "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22", + "resourceVersion": "403024", + "creationTimestamp": "2017-01-26T14:28:55Z", + "labels": { + "storage_pv_quota": "False" + }, + "annotations": { + "openshift.io/node-selector": "", + "openshift.io/sa.initialized-roles": "true", + "openshift.io/sa.scc.mcs": "s0:c1,c0", + "openshift.io/sa.scc.supplemental-groups": "1000000000/10000", + "openshift.io/sa.scc.uid-range": "1000000000/10000" + } + }, + "spec": { + "finalizers": [ + "kubernetes", + "openshift.io/origin" + ] + }, + "status": { + "phase": "Active" + } + }''' + + ns1 = '''{ + "kind": "Namespace", + "apiVersion": "v1", + "metadata": { + "name": "default", + "selfLink": "/api/v1/namespaces/default", + "uid": "c45b9547-e3d3-11e6-ba9c-0eece8f2ce22", + "resourceVersion": "403024", + "creationTimestamp": "2017-01-26T14:28:55Z", + "labels": { + "storage_pv_quota": "False", + "awesomens": "testinglabel" + }, + "annotations": { + "openshift.io/node-selector": "", + "openshift.io/sa.initialized-roles": "true", + "openshift.io/sa.scc.mcs": "s0:c1,c0", + "openshift.io/sa.scc.supplemental-groups": "1000000000/10000", + "openshift.io/sa.scc.uid-range": "1000000000/10000" + } + }, + "spec": { + "finalizers": [ + "kubernetes", + "openshift.io/origin" + ] + }, + "status": { + "phase": "Active" + } + }''' + + + mock_cmd.side_effect = [ + (0, ns, ''), + (0, '', ''), + (0, ns1, ''), + ] + + results = OCLabel.run_ansible(params, False) + + self.assertTrue(results['changed']) + self.assertTrue(results['results']['results']['labels'][0] == \ + {'storage_pv_quota': 'False', 'awesomens': 'testinglabel'}) + + def tearDown(self): + '''TearDown method''' + pass + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3