Skip to content

Commit

Permalink
test(inspec): expand unit tests for archives
Browse files Browse the repository at this point in the history
  • Loading branch information
noelmcloughlin committed Jun 16, 2019
1 parent 0b05c5d commit 27bc326
Show file tree
Hide file tree
Showing 18 changed files with 211 additions and 33 deletions.
3 changes: 1 addition & 2 deletions grafana/archive/clean.sls
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
grafana-cli-package-archive-clean-file-absent:
file.absent:
- names:
- {{ grafana.pkg.archive.name }}/grafana-6.2.2.darwin-amd64
- {{ grafana.pkg.archive.name }}/grafana-6.2.2.linux-amd64
- {{ grafana.pkg.archive.name }}
21 changes: 21 additions & 0 deletions grafana/archive/install.sls
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,34 @@
{%- from tplroot ~ "/map.jinja" import grafana with context %}
{%- from tplroot ~ "/jinja/macros.jinja" import format_kwargs with context %}
{%- if grafana.pkg.use_upstream_archive %}
grafana-package-archive-install-file-directory:
file.directory:
- name: {{ grafana.pkg.archive.name }}
- user: root
- group: root
- mode: 755
- makedirs: True
- require_in:
- archive: grafana-package-archive-install-archive-extracted
- recurse:
- user
- group
- mode
grafana-package-archive-install-archive-extracted:
archive.extracted:
{{- format_kwargs(grafana.pkg.archive) }}
- retry:
attempts: 3
until: True
interval: 60
splay: 10
- user: root
- group: root
- recurse:
- user
- group
{%- endif %}
6 changes: 3 additions & 3 deletions grafana/config/alternatives/clean.sls
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ include:
grafana-package-archive-remove-home-alternative-remove:
alternatives.remove:
- name: grafana-home
- path: {{ grafana.base_dir }}
- path: {{ grafana.pkg.archive.name }}
- onlyif: update-alternatives --get-selections |grep ^grafana-home
- require:
- sls: {{ sls_archive_clean if grafana.pkg.use_upstream_archive else sls_package_clean }}
{% for i in ['grafana',] %}
{% for i in ['grafana-cli', 'grafana-server'] %}
grafana-package-archive-remove-{{ i }}-alternative-remove:
alternatives.remove:
- name: link-{{ i }}
- path: {{ grafana.base_dir }}/{{ i }}
- path: {{ grafana.pkg.archive.name }}/bin/{{ i }}
- onlyif: update-alternatives --get-selections |grep ^link-{{ i }}
- require:
- sls: {{ sls_archive_clean if grafana.pkg.use_upstream_archive else sls_package_clean }}
Expand Down
28 changes: 14 additions & 14 deletions grafana/config/alternatives/install.sls
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,53 @@
{%- set tplroot = tpldir.split('/')[0] %}
{%- from tplroot ~ "/map.jinja" import grafana with context %}
{%- set sls_archive_install = tplroot ~ '.archive.install' %}
{%- set sls_package_install = tplroot ~ '.package.clean' %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
{%- if grains.kernel|lower == 'linux' and grafana.linux.altpriority|int > 0 %}
{%- if grafana.pkg.use_upstream_archive %}
{%- if grains.kernel|lower == 'linux' and grafana.linux.altpriority|int > 0 %}
include:
- {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }}
- {{ sls_archive_install }}
grafana-package-archive-install-home-alternative-install:
cmd.run:
- name: update-alternatives --install {{ grafana.dir }} grafana-home {{ grafana.base_dir }} {{grafana.linux.altpriority}}
- name: update-alternatives --install {{ grafana.dir }} grafana-home {{ grafana.pkg.archive.name }} {{grafana.linux.altpriority}}
- watch:
- archive: grafana-package-archive-install-archive-extracted
- require:
- {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }}
- sls: {{ sls_archive_install }}
- onlyif: {{ grains.os_family in ('Suse',) }}
alternatives.install:
- name: grafana-home
- link: {{ grafana.dir }}
- path: {{ grafana.base_dir }}
- path: {{ grafana.pkg.archive.name }}
- priority: {{ grafana.linux.altpriority }}
- order: 10
- watch:
- archive: grafana-package-archive-install-archive-extracted
- unless: {{ grains.os_family in ('Suse',) }}
- require:
- {{ sls_archive_install if grafana.pkg.use_upstream_archive else sls_package_install }}
- sls: {{ sls_archive_install }}
grafana-package-archive-install-home-alternative-set:
alternatives.set:
- name: grafana-home
- path: {{ grafana.base_dir }}
- path: {{ grafana.pkg.archive.name }}
- require:
- alternatives: grafana-package-archive-install-home-alternative-install
- unless: {{ grains.os_family in ('Suse',) }}
{% for i in ['grafana',] %}
{% for i in ['grafana-cli', 'grafana-server'] %}
grafana-package-archive-install-{{ i }}-alternative-install:
cmd.run:
- name: update-alternatives --install /usr/bin/{{i}} link-{{i}} {{ grafana.base_dir }}/{{i}} {{grafana.linux.altpriority}}
- name: update-alternatives --install /usr/bin/{{i}} link-{{i}} {{ grafana.pkg.archive.name }}/bin/{{i}} {{grafana.linux.altpriority}}
- require:
- cmd: grafana-package-archive-install-home-alternative-install
- onlyif: {{ grains.os_family in ('Suse',) }}
alternatives.install:
- name: link-{{ i }}
- link: /usr/bin/{{ i }}
- path: {{ grafana.base_dir }}/{{ i }}
- path: {{ grafana.pkg.archive.name }}/bin/{{ i }}
- priority: {{ grafana.linux.altpriority }}
- order: 10
- require:
Expand All @@ -62,10 +61,11 @@ grafana-package-archive-install-{{ i }}-alternative-install:
grafana-package-archive-install-{{ i }}-alternative-set:
alternatives.set:
- name: link-{{ i }}
- path: {{ grafana.base_dir }}/{{ i }}
- path: {{ grafana.pkg.archive.name }}/bin/{{ i }}
- require:
- alternatives: grafana-package-archive-install-{{ i }}-alternative-install
- unless: {{ grains.os_family in ('Suse',) }}
{% endfor %}
{% endfor %}
{%- endif %}
{%- endif %}
1 change: 0 additions & 1 deletion grafana/config/clean.sls
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ grafana-config-clean-file-absent:
- sls: {{ sls_alternatives_clean }}
{%- endif %}
~
2 changes: 1 addition & 1 deletion grafana/config/environ.sls
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ grafana-config-file-file-managed-environ_file:
}}
- mode: 640
- user: root
- group: {{ grafana.group }}
- group: {{ grafana.rootgroup if grafana.pkg.use_upstream_archive else grafana.group }}
- makedirs: True
- template: jinja
- context:
Expand Down
1 change: 1 addition & 0 deletions grafana/config/file.sls
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ grafana-config-file-file-managed-config_file:
- mode: 640
- user: root
- group: {{ grafana.group }}
- group: {{ grafana.rootgroup if grafana.pkg.use_upstream_archive else grafana.group }}
- makedirs: True
- template: jinja
- context:
Expand Down
10 changes: 9 additions & 1 deletion grafana/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# vim: ft=yaml
---
grafana:
dir: /opt/grafana
version: '6.2.2'
pkg:
name: grafana
Expand All @@ -15,8 +16,11 @@ grafana:
trim_output: True {# works in 2018.3.2. onwards #}
archive_suffix: tar.gz
archive_format: tar
enforce_toplevel: True
enforce_toplevel: False # needed for grafana
options: '--strip-components=1' # needed for grafana
pkgrepo: {}
kernel: {{ grains.kernel | lower }}
rootgroup: root
group: grafana
config_file: /etc/grafana/grafana.ini
config: {}
Expand All @@ -26,3 +30,7 @@ grafana:
name: grafana-server
user: grafana
group: grafana

linux:
#'Alternatives system' priority: zero disables (default)
altpriority: 0
12 changes: 12 additions & 0 deletions grafana/jinja/macros.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
# vim: ft=jinja
#
# Collection of common macros

{%- macro format_kwargs(kwarg) -%}
{%- filter indent(4) %}
{%- for k, v in kwarg|dictsort() %}
- {{ k }}: {{ v }}
{%- endfor %}
{%- endfilter %}
{%- endmacro %}
12 changes: 7 additions & 5 deletions grafana/map.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
{#- Start imports as #}
{%- import_yaml tplroot ~ "/defaults.yaml" as default_settings %}
{%- import_yaml tplroot ~ "/osfamilymap.yaml" as osfamilymap %}
{%- import_yaml tplroot ~ "/osarchmap.yaml" as osarchmap %}
{%- import_yaml tplroot ~ "/osmap.yaml" as osmap %}
{%- import_yaml tplroot ~ "/osfingermap.yaml" as osfingermap %}

{%- set defaults = salt['grains.filter_by'](default_settings,
default='grafana',
merge=salt['grains.filter_by'](osfamilymap, grain='os_family',
merge=salt['grains.filter_by'](osmap, grain='os',
merge=salt['grains.filter_by'](osfingermap, grain='osfinger',
merge=salt['pillar.get']('grafana:lookup', default={})
merge=salt['grains.filter_by'](osarchmap, grain='osarch',
merge=salt['grains.filter_by'](osmap, grain='os',
merge=salt['grains.filter_by'](osfingermap, grain='osfinger',
merge=salt['pillar.get']('grafana:lookup', default={})
)
)
)
)
Expand All @@ -33,8 +36,7 @@
'source_hash': uri + '.' + grafana.pkg.archive.archive_suffix + '.sha256',
'archive_format': grafana.pkg.archive.archive_format
}) %}
{%- do grafana.update({'base_dir': grafana.pkg.archive.name}) %}
{%- do grafana.environ.append('export PATH=${PATH}:' + grafana.pkg.archive.name) %}
{%- do grafana.environ.append('export PATH=${PATH}:' + grafana.pkg.archive.name + '/bin') %}
{%- endif %}

{#- Contactenate arguments #}
Expand Down
35 changes: 35 additions & 0 deletions grafana/osarchmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# vim: ft=yaml
#
# Setup variables using grains['osarch'] based logic.
# You just need to add the key:values for an `osarch` that differ
# from `defaults.yaml` + `os_family.yaml`.
# Only add an `osarch` which is/will be supported by the formula
#
# If you do not need to provide defaults via the `osarch` grain,
# you will need to provide at least an empty dict in this file, e.g.
# osarch: {}
---
amd64:
arch: amd64

x86_64:
arch: amd64

386:
arch: 386

arm64:
arch: arm64

armv6l:
arch: armv6l

armv7l:
arch: armv6l

ppc64le:
arch: ppc64le

s390x:
arch: s390x
3 changes: 3 additions & 0 deletions grafana/osfamilymap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ OpenBSD: {}
Solaris: {}

Windows:
dir: C:\\Program Files
pkg:
archive:
name: C:\\Program Files
archive_suffix: zip
archive_format: zip

MacOS:
rootgroup: {{ macos_group | d('') }}
group: {{ macos_group | d('') }}
service:
rootgroup: {{ macos_group | d('') }}
group: {{ macos_group | d('') }}
2 changes: 2 additions & 0 deletions grafana/package/clean.sls
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ include:
grafana-package-clean-pkg-removed:
pkg.removed:
- name: {{ grafana.pkg.name }}
{%- if 'config' in grafana and grafana.config %}
- require:
- sls: {{ sls_config_clean }}
{%- endif %}
6 changes: 6 additions & 0 deletions grafana/service/running.sls
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
include:
- {{ sls_config_file }}
grafana-service-running-service-unmasked:
service.unmasked:
- name: {{ grafana.service.name }}
- onlyif: systemctl list-unit-files | grep {{ grafana.service.name }} >/dev/null 2>&1
grafana-service-running-service-running:
service.running:
- name: {{ grafana.service.name }}
Expand All @@ -19,3 +24,4 @@ grafana-service-running-service-running:
- require:
- sls: {{ sls_config_file }}
{%- endif %}
- onlyif: systemctl list-unit-files | grep {{ grafana.service.name }} >/dev/null 2>&1
50 changes: 50 additions & 0 deletions test/integration/default/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Default InSpec Profile

This shows the implementation of the Default InSpec [profile](https://github.com/inspec/inspec/blob/master/docs/profiles.md).

## Verify a profile

InSpec ships with built-in features to verify a profile structure.

```bash
$ inspec check default
Summary
-------
Location: default
Profile: profile
Controls: 4
Timestamp: 2019-06-24T23:09:01+00:00
Valid: true

Errors
------

Warnings
--------
```

## Execute a profile

To run all **supported** controls on a local machine use `inspec exec /path/to/profile`.

```bash
$ inspec exec default
..

Finished in 0.0025 seconds (files took 0.12449 seconds to load)
8 examples, 0 failures
```

## Execute a specific control from a profile

To run one control from the profile use `inspec exec /path/to/profile --controls name`.

```bash
$ inspec exec default --controls package
.

Finished in 0.0025 seconds (files took 0.12449 seconds to load)
1 examples, 0 failures
```

See an [example control here](https://github.com/inspec/inspec/blob/master/examples/profile/controls/example.rb).
12 changes: 12 additions & 0 deletions test/integration/default/controls/environ_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
control 'grafana configuration environment' do
title 'should match desired lines'

describe file('/etc/default/grafana.sh') do
it { should be_file }
it { should be_owned_by 'root' }
it { should be_grouped_into 'root' }
its('mode') { should cmp '0644' }
its('content') { should include 'Your changes may be overwritten' }
its('content') { should include 'export PATH=${PATH}:/opt/grafana-2.10.0.linux-amd64/bin' }
end
end
12 changes: 6 additions & 6 deletions test/integration/default/inspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ maintainer: Alexander Weidinger
license: Apache-2.0
summary: Verify that the grafana formula is setup and configured correctly
supports:
- os-name: debian
- os-name: ubuntu
- os-name: centos
- os-name: fedora
- os-name: opensuse
- os-name: freebsd
- platform-name: debian
- platform-name: ubuntu
- platform-name: centos
- platform-name: fedora
- platform-name: opensuse
- platform-name: freebsd
Loading

0 comments on commit 27bc326

Please sign in to comment.