From b2bcb4c998d1e3efc5b316dbf3262a973c2c8517 Mon Sep 17 00:00:00 2001 From: Eric Veiras Galisson Date: Fri, 12 Apr 2019 17:34:35 +0200 Subject: [PATCH] test(inspec): switch tests to use Inspec --- .gitignore | 1 + .travis.yml | 72 +++++++++--- Gemfile | 6 + kitchen.yml | 104 ++++++++++++++++++ test/integration/default/controls/config.rb | 10 ++ test/integration/default/controls/install.rb | 7 ++ test/integration/default/controls/service.rb | 8 ++ test/integration/default/inspec.yml | 11 ++ .../salt/default/pillar/nginx.sls | 0 .../apply-all-tests/test_000_apply_state.py | 23 ---- tests/srv/salt/top.sls | 3 - tools/filltmpl.py | 27 ----- tools/run-tests.sh | 21 ---- tools/templates/Dockerfile.j2 | 14 --- 14 files changed, 204 insertions(+), 103 deletions(-) create mode 100644 Gemfile create mode 100644 kitchen.yml create mode 100644 test/integration/default/controls/config.rb create mode 100644 test/integration/default/controls/install.rb create mode 100644 test/integration/default/controls/service.rb create mode 100644 test/integration/default/inspec.yml rename tests/pytests/apply-all-tests/__init__.py => test/salt/default/pillar/nginx.sls (100%) delete mode 100644 tests/pytests/apply-all-tests/test_000_apply_state.py delete mode 100644 tests/srv/salt/top.sls delete mode 100644 tools/filltmpl.py delete mode 100755 tools/run-tests.sh delete mode 100644 tools/templates/Dockerfile.j2 diff --git a/.gitignore b/.gitignore index 4aa68cb..da4fbca 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ __pycache__/ Dockerfile.*_* ignore/ tmp/ +.kitchen/ diff --git a/.travis.yml b/.travis.yml index 9894711..597031c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 + diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..7b544d6 --- /dev/null +++ b/Gemfile @@ -0,0 +1,6 @@ +source "https://rubygems.org" + +gem 'kitchen-docker', '>= 2.9' +gem 'kitchen-salt', '>= 0.5.0' +gem 'kitchen-inspec', '>= 1.1' + diff --git a/kitchen.yml b/kitchen.yml new file mode 100644 index 0000000..155084f --- /dev/null +++ b/kitchen.yml @@ -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 + diff --git a/test/integration/default/controls/config.rb b/test/integration/default/controls/config.rb new file mode 100644 index 0000000..e165fad --- /dev/null +++ b/test/integration/default/controls/config.rb @@ -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 diff --git a/test/integration/default/controls/install.rb b/test/integration/default/controls/install.rb new file mode 100644 index 0000000..49aea2e --- /dev/null +++ b/test/integration/default/controls/install.rb @@ -0,0 +1,7 @@ +control 'Nginx package' do + title 'should be installed' + + describe package('nginx') do + it { should be_installed } + end +end diff --git a/test/integration/default/controls/service.rb b/test/integration/default/controls/service.rb new file mode 100644 index 0000000..5dad48c --- /dev/null +++ b/test/integration/default/controls/service.rb @@ -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 diff --git a/test/integration/default/inspec.yml b/test/integration/default/inspec.yml new file mode 100644 index 0000000..e49fb37 --- /dev/null +++ b/test/integration/default/inspec.yml @@ -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 diff --git a/tests/pytests/apply-all-tests/__init__.py b/test/salt/default/pillar/nginx.sls similarity index 100% rename from tests/pytests/apply-all-tests/__init__.py rename to test/salt/default/pillar/nginx.sls diff --git a/tests/pytests/apply-all-tests/test_000_apply_state.py b/tests/pytests/apply-all-tests/test_000_apply_state.py deleted file mode 100644 index cf0b620..0000000 --- a/tests/pytests/apply-all-tests/test_000_apply_state.py +++ /dev/null @@ -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) diff --git a/tests/srv/salt/top.sls b/tests/srv/salt/top.sls deleted file mode 100644 index 9f754f2..0000000 --- a/tests/srv/salt/top.sls +++ /dev/null @@ -1,3 +0,0 @@ -base: - '*': - - nginx diff --git a/tools/filltmpl.py b/tools/filltmpl.py deleted file mode 100644 index 0bbeace..0000000 --- a/tools/filltmpl.py +++ /dev/null @@ -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) diff --git a/tools/run-tests.sh b/tools/run-tests.sh deleted file mode 100755 index 550fa58..0000000 --- a/tools/run-tests.sh +++ /dev/null @@ -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 diff --git a/tools/templates/Dockerfile.j2 b/tools/templates/Dockerfile.j2 deleted file mode 100644 index 5686069..0000000 --- a/tools/templates/Dockerfile.j2 +++ /dev/null @@ -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