From a4cf9c4119623ad2023aefbe652a424cb567383c Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Mon, 30 Jan 2017 16:08:35 -0500 Subject: Adding unit test for oc_service. Added environment fix for non-standard oc installs. --- roles/lib_openshift/src/class/oc_service.py | 1 + roles/lib_openshift/src/lib/base.py | 8 +- roles/lib_openshift/src/sources.yml | 20 +-- roles/lib_openshift/src/test/unit/oc_service.py | 206 ++++++++++++++++++++++++ 4 files changed, 222 insertions(+), 13 deletions(-) create mode 100755 roles/lib_openshift/src/test/unit/oc_service.py (limited to 'roles/lib_openshift/src') 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() -- cgit v1.2.3