Skip to content

Commit

Permalink
Add test infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyphreak committed Nov 13, 2023
1 parent c0113b3 commit 633936c
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 27 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: Build
on:
pull_request:
branches:
- master

jobs:
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Check out the codebase.
uses: actions/checkout@v4

- name: Set up Python 3
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Update ubuntu repositories
run: sudo apt-get update

- name: Install make
run: sudo apt-get install -y make

- name: Build collection
run: make build
29 changes: 29 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: lint

on:
pull_request:
branches:
- master

jobs:
lint:
name: lint
runs-on: ubuntu-latest
steps:
- name: Check out the codebase
uses: actions/checkout@v4

- name: Set up python 3
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Update ubuntu repositories
run: sudo apt-get update

- name: Install make
run: sudo apt-get install -y make

- name: Lint code
run: make lint
62 changes: 62 additions & 0 deletions .github/workflows/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
name: molecule

on:
pull_request:
branches:
- master
schedule:
- cron: "0 5 * * 5"

jobs:
molecule:
name: molecule
runs-on: ubuntu-latest
strategy:
matrix:
scenario:
- name: install
- name: offline
- name: online
- name: stop
- name: start
- name: restart
image:
- name: ubuntu2204
command: /lib/systemd/systemd
- name: ubuntu2004
command: /lib/systemd/systemd
- name: debian11
command: /lib/systemd/systemd
steps:
- name: Check out the codebase
uses: actions/checkout@v4

- name: Set up python 3
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Update ubuntu repositories
run: sudo apt-get update

- name: Install make
run: sudo apt-get install -y make

- name: Install dependencies
run: make install

- name: Debug test environment
run: make debug

- name: Install role requirements
run: make requirements

- name: Run molecule tests.
run: make test
env:
PY_COLORS: '1'
ANSIBLE_FORCE_COLOR: '1'
MOLECULE_DOCKER_IMAGE: ${{ matrix.image.name }}
MOLECULE_DOCKER_COMMAND: ${{ matrix.image.command }}
MOLECULE_SCENARIO: ${{ matrix.scenario.name }}
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
name: Release
on:
push:
tags:
- "*"

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Check out the codebase.
uses: actions/checkout@v4

- name: Set up Python 3
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Update ubuntu repositories
run: sudo apt-get update

- name: Install make
run: sudo apt-get install -y make

- name: Release collection
run: make publish
env:
GALAXY_API_KEY: "${{ secrets.GALAXY_API_KEY }}"
28 changes: 28 additions & 0 deletions .github/workflows/version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: version
on:
pull_request:
branches:
- master

jobs:
version:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Check if galaxy.yml has been modified
run: |
# Get a list of all modified files in this PR
FILES_CHANGED=$(git diff --name-only HEAD^ HEAD)
echo "Files changed: $FILES_CHANGED"
# Check if galaxy.yml is in the list of changed files
if [[ $FILES_CHANGED != *"galaxy.yml"* ]]; then
echo "Error: galaxy.yml has not been modified."
exit 1
fi
4 changes: 4 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
fileignoreconfig:
- filename: README.md
ignore_detectors: [filecontent]
- filename: poetry.lock
ignore_detectors: [filecontent]
- filename: tests/test_filter.py
ignore_detectors: [filecontent]
- filename: plugins/filter/custom_filter.py
ignore_detectors: [filecontent]
- filename: .github/workflows/release.yml
ignore_detectors: [filecontent]
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ publish: build
poetry run ansible-galaxy collection publish --api-key ${GALAXY_API_KEY} \
"${COLLECTION_NAMESPACE}-${COLLECTION_NAME}-${COLLECTION_VERSION}.tar.gz"

build:
@poetry run ansible-galaxy collection build --force

dependency create prepare converge idempotence side-effect verify destroy login reset listt:
MOLECULE_DOCKER_IMAGE=${MOLECULE_DOCKER_IMAGE} poetry run molecule $@ -s ${MOLECULE_SCENARIO}

Expand Down
76 changes: 49 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,56 @@
# Ansible Collection - nephelaiio.mongodb
# Ansible Collection - nephelaiio.plugins

