Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add common unit test module and 'dhcp_relay' resource module unit tests #148

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
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