Switch to molecule for testing

Molecule is the defacto testing tool for ansible roles. Switch to it to
make it easier for users to test.

Change-Id: I881e895877c9702e62c4415d48b0f33f34f7487b
Depends-On: https://review.openstack.org/593369
Signed-off-by: Paul Belanger <pabelanger@redhat.com>
This commit is contained in:
Paul Belanger 2018-08-17 12:55:10 -04:00
parent 48531f50f1
commit 4666971ac5
15 changed files with 187 additions and 53 deletions

10
.yamllint Normal file
View File

@ -0,0 +1,10 @@
extends: default
rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
line-length: disable

View File

@ -1,3 +1,4 @@
---
- job:
name: ansible-role-nodepool-base
pre-run: tests/playbooks/pre.yaml
@ -7,7 +8,6 @@
- tests/collect-logs.yaml
roles:
- zuul: openstack/ansible-role-nodepool
- zuul: openstack/zuul-jobs
# Testing for nodepool_install_method: pip
- job:
@ -69,6 +69,7 @@
- ansible-role-nodepool-src-ubuntu-bionic
- ansible-role-nodepool-src-ubuntu-xenial
- tox-linters
- windmill-tox-molecule
gate:
jobs:
- ansible-role-nodepool-fedora-latest
@ -78,3 +79,4 @@
- ansible-role-nodepool-src-ubuntu-bionic
- ansible-role-nodepool-src-ubuntu-xenial
- tox-linters
- windmill-tox-molecule

View File

@ -1,10 +1,5 @@
# This is a cross-platform list tracking distribution packages needed by tests;
# see http://docs.openstack.org/infra/bindep/ for additional information.
git
libffi-devel [platform:rpm]
libffi-dev [platform:dpkg]
libselinux-python [platform:rpm]
libssl-dev [platform:dpkg]
openssl-devel [platform:rpm]
python2-dnf [platform:fedora]
docker.io [test platform:dpkg]
docker [test platform:fedora]

View File

@ -0,0 +1,9 @@
# Molecule managed
{% if item.registry is defined %}
FROM {{ item.registry.url }}/{{ item.image }}
{% else %}
FROM {{ item.image }}
{% endif %}
RUN apt-get update && apt-get install -y python sudo bash ca-certificates python-pip python3-pip && apt-get clean;

View File

@ -0,0 +1,34 @@
---
dependency:
name: galaxy
driver:
name: docker
lint:
name: yamllint
platforms:
- name: ubuntu-bionic
image: ubuntu:bionic
provisioner:
name: ansible
config_options:
ssh_connection:
pipelining: true
inventory:
group_vars:
all:
nodepool_service_nodepool_builder_manage: false
nodepool_service_nodepool_launcher_manage: false
lint:
name: ansible-lint
log: true
playbooks:
converge: ../../tests/playbooks/run.yaml
scenario:
name: default
verifier:
name: testinfra
directory: ../tests
options:
verbose: true
lint:
name: flake8

View File

@ -0,0 +1,27 @@
# Copyright 2018 Red Hat, 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.
import pytest
@pytest.fixture
def platform_docker(host):
return host.file('/.dockerenv').exists
@pytest.fixture(autouse=True)
def skip_platform_docker(request, platform_docker):
marker = request.node.get_marker('skip_if_docker')
if marker and platform_docker:
pytest.skip('Skipping docker')

View File

