|
| 1 | +# ***************************************************************************** |
| 2 | +# Copyright (c) 2025 IBM Corporation and other Contributors. |
| 3 | +# |
| 4 | +# All rights reserved. This program and the accompanying materials |
| 5 | +# are made available under the terms of the Eclipse Public License v1.0 |
| 6 | +# which accompanies this distribution, and is available at |
| 7 | +# http://www.eclipse.org/legal/epl-v10.html |
| 8 | +# |
| 9 | +# ***************************************************************************** |
| 10 | + |
| 11 | +import pytest |
| 12 | +from unittest import mock |
| 13 | +from unittest.mock import MagicMock |
| 14 | +from openshift.dynamic.exceptions import NotFoundError |
| 15 | +from kubernetes.client.rest import ApiException |
| 16 | + |
| 17 | +from mas.devops import mas |
| 18 | + |
| 19 | + |
| 20 | +CATALOG_ID = 'v9-250101-amd64' |
| 21 | +CATALOG_DISPLAY_NAME_VALID = f'IBM Maximo Operators {CATALOG_ID}' |
| 22 | +CATALOG_DISPLAY_NAME_INVALID = 'invalidCatalogName' |
| 23 | +IMAGE = 'testImage' |
| 24 | + |
| 25 | + |
| 26 | +# ----------------------------------------------------------------------------- |
| 27 | +# WARNING: All tests must be written with strictly no external dependencies. |
| 28 | +# Mocks must be used in place of any calls to OpenShift API etc. |
| 29 | +# ----------------------------------------------------------------------------- |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(autouse=True) |
| 33 | +@mock.patch('openshift.dynamic.DynamicClient') |
| 34 | +def dynamic_client(client): |
| 35 | + return client |
| 36 | + |
| 37 | + |
| 38 | +def test_get_current_catalog_success(dynamic_client): |
| 39 | + # 1. Create a mock catalogsource resources API |
| 40 | + catalog_api = MagicMock() |
| 41 | + |
| 42 | + # 2. Create a mock kubernetes resources API and attach the mock catalogsource API |
| 43 | + resources = MagicMock() |
| 44 | + resources.get.side_effect = lambda **kwargs: catalog_api if kwargs['api_version'] == 'operators.coreos.com/v1alpha1' \ |
| 45 | + and kwargs['kind'] == 'CatalogSource' else None |
| 46 | + |
| 47 | + # 3. Create a mock client using the mock resources API |
| 48 | + client = dynamic_client() |
| 49 | + client.resources = resources |
| 50 | + |
| 51 | + # 4. Create a mock catalogsource API response for the catalogsource mock |
| 52 | + spec = MagicMock() |
| 53 | + spec.displayName = CATALOG_DISPLAY_NAME_VALID |
| 54 | + spec.image = IMAGE |
| 55 | + catalog = MagicMock() |
| 56 | + catalog.spec = spec |
| 57 | + |
| 58 | + catalog_api.get.side_effect = lambda **kwargs: catalog if kwargs['name'] == 'ibm-operator-catalog' \ |
| 59 | + and kwargs['namespace'] == 'openshift-marketplace' else None |
| 60 | + |
| 61 | + # 5. Call the mock API |
| 62 | + current_catalog = mas.getCurrentCatalog(client) |
| 63 | + assert current_catalog['displayName'] == CATALOG_DISPLAY_NAME_VALID |
| 64 | + assert current_catalog['catalogId'] == CATALOG_ID |
| 65 | + assert current_catalog['image'] == IMAGE |
| 66 | + |
| 67 | + |
| 68 | +def test_get_current_catalog_not_found(dynamic_client): |
| 69 | + client = dynamic_client() |
| 70 | + resources = MagicMock() |
| 71 | + catalog_api = MagicMock() |
| 72 | + resources.get.side_effect = lambda **kwargs: catalog_api if kwargs['api_version'] == 'operators.coreos.com/v1alpha1' \ |
| 73 | + and kwargs['kind'] == 'CatalogSource' else None |
| 74 | + client.resources = resources |
| 75 | + catalog_api.get.side_effect = NotFoundError(ApiException(status='404')) |
| 76 | + assert mas.getCurrentCatalog(client) is None |
| 77 | + |
| 78 | + |
| 79 | +def test_get_current_catalog_invalid_id(dynamic_client): |
| 80 | + client = dynamic_client() |
| 81 | + resources = MagicMock() |
| 82 | + catalog_api = MagicMock() |
| 83 | + resources.get.side_effect = lambda **kwargs: catalog_api if kwargs['api_version'] == 'operators.coreos.com/v1alpha1' \ |
| 84 | + and kwargs['kind'] == 'CatalogSource' else None |
| 85 | + client.resources = resources |
| 86 | + catalog = MagicMock() |
| 87 | + catalog_api.get.side_effect = lambda **kwargs: catalog if kwargs['name'] == 'ibm-operator-catalog' \ |
| 88 | + and kwargs['namespace'] == 'openshift-marketplace' else None |
| 89 | + spec = MagicMock() |
| 90 | + catalog.spec = spec |
| 91 | + spec.displayName = CATALOG_DISPLAY_NAME_INVALID |
| 92 | + spec.image = IMAGE |
| 93 | + current_catalog = mas.getCurrentCatalog(client) |
| 94 | + assert current_catalog['displayName'] == CATALOG_DISPLAY_NAME_INVALID |
| 95 | + assert current_catalog['image'] == IMAGE |
| 96 | + assert current_catalog['catalogId'] is None |
0 commit comments