diff options
Diffstat (limited to 'roles/lib_openshift/src')
-rw-r--r-- | roles/lib_openshift/src/ansible/oc_image.py | 5 | ||||
-rw-r--r-- | roles/lib_openshift/src/class/oc_image.py | 33 | ||||
-rw-r--r-- | roles/lib_openshift/src/doc/image | 71 | ||||
-rwxr-xr-x | roles/lib_openshift/src/test/unit/test_oc_image.py (renamed from roles/lib_openshift/src/test/unit/oc_image.py) | 198 |
4 files changed, 157 insertions, 150 deletions
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 <jdiaz@redhat.com>" +- "Ivan Horvath<ihorvath@redhat.com>" 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/test_oc_image.py index 13e850ee1..943c8ca17 100755 --- a/roles/lib_openshift/src/test/unit/oc_image.py +++ b/roles/lib_openshift/src/test/unit/test_oc_image.py @@ -1,19 +1,11 @@ -#!/usr/bin/env python2 ''' - Unit tests for oc label + Unit tests for oc image ''' -# To run -# python -m unittest image -# -# . -# Ran 1 test in 0.597s -# -# OK - import os import sys import unittest import mock +import six # Removing invalid variable names for tests so that I can # keep them brief @@ -23,7 +15,7 @@ import mock # 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 +from oc_image import OCImage, locate_oc_binary # noqa: E402 class OCImageTest(unittest.TestCase): @@ -31,27 +23,18 @@ 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 ''' + ''' Testing a label 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", @@ -99,7 +82,7 @@ class OCImageTest(unittest.TestCase): } } ''' - + mock_cmd.side_effect = [ (0, istream, ''), ] @@ -116,19 +99,15 @@ class OCImageTest(unittest.TestCase): @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 ''' + ''' Testing a image present ''' 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", @@ -176,59 +155,11 @@ class OCImageTest(unittest.TestCase): } } ''' - 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, ''), + (1, '', 'Error from server: imagestreams "oso-rhel7-zagg-web" not found'), (0, '', ''), - (0, istream1, ''), + (0, istream, ''), ] mock_tmpfile_copy.side_effect = [ @@ -238,13 +169,112 @@ class OCImageTest(unittest.TestCase): results = OCImage.run_ansible(params, False) self.assertTrue(results['changed']) - self.assertTrue(results['results']['results']['labels'][0] == - {'storage_pv_quota': 'False', 'awesomens': 'testinglabel'}) + 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') - def tearDown(self): - '''TearDown method''' - pass + mock_env_get.side_effect = lambda _v, _d: '/bin:/usr/bin' + mock_shutil_which.side_effect = lambda _f, path=None: oc_bin -if __name__ == "__main__": - unittest.main() + self.assertEqual(locate_oc_binary(), oc_bin) |