@ -12,6 +12,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import pytest
def test_nodepool_user(host):
user = host.user('nodepool')
@ -62,6 +64,15 @@ def test_nodepool_images_directory(host):
assert f.mode == 0o755
def test_nodepool_logs_directory(host):
f = host.file('/var/log/nodepool')
assert f.exists
assert f.is_directory
assert f.user == 'nodepool'
assert f.group == 'nodepool'
assert f.mode == 0o755
def test_nodepool_builder_logging_config(host):
f = host.file('/etc/nodepool/builder-logging.conf')
assert f.exists
@ -71,14 +82,34 @@ def test_nodepool_builder_logging_config(host):
assert f.mode == 0o644
def test_nodepool_builder_service(host):
def test_nodepool_builder_service_config(host):
f = host.file('/etc/systemd/system/nodepool-builder.service')
assert f.exists
assert f.is_file
assert f.user == 'root'
assert f.group == 'root'
assert f.mode == 0o644
del f
f = host.file('/etc/systemd/system/nodepool-builder.service.d')
assert f.exists
assert f.is_directory
assert f.user == 'root'
assert f.group == 'root'
assert f.mode == 0o755
del f
f = host.file(
'/etc/systemd/system/nodepool-builder.service.d/override.conf')
assert f.exists
assert f.is_file
assert f.user == 'root'
assert f.group == 'root'
assert f.mode == 0o644
@pytest.mark.skip_if_docker()
def test_nodepool_builder_service(host):
service = host.service('nodepool-builder')
assert service.is_running
assert service.is_enabled
@ -93,14 +124,34 @@ def test_nodepool_launcher_logging_config(host):
assert f.mode == 0o644
def test_nodepool_launcher_service(host):
def test_nodepool_launcher_service_config(host):
f = host.file('/etc/systemd/system/nodepool-launcher.service')
assert f.exists
assert f.is_file
assert f.user == 'root'
assert f.group == 'root'
assert f.mode == 0o644
del f
f = host.file('/etc/systemd/system/nodepool-launcher.service.d')
assert f.exists
assert f.is_directory
assert f.user == 'root'
assert f.group == 'root'
assert f.mode == 0o755
del f
f = host.file(
'/etc/systemd/system/nodepool-launcher.service.d/override.conf')
assert f.exists
assert f.is_file
assert f.user == 'root'
assert f.group == 'root'
assert f.mode == 0o644
@pytest.mark.skip_if_docker()
def test_nodepool_launcher_service(host):
service = host.service('nodepool-launcher')
assert service.is_running
assert service.is_enabled

View File

@ -1,6 +1,7 @@
# This file is generated by Ansible
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
#
---
images-dir: /opt/nodepool/images
zookeeper-servers:

View File

@ -1,3 +1,3 @@
ansible-lint
hacking<0.11,>=0.10
junit2html
ara
docker
molecule

View File

@ -21,7 +21,6 @@
delegate_to: "{{ inventory_hostname }}"
with_items:
- "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/junit.xml"
- "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/junit.xml.html"
- name: Prepare nodepool log files
become: yes

View File

@ -1,17 +1,8 @@
- hosts: all
tasks:
- block:
- name: Run testinfra validation
include_role:
name: tox
vars:
tox_envlist: testinfra
tox_install_siblings: false
always:
- name: Run junit2html
include_role:
name: tox
vars:
tox_envlist: venv
tox_extra_args: -vv junit2html junit.xml
tox_install_siblings: false

21
tools/test-setup.sh Executable file
View File

@ -0,0 +1,21 @@
#!/bin/bash -ex
# Copyright 2018 Red Hat, 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.
if ! [ $(getent group docker) ]; then
sudo groupadd docker
fi
sudo gpasswd -a ${USER} docker
sudo service docker restart

24
tox.ini
View File

@ -17,10 +17,8 @@ commands=
sphinx-build -b html doc/source doc/build/html
[testenv:testinfra]
deps =
testinfra
commands =
pytest --sudo --junit-xml junit.xml tests/test_role.py
pytest --junit-xml junit.xml --sudo --verbose molecule/tests
[testenv:venv]
commands = {posargs}
@ -34,16 +32,12 @@ builtins = _
exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build
[testenv:linters]
setenv =
ANSIBLE_ROLES_PATH = ..
whitelist_externals = bash
commands =
# PEP8 Lint Check
flake8
# Ansible Lint Check
bash -c "find . -not -path '*/\.*' -type f -regex '.*.y[a]?ml' -print0 | \
xargs -t -n1 -0 ansible-lint"
# Ansible Syntax Check
bash -c "find tests -type f -regex '.*.y[a]?ml' -print | xargs -t -n1 \
ansible-playbook --syntax-check -i tests/inventory \
-e rolename=$(basename $(pwd)) > /dev/null"
molecule lint
[testenv:molecule]
sitepackages = True
setenv =
ANSIBLE_CALLBACK_PLUGINS = {envsitepackagesdir}/ara/plugins/callbacks
commands =
molecule test