From 633936c648a555537c0db24eeb401e835136dc2d Mon Sep 17 00:00:00 2001 From: Ted Cook Date: Sun, 12 Nov 2023 18:52:03 -0600 Subject: [PATCH] Add test infrastructure --- .github/workflows/build.yml | 28 +++++++++++++ .github/workflows/lint.yml | 29 +++++++++++++ .github/workflows/molecule.yml | 62 +++++++++++++++++++++++++++ .github/workflows/release.yml | 30 ++++++++++++++ .github/workflows/version.yml | 28 +++++++++++++ .talismanrc | 4 ++ Makefile | 3 ++ README.md | 76 ++++++++++++++++++++++------------ molecule/default/molecule.yml | 3 ++ molecule/default/verify.yml | 15 +++++++ 10 files changed, 251 insertions(+), 27 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/molecule.yml create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/version.yml create mode 100644 molecule/default/molecule.yml create mode 100644 molecule/default/verify.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..bcdeeda --- /dev/null +++ b/.github/workflows/build.yml @@ -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 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..e9cf909 --- /dev/null +++ b/.github/workflows/lint.yml @@ -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 diff --git a/.github/workflows/molecule.yml b/.github/workflows/molecule.yml new file mode 100644 index 0000000..45e3c70 --- /dev/null +++ b/.github/workflows/molecule.yml @@ -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 }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..aef8b6d --- /dev/null +++ b/.github/workflows/release.yml @@ -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 }}" diff --git a/.github/workflows/version.yml b/.github/workflows/version.yml new file mode 100644 index 0000000..29bcade --- /dev/null +++ b/.github/workflows/version.yml @@ -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 diff --git a/.talismanrc b/.talismanrc index fd51846..3c6208b 100644 --- a/.talismanrc +++ b/.talismanrc @@ -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] diff --git a/Makefile b/Makefile index 663939b..90086cf 100644 --- a/Makefile +++ b/Makefile @@ -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} diff --git a/README.md b/README.md index 7884e25..219f650 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..bf1edc2 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,3 @@ +--- +platforms: + - name: instance diff --git a/molecule/default/verify.yml b/molecule/default/verify.yml new file mode 100644 index 0000000..da939a5 --- /dev/null +++ b/molecule/default/verify.yml @@ -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