Skip to content

Commit

Permalink
node module UT added (#146)
Browse files Browse the repository at this point in the history
* node UT added

* fixed UT

* doc update

* Update ansible-test.yml

* Update ansible-test.yml

* increased coverage

* sanity fix

---------

Co-authored-by: Sachin Apagundi <[email protected]>
  • Loading branch information
ShivamSh3 and sachin-apa authored Jan 21, 2025
1 parent 650f7ce commit b7cfabb
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 7 deletions.
19 changes: 12 additions & 7 deletions .github/workflows/ansible-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ansible-version: [stable-2.17]
ansible-version: [stable-2.18]
steps:
- name: Check out code
uses: actions/checkout@v3
Expand Down Expand Up @@ -53,8 +53,11 @@ jobs:
- stable-2.15
- stable-2.16
- stable-2.17
- stable-2.18
- devel
exclude:
- ansible: stable-2.18
python: '3.10'
- ansible: devel
python: '3.10'

Expand All @@ -63,9 +66,11 @@ jobs:
uses: ansible-community/ansible-test-gh-action@release/v1
with:
testing-type: units
coverage: always
coverage: ${{ (matrix.python == '3.11' && matrix.ansible == 'stable-2.18') && 'always' || 'never' }}
ansible-core-version: ${{ matrix.ansible }}
target-python-version: ${{ matrix.python }}
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}


###
Expand All @@ -85,8 +90,11 @@ jobs:
- stable-2.15
- stable-2.16
- stable-2.17
- stable-2.18
- devel
exclude:
- ansible: stable-2.18
python: '3.10'
- ansible: devel
python: '3.10'

Expand All @@ -107,11 +115,8 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11"]
ansible-version: [stable-2.15, stable-2.16, stable-2.17, devel]
exclude:
- ansible-version: devel
python-version: '3.10'
python-version: ["3.11"]
ansible-version: [stable-2.18, devel]

steps:
# Important: This sets up your GITHUB_WORKSPACE environment variable
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/plugins/module_utils/mock_node_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright: (c) 2025, Dell Technologies

# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

"""Mock API responses for PowerScale Node module"""

from __future__ import (absolute_import, division, print_function)

__metaclass__ = type

MODULE_UTILS_PATH = 'ansible_collections.dellemc.powerscale.plugins.modules.node.utils'

NODE = {
"id": 1,
"lnn": 1,
"partitions": {
"count": 1,
"partitions": [
{
"block_size": 1024,
"capacity": 1957516,
"component_devices": "ada0p2",
"mount_point": "/",
"percent_used": "50%",
"statfs": {
"f_namemax": 255,
"f_owner": 0,
"f_type": 53,
"f_version": 538182936
},
"used": 909066
}
]
}
}


def api_exception_msg():
return 'get node info for PowerScale cluster'


def invalid_node_msg():
return 'Please provide a valid Node Id'
99 changes: 99 additions & 0 deletions tests/unit/plugins/modules/test_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Copyright: (c) 2025, Dell Technologies

# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

"""Unit Tests for Node module on PowerScale"""

from __future__ import (absolute_import, division, print_function)

__metaclass__ = type

import pytest
from mock.mock import MagicMock
from ansible_collections.dellemc.powerscale.tests.unit.plugins.module_utils.shared_library.initial_mock \
import utils


from ansible_collections.dellemc.powerscale.plugins.modules.node import ClusterNode, main
from ansible_collections.dellemc.powerscale.tests.unit.plugins.\
module_utils import mock_node_api as MockNodeApi
from ansible_collections.dellemc.powerscale.tests.unit.plugins.module_utils.mock_api_exception \
import MockApiException


class TestClusterNode():
get_node_args = {
'node_id': None,
}

@pytest.fixture
def node_module_mock(self, mocker):
mocker.patch(MockNodeApi.MODULE_UTILS_PATH +
'.ApiException', new=MockApiException)
node_module_mock = ClusterNode()
node_module_mock.module = MagicMock()
return node_module_mock

def test_get_node_info(self, node_module_mock):
self.get_node_args.update({
'node_id': 1,
'state': 'present',
'onefs_host': '1.2.3.4'
})
ob = MagicMock()
ob.node_info = {
'id': 1,
'lnn': 1,
'partitions': {
'count': 1,
'partitions': [
{
'block_size': 1024,
'capacity': 1957516,
'component_devices': 'ada0p2',
'mount_point': '/',
'percent_used': '50%',
'statfs': {
'f_namemax': 255,
'f_owner': 0,
'f_type': 53,
'f_version': 538182936
},
'used': 909066
}
]
}
}
node_module_mock.module.params = self.get_node_args
node_module_mock.cluster_api.get_cluster_node = MagicMock(
return_value=ob)
node_module_mock.perform_module_operation()

assert (
node_module_mock.module.exit_json.call_args[1]['cluster_node_details'])
assert node_module_mock.module.exit_json.call_args[1]['changed'] is False

# Scenario 2: When exception occured
node_module_mock.cluster_api.get_cluster_node = \
MagicMock(side_effect=utils.ApiException)
node_module_mock.perform_module_operation()

assert MockNodeApi.api_exception_msg() in \
node_module_mock.module.fail_json.call_args[1]['msg']

def test_get_node_info_absent(self, node_module_mock):
self.get_node_args.update({
'node_id': 1,
'state': 'absent',
})
node_module_mock.module.params = self.get_node_args
node_module_mock.cluster_api.get_cluster_node = MagicMock(
return_value={})
node_module_mock.perform_module_operation()

assert MockNodeApi.invalid_node_msg() in node_module_mock.module.fail_json.call_args[
1]['msg']

def test_main(self):
ob = main()
assert ob is None # nothing to assert as it doesn't return anything

0 comments on commit b7cfabb

Please sign in to comment.