Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Commit

Permalink
Update verify to test custom modules
Browse files Browse the repository at this point in the history
Updates the verify task to also run any custom module tests to exercise
those if included. Currently, this only includes some initial
sensu_go_check tasks.

Explicitly at the end of the molecule run to ensure we have a fully
created sensu-backend/agent/ctl envirnoment that has alraedy passed the
Inspec tests.

Signed-off-by: Jared Ledvina <jared@techsmix.net>
  • Loading branch information
jaredledvina committed Jun 15, 2019
1 parent 9e44f48 commit 2519433
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 75 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ group: xenial
language: python
python: "2.7"
env:
global:
- ANSIBLE_MODULE_UTILS=$TRAVIS_BUILD_DIR/module_utils
matrix:
- SCENARIO=debian
- SCENARIO=ubuntu
- SCENARIO=centos
Expand Down
8 changes: 3 additions & 5 deletions library/sensu_go_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
password:
aliases:
- url_password
default: P@ssword!
default: P@ssw0rd!
description:
- "Password to use when initially authenticating to the Sensu Go API."
- "Can be overriden with the environmental variable C(ANSIBLE_SENSU_GO_PASSWORD)"
Expand Down Expand Up @@ -284,18 +284,16 @@ def run_module():
low_flap_threshold=dict(type='int', default=0),
metadata=dict(
type='dict',
elements='dict',
options=dict(
annotations=dict(type='dict', elements='dict'),
labels=dict(type='dict', elements='dict')
annotations=dict(type='dict'),
labels=dict(type='dict')
)
),
output_metric_format=dict(type='str', default='', choices=['', 'nagios_perfdata', 'graphite_plaintext', 'influxdb_line', 'opentsdb_line']),
output_metric_handlers=dict(type='list', elements='str'),
proxy_entity_name=dict(type='str', default=''),
proxy_requests=dict(
type='dict',
elements='dict',
options=dict(
entity_attributes=dict(type='list', elements='tr'),
splay=dict(type='bool', default=False),
Expand Down
2 changes: 1 addition & 1 deletion module_utils/sensu_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__(self, argument_spec, attributes, resource, **kwargs):
),
url_password=dict(
type='str',
default='P@ssword!',
default='P@ssw0rd!',
no_log=True,
aliases=['password'],
fallback=(env_fallback, ['ANSIBLE_SENSU_GO_PASSWORD'])
Expand Down
2 changes: 2 additions & 0 deletions molecule/debian/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ verifier:
directory: ../shared/
lint:
name: rubocop
env:
ANSIBLE_LIBRARY: ../../library/
65 changes: 65 additions & 0 deletions molecule/shared/inspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
- name: Install system dependencies for Inspec
package:
name: "{{ item }}"
state: present
with_items: "{{ inspec_package_deps }}"

- name: Download Inspec
get_url:
url: "{{ inspec_downloads[inspec_version]['url'] }}"
dest: "{{ inspec_download_source_dir }}"
sha256sum: "{{ inspec_downloads[inspec_version]['sha256'] }}"
mode: 0755
register: inspec_download

- name: Install Inspec
yum:
name: "{{ inspec_download.dest }}"
state: latest
when: ansible_pkg_mgr == 'yum'

- name: Install Inspec
dnf:
name: "{{ inspec_download.dest }}"
state: latest
when: ansible_pkg_mgr == 'dnf'

- name: Install Inspec
apt:
deb: "{{ inspec_download.dest }}"
state: present
when: ansible_pkg_mgr == 'apt'

- name: Create Molecule directory for test files
file:
path: "{{ inspec_test_directory }}"
state: directory

- name: Copy Inspec tests to remote
copy:
src: "{{ item }}"
dest: "{{ inspec_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ playbook_dir }}/tests/test_*.rb"

- name: Register test files
shell: "ls {{ inspec_test_directory }}/test_*.rb"
register: test_files

- name: Execute Inspec tests
command: "{{ inspec_bin }} exec --chef-license accept {{ item }} --no-color --reporter progress"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
ignore_errors: true

- name: Display details about the Inspec results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"

- name: Fail when tests fail
fail:
msg: "Inspec failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"
18 changes: 14 additions & 4 deletions molecule/shared/modules/main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
---
- name: Ensure http on https fails
- name: Ensure https on http fails
sensu_go_check:
name: check_test
host: localhost
state: present
command: /bin/true
interval: 120
protocol: http
register: http_on_https
failed_when: http_on_https is not failed
protocol: https
register: https_on_http
failed_when: https_on_http is not failed
- name: Ensure agent port fails
sensu_go_check:
name: check_test
host: localhost
state: present
command: /bin/true
interval: 120
Expand All @@ -30,6 +32,7 @@
- name: Ensure bad password fails
sensu_go_check:
name: check_test
host: localhost
state: present
command: /bin/true
interval: 120
Expand All @@ -39,6 +42,7 @@
- name: Ensure nonexistant namespace fails
sensu_go_check:
name: check_test
host: localhost
state: present
command: /bin/true
interval: 120
Expand All @@ -48,6 +52,7 @@
- name: Ensure interval and cron fails
sensu_go_check:
name: check_test
host: localhost
state: present
command: /bin/true
interval: 120
Expand All @@ -57,12 +62,14 @@
- name: Ensure check_example is configured
sensu_go_check:
name: check_example
host: localhost
command: /bin/true
interval: 300
subscriptions: all
- name: Ensure check_example is already configured
sensu_go_check:
name: check_example
host: localhost
command: /bin/true
interval: 300
subscriptions: all
Expand All @@ -71,6 +78,7 @@
- name: Ensure check_example is changed
sensu_go_check:
name: check_example
host: localhost
command: /bin/true
interval: 120
timeout: 120
Expand All @@ -82,10 +90,12 @@
- name: Ensure check_example is absent
sensu_go_check:
name: check_example
host: localhost
state: absent
- name: Ensure check_example is already absent
sensu_go_check:
name: check_example
host: localhost
state: absent
register: check_example_is_already_absent
failed_when: check_example_is_already_absent is changed
2 changes: 2 additions & 0 deletions molecule/shared/tests/test_default.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Debian Specific Things
if os.debian?
describe package('curl') do
Expand Down
66 changes: 1 addition & 65 deletions molecule/shared/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,71 +26,7 @@
- lsof
- net-tools
tasks:
- name: Install system dependencies for Inspec
package:
name: "{{ item }}"
state: present
with_items: "{{ inspec_package_deps }}"

- name: Download Inspec
get_url:
url: "{{ inspec_downloads[inspec_version]['url'] }}"
dest: "{{ inspec_download_source_dir }}"
sha256sum: "{{ inspec_downloads[inspec_version]['sha256'] }}"
mode: 0755
register: inspec_download

- name: Install Inspec
yum:
name: "{{ inspec_download.dest }}"
state: latest
when: ansible_pkg_mgr == 'yum'

- name: Install Inspec
dnf:
name: "{{ inspec_download.dest }}"
state: latest
when: ansible_pkg_mgr == 'dnf'

- name: Install Inspec
apt:
deb: "{{ inspec_download.dest }}"
state: present
when: ansible_pkg_mgr == 'apt'

- name: Create Molecule directory for test files
file:
path: "{{ inspec_test_directory }}"
state: directory

- name: Copy Inspec tests to remote
copy:
src: "{{ item }}"
dest: "{{ inspec_test_directory }}/{{ item | basename }}"
with_fileglob:
- "{{ playbook_dir }}/tests/test_*.rb"

- name: Register test files
shell: "ls {{ inspec_test_directory }}/test_*.rb"
register: test_files

- name: Execute Inspec tests
command: "{{ inspec_bin }} exec --chef-license accept {{ item }} --no-color --reporter progress"
register: test_results
with_items: "{{ test_files.stdout_lines }}"
ignore_errors: true

- name: Display details about the Inspec results
debug:
msg: "{{ item.stdout_lines }}"
with_items: "{{ test_results.results }}"

- name: Fail when tests fail
fail:
msg: "Inspec failed to validate"
when: item.rc != 0
with_items: "{{ test_results.results }}"

- import_tasks: inspec.yml
- name: Execute all custom module test plays
include_tasks: "{{ item }}"
with_fileglob:
Expand Down

0 comments on commit 2519433

Please sign in to comment.