summaryrefslogtreecommitdiffstats
path: root/roles/lib_openshift/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'roles/lib_openshift/src/test')
-rwxr-xr-xroles/lib_openshift/src/test/integration/oc_scale.yml92
-rwxr-xr-xroles/lib_openshift/src/test/unit/oc_scale.py128
-rwxr-xr-xroles/lib_openshift/src/test/unit/oc_version.py50
3 files changed, 244 insertions, 26 deletions
diff --git a/roles/lib_openshift/src/test/integration/oc_scale.yml b/roles/lib_openshift/src/test/integration/oc_scale.yml
new file mode 100755
index 000000000..e96e16820
--- /dev/null
+++ b/roles/lib_openshift/src/test/integration/oc_scale.yml
@@ -0,0 +1,92 @@
+#!/usr/bin/ansible-playbook --module-path=../../../library/
+# ./oc_scale.yml -e "cli_master_test=$OPENSHIFT_MASTER
+---
+- hosts: "{{ cli_master_test }}"
+ gather_facts: no
+ user: root
+ tasks:
+ - name: list oc scale for default router dc
+ oc_scale:
+ state: list
+ name: router
+ namespace: default
+ kind: dc
+ register: scaleout
+ - debug: var=scaleout
+
+ - assert:
+ that:
+ - "'result' in scaleout"
+ - scaleout.result > 0
+ msg: "Did not find 'result' in returned value or result not > 0."
+
+ - name: get the rc for router
+ oc_obj:
+ state: list
+ kind: dc
+ namespace: default
+ selector: router=router
+ register: rcout
+ - debug:
+ msg: "{{ rcout.results.results[0]['items'][-1]['metadata']['name'] }}"
+
+ - name: scale dc to 1
+ oc_scale:
+ name: router
+ namespace: default
+ kind: dc
+ replicas: 1
+ register: scaleout
+ - debug: var=scaleout
+
+ # The preferred method here would be to let the module
+ # detect when its finished and time out
+ - name: let the scale happen
+ pause:
+ seconds: 10
+ when: scaleout.changed
+
+ - name: fetch the current router pods
+ oc_obj:
+ selector: router=router
+ namespace: default
+ kind: pod
+ state: list
+ register: pods
+ - debug: var=pods
+
+ - assert:
+ that:
+ - "'results' in pods and 'results' in pods.results"
+ - "{{ pods.results.results[0]['items']|length }} == 1"
+ msg: "Did not find 1 replica in scale results."
+
+ - name: scale dc to 2
+ oc_scale:
+ name: router
+ namespace: default
+ kind: dc
+ replicas: 2
+ register: scaleout
+ - debug: var=scaleout
+
+ # The preferred method here would be to let the module
+ # detect when its finished and time out
+ - name: let the scale happen
+ pause:
+ seconds: 30
+
+ - name: fetch the current router pods
+ oc_obj:
+ selector: router=router
+ namespace: default
+ kind: pod
+ state: list
+ register: pods
+ - debug: var=pods
+
+ - assert:
+ that:
+ - "'results' in pods and 'results' in pods.results"
+ - "{{ pods.results.results[0]['items']|length }} == 2"
+ msg: "Did not find 1 replica in scale results."
diff --git a/roles/lib_openshift/src/test/unit/oc_scale.py b/roles/lib_openshift/src/test/unit/oc_scale.py
new file mode 100755
index 000000000..c523592de
--- /dev/null
+++ b/roles/lib_openshift/src/test/unit/oc_scale.py
@@ -0,0 +1,128 @@
+#!/usr/bin/env python2
+'''
+ Unit tests for oc scale
+'''
+# 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_scale import OCScale # noqa: E402
+
+
+class OCScaleTest(unittest.TestCase):
+ '''
+ Test class for OCVersion
+ '''
+
+ def setUp(self):
+ ''' setup method will create a file and set to known configuration '''
+ pass
+
+ @mock.patch('oc_scale.OCScale.openshift_cmd')
+ def test_state_list(self, mock_openshift_cmd):
+ ''' Testing a get '''
+ params = {'name': 'router',
+ 'namespace': 'default',
+ 'replicas': 2,
+ 'state': 'list',
+ 'kind': 'dc',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ dc = '''{"kind": "DeploymentConfig",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "namespace": "default",
+ "selfLink": "/oapi/v1/namespaces/default/deploymentconfigs/router",
+ "uid": "a441eedc-e1ae-11e6-a2d5-0e6967f34d42",
+ "resourceVersion": "6558",
+ "generation": 8,
+ "creationTimestamp": "2017-01-23T20:58:07Z",
+ "labels": {
+ "router": "router"
+ }
+ },
+ "spec": {
+ "replicas": 2,
+ }
+ }'''
+
+ mock_openshift_cmd.side_effect = [
+ {"cmd": '/usr/bin/oc get dc router -n default',
+ 'results': dc,
+ 'returncode': 0}]
+
+ results = OCScale.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['result'][0], 2)
+
+ @mock.patch('oc_scale.OCScale.openshift_cmd')
+ def test_scale(self, mock_openshift_cmd):
+ ''' Testing a get '''
+ params = {'name': 'router',
+ 'namespace': 'default',
+ 'replicas': 3,
+ 'state': 'list',
+ 'kind': 'dc',
+ 'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'debug': False}
+
+ dc = '''{"kind": "DeploymentConfig",
+ "apiVersion": "v1",
+ "metadata": {
+ "name": "router",
+ "namespace": "default",
+ "selfLink": "/oapi/v1/namespaces/default/deploymentconfigs/router",
+ "uid": "a441eedc-e1ae-11e6-a2d5-0e6967f34d42",
+ "resourceVersion": "6558",
+ "generation": 8,
+ "creationTimestamp": "2017-01-23T20:58:07Z",
+ "labels": {
+ "router": "router"
+ }
+ },
+ "spec": {
+ "replicas": 3,
+ }
+ }'''
+
+ mock_openshift_cmd.side_effect = [
+ {"cmd": '/usr/bin/oc get dc router -n default',
+ 'results': dc,
+ 'returncode': 0},
+ {"cmd": '/usr/bin/oc create -f /tmp/router -n default',
+ 'results': '',
+ 'returncode': 0}
+ ]
+
+ results = OCScale.run_ansible(params, False)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['result'][0], 3)
+
+ def tearDown(self):
+ '''TearDown method'''
+ pass
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/roles/lib_openshift/src/test/unit/oc_version.py b/roles/lib_openshift/src/test/unit/oc_version.py
index 8d9128187..f927948be 100755
--- a/roles/lib_openshift/src/test/unit/oc_version.py
+++ b/roles/lib_openshift/src/test/unit/oc_version.py
@@ -13,6 +13,7 @@
import os
import sys
import unittest
+import mock
# Removing invalid variable names for tests so that I can
# keep them brief
@@ -25,25 +26,6 @@ sys.path.insert(0, module_path)
from oc_version import OCVersion # noqa: E402
-# pylint: disable=unused-argument
-def oc_cmd_mock(cmd, oadm=False, output=False, output_type='json', input_data=None):
- '''mock command for openshift_cmd'''
- version = '''oc v3.4.0.39
-kubernetes v1.4.0+776c994
-features: Basic-Auth GSSAPI Kerberos SPNEGO
-
-Server https://internal.api.opstest.openshift.com
-openshift v3.4.0.39
-kubernetes v1.4.0+776c994
-'''
- if 'version' in cmd:
- return {'stderr': None,
- 'stdout': version,
- 'returncode': 0,
- 'results': version,
- 'cmd': cmd}
-
-
class OCVersionTest(unittest.TestCase):
'''
Test class for OCVersion
@@ -51,15 +33,31 @@ class OCVersionTest(unittest.TestCase):
def setUp(self):
''' setup method will create a file and set to known configuration '''
- self.oc_ver = OCVersion(None, False)
- self.oc_ver.openshift_cmd = oc_cmd_mock
+ pass
- def test_get(self):
+ @mock.patch('oc_version.OCVersion.openshift_cmd')
+ def test_get(self, mock_openshift_cmd):
''' Testing a get '''
- results = self.oc_ver.get()
- self.assertEqual(results['oc_short'], '3.4')
- self.assertEqual(results['oc_numeric'], '3.4.0.39')
- self.assertEqual(results['kubernetes_numeric'], '1.4.0')
+ params = {'kubeconfig': '/etc/origin/master/admin.kubeconfig',
+ 'state': 'list',
+ 'debug': False}
+
+ mock_openshift_cmd.side_effect = [
+ {"cmd": "oc version",
+ "results": "oc v3.4.0.39\nkubernetes v1.4.0+776c994\n" +
+ "features: Basic-Auth GSSAPI Kerberos SPNEGO\n\n" +
+ "Server https://internal.api.opstest.openshift.com" +
+ "openshift v3.4.0.39\n" +
+ "kubernetes v1.4.0+776c994\n",
+ "returncode": 0}
+ ]
+
+ results = OCVersion.run_ansible(params)
+
+ self.assertFalse(results['changed'])
+ self.assertEqual(results['results']['oc_short'], '3.4')
+ self.assertEqual(results['results']['oc_numeric'], '3.4.0.39')
+ self.assertEqual(results['results']['kubernetes_numeric'], '1.4.0')
def tearDown(self):
'''TearDown method'''