Browse Source

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>
changes/10/593110/35
Paul Belanger 10 months ago
parent
commit
4666971ac5

+ 10
- 0
.yamllint View File

@@ -0,0 +1,10 @@
1
+extends: default
2
+
3
+rules:
4
+  braces:
5
+    max-spaces-inside: 1
6
+    level: error
7
+  brackets:
8
+    max-spaces-inside: 1
9
+    level: error
10
+  line-length: disable

+ 3
- 1
.zuul.yaml View File

@@ -1,3 +1,4 @@
1
+---
1 2
 - job:
2 3
     name: ansible-role-nodepool-base
3 4
     pre-run: tests/playbooks/pre.yaml
@@ -7,7 +8,6 @@
7 8
       - tests/collect-logs.yaml
8 9
     roles:
9 10
       - zuul: openstack/ansible-role-nodepool
10
-      - zuul: openstack/zuul-jobs
11 11
 
12 12
 # Testing for nodepool_install_method: pip
13 13
 - job:
@@ -69,6 +69,7 @@
69 69
         - ansible-role-nodepool-src-ubuntu-bionic
70 70
         - ansible-role-nodepool-src-ubuntu-xenial
71 71
         - tox-linters
72
+        - windmill-tox-molecule
72 73
     gate:
73 74
       jobs:
74 75
         - ansible-role-nodepool-fedora-latest
@@ -78,3 +79,4 @@
78 79
         - ansible-role-nodepool-src-ubuntu-bionic
79 80
         - ansible-role-nodepool-src-ubuntu-xenial
80 81
         - tox-linters
82
+        - windmill-tox-molecule

+ 2
- 7
bindep.txt View File

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

+ 8
- 8
meta/main.yml View File

@@ -23,13 +23,13 @@ galaxy_info:
23 23
   license: Apache
24 24
   min_ansible_version: 2.4
25 25
   platforms:
26
-  - name: Fedora
27
-    versions:
28
-    - 27
29
-  - name: Ubuntu
30
-    versions:
31
-    - 16.04
32
-    - 18.04
26
+    - name: Fedora
27
+      versions:
28
+        - 27
29
+    - name: Ubuntu
30
+      versions:
31
+        - 16.04
32
+        - 18.04
33 33
   categories:
34
-  - system
34
+    - system
35 35
 dependencies: []

+ 9
- 0
molecule/default/Dockerfile.j2 View File

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

+ 34
- 0
molecule/default/molecule.yml View File

@@ -0,0 +1,34 @@
1
+---
2
+dependency:
3
+  name: galaxy
4
+driver:
5
+  name: docker
6
+lint:
7
+  name: yamllint
8
+platforms:
9
+  - name: ubuntu-bionic
10
+    image: ubuntu:bionic
11
+provisioner:
12
+  name: ansible
13
+  config_options:
14
+    ssh_connection:
15
+      pipelining: true
16
+  inventory:
17
+    group_vars:
18
+      all:
19
+        nodepool_service_nodepool_builder_manage: false
20
+        nodepool_service_nodepool_launcher_manage: false
21
+  lint:
22
+    name: ansible-lint
23
+  log: true
24
+  playbooks:
25
+    converge: ../../tests/playbooks/run.yaml
26
+scenario:
27
+  name: default
28
+verifier:
29
+  name: testinfra
30
+  directory: ../tests
31
+  options:
32
+    verbose: true
33
+  lint:
34
+    name: flake8

+ 27
- 0
molecule/tests/conftest.py View File

@@ -0,0 +1,27 @@
1
+# Copyright 2018 Red Hat, Inc.
2
+#
3
+# Licensed under the Apache License, Version 2.0 (the "License");
4
+# you may not use this file except in compliance with the License.
5
+# You may obtain a copy of the License at
6
+#
7
+# http://www.apache.org/licenses/LICENSE-2.0
8
+#
9
+# Unless required by applicable law or agreed to in writing, software
10
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
+# License for the specific language governing permissions and limitations
13
+# under the License.
14
+
15
+import pytest
16
+
17
+
18
+@pytest.fixture
19
+def platform_docker(host):
20
+    return host.file('/.dockerenv').exists
21
+
22
+
23
+@pytest.fixture(autouse=True)
24
+def skip_platform_docker(request, platform_docker):
25
+    marker = request.node.get_marker('skip_if_docker')
26
+    if marker and platform_docker:
27
+        pytest.skip('Skipping docker')

tests/test_role.py → molecule/tests/test_role.py View File

@@ -12,6 +12,8 @@
12 12
 # License for the specific language governing permissions and limitations
13 13
 # under the License.
14 14
 
15
+import pytest
16
+
15 17
 
16 18
 def test_nodepool_user(host):
17 19
     user = host.user('nodepool')
@@ -62,6 +64,15 @@ def test_nodepool_images_directory(host):
62 64
     assert f.mode == 0o755
63 65
 
64 66
 
67
+def test_nodepool_logs_directory(host):
68
+    f = host.file('/var/log/nodepool')
69
+    assert f.exists
70
+    assert f.is_directory
71
+    assert f.user == 'nodepool'
72
+    assert f.group == 'nodepool'
73
+    assert f.mode == 0o755
74
+
75
+
65 76
 def test_nodepool_builder_logging_config(host):
66 77
     f = host.file('/etc/nodepool/builder-logging.conf')
