From 98412425bb35ebb42f27ac6f7e1729037e574721 Mon Sep 17 00:00:00 2001 From: Ivan Horvath Date: Tue, 21 Feb 2017 17:26:09 -0500 Subject: ocimage --- roles/lib_openshift/src/sources.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'roles/lib_openshift/src') diff --git a/roles/lib_openshift/src/sources.yml b/roles/lib_openshift/src/sources.yml index 7b6d9f1e0..47951b48a 100644 --- a/roles/lib_openshift/src/sources.yml +++ b/roles/lib_openshift/src/sources.yml @@ -100,6 +100,7 @@ oc_env.py: - class/oc_env.py - ansible/oc_env.py + oc_group.py: - doc/generated - doc/license @@ -111,6 +112,16 @@ oc_group.py: - class/oc_group.py - ansible/oc_group.py +oc_image.py: +- doc/generated +- doc/license +- lib/import.py +- doc/image +- ../../lib_utils/src/class/yedit.py +- lib/base.py +- class/oc_image.py +- ansible/oc_image.py + oc_label.py: - doc/generated - doc/license -- cgit v1.2.3 From ac9aecc926293ba2138b3fed0aa04288ee1ef970 Mon Sep 17 00:00:00 2001 From: Ivan Horvath Date: Thu, 23 Mar 2017 10:01:01 -0400 Subject: first step in ocimage --- roles/lib_openshift/src/ansible/oc_image.py | 35 ++++ roles/lib_openshift/src/class/oc_image.py | 90 ++++++++++ roles/lib_openshift/src/doc/image | 92 ++++++++++ roles/lib_openshift/src/test/unit/oc_image.py | 250 ++++++++++++++++++++++++++ 4 files changed, 467 insertions(+) create mode 100644 roles/lib_openshift/src/ansible/oc_image.py create mode 100644 roles/lib_openshift/src/class/oc_image.py create mode 100644 roles/lib_openshift/src/doc/image create mode 100755 roles/lib_openshift/src/test/unit/oc_image.py (limited to 'roles/lib_openshift/src') diff --git a/roles/lib_openshift/src/ansible/oc_image.py b/roles/lib_openshift/src/ansible/oc_image.py new file mode 100644 index 000000000..f7c52ace0 --- /dev/null +++ b/roles/lib_openshift/src/ansible/oc_image.py @@ -0,0 +1,35 @@ +# pylint: skip-file +# flake8: noqa + + +def main(): + ''' + ansible oc module for image import + ''' + + module = AnsibleModule( + argument_spec=dict( + kubeconfig=dict(default='/etc/origin/master/admin.kubeconfig', type='str'), + state=dict(default='present', type='str', + choices=['present', 'list']), + debug=dict(default=False, type='bool'), + namespace=dict(default='default', type='str'), + registry_url=dict(default=None, type='str'), + image_name=dict(default=None, type='str'), + image_tag=dict(default=None, type='str'), + content_type=dict(default='raw', choices=['yaml', 'json', 'raw'], type='str'), + force=dict(default=False, type='bool'), + ), + + supports_check_mode=True, + ) + + rval = OCImage.run_ansible(module.params, module.check_mode) + if 'failed' in rval: + module.fail_json(**rval) + + module.exit_json(**rval) + + +if __name__ == '__main__': + main() diff --git a/roles/lib_openshift/src/class/oc_image.py b/roles/lib_openshift/src/class/oc_image.py new file mode 100644 index 000000000..619e58057 --- /dev/null +++ b/roles/lib_openshift/src/class/oc_image.py @@ -0,0 +1,90 @@ +# pylint: skip-file + +# pylint: disable=too-many-arguments +class OCImage(OpenShiftCLI): + ''' Class to wrap the oc command line tools + ''' + def __init__(self, + namespace, + registry_url, + image_name, + image_tag, + kubeconfig='/etc/origin/master/admin.kubeconfig', + verbose=False): + ''' Constructor for OpenshiftOC ''' + super(OCImage, self).__init__(namespace, kubeconfig) + self.namespace = namespace + self.registry_url = registry_url + self.image_name = image_name + self.image_tag = image_tag + self.kubeconfig = kubeconfig + self.verbose = verbose + + def get(self): + '''return a image by name ''' + results = self._get('imagestream', self.image_name) + results['exists'] = False + if results['returncode'] == 0 and results['results'][0]: + results['exists'] = True + + if results['returncode'] != 0 and '"%s" not found' % self.image_name in results['stderr']: + results['returncode'] = 0 + + return results + + def create(self, url=None, name=None, tag=None): + '''Create an image ''' + + return self._import_image(url, name, tag) + + + @staticmethod + def run_ansible(params, check_mode): + ''' run the ansible idempotent code ''' + + ocimage = OCImage(params['namespace'], + params['registry_url'], + params['image_name'], + params['image_tag'], + kubeconfig=params['kubeconfig'], + verbose=params['debug']) + + state = params['state'] + + api_rval = ocimage.get() + + ##### + # Get + ##### + if state == 'list': + if api_rval['returncode'] != 0: + return {"failed": True, "msg": api_rval} + return {"changed": False, "results": api_rval, "state": "list"} + + if not params['image_name']: + return {"failed": True, "msg": 'Please specify a name when state is absent|present.'} + + if state == 'present': + + ######## + # Create + ######## + if not Utils.exists(api_rval['results'], params['image_name']): + + if check_mode: + return {"changed": False, "msg": 'CHECK_MODE: Would have performed a create'} + + api_rval = ocimage.create(params['registry_url'], + params['image_name'], + params['image_tag']) + + if api_rval['returncode'] != 0: + return {"failed": True, "msg": api_rval} + + return {"changed": True, "results": api_rval, "state": "present"} + + + # image exists, no change + return {"changed": False, "results": api_rval, "state": "present"} + + return {"failed": True, "changed": False, "results": "Unknown state passed. {0}".format(state), "state": "unknown"} diff --git a/roles/lib_openshift/src/doc/image b/roles/lib_openshift/src/doc/image new file mode 100644 index 000000000..fb3ed2503 --- /dev/null +++ b/roles/lib_openshift/src/doc/image @@ -0,0 +1,92 @@ +# flake8: noqa +# pylint: skip-file + +DOCUMENTATION = ''' +--- +module: oc_label +short_description: Create, modify, and idempotently manage openshift labels. +description: + - Modify openshift labels programmatically. +options: + state: + description: + - State controls the action that will be taken with resource + - 'present' will create or update and object to the desired state + - 'absent' will ensure certain labels are removed + - 'list' will read the labels + - 'add' will insert labels to the already existing labels + default: present + choices: ["present", "absent", "list", "add"] + aliases: [] + kubeconfig: + description: + - The path for the kubeconfig file to use for authentication + required: false + default: /etc/origin/master/admin.kubeconfig + aliases: [] + debug: + description: + - Turn on debug output. + required: false + default: False + aliases: [] + kind: + description: + - The kind of object that can be managed. + default: node + choices: + - node + - pod + - namespace + aliases: [] + labels: + description: + - A list of labels for the resource. + - Each list consists of a key and a value. + - eg, {'key': 'foo', 'value': 'bar'} + required: false + default: None + aliases: [] + selector: + description: + - The selector to apply to the resource query + required: false + default: None + aliases: [] +author: +- "Joel Diaz " +extends_documentation_fragment: [] +''' + +EXAMPLES = ''' +- name: Add a single label to a node's existing labels + oc_label: + name: ip-172-31-5-23.ec2.internal + state: add + kind: node + labels: + - key: logging-infra-fluentd + value: 'true' + +- name: remove a label from a node + oc_label: + name: ip-172-31-5-23.ec2.internal + state: absent + kind: node + labels: + - key: color + value: blue + +- name: Ensure node has these exact labels + oc_label: + name: ip-172-31-5-23.ec2.internal + state: present + kind: node + labels: + - key: color + value: green + - key: type + value: master + - key: environment + value: production +''' diff --git a/roles/lib_openshift/src/test/unit/oc_image.py b/roles/lib_openshift/src/test/unit/oc_image.py new file mode 100755 index 000000000..13e850ee1 --- /dev/null +++ b/roles/lib_openshift/src/test/unit/oc_image.py @@ -0,0 +1,250 @@ +#!/usr/bin/env python2 +''' + Unit tests for oc label +''' +# To run +# python -m unittest image +# +# . +# 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_image import OCImage # noqa: E402 + + +class OCImageTest(unittest.TestCase): + ''' + Test class for OCImage + ''' + + def setUp(self): + ''' setup method will create a file and set to known configuration ''' + pass + + @mock.patch('oc_image.Utils.create_tmpfile_copy') + @mock.patch('oc_image.OCImage._run') + def test_state_list(self, mock_cmd, mock_tmpfile_copy): + ''' Testing a image list ''' + params = {'registry_url': 'registry.ops.openshift.com', + 'image_name': 'oso-rhel7-zagg-web', + 'image_tag': 'int', + 'name': 'default', + 'namespace': 'default', + 'labels': None, + 'state': 'list', + 'kind': 'namespace', + 'selector': None, + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False} + + + istream = '''{ + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "oso-rhel7-zagg-web", + "namespace": "default", + "selfLink": "/oapi/v1/namespaces/default/imagestreams/oso-rhel7-zagg-web", + "uid": "6ca2b199-dcdb-11e6-8ffd-0a5f8e3e32be", + "resourceVersion": "8135944", + "generation": 1, + "creationTimestamp": "2017-01-17T17:36:05Z", + "annotations": { + "openshift.io/image.dockerRepositoryCheck": "2017-01-17T17:36:05Z" + } + }, + "spec": { + "tags": [ + { + "name": "int", + "annotations": null, + "from": { + "kind": "DockerImage", + "name": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web:int" + }, + "generation": 1, + "importPolicy": {} + } + ] + }, + "status": { + "dockerImageRepository": "172.30.183.164:5000/default/oso-rhel7-zagg-web", + "tags": [ + { + "tag": "int", + "items": [ + { + "created": "2017-01-17T17:36:05Z", + "dockerImageReference": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web@sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", + "image": "sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", + "generation": 1 + } + ] + } + ] + } + } + ''' + + mock_cmd.side_effect = [ + (0, istream, ''), + ] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mocked_kubeconfig', + ] + + results = OCImage.run_ansible(params, False) + + self.assertFalse(results['changed']) + self.assertEquals(results['results']['results'][0]['metadata']['name'], 'oso-rhel7-zagg-web') + + @mock.patch('oc_image.Utils.create_tmpfile_copy') + @mock.patch('oc_image.OCImage._run') + def test_state_present(self, mock_cmd, mock_tmpfile_copy): + ''' Testing a image list ''' + params = {'registry_url': 'registry.ops.openshift.com', + 'image_name': 'oso-rhel7-zagg-web', + 'image_tag': 'int', + 'name': 'default', + 'namespace': 'default', + 'state': 'present', + 'kind': 'namespace', + 'selector': None, + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False} + + + istream = '''{ + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "oso-rhel7-zagg-web", + "namespace": "default", + "selfLink": "/oapi/v1/namespaces/default/imagestreams/oso-rhel7-zagg-web", + "uid": "6ca2b199-dcdb-11e6-8ffd-0a5f8e3e32be", + "resourceVersion": "8135944", + "generation": 1, + "creationTimestamp": "2017-01-17T17:36:05Z", + "annotations": { + "openshift.io/image.dockerRepositoryCheck": "2017-01-17T17:36:05Z" + } + }, + "spec": { + "tags": [ + { + "name": "int", + "annotations": null, + "from": { + "kind": "DockerImage", + "name": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web:int" + }, + "generation": 1, + "importPolicy": {} + } + ] + }, + "status": { + "dockerImageRepository": "172.30.183.164:5000/default/oso-rhel7-zagg-web", + "tags": [ + { + "tag": "int", + "items": [ + { + "created": "2017-01-17T17:36:05Z", + "dockerImageReference": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web@sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", + "image": "sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", + "generation": 1 + } + ] + } + ] + } + } + ''' + istream1 = '''{ + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "oso-rhel7-zagg-web", + "namespace": "default", + "selfLink": "/oapi/v1/namespaces/default/imagestreams/oso-rhel7-zagg-web", + "uid": "6ca2b199-dcdb-11e6-8ffd-0a5f8e3e32be", + "resourceVersion": "8135944", + "generation": 1, + "creationTimestamp": "2017-01-17T17:36:05Z", + "annotations": { + "openshift.io/image.dockerRepositoryCheck": "2017-01-17T17:36:05Z" + } + }, + "spec": { + "tags": [ + { + "name": "int", + "annotations": null, + "from": { + "kind": "DockerImage", + "name": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web:int" + }, + "generation": 1, + "importPolicy": {} + } + ] + }, + "status": { + "dockerImageRepository": "172.30.183.164:5000/default/oso-rhel7-zagg-web", + "tags": [ + { + "tag": "int", + "items": [ + { + "created": "2017-01-17T17:36:05Z", + "dockerImageReference": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web@sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", + "image": "sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", + "generation": 1 + } + ] + } + ] + } + } + ''' + + + mock_cmd.side_effect = [ + (0, istream, ''), + (0, '', ''), + (0, istream1, ''), + ] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mocked_kubeconfig', + ] + + results = OCImage.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() -- cgit v1.2.3 From 8cc12c32d35ae0a86f13110d6ea9bdb6a411bbc9 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 23 Mar 2017 16:24:20 -0400 Subject: Fixing up test cases, linting, and added a return. --- roles/lib_openshift/src/ansible/oc_image.py | 5 +- roles/lib_openshift/src/class/oc_image.py | 33 +-- roles/lib_openshift/src/doc/image | 71 ++---- roles/lib_openshift/src/test/unit/oc_image.py | 250 ------------------ roles/lib_openshift/src/test/unit/test_oc_image.py | 280 +++++++++++++++++++++ 5 files changed, 323 insertions(+), 316 deletions(-) delete mode 100755 roles/lib_openshift/src/test/unit/oc_image.py create mode 100755 roles/lib_openshift/src/test/unit/test_oc_image.py (limited to 'roles/lib_openshift/src') diff --git a/roles/lib_openshift/src/ansible/oc_image.py b/roles/lib_openshift/src/ansible/oc_image.py index f7c52ace0..447d62f20 100644 --- a/roles/lib_openshift/src/ansible/oc_image.py +++ b/roles/lib_openshift/src/ansible/oc_image.py @@ -15,9 +15,8 @@ def main(): debug=dict(default=False, type='bool'), namespace=dict(default='default', type='str'), registry_url=dict(default=None, type='str'), - image_name=dict(default=None, type='str'), + image_name=dict(default=None, required=True, type='str'), image_tag=dict(default=None, type='str'), - content_type=dict(default='raw', choices=['yaml', 'json', 'raw'], type='str'), force=dict(default=False, type='bool'), ), @@ -25,11 +24,11 @@ def main(): ) rval = OCImage.run_ansible(module.params, module.check_mode) + if 'failed' in rval: module.fail_json(**rval) module.exit_json(**rval) - if __name__ == '__main__': main() diff --git a/roles/lib_openshift/src/class/oc_image.py b/roles/lib_openshift/src/class/oc_image.py index 619e58057..d25349127 100644 --- a/roles/lib_openshift/src/class/oc_image.py +++ b/roles/lib_openshift/src/class/oc_image.py @@ -1,9 +1,10 @@ # pylint: skip-file +# flake8: noqa + # pylint: disable=too-many-arguments class OCImage(OpenShiftCLI): - ''' Class to wrap the oc command line tools - ''' + ''' Class to import and create an imagestream object''' def __init__(self, namespace, registry_url, @@ -11,13 +12,11 @@ class OCImage(OpenShiftCLI): image_tag, kubeconfig='/etc/origin/master/admin.kubeconfig', verbose=False): - ''' Constructor for OpenshiftOC ''' + ''' Constructor for OCImage''' super(OCImage, self).__init__(namespace, kubeconfig) - self.namespace = namespace self.registry_url = registry_url self.image_name = image_name self.image_tag = image_tag - self.kubeconfig = kubeconfig self.verbose = verbose def get(self): @@ -27,21 +26,21 @@ class OCImage(OpenShiftCLI): if results['returncode'] == 0 and results['results'][0]: results['exists'] = True - if results['returncode'] != 0 and '"%s" not found' % self.image_name in results['stderr']: + if results['returncode'] != 0 and '"{}" not found'.format(self.image_name) in results['stderr']: results['returncode'] = 0 return results def create(self, url=None, name=None, tag=None): '''Create an image ''' - return self._import_image(url, name, tag) + # pylint: disable=too-many-return-statements @staticmethod def run_ansible(params, check_mode): ''' run the ansible idempotent code ''' - + ocimage = OCImage(params['namespace'], params['registry_url'], params['image_name'], @@ -61,14 +60,11 @@ class OCImage(OpenShiftCLI): return {"failed": True, "msg": api_rval} return {"changed": False, "results": api_rval, "state": "list"} - if not params['image_name']: - return {"failed": True, "msg": 'Please specify a name when state is absent|present.'} - + ######## + # Create + ######## if state == 'present': - ######## - # Create - ######## if not Utils.exists(api_rval['results'], params['image_name']): if check_mode: @@ -81,10 +77,15 @@ class OCImage(OpenShiftCLI): if api_rval['returncode'] != 0: return {"failed": True, "msg": api_rval} - return {"changed": True, "results": api_rval, "state": "present"} + # return the newly created object + api_rval = ocimage.get() + if api_rval['returncode'] != 0: + return {"failed": True, "msg": api_rval} + + return {"changed": True, "results": api_rval, "state": "present"} # image exists, no change return {"changed": False, "results": api_rval, "state": "present"} - return {"failed": True, "changed": False, "results": "Unknown state passed. {0}".format(state), "state": "unknown"} + return {"failed": True, "changed": False, "msg": "Unknown state passed. {0}".format(state)} diff --git a/roles/lib_openshift/src/doc/image b/roles/lib_openshift/src/doc/image index fb3ed2503..8a5507ca4 100644 --- a/roles/lib_openshift/src/doc/image +++ b/roles/lib_openshift/src/doc/image @@ -11,12 +11,10 @@ options: state: description: - State controls the action that will be taken with resource - - 'present' will create or update and object to the desired state - - 'absent' will ensure certain labels are removed + - 'present' will create. Does _not_ support update. - 'list' will read the labels - - 'add' will insert labels to the already existing labels default: present - choices: ["present", "absent", "list", "add"] + choices: ["present", "list"] aliases: [] kubeconfig: description: @@ -30,63 +28,42 @@ options: required: false default: False aliases: [] - kind: + registry_url: description: - - The kind of object that can be managed. - default: node - choices: - - node - - pod - - namespace + - The url for the registry so that openshift can pull the image + required: false + default: None aliases: [] - labels: + image_name: description: - - A list of labels for the resource. - - Each list consists of a key and a value. - - eg, {'key': 'foo', 'value': 'bar'} + - The name of the image being imported required: false - default: None + default: False aliases: [] - selector: + image_tag: description: - - The selector to apply to the resource query + - The tag of the image being imported required: false default: None aliases: [] author: -- "Joel Diaz " +- "Ivan Horvath" extends_documentation_fragment: [] ''' EXAMPLES = ''' -- name: Add a single label to a node's existing labels - oc_label: - name: ip-172-31-5-23.ec2.internal - state: add - kind: node - labels: - - key: logging-infra-fluentd - value: 'true' - -- name: remove a label from a node - oc_label: - name: ip-172-31-5-23.ec2.internal - state: absent - kind: node - labels: - - key: color - value: blue +- name: Get an imagestream + oc_image: + name: php55 + state: list + register: imageout -- name: Ensure node has these exact labels - oc_label: - name: ip-172-31-5-23.ec2.internal +- name: create an imagestream + oc_image: state: present - kind: node - labels: - - key: color - value: green - - key: type - value: master - - key: environment - value: production + image_name: php55 + image_tag: int + registry_url: registry.example.com + namespace: default + register: imageout ''' diff --git a/roles/lib_openshift/src/test/unit/oc_image.py b/roles/lib_openshift/src/test/unit/oc_image.py deleted file mode 100755 index 13e850ee1..000000000 --- a/roles/lib_openshift/src/test/unit/oc_image.py +++ /dev/null @@ -1,250 +0,0 @@ -#!/usr/bin/env python2 -''' - Unit tests for oc label -''' -# To run -# python -m unittest image -# -# . -# 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_image import OCImage # noqa: E402 - - -class OCImageTest(unittest.TestCase): - ''' - Test class for OCImage - ''' - - def setUp(self): - ''' setup method will create a file and set to known configuration ''' - pass - - @mock.patch('oc_image.Utils.create_tmpfile_copy') - @mock.patch('oc_image.OCImage._run') - def test_state_list(self, mock_cmd, mock_tmpfile_copy): - ''' Testing a image list ''' - params = {'registry_url': 'registry.ops.openshift.com', - 'image_name': 'oso-rhel7-zagg-web', - 'image_tag': 'int', - 'name': 'default', - 'namespace': 'default', - 'labels': None, - 'state': 'list', - 'kind': 'namespace', - 'selector': None, - 'kubeconfig': '/etc/origin/master/admin.kubeconfig', - 'debug': False} - - - istream = '''{ - "kind": "ImageStream", - "apiVersion": "v1", - "metadata": { - "name": "oso-rhel7-zagg-web", - "namespace": "default", - "selfLink": "/oapi/v1/namespaces/default/imagestreams/oso-rhel7-zagg-web", - "uid": "6ca2b199-dcdb-11e6-8ffd-0a5f8e3e32be", - "resourceVersion": "8135944", - "generation": 1, - "creationTimestamp": "2017-01-17T17:36:05Z", - "annotations": { - "openshift.io/image.dockerRepositoryCheck": "2017-01-17T17:36:05Z" - } - }, - "spec": { - "tags": [ - { - "name": "int", - "annotations": null, - "from": { - "kind": "DockerImage", - "name": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web:int" - }, - "generation": 1, - "importPolicy": {} - } - ] - }, - "status": { - "dockerImageRepository": "172.30.183.164:5000/default/oso-rhel7-zagg-web", - "tags": [ - { - "tag": "int", - "items": [ - { - "created": "2017-01-17T17:36:05Z", - "dockerImageReference": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web@sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", - "image": "sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", - "generation": 1 - } - ] - } - ] - } - } - ''' - - mock_cmd.side_effect = [ - (0, istream, ''), - ] - - mock_tmpfile_copy.side_effect = [ - '/tmp/mocked_kubeconfig', - ] - - results = OCImage.run_ansible(params, False) - - self.assertFalse(results['changed']) - self.assertEquals(results['results']['results'][0]['metadata']['name'], 'oso-rhel7-zagg-web') - - @mock.patch('oc_image.Utils.create_tmpfile_copy') - @mock.patch('oc_image.OCImage._run') - def test_state_present(self, mock_cmd, mock_tmpfile_copy): - ''' Testing a image list ''' - params = {'registry_url': 'registry.ops.openshift.com', - 'image_name': 'oso-rhel7-zagg-web', - 'image_tag': 'int', - 'name': 'default', - 'namespace': 'default', - 'state': 'present', - 'kind': 'namespace', - 'selector': None, - 'kubeconfig': '/etc/origin/master/admin.kubeconfig', - 'debug': False} - - - istream = '''{ - "kind": "ImageStream", - "apiVersion": "v1", - "metadata": { - "name": "oso-rhel7-zagg-web", - "namespace": "default", - "selfLink": "/oapi/v1/namespaces/default/imagestreams/oso-rhel7-zagg-web", - "uid": "6ca2b199-dcdb-11e6-8ffd-0a5f8e3e32be", - "resourceVersion": "8135944", - "generation": 1, - "creationTimestamp": "2017-01-17T17:36:05Z", - "annotations": { - "openshift.io/image.dockerRepositoryCheck": "2017-01-17T17:36:05Z" - } - }, - "spec": { - "tags": [ - { - "name": "int", - "annotations": null, - "from": { - "kind": "DockerImage", - "name": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web:int" - }, - "generation": 1, - "importPolicy": {} - } - ] - }, - "status": { - "dockerImageRepository": "172.30.183.164:5000/default/oso-rhel7-zagg-web", - "tags": [ - { - "tag": "int", - "items": [ - { - "created": "2017-01-17T17:36:05Z", - "dockerImageReference": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web@sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", - "image": "sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", - "generation": 1 - } - ] - } - ] - } - } - ''' - istream1 = '''{ - "kind": "ImageStream", - "apiVersion": "v1", - "metadata": { - "name": "oso-rhel7-zagg-web", - "namespace": "default", - "selfLink": "/oapi/v1/namespaces/default/imagestreams/oso-rhel7-zagg-web", - "uid": "6ca2b199-dcdb-11e6-8ffd-0a5f8e3e32be", - "resourceVersion": "8135944", - "generation": 1, - "creationTimestamp": "2017-01-17T17:36:05Z", - "annotations": { - "openshift.io/image.dockerRepositoryCheck": "2017-01-17T17:36:05Z" - } - }, - "spec": { - "tags": [ - { - "name": "int", - "annotations": null, - "from": { - "kind": "DockerImage", - "name": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web:int" - }, - "generation": 1, - "importPolicy": {} - } - ] - }, - "status": { - "dockerImageRepository": "172.30.183.164:5000/default/oso-rhel7-zagg-web", - "tags": [ - { - "tag": "int", - "items": [ - { - "created": "2017-01-17T17:36:05Z", - "dockerImageReference": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web@sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", - "image": "sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", - "generation": 1 - } - ] - } - ] - } - } - ''' - - - mock_cmd.side_effect = [ - (0, istream, ''), - (0, '', ''), - (0, istream1, ''), - ] - - mock_tmpfile_copy.side_effect = [ - '/tmp/mocked_kubeconfig', - ] - - results = OCImage.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() diff --git a/roles/lib_openshift/src/test/unit/test_oc_image.py b/roles/lib_openshift/src/test/unit/test_oc_image.py new file mode 100755 index 000000000..943c8ca17 --- /dev/null +++ b/roles/lib_openshift/src/test/unit/test_oc_image.py @@ -0,0 +1,280 @@ +''' + Unit tests for oc image +''' +import os +import sys +import unittest +import mock +import six + +# 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_image import OCImage, locate_oc_binary # noqa: E402 + + +class OCImageTest(unittest.TestCase): + ''' + Test class for OCImage + ''' + + @mock.patch('oc_image.Utils.create_tmpfile_copy') + @mock.patch('oc_image.OCImage._run') + def test_state_list(self, mock_cmd, mock_tmpfile_copy): + ''' Testing a label list ''' + params = {'registry_url': 'registry.ops.openshift.com', + 'image_name': 'oso-rhel7-zagg-web', + 'image_tag': 'int', + 'namespace': 'default', + 'state': 'list', + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False} + + istream = '''{ + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "oso-rhel7-zagg-web", + "namespace": "default", + "selfLink": "/oapi/v1/namespaces/default/imagestreams/oso-rhel7-zagg-web", + "uid": "6ca2b199-dcdb-11e6-8ffd-0a5f8e3e32be", + "resourceVersion": "8135944", + "generation": 1, + "creationTimestamp": "2017-01-17T17:36:05Z", + "annotations": { + "openshift.io/image.dockerRepositoryCheck": "2017-01-17T17:36:05Z" + } + }, + "spec": { + "tags": [ + { + "name": "int", + "annotations": null, + "from": { + "kind": "DockerImage", + "name": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web:int" + }, + "generation": 1, + "importPolicy": {} + } + ] + }, + "status": { + "dockerImageRepository": "172.30.183.164:5000/default/oso-rhel7-zagg-web", + "tags": [ + { + "tag": "int", + "items": [ + { + "created": "2017-01-17T17:36:05Z", + "dockerImageReference": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web@sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", + "image": "sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", + "generation": 1 + } + ] + } + ] + } + } + ''' + + mock_cmd.side_effect = [ + (0, istream, ''), + ] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mocked_kubeconfig', + ] + + results = OCImage.run_ansible(params, False) + + self.assertFalse(results['changed']) + self.assertEquals(results['results']['results'][0]['metadata']['name'], 'oso-rhel7-zagg-web') + + @mock.patch('oc_image.Utils.create_tmpfile_copy') + @mock.patch('oc_image.OCImage._run') + def test_state_present(self, mock_cmd, mock_tmpfile_copy): + ''' Testing a image present ''' + params = {'registry_url': 'registry.ops.openshift.com', + 'image_name': 'oso-rhel7-zagg-web', + 'image_tag': 'int', + 'namespace': 'default', + 'state': 'present', + 'kubeconfig': '/etc/origin/master/admin.kubeconfig', + 'debug': False} + + istream = '''{ + "kind": "ImageStream", + "apiVersion": "v1", + "metadata": { + "name": "oso-rhel7-zagg-web", + "namespace": "default", + "selfLink": "/oapi/v1/namespaces/default/imagestreams/oso-rhel7-zagg-web", + "uid": "6ca2b199-dcdb-11e6-8ffd-0a5f8e3e32be", + "resourceVersion": "8135944", + "generation": 1, + "creationTimestamp": "2017-01-17T17:36:05Z", + "annotations": { + "openshift.io/image.dockerRepositoryCheck": "2017-01-17T17:36:05Z" + } + }, + "spec": { + "tags": [ + { + "name": "int", + "annotations": null, + "from": { + "kind": "DockerImage", + "name": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web:int" + }, + "generation": 1, + "importPolicy": {} + } + ] + }, + "status": { + "dockerImageRepository": "172.30.183.164:5000/default/oso-rhel7-zagg-web", + "tags": [ + { + "tag": "int", + "items": [ + { + "created": "2017-01-17T17:36:05Z", + "dockerImageReference": "registry.ops.openshift.com/ops/oso-rhel7-zagg-web@sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", + "image": "sha256:645bab780cf18a9b764d64b02ca65c39d13cb16f19badd0a49a1668629759392", + "generation": 1 + } + ] + } + ] + } + } + ''' + + mock_cmd.side_effect = [ + (1, '', 'Error from server: imagestreams "oso-rhel7-zagg-web" not found'), + (0, '', ''), + (0, istream, ''), + ] + + mock_tmpfile_copy.side_effect = [ + '/tmp/mocked_kubeconfig', + ] + + results = OCImage.run_ansible(params, False) + + self.assertTrue(results['changed']) + self.assertTrue(results['results']['results'][0]['metadata']['name'] == 'oso-rhel7-zagg-web') + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_fallback(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup fallback ''' + + mock_env_get.side_effect = lambda _v, _d: '' + + mock_path_exists.side_effect = lambda _: False + + self.assertEqual(locate_oc_binary(), 'oc') + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_in_path(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup in path ''' + + oc_bin = '/usr/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_path_exists.side_effect = lambda f: f == oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_in_usr_local(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup in /usr/local/bin ''' + + oc_bin = '/usr/local/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_path_exists.side_effect = lambda f: f == oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY3, 'py2 test only') + @mock.patch('os.path.exists') + @mock.patch('os.environ.get') + def test_binary_lookup_in_home(self, mock_env_get, mock_path_exists): + ''' Testing binary lookup in ~/bin ''' + + oc_bin = os.path.expanduser('~/bin/oc') + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_path_exists.side_effect = lambda f: f == oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_fallback_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup fallback ''' + + mock_env_get.side_effect = lambda _v, _d: '' + + mock_shutil_which.side_effect = lambda _f, path=None: None + + self.assertEqual(locate_oc_binary(), 'oc') + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_in_path_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup in path ''' + + oc_bin = '/usr/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_shutil_which.side_effect = lambda _f, path=None: oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_in_usr_local_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup in /usr/local/bin ''' + + oc_bin = '/usr/local/bin/oc' + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_shutil_which.side_effect = lambda _f, path=None: oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) + + @unittest.skipIf(six.PY2, 'py3 test only') + @mock.patch('shutil.which') + @mock.patch('os.environ.get') + def test_binary_lookup_in_home_py3(self, mock_env_get, mock_shutil_which): + ''' Testing binary lookup in ~/bin ''' + + oc_bin = os.path.expanduser('~/bin/oc') + + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + + mock_shutil_which.side_effect = lambda _f, path=None: oc_bin + + self.assertEqual(locate_oc_binary(), oc_bin) -- cgit v1.2.3 From 58f11324d9f0da8975d2a45516d0aeadd1bb0971 Mon Sep 17 00:00:00 2001 From: Kenny Woodson Date: Thu, 23 Mar 2017 17:31:17 -0400 Subject: Adding namespace to doc. --- roles/lib_openshift/src/doc/image | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'roles/lib_openshift/src') diff --git a/roles/lib_openshift/src/doc/image b/roles/lib_openshift/src/doc/image index 8a5507ca4..18cf4e168 100644 --- a/roles/lib_openshift/src/doc/image +++ b/roles/lib_openshift/src/doc/image @@ -3,7 +3,7 @@ DOCUMENTATION = ''' --- -module: oc_label +module: oc_image short_description: Create, modify, and idempotently manage openshift labels. description: - Modify openshift labels programmatically. @@ -22,6 +22,12 @@ options: required: false default: /etc/origin/master/admin.kubeconfig aliases: [] + namespace: + description: + - The namespace where this object lives + required: false + default: default + aliases: [] debug: description: - Turn on debug output. -- cgit v1.2.3