From 19ed620b78e3104034b6c46dcc239c5e9bea1720 Mon Sep 17 00:00:00 2001 From: Josef Harte Date: Fri, 5 Sep 2025 11:32:05 +0100 Subject: [PATCH 1/6] function to list AI Service instances --- src/mas/devops/mas.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/mas/devops/mas.py b/src/mas/devops/mas.py index 46419753..c9e8835f 100644 --- a/src/mas/devops/mas.py +++ b/src/mas/devops/mas.py @@ -134,6 +134,22 @@ def listMasInstances(dynClient: DynamicClient) -> list: return suites +def listAiServiceInstances(dynClient: DynamicClient) -> list: + """ + Get a list of AI Service instances on the cluster + """ + api = dynClient.resources.get(api_version="aiservice.ibm.com/v1", kind="AIServiceApp") + + instances = api.get().to_dict()['items'] + if len(instances) > 0: + logger.info(f"There are {len(instances)} AI Service instances installed on this cluster:") + for instance in instances: + logger.info(f" * {instance['metadata']['name']} v{instance['status']['versions']['reconciled']}") + else: + logger.info("There are no AI Service instances installed on this cluster") + return instances + + def getWorkspaceId(dynClient: DynamicClient, instanceId: str) -> str: """ Get the MAS workspace ID for namespace "mas-{instanceId}-core" From f76af339a3ff76ad78320ea894037567cdd198d6 Mon Sep 17 00:00:00 2001 From: Josef Harte Date: Mon, 8 Sep 2025 13:48:26 +0100 Subject: [PATCH 2/6] refactor --- src/mas/devops/mas.py | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/mas/devops/mas.py b/src/mas/devops/mas.py index c9e8835f..4a7ca8c6 100644 --- a/src/mas/devops/mas.py +++ b/src/mas/devops/mas.py @@ -122,31 +122,28 @@ def listMasInstances(dynClient: DynamicClient) -> list: """ Get a list of MAS instances on the cluster """ - suitesAPI = dynClient.resources.get(api_version="core.mas.ibm.com/v1", kind="Suite") - - suites = suitesAPI.get().to_dict()['items'] - if len(suites) > 0: - logger.info(f"There are {len(suites)} MAS instances installed on this cluster:") - for suite in suites: - logger.info(f" * {suite['metadata']['name']} v{suite['status']['versions']['reconciled']}") - else: - logger.info("There are no MAS instances installed on this cluster") - return suites + return listInstances(dynClient, "core.mas.ibm.com/v1", "Suite") def listAiServiceInstances(dynClient: DynamicClient) -> list: """ Get a list of AI Service instances on the cluster """ - api = dynClient.resources.get(api_version="aiservice.ibm.com/v1", kind="AIServiceApp") + return listInstances(dynClient, "aiservice.ibm.com/v1", "AIServiceApp") + +def listInstances(dynClient: DynamicClient, apiVersion: str, kind: str) -> list: + """ + Get a list of instances of a particular CR on the cluster + """ + api = dynClient.resources.get(api_version=apiVersion, kind=kind) instances = api.get().to_dict()['items'] if len(instances) > 0: - logger.info(f"There are {len(instances)} AI Service instances installed on this cluster:") - for instance in instances: - logger.info(f" * {instance['metadata']['name']} v{instance['status']['versions']['reconciled']}") + logger.info(f"There are {len(instances)} {kind} instances installed on this cluster:") + for instance in instances: + logger.info(f" * {instance['metadata']['name']} v{instance['status']['versions']['reconciled']}") else: - logger.info("There are no AI Service instances installed on this cluster") + logger.info(f"There are no {kind} instances installed on this cluster") return instances From 05ff861f80086f9b32456dc925d737a1ad1d6529 Mon Sep 17 00:00:00 2001 From: Josef Harte Date: Fri, 19 Sep 2025 11:17:27 +0100 Subject: [PATCH 3/6] unit tests --- Makefile | 3 ++ setup.py | 3 +- test/unit/test_mas.py | 86 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 test/unit/test_mas.py diff --git a/Makefile b/Makefile index 6e7d605e..00dcfb2b 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,9 @@ build: venv rm -f README.rst . venv/bin/activate && python -m build +unit-test: venv install + . venv/bin/activate && pytest test/unit + lint: venv rm -f README.rst . venv/bin/activate && flake8 src --count --select=E9,F63,F7,F82 --show-source --statistics && flake8 src --count --exit-zero --max-complexity=10 --max-line-length=200 --statistics diff --git a/setup.py b/setup.py index c46f6a3f..79f8d274 100644 --- a/setup.py +++ b/setup.py @@ -70,7 +70,8 @@ def get_version(rel_path): 'flake8', # MIT License 'pytest', # MIT License 'pytest-mock', # MIT License - 'requests-mock' # Apache Software License + 'requests-mock', # Apache Software License + 'setuptools', # MIT License ] }, classifiers=[ diff --git a/test/unit/test_mas.py b/test/unit/test_mas.py new file mode 100644 index 00000000..ddc63ecc --- /dev/null +++ b/test/unit/test_mas.py @@ -0,0 +1,86 @@ +# ***************************************************************************** +# Copyright (c) 2025 IBM Corporation and other Contributors. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# ***************************************************************************** + +import pytest +from unittest import mock +from unittest.mock import MagicMock +from openshift.dynamic.exceptions import NotFoundError +from kubernetes.client.rest import ApiException + +from mas.devops import mas + + +CATALOG_ID = 'v9-250101-amd64' +CATALOG_DISPLAY_NAME_VALID = f'IBM Maximo Operators {CATALOG_ID}' +CATALOG_DISPLAY_NAME_INVALID = 'invalidCatalogName' +IMAGE = 'testImage' + + +############################################################################## +# WARNING: All tests must be written with strictly no external dependencies. +# Mocks must be used in place of any calls to OpenShift API etc. +############################################################################## + + +@pytest.fixture(autouse=True) +@mock.patch('openshift.dynamic.DynamicClient') +def dynamic_client(client): + return client + + +def test_get_current_catalog_success(dynamic_client): + client = dynamic_client() + resources = MagicMock() + catalog_api = MagicMock() + resources.get.side_effect = lambda **kwargs: catalog_api if kwargs['api_version'] == 'operators.coreos.com/v1alpha1' \ + and kwargs['kind'] == 'CatalogSource' else None + client.resources = resources + catalog = MagicMock() + catalog_api.get.side_effect = lambda **kwargs: catalog if kwargs['name'] == 'ibm-operator-catalog' \ + and kwargs['namespace'] == 'openshift-marketplace' else None + spec = MagicMock() + catalog.spec = spec + spec.displayName = CATALOG_DISPLAY_NAME_VALID + spec.image = IMAGE + current_catalog = mas.getCurrentCatalog(client) + assert current_catalog['displayName'] == CATALOG_DISPLAY_NAME_VALID + assert current_catalog['catalogId'] == CATALOG_ID + assert current_catalog['image'] == IMAGE + + +def test_get_current_catalog_not_found(dynamic_client): + client = dynamic_client() + resources = MagicMock() + catalog_api = MagicMock() + resources.get.side_effect = lambda **kwargs: catalog_api if kwargs['api_version'] == 'operators.coreos.com/v1alpha1' \ + and kwargs['kind'] == 'CatalogSource' else None + client.resources = resources + catalog_api.get.side_effect = NotFoundError(ApiException(status='404')) + assert mas.getCurrentCatalog(client) is None + + +def test_get_current_catalog_invalid_id(dynamic_client): + client = dynamic_client() + resources = MagicMock() + catalog_api = MagicMock() + resources.get.side_effect = lambda **kwargs: catalog_api if kwargs['api_version'] == 'operators.coreos.com/v1alpha1' \ + and kwargs['kind'] == 'CatalogSource' else None + client.resources = resources + catalog = MagicMock() + catalog_api.get.side_effect = lambda **kwargs: catalog if kwargs['name'] == 'ibm-operator-catalog' \ + and kwargs['namespace'] == 'openshift-marketplace' else None + spec = MagicMock() + catalog.spec = spec + spec.displayName = CATALOG_DISPLAY_NAME_INVALID + spec.image = IMAGE + current_catalog = mas.getCurrentCatalog(client) + assert current_catalog['displayName'] == CATALOG_DISPLAY_NAME_INVALID + assert current_catalog['image'] == IMAGE + assert current_catalog['catalogId'] is None From be603aad11eaf9f97c1cace9325dc208223b5156 Mon Sep 17 00:00:00 2001 From: Josef Harte Date: Fri, 19 Sep 2025 11:30:04 +0100 Subject: [PATCH 4/6] add init file --- test/unit/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/unit/__init__.py diff --git a/test/unit/__init__.py b/test/unit/__init__.py new file mode 100644 index 00000000..e69de29b From 8e891c8a02d999f368a405c1ac3dc0c7e8887dbf Mon Sep 17 00:00:00 2001 From: David Parker Date: Thu, 23 Oct 2025 11:22:41 +0100 Subject: [PATCH 5/6] Add some doc/comments & remove unnecessary __init__.py file --- Makefile | 7 ++++++- test/unit/__init__.py | 0 test/unit/test_mas.py | 26 ++++++++++++++++++-------- 3 files changed, 24 insertions(+), 9 deletions(-) delete mode 100644 test/unit/__init__.py diff --git a/Makefile b/Makefile index 00dcfb2b..abfd9557 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,12 @@ build: venv rm -f README.rst . venv/bin/activate && python -m build -unit-test: venv install +# Note: "make install" needs to be ran once before this target will work, but we +# don't want to set it as a dependency otherwise it unnecessarily slows down +# fast implement/test cycles. "make install" created an editable install of the +# package which is linked to the files you are editing so there is no need to +# re-install after each change. +unit-test: . venv/bin/activate && pytest test/unit lint: venv diff --git a/test/unit/__init__.py b/test/unit/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/test/unit/test_mas.py b/test/unit/test_mas.py index ddc63ecc..a50057b2 100644 --- a/test/unit/test_mas.py +++ b/test/unit/test_mas.py @@ -23,10 +23,10 @@ IMAGE = 'testImage' -############################################################################## +# ----------------------------------------------------------------------------- # WARNING: All tests must be written with strictly no external dependencies. # Mocks must be used in place of any calls to OpenShift API etc. -############################################################################## +# ----------------------------------------------------------------------------- @pytest.fixture(autouse=True) @@ -36,19 +36,29 @@ def dynamic_client(client): def test_get_current_catalog_success(dynamic_client): - client = dynamic_client() - resources = MagicMock() + # 1. Create a mock catalogsource resources API catalog_api = MagicMock() + + # 2. Create a mock kubernetes resources API and attach the mock catalogsource API + resources = MagicMock() resources.get.side_effect = lambda **kwargs: catalog_api if kwargs['api_version'] == 'operators.coreos.com/v1alpha1' \ and kwargs['kind'] == 'CatalogSource' else None + + # 3. Create a mock client using the mock resources API + client = dynamic_client() client.resources = resources - catalog = MagicMock() - catalog_api.get.side_effect = lambda **kwargs: catalog if kwargs['name'] == 'ibm-operator-catalog' \ - and kwargs['namespace'] == 'openshift-marketplace' else None + + # 4. Create a mock catalogsource API response for the catalogsource mock spec = MagicMock() - catalog.spec = spec spec.displayName = CATALOG_DISPLAY_NAME_VALID spec.image = IMAGE + catalog = MagicMock() + catalog.spec = spec + + catalog_api.get.side_effect = lambda **kwargs: catalog if kwargs['name'] == 'ibm-operator-catalog' \ + and kwargs['namespace'] == 'openshift-marketplace' else None + + # 5. Call the mock API current_catalog = mas.getCurrentCatalog(client) assert current_catalog['displayName'] == CATALOG_DISPLAY_NAME_VALID assert current_catalog['catalogId'] == CATALOG_ID From ba200d878276d92a5b512bad02300dc2d7ee05a5 Mon Sep 17 00:00:00 2001 From: David Parker Date: Thu, 23 Oct 2025 11:28:13 +0100 Subject: [PATCH 6/6] Fix import error Fix import error: import file mismatch: imported module 'test_mas' has this __file__ attribute: /home/runner/work/python-devops/python-devops/test/src/test_mas.py which is not the same as the test file we want to collect: /home/runner/work/python-devops/python-devops/test/unit/test_mas.py HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules --- Makefile | 2 +- test/{unit/test_mas.py => src/mock/test_mas_mock.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{unit/test_mas.py => src/mock/test_mas_mock.py} (100%) diff --git a/Makefile b/Makefile index abfd9557..d8d765a3 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ build: venv # package which is linked to the files you are editing so there is no need to # re-install after each change. unit-test: - . venv/bin/activate && pytest test/unit + . venv/bin/activate && pytest test/src/mock lint: venv rm -f README.rst diff --git a/test/unit/test_mas.py b/test/src/mock/test_mas_mock.py similarity index 100% rename from test/unit/test_mas.py rename to test/src/mock/test_mas_mock.py