From 611d36434c615fedd5c17d0aa62775eeb7b0861e Mon Sep 17 00:00:00 2001 From: Dmitriy Rabotyagov Date: Mon, 6 Jan 2025 16:31:12 +0100 Subject: [PATCH] Replace functional tests with molecule As effort to sunset tests repo, we replace functional test that were running for the role with a molecule. Due to docker limitations, I had to comment out swap tests, as while it's possible to create a container with swap, I didn't find a way to `swapon` from inside of the container. Depends-On: https://review.opendev.org/c/openstack/openstack-ansible/+/938571 Change-Id: I80297a01ff6e820591325529c3457302e021469a --- Vagrantfile | 46 ---------- manual-test.rc | 23 ----- molecule/default/molecule.yml | 36 ++++++++ requirements.yml | 3 + run_tests.sh | 88 ------------------- tests/ansible-role-requirements.yaml | 1 - tests/group_vars/all_containers.yml | 26 ------ tests/inventory | 2 - .../{host_vars/localhost.yml => prepare.yml} | 17 ++-- tests/test-create-ext4-dev.yml | 12 +-- tests/test-create-nfs-dev.yml | 11 +-- tests/test-create-swap-dev.yml | 5 +- tests/test.yml | 40 ++------- ...{systemd_init-overrides.yml => verify.yml} | 27 +++--- tox.ini | 56 ++++-------- zuul.d/project.yaml | 2 +- 16 files changed, 100 insertions(+), 295 deletions(-) delete mode 100644 Vagrantfile delete mode 100644 manual-test.rc create mode 100644 molecule/default/molecule.yml delete mode 100755 run_tests.sh delete mode 120000 tests/ansible-role-requirements.yaml delete mode 100644 tests/group_vars/all_containers.yml delete mode 100644 tests/inventory rename tests/{host_vars/localhost.yml => prepare.yml} (62%) rename tests/{systemd_init-overrides.yml => verify.yml} (54%) diff --git a/Vagrantfile b/Vagrantfile deleted file mode 100644 index 2a83de4..0000000 --- a/Vagrantfile +++ /dev/null @@ -1,46 +0,0 @@ -# Note: -# This file is maintained in the openstack-ansible-tests repository. -# https://opendev.org/openstack/openstack-ansible-tests/src/Vagrantfile -# -# If you need to perform any change on it, you should modify the central file, -# then, an OpenStack CI job will propagate your changes to every OSA repository -# since every repo uses the same Vagrantfile - -# Verify whether required plugins are installed. -required_plugins = [ "vagrant-disksize" ] -required_plugins.each do |plugin| - if not Vagrant.has_plugin?(plugin) - raise "The vagrant plugin #{plugin} is required. Please run `vagrant plugin install #{plugin}`" - end -end - -Vagrant.configure(2) do |config| - config.vm.provider "virtualbox" do |v| - v.memory = 6144 - v.cpus = 2 - # https://github.com/hashicorp/vagrant/issues/9524 - v.customize ["modifyvm", :id, "--audio", "none"] - end - - config.vm.synced_folder ".", "/vagrant", type: "rsync" - - config.vm.provision "shell", - privileged: false, - inline: <<-SHELL - cd /vagrant - ./run_tests.sh - SHELL - - config.vm.define "centos8" do |centos8| - centos8.vm.box = "centos/8" - end - - config.vm.define "debian10" do |debian10| - debian10.vm.box = "debian/buster64" - end - - config.vm.define "ubuntu2004" do |focal| - focal.disksize.size = "40GB" - focal.vm.box = "ubuntu/focal64" - end -end diff --git a/manual-test.rc b/manual-test.rc deleted file mode 100644 index a3e9ec9..0000000 --- a/manual-test.rc +++ /dev/null @@ -1,23 +0,0 @@ -export VIRTUAL_ENV=$(pwd) -export ANSIBLE_HOST_KEY_CHECKING=False -export ANSIBLE_SSH_CONTROL_PATH=/tmp/%%h-%%r - -# This is required as the default is '/etc/ansible/roles' or a path -# specified in ansible.cfg -export ANSIBLE_ROLES_PATH=${HOME}/.ansible/roles:$(pwd)/.. -export ANSIBLE_COLLECTIONS_PATHS="${HOME}/.ansible:$(pwd)/.." -export ANSIBLE_COLLECTIONS_PATH="${HOME}/.ansible:$(pwd)/.." - -export ANSIBLE_SSH_ARGS="-o ControlMaster=no \ - -o UserKnownHostsFile=/dev/null \ - -o StrictHostKeyChecking=no \ - -o ServerAliveInterval=64 \ - -o ServerAliveCountMax=1024 \ - -o Compression=no \ - -o TCPKeepAlive=yes \ - -o VerifyHostKeyDNS=no \ - -o ForwardX11=no \ - -o ForwardAgent=yes" - -echo "Run manual functional tests by executing the following:" -echo "# ./.tox/functional/bin/ansible-playbook -i tests/inventory tests/test.yml" diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml new file mode 100644 index 0000000..398b9c3 --- /dev/null +++ b/molecule/default/molecule.yml @@ -0,0 +1,36 @@ +--- +dependency: + name: galaxy + options: + requirements-file: requirements.yml + force: ${GALAXY_FORCE:-false} + +driver: + name: docker + +platforms: + - name: "systemd-mount-${MOLECULE_SCENARIO_NAME}" + image: "${DOCKER_REGISTRY:-quay.io/gotmax23}/${DOCKER_IMAGE_TAG:-debian-systemd:bookworm}" + command: ${DOCKER_COMMAND:-""} + pre_build_image: true + privileged: true + systemd: true + groups: + - mount_hosts + volumes: + - /tmp/systemd_mount_nfs:/srv/nfs/test:rw + +provisioner: + name: ansible + lint: + name: ansible-lint + playbooks: + prepare: ../../tests/prepare.yml + converge: ../../tests/test.yml + verify: ../../tests/verify.yml + config_options: + defaults: + inject_facts_as_vars: false + +scenario: + name: default diff --git a/requirements.yml b/requirements.yml index 58d02bb..d0464a3 100644 --- a/requirements.yml +++ b/requirements.yml @@ -3,3 +3,6 @@ collections: - name: https://opendev.org/openstack/ansible-config_template version: master type: git + - name: https://github.com/ansible-collections/community.general + type: git + version: main diff --git a/run_tests.sh b/run_tests.sh deleted file mode 100755 index 35280c8..0000000 --- a/run_tests.sh +++ /dev/null @@ -1,88 +0,0 @@ -#!/usr/bin/env bash -# Copyright 2015, Rackspace US, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# PURPOSE: -# This script clones the openstack-ansible-tests repository to the -# tests/common folder in order to be able to re-use test components -# for role testing. This is intended to be the thinnest possible -# shim for test execution outside of OpenStack CI. - -# WARNING: -# This file is maintained in the openstack-ansible-tests repository. -# https://opendev.org/openstack/openstack-ansible-tests/src/run_tests.sh -# If you need to modify this file, update the one in the openstack-ansible-tests -# repository and then update this file as well. The purpose of this file is to -# prepare the host and then execute all the tox tests. -# - -## Shell Opts ---------------------------------------------------------------- -set -xeu - -## Vars ---------------------------------------------------------------------- - -WORKING_DIR="$(readlink -f $(dirname $0))" -OSA_PROJECT_NAME="$(sed -n 's|^project=openstack/\(.*\).git$|\1|p' $(pwd)/.gitreview)" - -COMMON_TESTS_PATH="${WORKING_DIR}/tests/common" -TESTING_HOME=${TESTING_HOME:-$HOME} -ZUUL_TESTS_CLONE_LOCATION="/home/zuul/src/opendev.org/openstack/openstack-ansible-tests" - -# Use .gitreview as the key to determine the appropriate -# branch to clone for tests. -TESTING_BRANCH=$(awk -F'=' '/defaultbranch/ {print $2}' "${WORKING_DIR}/.gitreview") -if [[ "${TESTING_BRANCH}" == "" ]]; then - TESTING_BRANCH="master" -fi - -## Main ---------------------------------------------------------------------- - -# Source distribution information -source /etc/os-release || source /usr/lib/os-release - -# Figure out the appropriate package install command -case ${ID,,} in - centos|rhel|fedora|rocky) pkg_mgr_cmd="dnf install -y" ;; - ubuntu|debian) pkg_mgr_cmd="apt-get install -y" ;; - *) echo "unsupported distribution: ${ID,,}"; exit 1 ;; -esac - -# Install git so that we can clone the tests repo if git is not available -which git &>/dev/null || eval sudo "${pkg_mgr_cmd}" git - -# Clone the tests repo for access to the common test script -if [[ ! -d "${COMMON_TESTS_PATH}" ]]; then - # The tests repo doesn't need a clone, we can just - # symlink it. - if [[ "${OSA_PROJECT_NAME}" == "openstack-ansible-tests" ]]; then - ln -s "${WORKING_DIR}" "${COMMON_TESTS_PATH}" - - # In zuul v3 any dependent repository is placed into - # /home/zuul/src/opendev.org, so we check to see - # if there is a tests checkout there already. If so, we - # symlink that and use it. - elif [[ -d "${ZUUL_TESTS_CLONE_LOCATION}" ]]; then - ln -s "${ZUUL_TESTS_CLONE_LOCATION}" "${COMMON_TESTS_PATH}" - - # Otherwise we're clearly not in zuul or using a previously setup - # repo in some way, so just clone it from upstream. - else - git clone -b "${TESTING_BRANCH}" \ - https://opendev.org/openstack/openstack-ansible-tests \ - "${COMMON_TESTS_PATH}" - fi -fi - -# Execute the common test script -source tests/common/run_tests_common.sh diff --git a/tests/ansible-role-requirements.yaml b/tests/ansible-role-requirements.yaml deleted file mode 120000 index e2ff04e..0000000 --- a/tests/ansible-role-requirements.yaml +++ /dev/null @@ -1 +0,0 @@ -../ansible-role-requirements.yaml \ No newline at end of file diff --git a/tests/group_vars/all_containers.yml b/tests/group_vars/all_containers.yml deleted file mode 100644 index 99b8fa2..0000000 --- a/tests/group_vars/all_containers.yml +++ /dev/null @@ -1,26 +0,0 @@ ---- -# Copyright 2018, Rackspace US, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -container_networks: - management_address: - address: "{{ ansible_host }}" - bridge: "br-mgmt" - interface: "eth1" - netmask: "255.255.255.0" - type: "veth" - -physical_host: localhost -properties: - service_name: "{{ inventory_hostname }}" diff --git a/tests/inventory b/tests/inventory deleted file mode 100644 index 68b2dac..0000000 --- a/tests/inventory +++ /dev/null @@ -1,2 +0,0 @@ -[all] -localhost diff --git a/tests/host_vars/localhost.yml b/tests/prepare.yml similarity index 62% rename from tests/host_vars/localhost.yml rename to tests/prepare.yml index cc396ba..d7fc953 100644 --- a/tests/host_vars/localhost.yml +++ b/tests/prepare.yml @@ -1,5 +1,5 @@ --- -# Copyright 2018, Rackspace US, Inc. +# Copyright 2025, Cleura AB. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,14 +13,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -neutron_provider_networks: - network_types: "vxlan,flat" - network_mappings: "flat:eth12" - network_vxlan_ranges: "1:1000" +# - name: Importing test-create-swap-dev +# ansible.builtin.import_playbook: test-create-swap-dev.yml -neutron_local_ip: 10.1.2.1 +- name: Importing test-create-nfs-dev + ansible.builtin.import_playbook: test-create-nfs-dev.yml - -bridges: - - name: "br-mgmt" - ip_addr: "10.1.1.1" +- name: Importing test-create-ext4-dev + ansible.builtin.import_playbook: test-create-ext4-dev.yml diff --git a/tests/test-create-ext4-dev.yml b/tests/test-create-ext4-dev.yml index 3493e90..20faa96 100644 --- a/tests/test-create-ext4-dev.yml +++ b/tests/test-create-ext4-dev.yml @@ -14,11 +14,13 @@ # limitations under the License. - name: Configure ext4 sparse file - hosts: localhost - user: root - become: true - connection: local + hosts: mount_hosts tasks: + - name: Install required packages + ansible.builtin.package: + name: e2fsprogs + state: present + - name: Create base directories file: path: "/var/lib" @@ -31,7 +33,7 @@ register: sparse_file - name: Format the sparse file - filesystem: + community.general.filesystem: fstype: ext4 dev: /var/lib/sparse-file.img when: diff --git a/tests/test-create-nfs-dev.yml b/tests/test-create-nfs-dev.yml index 803e839..977c475 100644 --- a/tests/test-create-nfs-dev.yml +++ b/tests/test-create-nfs-dev.yml @@ -14,15 +14,13 @@ # limitations under the License. - name: Create an NFS backing store - hosts: localhost - user: root - become: true - connection: local + hosts: mount_hosts tasks: - name: Install NFS packages package: - name: "{{ nfs_package[ansible_facts['distribution'].split()[0] | lower] }}" + name: "{{ nfs_package[ansible_facts['os_family'] | lower] }}" state: present + update_cache: "{{ (ansible_facts['pkg_mgr'] == 'apt') | ternary(true, omit) }}" - name: create the system group for nfs group: @@ -78,6 +76,5 @@ - skip_ansible_lint vars: nfs_package: - ubuntu: "nfs-kernel-server" debian: "nfs-kernel-server" - centos: "nfs-utils" + redhat: "nfs-utils" diff --git a/tests/test-create-swap-dev.yml b/tests/test-create-swap-dev.yml index ff37553..5da966d 100644 --- a/tests/test-create-swap-dev.yml +++ b/tests/test-create-swap-dev.yml @@ -14,10 +14,7 @@ # limitations under the License. - name: Configure swap sparse file - hosts: localhost - user: root - become: true - connection: local + hosts: mount_hosts tasks: - name: Create swap file command: "dd if=/dev/zero of=/var/lib/test-swap.img bs=1M count=128" diff --git a/tests/test.yml b/tests/test.yml index a2049af..4f0af0e 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -14,33 +14,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -- import_playbook: test-create-swap-dev.yml - -- import_playbook: test-create-nfs-dev.yml - -- import_playbook: test-create-ext4-dev.yml - - name: Playbook for role testing - hosts: localhost - connection: local - user: root - become: true + hosts: mount_hosts roles: - - role: "systemd_mount" - - post_tasks: - - name: Ensure mount are mounted - command: grep -w '{{ item }}' /proc/mounts - with_items: - - /var/lib/sparse-file - - /var/lib/test - tags: - - skip_ansible_lint - - - name: Ensure swap is enabled - shell: swapon | grep -w '/var/lib/test-swap.img' - tags: - - skip_ansible_lint + - role: "{{ playbook_dir | dirname | basename }}" vars: systemd_mounts: @@ -54,12 +31,13 @@ Unit: ConditionPathExists: '/var/lib/sparse-file.img' - - what: "/var/lib/test-swap.img" - priority: "0" - options: "%%" - type: "swap" - state: 'started' - enabled: true + # NOTE: Swap can't be mounted inside of the docker container + # - what: "/var/lib/test-swap.img" + # priority: "0" + # options: "%%" + # type: "swap" + # state: 'started' + # enabled: true - what: "127.0.0.1:/srv/nfs/test" where: "/var/lib/test" diff --git a/tests/systemd_init-overrides.yml b/tests/verify.yml similarity index 54% rename from tests/systemd_init-overrides.yml rename to tests/verify.yml index 581afdf..f1b1565 100644 --- a/tests/systemd_init-overrides.yml +++ b/tests/verify.yml @@ -1,5 +1,5 @@ --- -# Copyright 2018, Rackspace US, Inc. +# Copyright 2025, Cleura AB # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,14 +13,19 @@ # See the License for the specific language governing permissions and # limitations under the License. -tempest_run: yes -tempest_venv_tag: "{{ tempest_git_install_branch }}" -tempest_venv_bin: "/opt/tempest_{{ tempest_venv_tag }}/bin" -tempest_log_dir: "/var/log/" -tempest_test_whitelist: - - tempest.scenario.test_server_basic_ops.TestServerBasicOps.test_server_basic_ops +- name: Playbook for role testing + hosts: mount_hosts + tasks: + - name: Ensure mount are mounted + command: grep -w '{{ item }}' /proc/mounts + changed_when: false + with_items: + - /var/lib/sparse-file + - /var/lib/test + tags: + - skip_ansible_lint -neutron_provider_networks: - network_types: "vxlan,flat" - network_mappings: "flat:eth12" - network_vxlan_ranges: "1:1000" + # - name: Ensure swap is enabled + # shell: swapon | grep -w '/var/lib/test-swap.img' + # tags: + # - skip_ansible_lint diff --git a/tox.ini b/tox.ini index 26c42a0..d80726b 100644 --- a/tox.ini +++ b/tox.ini @@ -1,7 +1,7 @@ [tox] -minversion = 3.1 +minversion = 4.0 skipsdist = True -envlist = docs,linters,functional +envlist = docs,molecule ignore_basepython_conflict = True [testenv] @@ -67,43 +67,19 @@ commands = {posargs} -[testenv:pep8] +[testenv:molecule] +# You can use DOCKER_REGISTRY and DOCKER_IMAGE_TAG to switch between +# tested distros. I.e: +# DOCKER_IMAGE_TAG=ubuntu-systemd:jammy tox -e molecule +deps = + -c{env:TOX_CONSTRAINTS_FILE:https://releases.openstack.org/constraints/upper/master} + -rhttps://opendev.org/openstack/openstack-ansible/raw/branch/{env:TEST_BRANCH:master}/test-requirements.txt + commands = - bash -c "{toxinidir}/tests/common/test-pep8.sh" + molecule test - -[flake8] -# Ignores the following rules due to how ansible modules work in general -# F403 'from ansible.module_utils.basic import *' used; -# unable to detect undefined names -ignore=F403 - - -[testenv:bashate] -commands = - bash -c "{toxinidir}/tests/common/test-bashate.sh" - - -[testenv:ansible-syntax] -commands = - bash -c "{toxinidir}/tests/common/test-ansible-syntax.sh" - - -[testenv:ansible-lint] -commands = - bash -c "{toxinidir}/tests/common/test-ansible-lint.sh" - - -[testenv:functional] -commands = - bash -c "{toxinidir}/tests/common/test-ansible-functional.sh" - - -[testenv:linters] -commands = - bash -c "{toxinidir}/tests/common/test-ansible-env-prep.sh" - {[testenv:pep8]commands} - {[testenv:bashate]commands} - {[testenv:ansible-lint]commands} - {[testenv:ansible-syntax]commands} - {[testenv:docs]commands} +passenv = + {[testenv]passenv} + DOCKER_REGISTRY + DOCKER_IMAGE_TAG + DOCKER_COMMAND diff --git a/zuul.d/project.yaml b/zuul.d/project.yaml index 7e93cb3..61df752 100644 --- a/zuul.d/project.yaml +++ b/zuul.d/project.yaml @@ -16,7 +16,7 @@ - project: templates: - openstack-ansible-linters-jobs - - openstack-ansible-role-jobs + - openstack-ansible-molecule - openstack-ansible-deploy-aio_metal-jobs - openstack-ansible-deploy-aio_distro_metal-jobs - check-requirements