Skip to content

Commit

Permalink
Add common unit test module and 'dhcp_relay' resource module unit tes…
Browse files Browse the repository at this point in the history
…ts (#148)
  • Loading branch information
ArunSaravananBalachandran authored Mar 7, 2023
1 parent 011dcc7 commit a8914ec
Show file tree
Hide file tree
Showing 12 changed files with 924 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ansible-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,40 @@ jobs:

- name: Run sanity tests
run: ansible-test sanity --docker -v --color

unit:
name: Unit tests
strategy:
matrix:
ansible-version:
- stable-2.14
python-version:
- '3.9'
runs-on: ubuntu-latest
steps:

- name: Check out code
uses: actions/checkout@v1
with:
path: ansible_collections/dellemc/enterprise_sonic

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}

- name: Install ansible-base (${{ matrix.ansible-version }})
run: pip install https://github.com/ansible/ansible/archive/${{ matrix.ansible-version }}.tar.gz --disable-pip-version-check

- name: Install ansible and python prerequistes
run: |
ansible-galaxy collection install ansible.netcommon -p ../../
pip install coverage==6.5.0
- name: Run unit tests using ansible-test
run: ansible-test units -v --color --python ${{ matrix.python-version }} --coverage --docker

- name: Generate coverage report
run: |
ansible-test coverage combine --export tests/output/coverage/
ansible-test coverage report
Empty file added tests/unit/compat/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions tests/unit/compat/mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Compatibility shim for mock imports in modules and module_utils.
This can be removed once support for Python 2.7 is dropped.
"""
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

try:
from unittest.mock import (
call,
patch,
mock_open,
MagicMock,
Mock,
)
except ImportError:
from mock import (
call,
patch,
mock_open,
MagicMock,
Mock,
)
29 changes: 29 additions & 0 deletions tests/unit/compat/unittest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# (c) 2014, Toshio Kuratomi <[email protected]>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

# Allow wildcard import because we really do want to import all of
# unittests's symbols into this compat shim
# pylint: disable=wildcard-import,unused-wildcard-import
from unittest import *

if not hasattr(TestCase, 'assertRaisesRegex'):
# added in Python 3.2
TestCase.assertRaisesRegex = TestCase.assertRaisesRegexp
Empty file added tests/unit/modules/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions tests/unit/modules/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (c) 2017 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import json

import pytest

from ansible.module_utils.six import string_types
from ansible.module_utils._text import to_bytes
from ansible.module_utils.common._collections_compat import MutableMapping


@pytest.fixture
def patch_ansible_module(request, mocker):
if isinstance(request.param, string_types):
args = request.param
elif isinstance(request.param, MutableMapping):
if 'ANSIBLE_MODULE_ARGS' not in request.param:
request.param = {'ANSIBLE_MODULE_ARGS': request.param}
if '_ansible_remote_tmp' not in request.param['ANSIBLE_MODULE_ARGS']:
request.param['ANSIBLE_MODULE_ARGS']['_ansible_remote_tmp'] = '/tmp'
if '_ansible_keep_remote_files' not in request.param['ANSIBLE_MODULE_ARGS']:
request.param['ANSIBLE_MODULE_ARGS']['_ansible_keep_remote_files'] = False
args = json.dumps(request.param)
else:
raise Exception('Malformed data to the patch_ansible_module pytest fixture')

mocker.patch('ansible.module_utils.basic._ANSIBLE_ARGS', to_bytes(args))
Empty file.
Empty file.
Loading

0 comments on commit a8914ec

Please sign in to comment.