Skip to content

Commit 05ff861

Browse files
Josef HarteJosef Harte
authored andcommitted
unit tests
1 parent 7bc5cce commit 05ff861

3 files changed

Lines changed: 91 additions & 1 deletion

File tree

Makefile

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

16+
unit-test: venv install
17+
. venv/bin/activate && pytest test/unit
18+
1619
lint: venv
1720
rm -f README.rst
1821
. 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=[

test/unit/test_mas.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
client = dynamic_client()
40+
resources = MagicMock()
41+
catalog_api = MagicMock()
42+
resources.get.side_effect = lambda **kwargs: catalog_api if kwargs['api_version'] == 'operators.coreos.com/v1alpha1' \
43+
and kwargs['kind'] == 'CatalogSource' else None
44+
client.resources = resources
45+
catalog = MagicMock()
46+
catalog_api.get.side_effect = lambda **kwargs: catalog if kwargs['name'] == 'ibm-operator-catalog' \
47+
and kwargs['namespace'] == 'openshift-marketplace' else None
48+
spec = MagicMock()
49+
catalog.spec = spec
50+
spec.displayName = CATALOG_DISPLAY_NAME_VALID
51+
spec.image = IMAGE
52+
current_catalog = mas.getCurrentCatalog(client)
53+
assert current_catalog['displayName'] == CATALOG_DISPLAY_NAME_VALID
54+
assert current_catalog['catalogId'] == CATALOG_ID
55+
assert current_catalog['image'] == IMAGE
56+
57+
58+
def test_get_current_catalog_not_found(dynamic_client):
59+
client = dynamic_client()
60+
resources = MagicMock()
61+
catalog_api = MagicMock()
62+
resources.get.side_effect = lambda **kwargs: catalog_api if kwargs['api_version'] == 'operators.coreos.com/v1alpha1' \
63+
and kwargs['kind'] == 'CatalogSource' else None
64+
client.resources = resources
65+
catalog_api.get.side_effect = NotFoundError(ApiException(status='404'))
66+
assert mas.getCurrentCatalog(client) is None
67+
68+
69+
def test_get_current_catalog_invalid_id(dynamic_client):
70+
client = dynamic_client()
71+
resources = MagicMock()
72+
catalog_api = MagicMock()
73+
resources.get.side_effect = lambda **kwargs: catalog_api if kwargs['api_version'] == 'operators.coreos.com/v1alpha1' \
74+
and kwargs['kind'] == 'CatalogSource' else None
75+
client.resources = resources
76+
catalog = MagicMock()
77+
catalog_api.get.side_effect = lambda **kwargs: catalog if kwargs['name'] == 'ibm-operator-catalog' \
78+
and kwargs['namespace'] == 'openshift-marketplace' else None
79+
spec = MagicMock()
80+
catalog.spec = spec
81+
spec.displayName = CATALOG_DISPLAY_NAME_INVALID
82+
spec.image = IMAGE
83+
current_catalog = mas.getCurrentCatalog(client)
84+
assert current_catalog['displayName'] == CATALOG_DISPLAY_NAME_INVALID
85+
assert current_catalog['image'] == IMAGE
86+
assert current_catalog['catalogId'] is None

0 commit comments

Comments
 (0)