Browse Source

Switch to testinfra for role validation

We can switch to using testinfra to help ensure ansible did everything
properly.

Change-Id: I9e21cd16f1149d76308043a97658c93e7f14ac42
Signed-off-by: Paul Belanger <pabelanger@redhat.com>
changes/88/591888/27
Paul Belanger 10 months ago
parent
commit
efc9d57e2c
7 changed files with 143 additions and 138 deletions
  1. 3
    1
      .zuul.yaml
  2. 1
    0
      test-requirements.txt
  3. 10
    0
      tests/collect-logs.yaml
  4. 17
    0
      tests/playbooks/post.yaml
  5. 0
    137
      tests/playbooks/run.yaml
  6. 106
    0
      tests/test_role.py
  7. 6
    0
      tox.ini

+ 3
- 1
.zuul.yaml View File

@@ -2,7 +2,9 @@
2 2
     name: ansible-role-nodepool-base
3 3
     pre-run: tests/playbooks/pre.yaml
4 4
     run: tests/playbooks/run.yaml
5
-    post-run: tests/collect-logs.yaml
5
+    post-run:
6
+      - tests/playbooks/post.yaml
7
+      - tests/collect-logs.yaml
6 8
     roles:
7 9
       - zuul: openstack/ansible-role-nodepool
8 10
       - zuul: openstack/zuul-jobs

+ 1
- 0
test-requirements.txt View File

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

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

@@ -13,6 +13,16 @@
13 13
         - nodepool-builder
14 14
         - nodepool-launcher
15 15
 
16
+    - name: Collect testinfra reports
17
+      synchronize:
18
+        dest: "{{ zuul_output_dir }}/logs"
19
+        src: "{{ item }}"
20
+        verify_host: true
21
+      delegate_to: "{{ inventory_hostname }}"
22
+      with_items:
23
+        - "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/junit.xml"
24
+        - "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/junit.xml.html"
25
+
16 26
     - name: Prepare nodepool log files
17 27
       become: yes
18 28
       synchronize:

+ 17
- 0
tests/playbooks/post.yaml View File

@@ -0,0 +1,17 @@
1
+- hosts: all
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

+ 0
- 137
tests/playbooks/run.yaml View File

@@ -34,105 +34,6 @@
34 34
           - nodepool_service_nodepool_builder
35 35
           - nodepool_service_nodepool_launcher
36 36
 
