Browse Source

test(inspec): switch tests to use Inspec

tags/v0.55.0
Eric Veiras Galisson 5 years ago
parent
commit
b2bcb4c998
14 changed files with 204 additions and 103 deletions
  1. +1
    -0
      .gitignore
  2. +57
    -15
      .travis.yml
  3. +6
    -0
      Gemfile
  4. +104
    -0
      kitchen.yml
  5. +10
    -0
      test/integration/default/controls/config.rb
  6. +7
    -0
      test/integration/default/controls/install.rb
  7. +8
    -0
      test/integration/default/controls/service.rb
  8. +11
    -0
      test/integration/default/inspec.yml
  9. +0
    -0
      test/salt/default/pillar/nginx.sls
  10. +0
    -23
      tests/pytests/apply-all-tests/test_000_apply_state.py
  11. +0
    -3
      tests/srv/salt/top.sls
  12. +0
    -27
      tools/filltmpl.py
  13. +0
    -21
      tools/run-tests.sh
  14. +0
    -14
      tools/templates/Dockerfile.j2

+ 1
- 0
.gitignore View File

@@ -10,3 +10,4 @@ __pycache__/
Dockerfile.*_*
ignore/
tmp/
.kitchen/

+ 57
- 15
.travis.yml View File

@@ -1,24 +1,66 @@
env:
matrix:
- OS_ID: centos_master_2017.7.2
- OS_ID: debian_master_2017.7.2
- OS_ID: opensuse_master_2017.7.2
- OS_ID: ubuntu_master_2016.11.3
- OS_ID: ubuntu_master_2017.7.2
stages:
- test
- commitlint
- name: release
if: branch = master AND type != pull_request

sudo: required
language: python
cache: bundler
language: ruby

services:
- docker
- docker

before_install:
- pip install Jinja2
- python ${TRAVIS_BUILD_DIR}/tools/filltmpl.py nginx ${OS_ID}
- gem install bundle
- bundle install

install:
- docker build --force-rm -t "nginx:salt-testing-${OS_ID}" -f "Dockerfile.${OS_ID}" .
# Make sure the instances listed below match up with
# 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:
- ./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
- 0
Gemfile View 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
- 0
kitchen.yml View 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
- 0
test/integration/default/controls/config.rb View 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
- 0
test/integration/default/controls/install.rb View File

@@ -0,0 +1,7 @@
control 'Nginx package' do
title 'should be installed'

describe package('nginx') do
it { should be_installed }
end
end

+ 8
- 0
test/integration/default/controls/service.rb View 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
- 0
test/integration/default/inspec.yml View 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

tests/pytests/apply-all-tests/__init__.py → test/salt/default/pillar/nginx.sls View File


+ 0
- 23
tests/pytests/apply-all-tests/test_000_apply_state.py View File

@@ -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)

+ 0
- 3
tests/srv/salt/top.sls View File

@@ -1,3 +0,0 @@
base:
'*':
- nginx

+ 0
- 27
tools/filltmpl.py View File

@@ -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)

+ 0
- 21
tools/run-tests.sh View File

@@ -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

+ 0
- 14
tools/templates/Dockerfile.j2 View File

@@ -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…
Cancel
Save