summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenny Woodson <kwoodson@redhat.com>2017-01-30 16:08:35 -0500
committerKenny Woodson <kwoodson@redhat.com>2017-01-31 14:28:38 -0500
commita4cf9c4119623ad2023aefbe652a424cb567383c (patch)
tree9389fd080bbcce738bc327fc88077000a92dc3a8
parent494f7e410df1641eb598e81f65153867071d5fee (diff)
downloadopenshift-a4cf9c4119623ad2023aefbe652a424cb567383c.tar.gz
openshift-a4cf9c4119623ad2023aefbe652a424cb567383c.tar.bz2
openshift-a4cf9c4119623ad2023aefbe652a424cb567383c.tar.xz
openshift-a4cf9c4119623ad2023aefbe652a424cb567383c.zip
Adding unit test for oc_service. Added environment fix for non-standard oc installs.
-rw-r--r--roles/lib_openshift/library/oadm_manage_node.py8
-rw-r--r--roles/lib_openshift/library/oc_edit.py8
-rw-r--r--roles/lib_openshift/library/oc_obj.py8
-rw-r--r--roles/lib_openshift/library/oc_route.py8
-rw-r--r--roles/lib_openshift/library/oc_scale.py8
-rw-r--r--roles/lib_openshift/library/oc_secret.py8
-rw-r--r--roles/lib_openshift/library/oc_service.py213
-rw-r--r--roles/lib_openshift/library/oc_serviceaccount.py8
-rw-r--r--roles/lib_openshift/library/oc_version.py8
-rw-r--r--roles/lib_openshift/src/class/oc_service.py1
-rw-r--r--roles/lib_openshift/src/lib/base.py8
-rw-r--r--roles/lib_openshift/src/sources.yml20
-rwxr-xr-xroles/lib_openshift/src/test/unit/oc_service.py206
13 files changed, 377 insertions, 135 deletions
diff --git a/roles/lib_openshift/library/oadm_manage_node.py b/roles/lib_openshift/library/oadm_manage_node.py
index 88e573de6..1da88bd0c 100644
--- a/roles/lib_openshift/library/oadm_manage_node.py
+++ b/roles/lib_openshift/library/oadm_manage_node.py
@@ -910,11 +910,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)
@@ -925,9 +927,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'])
diff --git a/roles/lib_openshift/library/oc_edit.py b/roles/lib_openshift/library/oc_edit.py
index 54036231b..bbbefc610 100644
--- a/roles/lib_openshift/library/oc_edit.py
+++ b/roles/lib_openshift/library/oc_edit.py
@@ -938,11 +938,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)
@@ -953,9 +955,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'])
diff --git a/roles/lib_openshift/library/oc_obj.py b/roles/lib_openshift/library/oc_obj.py
index 88880223d..9fd16c03a 100644
--- a/roles/lib_openshift/library/oc_obj.py
+++ b/roles/lib_openshift/library/oc_obj.py
@@ -917,11 +917,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)
@@ -932,9 +934,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'])
diff --git a/roles/lib_openshift/library/oc_route.py b/roles/lib_openshift/library/oc_route.py
index 0419c7bc7..38063886c 100644
--- a/roles/lib_openshift/library/oc_route.py
+++ b/roles/lib_openshift/library/oc_route.py
@@ -942,11 +942,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)
@@ -957,9 +959,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'])
diff --git a/roles/lib_openshift/library/oc_scale.py b/roles/lib_openshift/library/oc_scale.py
index 7eec9f81a..30ddc1801 100644
--- a/roles/lib_openshift/library/oc_scale.py
+++ b/roles/lib_openshift/library/oc_scale.py
@@ -892,11 +892,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)
@@ -907,9 +909,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'])
diff --git a/roles/lib_openshift/library/oc_secret.py b/roles/lib_openshift/library/oc_secret.py
index f71291d53..b2a689def 100644
--- a/roles/lib_openshift/library/oc_secret.py
+++ b/roles/lib_openshift/library/oc_secret.py
@@ -938,11 +938,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)
@@ -953,9 +955,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'])
diff --git a/roles/lib_openshift/library/oc_service.py b/roles/lib_openshift/library/oc_service.py
index b06ff9583..a45e70fa8 100644
--- a/roles/lib_openshift/library/oc_service.py
+++ b/roles/lib_openshift/library/oc_service.py
@@ -119,14 +119,14 @@ options:
description:
- The type of session affinity to use.
required: false
- default: None
+ default: 'None'
aliases: []
service_type:
description:
- The type of service desired. Each option tells the service to behave accordingly.
- https://kubernetes.io/docs/user-guide/services/
required: false
- default: None
+ default: ClusterIP
choices:
- ClusterIP
- NodePort
@@ -941,18 +941,32 @@ class OpenShiftCLI(object):
cmd.append('--confirm')
return self.openshift_cmd(cmd)
+ 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=curr_env)
+
+ stdout, stderr = proc.communicate(input_data)
+
+ return proc.returncode, stdout, stderr
+
# pylint: disable=too-many-arguments,too-many-branches
def openshift_cmd(self, cmd, oadm=False, output=False, output_type='json', input_data=None):
'''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'])
- elif self.namespace:
+ elif self.namespace is not None and self.namespace.lower() not in ['none', 'emtpy']: # E501
cmds.extend(['-n', self.namespace])
cmds.extend(cmd)
@@ -964,18 +978,13 @@ class OpenShiftCLI(object):
if self.verbose:
print(' '.join(cmds))
- proc = subprocess.Popen(cmds,
- stdin=subprocess.PIPE,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- env={'KUBECONFIG': self.kubeconfig})
+ returncode, stdout, stderr = self._run(cmds, input_data)
- stdout, stderr = proc.communicate(input_data)
- rval = {"returncode": proc.returncode,
+ rval = {"returncode": returncode,
"results": results,
"cmd": ' '.join(cmds)}
- if proc.returncode == 0:
+ if returncode == 0:
if output:
if output_type == 'json':
try:
@@ -1383,12 +1392,11 @@ class Service(Yedit):
'''add cluster ip'''
self.put(Service.portal_ip, pip)
-
-
# -*- -*- -*- End included fragment: lib/service.py -*- -*- -*-
# -*- -*- -*- Begin included fragment: class/oc_service.py -*- -*- -*-
+
# pylint: disable=too-many-instance-attributes
class OCService(OpenShiftCLI):
''' Class to wrap the oc command line tools '''
@@ -1443,6 +1451,7 @@ class OCService(OpenShiftCLI):
result['clusterip'] = self.service.get('spec.clusterIP')
elif 'services \"%s\" not found' % self.config.name in result['stderr']:
result['clusterip'] = ''
+ result['returncode'] = 0
return result
@@ -1467,7 +1476,92 @@ class OCService(OpenShiftCLI):
skip = ['clusterIP', 'portalIP']
return not Utils.check_def_equal(self.user_svc.yaml_dict, self.service.yaml_dict, skip_keys=skip, debug=True)
+ @staticmethod
+ def run_ansible(params, check_mode):
+ '''Run the idempotent ansible code'''
+ oc_svc = OCService(params['name'],
+ params['namespace'],
+ params['labels'],
+ params['selector'],
+ params['clusterip'],
+ params['portalip'],
+ params['ports'],
+ params['session_affinity'],
+ params['service_type'])
+
+ state = params['state']
+
+ api_rval = oc_svc.get()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval}
+
+ #####
+ # Get
+ #####
+ if state == 'list':
+ return {'changed': False, 'results': api_rval, 'state': state}
+
+ ########
+ # Delete
+ ########
+ if state == 'absent':
+ if oc_svc.exists():
+
+ if check_mode:
+ return {'changed': True,
+ 'msg': 'CHECK_MODE: Would have performed a delete.'} # noqa: E501
+
+ api_rval = oc_svc.delete()
+
+ return {'changed': True, 'results': api_rval, 'state': state}
+
+ return {'changed': False, 'state': state}
+
+ if state == 'present':
+ ########
+ # Create
+ ########
+ if not oc_svc.exists():
+
+ if check_mode:
+ return {'changed': True,
+ 'msg': 'CHECK_MODE: Would have performed a create.'} # noqa: E501
+
+ # Create it here
+ api_rval = oc_svc.create()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval}
+
+ # return the created object
+ api_rval = oc_svc.get()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval}
+ return {'changed': True, 'results': api_rval, 'state': state}
+
+ ########
+ # Update
+ ########
+ if oc_svc.needs_update():
+ api_rval = oc_svc.update()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval}
+
+ # return the created object
+ api_rval = oc_svc.get()
+
+ if api_rval['returncode'] != 0:
+ return {'failed': True, 'msg': api_rval}
+
+ return {'changed': True, 'results': api_rval, 'state': state}
+
+ return {'changed': False, 'results': api_rval, 'state': state}
+
+ return {'failed': True, 'msg': 'UNKNOWN state passed. [%s]' % state}
# -*- -*- -*- End included fragment: class/oc_service.py -*- -*- -*-
@@ -1496,92 +1590,15 @@ def main():
),
supports_check_mode=True,
)
- oc_svc = OCService(module.params['name'],
- module.params['namespace'],
- module.params['labels'],
- module.params['selector'],
- module.params['clusterip'],
- module.params['portalip'],
- module.params['ports'],
- module.params['session_affinity'],
- module.params['service_type'])
-
- state = module.params['state']
-
- api_rval = oc_svc.get()
-
- #####
- # Get
- #####
- if state == 'list':
- module.exit_json(changed=False, results=api_rval, state="list")
-
- ########
- # Delete
- ########
- if state == 'absent':
- if oc_svc.exists():
-
- if module.check_mode:
- module.exit_json(changed=False, msg='Would have performed a delete.')
-
- api_rval = oc_svc.delete()
-
- module.exit_json(changed=True, results=api_rval, state="absent")
-
- module.exit_json(changed=False, state="absent")
-
- if state == 'present':
- ########
- # Create
- ########
- if not oc_svc.exists():
-
- if module.check_mode:
- module.exit_json(changed=False, msg='Would have performed a create.')
-
- # Create it here
- api_rval = oc_svc.create()
-
- if api_rval['returncode'] != 0:
- module.fail_json(msg=api_rval)
-
- # return the created object
- api_rval = oc_svc.get()
-
- if api_rval['returncode'] != 0:
- module.fail_json(msg=api_rval)
-
- module.exit_json(changed=True, results=api_rval, state="present")
-
- ########
- # Update
- ########
- if oc_svc.needs_update():
- api_rval = oc_svc.update()
-
- if api_rval['returncode'] != 0:
- module.fail_json(msg=api_rval)
-
- # return the created object
- api_rval = oc_svc.get()
-
- if api_rval['returncode'] != 0:
- module.fail_json(msg=api_rval)
-
- module.exit_json(changed=True, results=api_rval, state="present")
- module.exit_json(changed=False, results=api_rval, state="present")
+ rval = OCService.run_ansible(module.params, module.check_mode)
+ if 'failed' in rval:
+ return module.fail_json(**rval)
- module.exit_json(failed=True,
- changed=False,
- results='Unknown state passed. %s' % state,
- state="unknown")
+ return module.exit_json(**rval)
-# pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import, locally-disabled
-# import module snippets. This are required
-from ansible.module_utils.basic import *
-main()
+if __name__ == '__main__':
+ main()
# -*- -*- -*- End included fragment: ansible/oc_service.py -*- -*- -*-
diff --git a/roles/lib_openshift/library/oc_serviceaccount.py b/roles/lib_openshift/library/oc_serviceaccount.py
index ff93b9b23..55ab099f8 100644
--- a/roles/lib_openshift/library/oc_serviceaccount.py
+++ b/roles/lib_openshift/library/oc_serviceaccount.py
@@ -890,11 +890,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)
@@ -905,9 +907,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'])
diff --git a/roles/lib_openshift/library/oc_version.py b/roles/lib_openshift/library/oc_version.py
index 4a20b72fe..63d16d451 100644
--- a/roles/lib_openshift/library/oc_version.py
+++ b/roles/lib_openshift/library/oc_version.py
@@ -862,11 +862,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)
@@ -877,9 +879,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'])
diff --git a/roles/lib_openshift/src/class/oc_service.py b/roles/lib_openshift/src/class/oc_service.py
index 6871e00d1..1b92a45b2 100644
--- a/roles/lib_openshift/src/class/oc_service.py
+++ b/roles/lib_openshift/src/class/oc_service.py
@@ -56,6 +56,7 @@ class OCService(OpenShiftCLI):
result['clusterip'] = self.service.get('spec.clusterIP')
elif 'services \"%s\" not found' % self.config.name in result['stderr']:
result['clusterip'] = ''
+ result['returncode'] = 0
return result
diff --git a/roles/lib_openshift/src/lib/base.py b/roles/lib_openshift/src/lib/base.py
index d0d6c7afc..55f7d3146 100644
--- a/roles/lib_openshift/src/lib/base.py
+++ b/roles/lib_openshift/src/lib/base.py
@@ -207,11 +207,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)
@@ -222,9 +224,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'])
diff --git a/roles/lib_openshift/src/sources.yml b/roles/lib_openshift/src/sources.yml
index b363e0798..32c3711d1 100644
--- a/roles/lib_openshift/src/sources.yml
+++ b/roles/lib_openshift/src/sources.yml
@@ -63,27 +63,27 @@ oc_secret.py:
- class/oc_secret.py
- ansible/oc_secret.py
-oc_service.py:
+oc_serviceaccount.py:
- doc/generated
- doc/license
- lib/import.py
-- doc/service
+- doc/serviceaccount
- ../../lib_utils/src/class/yedit.py
- lib/base.py
-- lib/service.py
-- class/oc_service.py
-- ansible/oc_service.py
+- lib/serviceaccount.py
+- class/oc_serviceaccount.py
+- ansible/oc_serviceaccount.py
-oc_serviceaccount.py:
+oc_service.py:
- doc/generated
- doc/license
- lib/import.py
-- doc/serviceaccount
+- doc/service
- ../../lib_utils/src/class/yedit.py
- lib/base.py
-- lib/serviceaccount.py
-- class/oc_serviceaccount.py
-- ansible/oc_serviceaccount.py
+- lib/service.py
+- class/oc_service.py
+- ansible/oc_service.py
oc_version.py:
- doc/generated
diff --git a/roles/lib_openshift/src/test/unit/oc_service.py b/roles/lib_openshift/src/test/unit/oc_service.py
new file mode 100755
index 000000000..69f7dd49c
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/oc_service.py
@@ -0,0 +1,206 @@
+#!/usr/bin/env python2
+'''
+ Unit tests for oc service
+'''
+# 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_service import OCService # noqa: E402
+
+
+# pylint: disable=too-many-public-methods
+class OCServiceTest(unittest.TestCase):
+ '''
+ Test class for OCService
+ '''
+
+ def setUp(self):
+ ''' setup method will create a file and set to known configuration '''
+ pass
+
+ @mock.patch('oc_service.OCService._run')
+ def test_state_list(self, mock_cmd):
+ ''' Testing a get '''
+ params = {'name': 'router',
+ 'namespace': 'default',
+ 'ports': None,
+ 'state': 'list',
+ 'labels': None,
+ 'clusterip': None,
+ 'portalip': None,
+ 'selector': None,
+ 'session_affinity': None,
+ 'service_type': None,
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ service = '''{
+ "kind": "Service",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "namespace": "default",
+ "selfLink": "/api/v1/namespaces/default/services/router",
+ "uid": "fabd2440-e3d8-11e6-951c-0e3dd518cefa",
+ "resourceVersion": "3206",
+ "creationTimestamp": "2017-01-26T15:06:14Z",
+ "labels": {
+ "router": "router"
+ }
+ },
+ "spec": {
+ "ports": [
+ {
+ "name": "80-tcp",
+ "protocol": "TCP",
+ "port": 80,
+ "targetPort": 80
+ },
+ {
+ "name": "443-tcp",
+ "protocol": "TCP",
+ "port": 443,
+ "targetPort": 443
+ },
+ {
+ "name": "1936-tcp",
+ "protocol": "TCP",
+ "port": 1936,
+ "targetPort": 1936
+ },
+ {
+ "name": "5000-tcp",
+ "protocol": "TCP",
+ "port": 5000,
+ "targetPort": 5000
+ }
+ ],
+ "selector": {
+ "router": "router"
+ },
+ "clusterIP": "172.30.129.161",
+ "type": "ClusterIP",
+ "sessionAffinity": "None"
+ },
+ "status": {
+ "loadBalancer": {}
+ }
+ }'''
+ mock_cmd.side_effect = [
+ (0, service, '')
+ ]
+
+ results = OCService.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'router')
+
+ @mock.patch('oc_service.OCService._run')
+ def test_create(self, mock_cmd):
+ ''' Testing a create service '''
+ params = {'name': 'router',
+ 'namespace': 'default',
+ 'ports': {'name': '9000-tcp',
+ 'port': 9000,
+ 'protocol': 'TCP',
+ 'targetPOrt': 9000},
+ 'state': 'present',
+ 'labels': None,
+ 'clusterip': None,
+ 'portalip': None,
+ 'selector': {'router': 'router'},
+ 'session_affinity': 'ClientIP',
+ 'service_type': 'ClusterIP',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ service = '''{
+ "kind": "Service",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "namespace": "default",
+ "selfLink": "/api/v1/namespaces/default/services/router",
+ "uid": "fabd2440-e3d8-11e6-951c-0e3dd518cefa",
+ "resourceVersion": "3206",
+ "creationTimestamp": "2017-01-26T15:06:14Z",
+ "labels": {
+ "router": "router"
+ }
+ },
+ "spec": {
+ "ports": [
+ {
+ "name": "80-tcp",
+ "protocol": "TCP",
+ "port": 80,
+ "targetPort": 80
+ },
+ {
+ "name": "443-tcp",
+ "protocol": "TCP",
+ "port": 443,
+ "targetPort": 443
+ },
+ {
+ "name": "1936-tcp",
+ "protocol": "TCP",
+ "port": 1936,
+ "targetPort": 1936
+ },
+ {
+ "name": "5000-tcp",
+ "protocol": "TCP",
+ "port": 5000,
+ "targetPort": 5000
+ }
+ ],
+ "selector": {
+ "router": "router"
+ },
+ "clusterIP": "172.30.129.161",
+ "type": "ClusterIP",
+ "sessionAffinity": "None"
+ },
+ "status": {
+ "loadBalancer": {}
+ }
+ }'''
+ mock_cmd.side_effect = [
+ (1, '', 'Error from server: services "router" not found'),
+ (1, '', 'Error from server: services "router" not found'),
+ (0, service, ''),
+ (0, service, '')
+ ]
+
+ results = OCService.run_ansible(params, False)
+
+ self.assertTrue(results['changed'])
+ self.assertTrue(results['results']['returncode'] == 0)
+ self.assertEqual(results['results']['results'][0]['metadata']['name'], 'router')
+
+ def tearDown(self):
+ '''TearDown method'''
+ pass
+
+
+if __name__ == "__main__":
+ unittest.main()