From a11970d30c88d188392ec217c055b6b8169b3769 Mon Sep 17 00:00:00 2001 From: Joel Diaz Date: Tue, 28 Feb 2017 16:22:08 +0000 Subject: clean up and clarify docs/comments update unit tests --- roles/lib_openshift/src/class/oc_user.py | 4 +- roles/lib_openshift/src/doc/user | 66 ++++++++++- .../lib_openshift/src/test/integration/oc_user.yml | 4 +- roles/lib_openshift/src/test/unit/oc_user.py | 117 ------------------- roles/lib_openshift/src/test/unit/test_oc_user.py | 127 +++++++++++++++++++++ 5 files changed, 195 insertions(+), 123 deletions(-) delete mode 100755 roles/lib_openshift/src/test/unit/oc_user.py create mode 100755 roles/lib_openshift/src/test/unit/test_oc_user.py (limited to 'roles/lib_openshift/src') diff --git a/roles/lib_openshift/src/class/oc_user.py b/roles/lib_openshift/src/class/oc_user.py index 17b679289..d9e4eac13 100644 --- a/roles/lib_openshift/src/class/oc_user.py +++ b/roles/lib_openshift/src/class/oc_user.py @@ -19,14 +19,14 @@ class OCUser(OpenShiftCLI): @property def user(self): - ''' property function service''' + ''' property function user''' if not self._user: self.get() return self._user @user.setter def user(self, data): - ''' setter function for yedit var ''' + ''' setter function for user ''' self._user = data def exists(self): diff --git a/roles/lib_openshift/src/doc/user b/roles/lib_openshift/src/doc/user index cae108415..65ee01eb7 100644 --- a/roles/lib_openshift/src/doc/user +++ b/roles/lib_openshift/src/doc/user @@ -37,13 +37,13 @@ options: aliases: [] full_name: description: - - String with the full name/description of th user. + - String with the full name/description of the user. required: false default: None aliases: [] groups: description: - - List of groups the user should be a member of. + - List of groups the user should be a member of. This does not add/update the legacy 'groups' field in the OpenShift user object, but makes user entries into the appropriate OpenShift group object for the given user. required: false default: [] aliases: [] @@ -58,9 +58,71 @@ EXAMPLES = ''' state: present username: johndoe full_name "John Doe" + groups: + - dedicated-admins + register: user_johndoe + +user_johndoe variable will have contents like: +ok: [ded-int-aws-master-61034] => { + "user_johndoe": { + "changed": true, + "results": { + "cmd": "oc -n default get users johndoe -o json", + "results": [ + { + "apiVersion": "v1", + "fullName": "John DOe", + "groups": null, + "identities": null, + "kind": "User", + "metadata": { + "creationTimestamp": "2017-02-28T15:09:21Z", + "name": "johndoe", + "resourceVersion": "848781", + "selfLink": "/oapi/v1/users/johndoe", + "uid": "e23d3300-fdc7-11e6-9e3e-12822d6b7656" + } + } + ], + "returncode": 0 + }, + "state": "present" + } +} +'groups' is empty because this field is the OpenShift user object's 'group' field. - name: Ensure user does not exist oc_user: state: absent username: johndoe + +- name: List user's info + oc_user: + state: list + username: johndoe + register: user_johndoe + +user_johndoe will have contents similar to: +ok: [ded-int-aws-master-61034] => { + "user_johndoe": { + "changed": false, + "results": [ + { + "apiVersion": "v1", + "fullName": "John Doe", + "groups": null, + "identities": null, + "kind": "User", + "metadata": { + "creationTimestamp": "2017-02-28T15:04:44Z", + "name": "johndoe", + "resourceVersion": "848280", + "selfLink": "/oapi/v1/users/johndoe", + "uid": "3d479ad2-fdc7-11e6-9e3e-12822d6b7656" + } + } + ], + "state": "list" + } +} ''' diff --git a/roles/lib_openshift/src/test/integration/oc_user.yml b/roles/lib_openshift/src/test/integration/oc_user.yml index 7d6221e64..ad1f9d188 100755 --- a/roles/lib_openshift/src/test/integration/oc_user.yml +++ b/roles/lib_openshift/src/test/integration/oc_user.yml @@ -142,8 +142,8 @@ register: user_out - name: assert test group created assert: - that: user_out['results'][0]['metadata']['name'] == "integration-test-group" and - user_out['results'][0]['users'] is not defined + that: user_out['results']['results'][0]['metadata']['name'] == "integration-test-group" + msg: "{{ user_out }}" - name: create user with group membership oc_user: diff --git a/roles/lib_openshift/src/test/unit/oc_user.py b/roles/lib_openshift/src/test/unit/oc_user.py deleted file mode 100755 index 920b06d0e..000000000 --- a/roles/lib_openshift/src/test/unit/oc_user.py +++ /dev/null @@ -1,117 +0,0 @@ -#!/usr/bin/env python2 -''' - Unit tests for oc user -''' -# To run -# ./oc_user.py -# -# .. -# ---------------------------------------------------------------------- -# Ran 2 tests in 0.003s -# -# 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_user import OCUser # noqa: E402 - - -class OCUserTest(unittest.TestCase): - ''' - Test class for OCUser - ''' - - def setUp(self): - ''' setup method will create a file and set to known configuration ''' - pass - - @mock.patch('oc_user.OCUser._run') - def test_state_list(self, mock_cmd): - ''' Testing a user list ''' - params = {'username': 'testuser@email.com', - 'state': 'list', - 'kubeconfig': '/etc/origin/master/admin.kubeconfig', - 'full_name': None, - 'groups': [], - 'debug': False} - - user = '''{ - "kind": "User", - "apiVersion": "v1", - "metadata": { - "name": "testuser@email.com", - "selfLink": "/oapi/v1/users/testuser@email.com", - "uid": "02fee6c9-f20d-11e6-b83b-12e1a7285e80", - "resourceVersion": "38566887", - "creationTimestamp": "2017-02-13T16:53:58Z" - }, - "fullName": "Test User", - "identities": null, - "groups": null - }''' - - mock_cmd.side_effect = [ - (0, user, ''), - ] - - results = OCUser.run_ansible(params, False) - - self.assertFalse(results['changed']) - self.assertTrue(results['results'][0]['metadata']['name'] == "testuser@email.com") - - @mock.patch('oc_user.OCUser._run') - def test_state_present(self, mock_cmd): - ''' Testing a user list ''' - params = {'username': 'testuser@email.com', - 'state': 'present', - 'kubeconfig': '/etc/origin/master/admin.kubeconfig', - 'full_name': 'Test User', - 'groups': [], - 'debug': False} - - created_user = '''{ - "kind": "User", - "apiVersion": "v1", - "metadata": { - "name": "testuser@email.com", - "selfLink": "/oapi/v1/users/testuser@email.com", - "uid": "8d508039-f224-11e6-b83b-12e1a7285e80", - "resourceVersion": "38646241", - "creationTimestamp": "2017-02-13T19:42:28Z" - }, - "fullName": "Test User", - "identities": null, - "groups": null - }''' - - mock_cmd.side_effect = [ - (1, '', 'Error from server: users "testuser@email.com" not found'), # get - (1, '', 'Error from server: users "testuser@email.com" not found'), # get - (0, 'user "testuser@email.com" created', ''), # create - (0, created_user, ''), # get - ] - - results = OCUser.run_ansible(params, False) - - self.assertTrue(results['changed']) - self.assertTrue(results['results']['results'][0]['metadata']['name'] == - "testuser@email.com") - - def tearDown(self): - '''TearDown method''' - pass - - -if __name__ == "__main__": - unittest.main() diff --git a/roles/lib_openshift/src/test/unit/test_oc_user.py b/roles/lib_openshift/src/test/unit/test_oc_user.py new file mode 100755 index 000000000..933e96ae2 --- /dev/null +++ b/roles/lib_openshift/src/test/unit/test_oc_user.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python2 +''' + Unit tests for oc user +''' +# To run +# ./oc_user.py +# +# .. +# ---------------------------------------------------------------------- +# Ran 2 tests in 0.003s +# +# 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_user import OCUser, locate_oc_binary # noqa: E402 + + +class OCUserTest(unittest.TestCase): + ''' + Test class for OCUser + ''' + + def setUp(self): + ''' setup method will create a file and set to known configuration ''' + pass + + @mock.patch('oc_user.Utils.create_tmpfile_copy') + @mock.patch('oc_user.OCUser._run') + def test_state_list(self, mock_cmd, mock_tmpfile_copy): + ''' Testing a user list ''' + params = {'username': 'testuser@email.com', + 'state': 'list', + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'full_name': None, + 'groups': [], + 'debug': False} + + user = '''{ + "kind": "User", + "apiVersion": "v1", + "metadata": { + "name": "testuser@email.com", + "selfLink": "/oapi/v1/users/testuser@email.com", + "uid": "02fee6c9-f20d-11e6-b83b-12e1a7285e80", + "resourceVersion": "38566887", + "creationTimestamp": "2017-02-13T16:53:58Z" + }, + "fullName": "Test User", + "identities": null, + "groups": null + }''' + + mock_cmd.side_effect = [ + (0, user, ''), + ] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mocked_kubeconfig', + ] + + results = OCUser.run_ansible(params, False) + + self.assertFalse(results['changed']) + self.assertTrue(results['results'][0]['metadata']['name'] == "testuser@email.com") + + @mock.patch('oc_user.Utils.create_tmpfile_copy') + @mock.patch('oc_user.OCUser._run') + def test_state_present(self, mock_cmd, mock_tmpfile_copy): + ''' Testing a user list ''' + params = {'username': 'testuser@email.com', + 'state': 'present', + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'full_name': 'Test User', + 'groups': [], + 'debug': False} + + created_user = '''{ + "kind": "User", + "apiVersion": "v1", + "metadata": { + "name": "testuser@email.com", + "selfLink": "/oapi/v1/users/testuser@email.com", + "uid": "8d508039-f224-11e6-b83b-12e1a7285e80", + "resourceVersion": "38646241", + "creationTimestamp": "2017-02-13T19:42:28Z" + }, + "fullName": "Test User", + "identities": null, + "groups": null + }''' + + mock_cmd.side_effect = [ + (1, '', 'Error from server: users "testuser@email.com" not found'), # get + (1, '', 'Error from server: users "testuser@email.com" not found'), # get + (0, 'user "testuser@email.com" created', ''), # create + (0, created_user, ''), # get + ] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mocked_kubeconfig', + ] + + results = OCUser.run_ansible(params, False) + + self.assertTrue(results['changed']) + self.assertTrue(results['results']['results'][0]['metadata']['name'] == + "testuser@email.com") + + def tearDown(self): + '''TearDown method''' + pass + + +if __name__ == "__main__": + unittest.main() -- cgit v1.2.3