summaryrefslogtreecommitdiffstats
path: root/roles
diff options
context:
space:
mode:
Diffstat (limited to 'roles')
-rw-r--r--roles/lib_openshift/library/oc_label.py60
-rw-r--r--roles/lib_openshift/src/class/oc_label.py48
-rw-r--r--roles/lib_openshift/src/doc/label4
-rwxr-xr-xroles/lib_openshift/src/test/unit/oc_label.py188
4 files changed, 245 insertions, 55 deletions
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
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()