[![Build Status](https://github.com/nephelaiio/ansible-collection-plugins/actions/workflows/molecule.yml/badge.svg)](https://github.com/nephelaiio/ansible-collection-plugins/actions/wofklows/molecule.yml)
[![Ansible Galaxy](http://img.shields.io/badge/ansible--galaxy-nephelaiio.plugins.vim-blue.svg)](https://galaxy.ansible.com/ui/repo/published/nephelaiio/plugins/)

An [ansible collection](https://galaxy.ansible.com/ui/repo/published/nephelaiio/plugins/) for utility filters and tests

## Collection plugins

Filters:

- is_hash(d): Checks if the given object has a callable 'get' attribute.
- merge_dicts(x, y): Merges two dictionaries, with values from the second dictionary overwriting those from the first.
- merge_dicts_reverse(x, y): Merges two dictionaries in reverse order, giving priority to the first dictionary.
- filename(basename): Extracts the filename (without extension) from a full file name.
- map_format(value, pattern): Applies Python string formatting on an object, especially useful for dynamic string construction.
- map_values(d): Extracts values from a dictionary and returns them as a list.
- reverse_record(record): Reverses IP address and hostname in a record for reverse DNS lookup.
- with_ext(basename, ext): Appends an extension to a given basename.
- zone_fwd(zone, servers): Creates a DNS forward zone configuration.
- head(x): Returns the first element of a sequence.
- tail(x): Returns all but the first element of a sequence.
- split_with(x, d): Splits a string by the specified delimiter.
- join_with(x, d): Joins a list of strings using a specified delimiter.
- alias_keys(d, alias): Creates a new dictionary with keys renamed according to the provided alias mapping.
- map_attributes(d, atts): Maps selected attributes from a dictionary into a new list.
- select_attributes(d, atts): Selects specific attributes from a dictionary to create a new one.
- drop_attributes(d, x): Removes specified attributes from a dictionary.
- to_dict(x, key): Converts a sequence or a value into a dictionary.
- merge_item(item, key_attr): Merges an item's attributes into a dictionary.
- key_item(item, key_attr, remove_key): Converts an item into a key-value pair, optionally removing the key attribute.
- dict_to_list(d, key_attr): Converts a dictionary into a list of merged items.
- list_to_dict(l, key_attr, remove_key): Converts a list into a dictionary, keying items by specified attributes.
- to_kv(d, sep, prefix): Converts a nested dictionary or list into a flat key-value pair representation.
- to_safe_yaml(ds, indent): Converts a data structure into a YAML string representation.
- sorted_get(d, ks): Retrieves the value for the first key found in a list of keys from a dictionary.
- ip_range(spec): Generates a list of IP addresses within a specified range.
- map_flatten(o, env): Flattens a nested dictionary or list into a single-level dictionary with compound keys.
- map_join(d, atts, sep): Joins selected attributes from a dictionary into a single string.
- merge_join(d, attr, atts, sep): Merges selected attributes into a single string and adds it to the dictionary.
- map_group(l, key_atts, group_att): Groups a list of dictionaries by specified key attributes.
- is_any_true(xs): Returns True if any element in the provided iterable is truthy.
- is_all_true(xs): Returns True if all elements in the provided iterable are truthy.
- search_regex(r, s): Checks if a string matches a given regex pattern.

Test:
- test_network(record=None, net="0.0.0.0/0", prop="ansible_host"): Tests if an IP address in a given record falls within a specified network range.
- test_property(record=None, regex=".*", prop=""): Tests if the value of a specified property in a given record matches a regular expression.

[![Build Status](https://github.com/nephelaiio/ansible-collection-mongodb/actions/workflows/molecule.yml/badge.svg)](https://github.com/nephelaiio/ansible-collection-mongodb/actions/wofklows/molecule.yml)
[![Ansible Galaxy](http://img.shields.io/badge/ansible--galaxy-nephelaiio.mongodb.vim-blue.svg)](https://galaxy.ansible.com/ui/repo/published/nephelaiio/mongodb/)

An [ansible collection](https://galaxy.ansible.com/ui/repo/published/nephelaiio/mongodb/) to install and manage MongoDB clusters

## Collection roles

* nephelaiio.mongodb.mongos
* nephelaiio.mongodb.mongodb

## Collection playbooks

* nephelaiio.mongodb.install: Install and (re)configure cluster
* nephelaiio.mongodb.offline: Stop mongos cluster services
* nephelaiio.mongodb.online: Start mongos cluster services
* nephelaiio.mongodb.stop: Stop all cluster services
* nephelaiio.mongodb.start: Start all cluster services

## Testing

Please make sure your environment has [docker](https://www.docker.com) installed in order to run role validation tests. Additional python dependencies are listed in the [requirements file](https://github.com/nephelaiio/ansible-role-requirements/blob/master/requirements.txt)

Role is tested against the following distributions (docker images):

* Ubuntu Focal
* Ubuntu Bionic
* Debian Bookworm

You can test the collection directly from sources using command `make test`
Role is tested using molecule through Github Actions on Ubuntu latest. You can test the collection directly from sources using command `make test`

## License

Expand Down
3 changes: 3 additions & 0 deletions molecule/default/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
platforms:
- name: instance
15 changes: 15 additions & 0 deletions molecule/default/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Verify
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Test
ansible.builtin.fail:
vars:
prefixes: ['a', 'b']
suffix: 'suffix'
expected: 'a:suffix,b:suffix'
filter: 'nephelaiio.plugins.map_format'
result: "{{ prefixes | map(filter, '%s:' + suffix) | join(',') }}"
when: result != expected

0 comments on commit 633936c

Please sign in to comment.