37
-    - name: Ensure nodepool_user_name is nodepool.
38
-      shell: /usr/bin/getent passwd nodepool
39
-      tags: skip_ansible_lint
40
-
41
-    - name: Ensure nodepool_user_group is nodepool.
42
-      shell: /usr/bin/getent group nodepool
43
-      tags: skip_ansible_lint
44
-
45
-    - name: Register nodepool_user_home_getent.
46
-      shell: '/usr/bin/getent passwd nodepool | cut -d: -f6'
47
-      register: nodepool_user_home_getent
48
-      tags: skip_ansible_lint
49
-
50
-    - name: Assert nodepool_user_home is /var/lib/nodepool.
51
-      assert:
52
-        that:
53
-          - nodepool_user_home_getent.stdout == '/var/lib/nodepool'
54
-
55
-    - name: Register /opt/nodepool/images
56
-      stat:
57
-        path: /opt/nodepool/images
58
-      register: _nodepool_images_dir_stat
59
-
60
-    - name: Assert /opt/nodepool/images tests.
61
-      assert:
62
-        that:
63
-          - _nodepool_images_dir_stat.stat.exists
64
-          - _nodepool_images_dir_stat.stat.isdir
65
-          - _nodepool_images_dir_stat.stat.pw_name == 'nodepool'
66
-          - _nodepool_images_dir_stat.stat.gr_name == 'nodepool'
67
-          - _nodepool_images_dir_stat.stat.mode == '0755'
68
-
69
-    - name: Register /etc/nodepool/nodepool.yaml
70
-      stat:
71
-        path: /etc/nodepool/nodepool.yaml
72
-      register: nodepool_yaml_stat
73
-
74
-    - name: Assert nodepool_yaml_stat tests.
75
-      assert:
76
-        that:
77
-          - nodepool_yaml_stat.stat.exists
78
-          - nodepool_yaml_stat.stat.isreg
79
-          - nodepool_yaml_stat.stat.pw_name == 'nodepool'
80
-          - nodepool_yaml_stat.stat.gr_name == 'nodepool'
81
-          - nodepool_yaml_stat.stat.mode == '0644'
82
-
83
-    - name: Register /etc/nodepool/builder-logging.conf
84
-      stat:
85
-        path: /etc/nodepool/builder-logging.conf
86
-      register: builder_logging_conf_stat
87
-
88
-    - name: Assert buidler_logging_conf_stat tests.
89
-      assert:
90
-        that:
91
-          - builder_logging_conf_stat.stat.exists
92
-          - builder_logging_conf_stat.stat.isreg
93
-          - builder_logging_conf_stat.stat.pw_name == 'nodepool'
94
-          - builder_logging_conf_stat.stat.gr_name == 'nodepool'
95
-          - builder_logging_conf_stat.stat.mode == '0644'
96
-
97
-    - name: Register /etc/nodepool/launcher-logging.conf
98
-      stat:
99
-        path: /etc/nodepool/launcher-logging.conf
100
-      register: launcher_logging_conf_stat
101
-
102
-    - name: Assert buidler_logging_conf_stat tests.
103
-      assert:
104
-        that:
105
-          - launcher_logging_conf_stat.stat.exists
106
-          - launcher_logging_conf_stat.stat.isreg
107
-          - launcher_logging_conf_stat.stat.pw_name == 'nodepool'
108
-          - launcher_logging_conf_stat.stat.gr_name == 'nodepool'
109
-          - launcher_logging_conf_stat.stat.mode == '0644'
110
-
111
-    - name: Register /etc/nodepool/secure.conf
112
-      stat:
113
-        path: /etc/nodepool/secure.conf
114
-      register: secure_conf_stat
115
-
116
-    - name: Assert secure_conf_stat tests.
117
-      assert:
118
-        that:
119
-          - secure_conf_stat.stat.exists
120
-          - secure_conf_stat.stat.isreg
121
-          - secure_conf_stat.stat.pw_name == 'nodepool'
122
-          - secure_conf_stat.stat.gr_name == 'nodepool'
123
-          - secure_conf_stat.stat.mode == '0600'
124
-
125
-    - name: Register nodepool_user_home_stat.
126
-      stat:
127
-        path: /var/lib/nodepool
128
-      register: nodepool_user_home_stat
129
-
130
-    - name: Assert nodepool_user_home tests.
131
-      assert:
132
-        that:
133
-          - nodepool_user_home_stat.stat.exists
134
-          - nodepool_user_home_stat.stat.isdir
135
-
136 37
     - name: Register nodepool_git_dest_stat.
137 38
       stat:
138 39
         path: /home/zuul/src/git.openstack.org/openstack-infra/nodepool
@@ -145,41 +46,3 @@
145 46
           - nodepool_git_dest_stat.stat.exists
146 47
           - nodepool_git_dest_stat.stat.isdir
147 48
       when: nodepool_install_method == 'git'
148
-
149
-    - name: Register /etc/systemd/system/nodepool-builder.service
150
-      stat:
151
-        path: /etc/systemd/system/nodepool-builder.service
152
-      register: _nodepool_builder_service_systemd_stat
153
-
154
-    - name: Assert _nodepool_builder_service_systemd_stat tests.
155
-      assert:
156
-        that:
157
-          - _nodepool_builder_service_systemd_stat.stat.exists
158
-          - _nodepool_builder_service_systemd_stat.stat.isreg
159
-          - _nodepool_builder_service_systemd_stat.stat.pw_name == 'root'
160
-          - _nodepool_builder_service_systemd_stat.stat.gr_name == 'root'
161
-          - _nodepool_builder_service_systemd_stat.stat.mode == '0644'
162
-
163
-    - name: Ensure nodepool-builder is running.
164
-      become: yes
165
-      shell: /usr/sbin/service nodepool-builder status
166
-      tags: skip_ansible_lint
167
-
168
-    - name: Register /etc/systemd/system/nodepool-launcher.service
169
-      stat:
170
-        path: /etc/systemd/system/nodepool-launcher.service
171
-      register: _nodepool_launcher_service_systemd_stat
172
-
173
-    - name: Assert _nodepool_launcher_service_systemd_stat tests.
174
-      assert:
175
-        that:
176
-          - _nodepool_launcher_service_systemd_stat.stat.exists
177
-          - _nodepool_launcher_service_systemd_stat.stat.isreg
178
-          - _nodepool_launcher_service_systemd_stat.stat.pw_name == 'root'
179
-          - _nodepool_launcher_service_systemd_stat.stat.gr_name == 'root'
180
-          - _nodepool_launcher_service_systemd_stat.stat.mode == '0644'
181
-
182
-    - name: Ensure nodepool-launcher is running.
183
-      become: yes
184
-      shell: /usr/sbin/service nodepool-launcher status
185
-      tags: skip_ansible_lint

