Your race director for the next hour
Breuninger
Operations Core Tooling
DevOps & Operations Engineer
Let’s talk about
it´s just really cool to describe your whole infrastructure with code
Code without tests is broken by design.
So, how do we build this test driven?
Answer:
Build and test a Docker image
Dockerfile
FROM python:alpine3.11
RUN adduser --disabled-password --gecos '' -u 1001 app
USER app
WORKDIR /home/app
COPY --chown=app:app app.py app.py
EXPOSE 8000
CMD [ "python3", "app.py" ]
Static analysis Github
Validate the structure of a container image
schemaVersion: 2.0.0
fileExistenceTests:
- name: "app.py"
path: "/home/app/app.py"
shouldExist: true
uid: 1001
metadataTest:
exposedPorts: ["8000"]
cmd: ["python3", "app.py"]
workdir: "/home/app"
Linter Static analysis Github
A smarter Dockerfile linter that helps you build best practice Docker images
Unit test
docker run --rm --name tdd-example-test -d -p 8000:8000 tdd-example:latest
attempt_counter=0
max_attempts=5
until $(curl --output /dev/null --silent --fail http://localhost:8000); do
if [ ${attempt_counter} -eq ${max_attempts} ];then
echo "Max attempts reached"
exit 1
fi
echo '.'
attempt_counter=$(($attempt_counter+1))
sleep 5
done
docker stop tdd-example-test
Testing Ansible roles
Modular framework for testing Ansible roles in many scenarios and distributions
Drivers:
Ansible, Docker, Podman, Vagrant, Cloud providers
Verifiers:
Ansible-Lint, yamllint, Ansible, Inspec, Testinfra, Goss
Linter Github
Checks playbooks for practices and behaviour that could potentially be improved.
$ ansible-lint geerlingguy.apache
[502] All tasks should be named
/Users/chouseknecht/.ansible/roles/geerlingguy.apache/tasks/main.yml:29
Task/Handler: include_vars apache-22.yml
A linter for YAML files.
Enforce best practices and formats for yaml files.
Unit test Integration test Github
Use ansible facts and modules and assert as test driver.
- name: Populate service facts
service_facts:
- name: Assert services are running
assert:
that:
- ansible_facts.services['docker.service'].state == 'running'
- name: Check Cadvisor web is available
uri: url="http://localhost:8089/containers/" status_code=[200]
register: result
until: result.status == 200
retries: 60
delay: 1
With Testinfra you can write unit tests in Python to test actual state of your servers.
def test_nginx_is_installed(host):
nginx = host.package("nginx")
assert nginx.is_installed
assert nginx.version.startswith("1.2")
def test_nginx_running_and_enabled(host):
nginx = host.service("nginx")
assert nginx.is_running
assert nginx.is_enabled
Make use of the well known pytest framework. Inspired by Serverspec.
Unit test Github
Goss is a simple YAML based tool for validating a server’s configuration. Allows generating tests from current system status. No coding required.
$ cat goss.yaml
port:
tcp:22:
listening: true
ip:
- 0.0.0.0
service:
sshd:
enabled: true
running: true
Can also be used for verifying docker images!
Unit test Integration test Github Docs
Chef InSpec is an testing framework for infrastructure with a human- and machine-readable DSL.
describe port(80) do
it { should_not be_listening }
end
describe port(443) do
it { should be_listening }
its('protocols') {should include 'tcp'}
end
Provides a wide range of controls for servers and cloud providers like AWS or Azure.
Build an test AMI with Packer
Workflow
Packer is a free and open source tool for creating golden images for multiple platforms
Supports multiple platforms like AWS, Azure, GCP, VMWare and Docker.
Unit test Integration test Github Docs
Chef InSpec is an testing framework for infrastructure with a human- and machine-readable DSL.
describe port(80) do
it { should_not be_listening }
end
describe port(443) do
it { should be_listening }
its('protocols') {should include 'tcp'}
end
Provides a wide range of controls for servers and cloud providers like AWS or Azure.
Test Terraform modules
Testing infrastructure requires a real world infrastructure!
This will deploy and destroy many resources!
Tipp: Use an isolated sandbox account and nuke this account every night
Test framework Github
Kitchen-Terraform enables verification of infrastructure systems provisioned with Terraform.
Test framework Unit test Integration test Github
Terratest is a Go library that makes it easier to write automated tests for your infrastructure code. It provides a variety of helper functions and patterns for common infrastructure testing tasks.
Testing infrastructure code is a solved problem!
Split large projects into handy modules
Questions?