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

Tests #1

Closed
wants to merge 7 commits into from
Closed
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
73 changes: 73 additions & 0 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
name: 🧪
on:
push:
pull_request:

jobs:
tests:
name: >-
Ⓐ${{ matrix.ansible-core-version }}
@
🐍${{ matrix.python-version }}:
${{ matrix.testing-type }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
docker-image:
- default
git-checkout-ref:
-
pre-test-cmd:
-
target:
-
target-python-version:
-
test-deps:
-
include:
- ansible-core-version: stable-2.12
python-version: '3.8'
testing-type: sanity
- ansible-core-version: devel
python-version: '3.9'
testing-type: units
- ansible-core-version: stable-2.13
python-version: '3.10'
testing-type: integration
steps:
- name: Checkout
uses: actions/checkout@v3
with:
path: .tmp-action-checkout/

- name: Run ${{ matrix.testing-type }} tests
uses: ./.tmp-action-checkout/
with:
ansible-core-version: ${{ matrix.ansible-core-version }}
collection-root: .internal/ansible/ansible_collections/internal/test
docker-image: ${{ matrix.docker-image }}
git-checkout-ref: ${{ matrix.git-checkout-ref }}
pre-test-cmd: ${{ matrix.pre-test-cmd }}
python-version: ${{ matrix.python-version }}
target: ${{ matrix.target }}
target-python-version: ${{ matrix.target-python-version }}
testing-type: ${{ matrix.testing-type }}
test-deps: ${{ matrix.test-deps }}

check: # This job does nothing and is only used for the branch protection
if: always()

needs:
- tests

runs-on: Ubuntu-latest

steps:
- name: Decide whether the needed jobs succeeded or failed
uses: re-actors/alls-green@release/v1
with:
jobs: ${{ toJSON(needs) }}
...
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
namespace: internal
name: test
description: A collection internal to this repository for running tests.
version: 0.1.0
readme: README.md
authors:
- Felix Fontein (@felixfontein)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2022 Felix Fontein <[email protected]>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function
__metaclass__ = type


DOCUMENTATION = r'''
---
module: test
short_description: Test module
version_added: 0.1.0
author:
- Felix Fontein (@felixfontein)
description:
- Just a test.
notes:
- Does not support C(check_mode).

options:
test:
description: Some string that is echoed back.
required: true
type: str
'''

EXAMPLES = r'''
- name: Does nothing
internal.test.test:
test: foo
'''

RETURN = r'''
test:
description: The value of the I(test) input.
type: str
returned: success
'''

from ansible.module_utils.basic import AnsibleModule


def main():
module = AnsibleModule(argument_spec={'test': {'type': 'str', 'required': True}})
module.exit_json(test=module.params['test'])


if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/output/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shippable/posix/group1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Call module
test:
test: foo
register: result

- name: Check return value
assert:
that:
- result.test == 'foo'
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) 2020 Felix Fontein <[email protected]>
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

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

import json

import pytest

from ansible.module_utils import basic
from ansible.module_utils.common.text.converters import to_bytes

from ansible_collections.internal.test.plugins.modules import test

try:
from unittest import mock
except ImportError:
import mock


def set_module_args(args):
if '_ansible_remote_tmp' not in args:
args['_ansible_remote_tmp'] = '/tmp'
if '_ansible_keep_remote_files' not in args:
args['_ansible_keep_remote_files'] = False

args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
basic._ANSIBLE_ARGS = to_bytes(args)


class AnsibleExitJson(Exception):
def __init__(self, kwargs):
self.kwargs = kwargs


def exit_json(*args, **kwargs):
if 'changed' not in kwargs:
kwargs['changed'] = False
raise AnsibleExitJson(kwargs)


def test_test():
set_module_args({'test': 'foo'})
with mock.patch.multiple(basic.AnsibleModule, exit_json=exit_json):
with pytest.raises(AnsibleExitJson) as e:
test.main()
assert 'test' in e.value.kwargs
assert e.value.kwargs['test'] == 'foo'
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ and https://github.com/ansible/ansible/branches/all?query=stable- for
ideas **(DEFAULT: `stable-2.13`)**


### `collection-root`

Path to collection root relative to repository root **(DEFAULT: `.`)**


### `docker-image`

A container image spawned by `ansible-test` **(OPTIONAL)**
Expand Down
7 changes: 5 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ inputs:
ideas.
default: stable-2.13
required: true
collection-root:
description: Collection root relative to repository root
default: .
docker-image:
description: Docker image used by ansible-test
default:
Expand Down Expand Up @@ -145,7 +148,7 @@ runs:
format(name=coll_name, ns=coll_ns)
)
shell: python
working-directory: .tmp-ansible-collection-checkout
working-directory: .tmp-ansible-collection-checkout/${{ inputs.collection-root }}
- name: Close an expandable block of code
run: >-
echo ::endgroup::
Expand All @@ -164,7 +167,7 @@ runs:
"${{ steps.collection-metadata.outputs.collection-namespace-path }}"
;
mv -v
".tmp-ansible-collection-checkout"
".tmp-ansible-collection-checkout/${{ inputs.collection-root }}"
"${{ steps.collection-metadata.outputs.checkout-path }}"
;
set +x
Expand Down