test(inspec): switch tests to use Inspec
This commit is contained in:
parent
62e8ac0b33
commit
b2bcb4c998
1
.gitignore
vendored
1
.gitignore
vendored
@ -10,3 +10,4 @@ __pycache__/
|
|||||||
Dockerfile.*_*
|
Dockerfile.*_*
|
||||||
ignore/
|
ignore/
|
||||||
tmp/
|
tmp/
|
||||||
|
.kitchen/
|
||||||
|
72
.travis.yml
72
.travis.yml
@ -1,24 +1,66 @@
|
|||||||
env:
|
stages:
|
||||||
matrix:
|
- test
|
||||||
- OS_ID: centos_master_2017.7.2
|
- commitlint
|
||||||
- OS_ID: debian_master_2017.7.2
|
- name: release
|
||||||
- OS_ID: opensuse_master_2017.7.2
|
if: branch = master AND type != pull_request
|
||||||
- OS_ID: ubuntu_master_2016.11.3
|
|
||||||
- OS_ID: ubuntu_master_2017.7.2
|
|
||||||
|
|
||||||
sudo: required
|
sudo: required
|
||||||
|
cache: bundler
|
||||||
language: python
|
language: ruby
|
||||||
|
|
||||||
services:
|
services:
|
||||||
- docker
|
- docker
|
||||||
|
|
||||||
before_install:
|
before_install:
|
||||||
- pip install Jinja2
|
- gem install bundle
|
||||||
- python ${TRAVIS_BUILD_DIR}/tools/filltmpl.py nginx ${OS_ID}
|
- bundle install
|
||||||
|
|
||||||
install:
|
# Make sure the instances listed below match up with
|
||||||
- docker build --force-rm -t "nginx:salt-testing-${OS_ID}" -f "Dockerfile.${OS_ID}" .
|
# the `platforms` defined in `kitchen.yml`
|
||||||
|
env:
|
||||||
|
matrix:
|
||||||
|
- INSTANCE: default-debian-9
|
||||||
|
- INSTANCE: default-debian-8
|
||||||
|
- INSTANCE: default-ubuntu-1804
|
||||||
|
- INSTANCE: default-ubuntu-1604
|
||||||
|
- INSTANCE: default-centos-7
|
||||||
|
# - INSTANCE: default-centos-6
|
||||||
|
- INSTANCE: default-fedora
|
||||||
|
- INSTANCE: default-opensuse-leap-salt-minion
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- ./tools/run-tests.sh nginx ${OS_ID}
|
- bundle exec kitchen verify ${INSTANCE}
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
include:
|
||||||
|
# Define the commitlint stage
|
||||||
|
- stage: commitlint
|
||||||
|
language: node_js
|
||||||
|
node_js: lts/*
|
||||||
|
before_install: skip
|
||||||
|
script:
|
||||||
|
- npm install @commitlint/config-conventional -D
|
||||||
|
- npm install @commitlint/travis-cli -D
|
||||||
|
- commitlint-travis
|
||||||
|
# Define the release stage that runs semantic-release
|
||||||
|
- stage: release
|
||||||
|
language: node_js
|
||||||
|
node_js: lts/*
|
||||||
|
before_install: skip
|
||||||
|
script:
|
||||||
|
# Update `AUTHORS.md`
|
||||||
|
- export MAINTAINER_TOKEN=${GH_TOKEN}
|
||||||
|
- go get github.com/myii/maintainer
|
||||||
|
- maintainer contributor
|
||||||
|
|
||||||
|
# Install all dependencies required for `semantic-release`
|
||||||
|
- npm install @semantic-release/changelog@3 -D
|
||||||
|
- npm install @semantic-release/exec@3 -D
|
||||||
|
- npm install @semantic-release/git@7 -D
|
||||||
|
deploy:
|
||||||
|
provider: script
|
||||||
|
skip_cleanup: true
|
||||||
|
script:
|
||||||
|
# Run `semantic-release`
|
||||||
|
- npx semantic-release@15
|
||||||
|
|
||||||
|
6
Gemfile
Normal file
6
Gemfile
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem 'kitchen-docker', '>= 2.9'
|
||||||
|
gem 'kitchen-salt', '>= 0.5.0'
|
||||||
|
gem 'kitchen-inspec', '>= 1.1'
|
||||||
|
|
104
kitchen.yml
Normal file
104
kitchen.yml
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# vim: ft=yaml
|
||||||
|
---
|
||||||
|
# For help on this file's format, see https://kitchen.ci/
|
||||||
|
driver:
|
||||||
|
name: docker
|
||||||
|
|
||||||
|
driver_config:
|
||||||
|
use_sudo: false
|
||||||
|
privileged: true
|
||||||
|
provision_command: mkdir -p /run/sshd
|
||||||
|
|
||||||
|
# Make sure the platforms listed below match up with
|
||||||
|
# the `env.matrix` instances defined in `.travis.yml`
|
||||||
|
platforms:
|
||||||
|
# The `run_command` used for each platform is required to
|
||||||
|
# test `systemd` services in docker
|
||||||
|
- name: debian-9
|
||||||
|
driver_config:
|
||||||
|
image: debian:9
|
||||||
|
run_command: /lib/systemd/systemd
|
||||||
|
provision_command:
|
||||||
|
- apt-get update && apt-get install -y udev
|
||||||
|
- name: debian-8
|
||||||
|
driver_config:
|
||||||
|
image: debian:8
|
||||||
|
run_command: /lib/systemd/systemd
|
||||||
|
provision_command:
|
||||||
|
- apt-get update && apt-get install -y udev
|
||||||
|
- name: ubuntu-18.04
|
||||||
|
driver_config:
|
||||||
|
image: ubuntu:18.04
|
||||||
|
run_command: /lib/systemd/systemd
|
||||||
|
provision_command:
|
||||||
|
- apt-get update && apt-get install -y udev
|
||||||
|
- name: ubuntu-16.04
|
||||||
|
driver_config:
|
||||||
|
image: ubuntu:16.04
|
||||||
|
run_command: /lib/systemd/systemd
|
||||||
|
provision_command:
|
||||||
|
- apt-get update && apt-get install -y udev
|
||||||
|
- name: centos-7
|
||||||
|
driver_config:
|
||||||
|
image: centos:7
|
||||||
|
run_command: /usr/lib/systemd/systemd
|
||||||
|
# - name: centos-6
|
||||||
|
# driver_config:
|
||||||
|
# image: centos:6
|
||||||
|
# run_command: /usr/lib/systemd/systemd
|
||||||
|
- name: fedora
|
||||||
|
driver_config:
|
||||||
|
image: fedora
|
||||||
|
run_command: /usr/lib/systemd/systemd
|
||||||
|
provision_command:
|
||||||
|
- yum -y update && yum -y install udev
|
||||||
|
# As of February 2019, there have been problems getting `opensuse` to work:
|
||||||
|
# * `opensuse` is deprecated
|
||||||
|
# * `opensuse/leap` grabs `15.x`, which doesn't run the `inspec` tests
|
||||||
|
# * `opensuse/tumbleweed` doesn't install `salt-minion`
|
||||||
|
# * `opensuse/leap:42.3` does work
|
||||||
|
# * `opensuse/salt-minion` uses `42.3` with `salt-minion` pre-installed
|
||||||
|
- name: opensuse-leap-salt-minion
|
||||||
|
driver_config:
|
||||||
|
image: opensuse/salt-minion
|
||||||
|
run_command: /usr/lib/systemd/systemd
|
||||||
|
provision_command:
|
||||||
|
- zypper refresh && zypper install -y udev
|
||||||
|
- systemctl enable sshd.service
|
||||||
|
- cat /etc/os-release
|
||||||
|
|
||||||
|
provisioner:
|
||||||
|
name: salt_solo
|
||||||
|
log_level: debug
|
||||||
|
require_chef: false
|
||||||
|
salt_version: latest
|
||||||
|
formula: nginx
|
||||||
|
salt_copy_filter:
|
||||||
|
- .kitchen
|
||||||
|
- .git
|
||||||
|
pillars_from_files:
|
||||||
|
nginx.sls: test/salt/default/pillar/nginx.sls
|
||||||
|
pillars:
|
||||||
|
top.sls:
|
||||||
|
base:
|
||||||
|
'*':
|
||||||
|
- nginx
|
||||||
|
state_top:
|
||||||
|
base:
|
||||||
|
'*':
|
||||||
|
- nginx.ng
|
||||||
|
|
||||||
|
verifier:
|
||||||
|
# https://www.inspec.io/
|
||||||
|
name: inspec
|
||||||
|
sudo: true
|
||||||
|
# cli, documentation, html, progress, json, json-min, json-rspec, junit
|
||||||
|
reporter:
|
||||||
|
- cli
|
||||||
|
inspec_tests:
|
||||||
|
- path: test/integration/default
|
||||||
|
|
||||||
|
suites:
|
||||||
|
- name: default
|
||||||
|
|
10
test/integration/default/controls/config.rb
Normal file
10
test/integration/default/controls/config.rb
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
control 'Nginx configuration' do
|
||||||
|
title 'should match desired lines'
|
||||||
|
|
||||||
|
describe file('/etc/nginx/nginx.conf') do
|
||||||
|
it { should be_file }
|
||||||
|
it { should be_owned_by 'root' }
|
||||||
|
it { should be_grouped_into 'root' }
|
||||||
|
its('mode') { should cmp '0644' }
|
||||||
|
end
|
||||||
|
end
|
7
test/integration/default/controls/install.rb
Normal file
7
test/integration/default/controls/install.rb
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
control 'Nginx package' do
|
||||||
|
title 'should be installed'
|
||||||
|
|
||||||
|
describe package('nginx') do
|
||||||
|
it { should be_installed }
|
||||||
|
end
|
||||||
|
end
|
8
test/integration/default/controls/service.rb
Normal file
8
test/integration/default/controls/service.rb
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
control 'Nginx service' do
|
||||||
|
title 'should be running and enabled'
|
||||||
|
|
||||||
|
describe service('nginx') do
|
||||||
|
it { should be_enabled }
|
||||||
|
it { should be_running }
|
||||||
|
end
|
||||||
|
end
|
11
test/integration/default/inspec.yml
Normal file
11
test/integration/default/inspec.yml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
name: nginx
|
||||||
|
title: Nginx Formula
|
||||||
|
maintainer: Saltstack-formulas
|
||||||
|
license: Apache-2.0
|
||||||
|
summary: Verify that the nginx formula is setup and configured correctly
|
||||||
|
supports:
|
||||||
|
- os-name: debian
|
||||||
|
- os-name: ubuntu
|
||||||
|
- os-name: centos
|
||||||
|
- os-name: fedora
|
||||||
|
- os-name: opensuse
|
@ -1,23 +0,0 @@
|
|||||||
from subprocess import check_output
|
|
||||||
from unittest import TestCase
|
|
||||||
|
|
||||||
|
|
||||||
class ApplyStateTest(TestCase):
|
|
||||||
|
|
||||||
def test_000_apply(self):
|
|
||||||
state_apply_response = check_output(["salt-call", "--local", "state.apply"])
|
|
||||||
print('')
|
|
||||||
print('-' * 50)
|
|
||||||
print('state_apply_response:')
|
|
||||||
print(state_apply_response)
|
|
||||||
print('-' * 50)
|
|
||||||
print('')
|
|
||||||
|
|
||||||
state_apply_response = state_apply_response.split('\n')
|
|
||||||
summary = state_apply_response[-8:]
|
|
||||||
failed = 0
|
|
||||||
for line in summary:
|
|
||||||
if line.startswith('Failed:'):
|
|
||||||
failed = int(line.split(':').pop().strip())
|
|
||||||
|
|
||||||
self.assertEqual(failed, 0)
|
|
@ -1,3 +0,0 @@
|
|||||||
base:
|
|
||||||
'*':
|
|
||||||
- nginx
|
|
@ -1,27 +0,0 @@
|
|||||||
import os
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from jinja2 import Template
|
|
||||||
|
|
||||||
# base/tests
|
|
||||||
dir_path = os.path.dirname(os.path.realpath(__file__))
|
|
||||||
|
|
||||||
# base
|
|
||||||
base_path = os.path.dirname(dir_path)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
formula_name = sys.argv[1]
|
|
||||||
image_tag = sys.argv[2]
|
|
||||||
|
|
||||||
template = Template(
|
|
||||||
open(os.path.join(dir_path, 'templates', 'Dockerfile.j2')).read()
|
|
||||||
)
|
|
||||||
|
|
||||||
dockerfile = template.render({
|
|
||||||
'formula_name': formula_name,
|
|
||||||
'image_tag': image_tag
|
|
||||||
})
|
|
||||||
|
|
||||||
with open(os.path.join(base_path, 'Dockerfile.{}'.format(image_tag)), 'w') as fh:
|
|
||||||
fh.write(dockerfile)
|
|
@ -1,21 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
set -ev
|
|
||||||
|
|
||||||
test -z $2 && echo "Usage: ${0} FORMULA_NAME OS_ID" && exit 1
|
|
||||||
export FORMULA_NAME=$1
|
|
||||||
export OS_ID=$2
|
|
||||||
|
|
||||||
|
|
||||||
function docker-run-pytest() {
|
|
||||||
docker run --rm \
|
|
||||||
-v "$@":/opt/tests \
|
|
||||||
--env=STAGE=TEST \
|
|
||||||
-h "salt-testing-${OS_ID}" \
|
|
||||||
--name "salt-testing-${OS_ID}" \
|
|
||||||
-it ${FORMULA_NAME}:"salt-testing-${OS_ID}" \
|
|
||||||
pytest -sv /opt/tests
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in $(find $PWD/tests/pytests/* -maxdepth 0 -type d); do
|
|
||||||
docker-run-pytest $i;
|
|
||||||
done
|
|
@ -1,14 +0,0 @@
|
|||||||
FROM simplyadrian/allsalt:{{ image_tag }}
|
|
||||||
|
|
||||||
{% if 'debian' in image_tag or 'ubuntu' in image_tag -%}
|
|
||||||
RUN apt-get update && \
|
|
||||||
apt-get install -y python-pip
|
|
||||||
{% endif %}
|
|
||||||
|
|
||||||
RUN pip install pytest && \
|
|
||||||
sed -i "s/#master: salt/master: localhost/g" /etc/salt/minion
|
|
||||||
|
|
||||||
ADD tests/srv /srv
|
|
||||||
ADD {{ formula_name }} /srv/salt/{{ formula_name }}
|
|
||||||
|
|
||||||
WORKDIR /srv/salt
|
|
Loading…
Reference in New Issue
Block a user