Skip to content

Commit 4c83e30

Browse files
authored
Merge branch 'stable' into update-251030
2 parents 2a7cb3e + 33debc8 commit 4c83e30

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ build: venv
1313
rm -f README.rst
1414
. venv/bin/activate && python -m build
1515

16+
# Note: "make install" needs to be ran once before this target will work, but we
17+
# don't want to set it as a dependency otherwise it unnecessarily slows down
18+
# fast implement/test cycles. "make install" created an editable install of the
19+
# package which is linked to the files you are editing so there is no need to
20+
# re-install after each change.
21+
unit-test:
22+
. venv/bin/activate && pytest test/src/mock
23+
1624
lint: venv
1725
rm -f README.rst
1826
. 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

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def get_version(rel_path):
7070
'flake8', # MIT License
7171
'pytest', # MIT License
7272
'pytest-mock', # MIT License
73-
'requests-mock' # Apache Software License
73+
'requests-mock', # Apache Software License
74+
'setuptools', # MIT License
7475
]
7576
},
7677
classifiers=[

src/mas/devops/mas.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ def getDefaultStorageClasses(dynClient: DynamicClient) -> dict:
7070
result.rwo = "ocs-external-storagecluster-ceph-rbd"
7171
result.rwx = "ocs-external-storagecluster-cephfs"
7272
break
73+
elif storageClass.metadata.name == "longhorn":
74+
result.provider = "longhorn"
75+
result.providerName = "Longhorn"
76+
result.rwo = "longhorn"
77+
result.rwx = "longhorn"
78+
break
7379
elif storageClass.metadata.name == "nfs-client":
7480
result.provider = "nfs"
7581
result.providerName = "NFS Client"

test/src/mock/test_mas_mock.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)