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/library/oc_label.py | 60 +++++++++++++++++---------------- 1 file changed, 31 insertions(+), 29 deletions(-) (limited to 'roles/lib_openshift/library/oc_label.py') diff --git a/roles/lib_openshift/library/oc_label.py b/roles/lib_openshift/library/oc_label.py index a876346f4..8840bfefa 100644 --- a/roles/lib_openshift/library/oc_label.py +++ b/roles/lib_openshift/library/oc_label.py @@ -56,7 +56,6 @@ options: state: description: - State represents whether to create, modify, delete, or list - required: true default: present choices: ["present", "absent", "list", "add"] aliases: [] @@ -75,8 +74,7 @@ options: kind: description: - The kind of object that can be managed. - required: True - default: None + default: node choices: - node - pod @@ -888,11 +886,13 @@ class OpenShiftCLI(object): def _run(self, cmds, input_data): ''' Actually executes the command. This makes mocking easier. ''' + curr_env = os.environ.copy() + curr_env.update({'KUBECONFIG': self.kubeconfig}) proc = subprocess.Popen(cmds, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, - env={'KUBECONFIG': self.kubeconfig}) + env=curr_env) stdout, stderr = proc.communicate(input_data) @@ -903,9 +903,9 @@ class OpenShiftCLI(object): '''Base command for oc ''' cmds = [] if oadm: - cmds = ['/usr/bin/oadm'] + cmds = ['oadm'] else: - cmds = ['/usr/bin/oc'] + cmds = ['oc'] if self.all_namespaces: cmds.extend(['--all-namespaces']) @@ -1212,6 +1212,7 @@ class OpenShiftCLIConfig(object): # -*- -*- -*- Begin included fragment: class/oc_label.py -*- -*- -*- + # pylint: disable=too-many-instance-attributes class OCLabel(OpenShiftCLI): ''' Class to wrap the oc command line tools ''' @@ -1233,12 +1234,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.get()['results']['labels'] + return self._curr_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''' @@ -1252,9 +1263,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 @@ -1262,9 +1271,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 @@ -1283,8 +1291,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) @@ -1294,7 +1301,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() @@ -1311,7 +1317,7 @@ class OCLabel(OpenShiftCLI): extra_labels = self.get_extra_current_labels() if len(extra_labels) > 0: - return True + return True else: return False @@ -1357,8 +1363,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 @@ -1366,17 +1373,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 -- cgit v1.2.1