+ 106
- 0
tests/test_role.py View File

@@ -0,0 +1,106 @@
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
+
16
+def test_nodepool_user(host):
17
+    user = host.user('nodepool')
18
+    assert user.exists
19
+    assert user.name == 'nodepool'
20
+    assert user.group == 'nodepool'
21
+    assert user.home == '/var/lib/nodepool'
22
+
23
+    f = host.file('/var/lib/nodepool')
24
+    assert f.exists
25
+    assert f.is_directory
26
+    assert f.user == 'nodepool'
27
+    assert f.group == 'nodepool'
28
+    # TODO(pabelanger): Validate mode
29
+
30
+
31
+def test_nodepool_config(host):
32
+    f = host.file('/etc/nodepool')
33
+    assert f.exists
34
+    assert f.is_directory
35
+    assert f.user == 'nodepool'
36
+    assert f.group == 'nodepool'
37
+    # TODO(pabelanger): Validate mode
38
+    del f
39
+
40
+    f = host.file('/etc/nodepool/nodepool.yaml')
41
+    assert f.exists
42
+    assert f.is_file
43
+    assert f.user == 'nodepool'
44
+    assert f.group == 'nodepool'
45
+    assert f.mode == 0o644
46
+    del f
47
+
48
+    f = host.file('/etc/nodepool/secure.conf')
49
+    assert f.exists
50
+    assert f.is_file
51
+    assert f.user == 'nodepool'
52
+    assert f.group == 'nodepool'
53
+    assert f.mode == 0o600
54
+
55
+
56
+def test_nodepool_images_directory(host):
57
+    f = host.file('/opt/nodepool/images')
58
+    assert f.exists
59
+    assert f.is_directory
60
+    assert f.user == 'nodepool'
61
+    assert f.group == 'nodepool'
62
+    assert f.mode == 0o755
63
+
64
+
65
+def test_nodepool_builder_logging_config(host):
66
+    f = host.file('/etc/nodepool/builder-logging.conf')
67
+    assert f.exists
68
+    assert f.is_file
69
+    assert f.user == 'nodepool'
70
+    assert f.group == 'nodepool'
71
+    assert f.mode == 0o644
72
+
73
+
74
+def test_nodepool_builder_service(host):
75
+    f = host.file('/etc/systemd/system/nodepool-builder.service')
76
+    assert f.exists
77
+    assert f.is_file
78
+    assert f.user == 'root'
79
+    assert f.group == 'root'
80
+    assert f.mode == 0o644
81
+
82
+    service = host.service('nodepool-builder')
83
+    assert service.is_running
84
+    assert service.is_enabled
85
+
86
+
87
+def test_nodepool_launcher_logging_config(host):
88
+    f = host.file('/etc/nodepool/launcher-logging.conf')
89
+    assert f.exists
90
+    assert f.is_file
91
+    assert f.user == 'nodepool'
92
+    assert f.group == 'nodepool'
93
+    assert f.mode == 0o644
94
+
95
+
96
+def test_nodepool_launcher_service(host):
97
+    f = host.file('/etc/systemd/system/nodepool-launcher.service')
98
+    assert f.exists
99
+    assert f.is_file
100
+    assert f.user == 'root'
101
+    assert f.group == 'root'
102
+    assert f.mode == 0o644
103
+
104
+    service = host.service('nodepool-launcher')
105
+    assert service.is_running
106
+    assert service.is_enabled

+ 6
- 0
tox.ini View File

@@ -16,6 +16,12 @@ commands=
16 16
   doc8 doc
17 17
   sphinx-build -b html doc/source doc/build/html
18 18
 
19
+[testenv:testinfra]
20
+deps =
21
+  testinfra
22
+commands =
23
+  pytest --sudo --junit-xml junit.xml tests/test_role.py
24
+
19 25
 [testenv:venv]
20 26
 commands = {posargs}
21 27
 

Loading…
Cancel
Save