67 78
     assert f.exists
@@ -71,14 +82,34 @@ def test_nodepool_builder_logging_config(host):
71 82
     assert f.mode == 0o644
72 83
 
73 84
 
74
-def test_nodepool_builder_service(host):
85
+def test_nodepool_builder_service_config(host):
75 86
     f = host.file('/etc/systemd/system/nodepool-builder.service')
76 87
     assert f.exists
77 88
     assert f.is_file
78 89
     assert f.user == 'root'
79 90
     assert f.group == 'root'
80 91
     assert f.mode == 0o644
92
+    del f
93
+
94
+    f = host.file('/etc/systemd/system/nodepool-builder.service.d')
95
+    assert f.exists
96
+    assert f.is_directory
97
+    assert f.user == 'root'
98
+    assert f.group == 'root'
99
+    assert f.mode == 0o755
100
+    del f
101
+
102
+    f = host.file(
103
+        '/etc/systemd/system/nodepool-builder.service.d/override.conf')
104
+    assert f.exists
105
+    assert f.is_file
106
+    assert f.user == 'root'
107
+    assert f.group == 'root'
108
+    assert f.mode == 0o644
109
+
81 110
 
111
+@pytest.mark.skip_if_docker()
112
+def test_nodepool_builder_service(host):
82 113
     service = host.service('nodepool-builder')
83 114
     assert service.is_running
84 115
     assert service.is_enabled
@@ -93,14 +124,34 @@ def test_nodepool_launcher_logging_config(host):
93 124
     assert f.mode == 0o644
94 125
 
95 126
 
96
-def test_nodepool_launcher_service(host):
127
+def test_nodepool_launcher_service_config(host):
97 128
     f = host.file('/etc/systemd/system/nodepool-launcher.service')
98 129
     assert f.exists
99 130
     assert f.is_file
100 131
     assert f.user == 'root'
101 132
     assert f.group == 'root'
102 133
     assert f.mode == 0o644
134
+    del f
103 135
 
136
+    f = host.file('/etc/systemd/system/nodepool-launcher.service.d')
137
+    assert f.exists
138
+    assert f.is_directory
139
+    assert f.user == 'root'
140
+    assert f.group == 'root'
141
+    assert f.mode == 0o755
142
+    del f
143
+
144
+    f = host.file(
145
+        '/etc/systemd/system/nodepool-launcher.service.d/override.conf')
146
+    assert f.exists
147
+    assert f.is_file
148
+    assert f.user == 'root'
149
+    assert f.group == 'root'
150
+    assert f.mode == 0o644
151
+
152
+
153
+@pytest.mark.skip_if_docker()
154
+def test_nodepool_launcher_service(host):
104 155
     service = host.service('nodepool-launcher')
105 156
     assert service.is_running
106 157
     assert service.is_enabled

+ 1
- 1
tasks/install/pip.yaml View File

@@ -16,7 +16,7 @@
16 16
   become: true
17 17
   pip:
18 18
     executable: "{{ nodepool_pip_executable|default(omit) }}"
19
-    editable : "{{ nodepool_pip_editable|default(omit) }}"
19
+    editable: "{{ nodepool_pip_editable|default(omit) }}"
20 20
     extra_args: "{{ nodepool_pip_extra_args|default(omit) }}"
21 21
     name: "{{ nodepool_pip_name }}"
22 22
     version: "{{ nodepool_pip_version|default(omit) }}"

+ 1
- 0
templates/etc/nodepool/nodepool.yaml View File

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

+ 3
- 3
test-requirements.txt View File

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

+ 0
- 1
tests/collect-logs.yaml View File

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

+ 6
- 15
tests/playbooks/post.yaml View File

@@ -1,17 +1,8 @@
1 1
 - hosts: all
2 2
   tasks:
3
-    - block:
4
-      - name: Run testinfra validation
5
-        include_role:
6
-          name: tox
7
-        vars:
8
-          tox_envlist: testinfra
9
-          tox_install_siblings: false
10
-      always:
11
-      - name: Run junit2html
12
-        include_role:
13
-          name: tox
14
-        vars:
15
-          tox_envlist: venv
16
-          tox_extra_args: -vv junit2html junit.xml
17
-          tox_install_siblings: false
3
+    - name: Run testinfra validation
4
+      include_role:
5
+        name: tox
6
+      vars:
7
+        tox_envlist: testinfra
8
+        tox_install_siblings: false

+ 21
- 0
tools/test-setup.sh View File

@@ -0,0 +1,21 @@
1
+#!/bin/bash -ex
2
+# Copyright 2018 Red Hat, Inc.
3
+#
4
+# Licensed under the Apache License, Version 2.0 (the "License");
5
+# you may not use this file except in compliance with the License.
6
+# You may obtain a copy of the License at
7
+#
8
+#    http://www.apache.org/licenses/LICENSE-2.0
9
+#
10
+# Unless required by applicable law or agreed to in writing, software
11
+# distributed under the License is distributed on an "AS IS" BASIS,
12
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13
+# implied.
14
+# See the License for the specific language governing permissions and
15
+# limitations under the License.
16
+
17
+if ! [ $(getent group docker) ]; then
18
+    sudo groupadd docker
19
+fi
20
+sudo gpasswd -a ${USER} docker
21
+sudo service docker restart

+ 8
- 14
tox.ini View File

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

Loading…
Cancel
Save