This is an example of how to utilize Kitchen-Terraform to test a Docker container running on localhost configured with the Terraform Docker Provider.
mkdir -p docker_provider_example/test/integration/example/controls
cd docker_provider_example
Gemfile
in the root of the project by running:touch Gemfile
Gemfile
.source 'https://rubygems.org/' do
gem 'kitchen-terraform', '~> 7.0'
end
gem install bundler
bundle install
.kitchen.yml
file in the root of the project.touch .kitchen.yml
.kitchen.yml
file.---
driver:
name: terraform
provisioner:
name: terraform
verifier:
name: terraform
systems:
- name: docker container
backend: ssh
password: root
hosts_output: container_host
controls:
- operating_system
port: 2222
- name: localhost
backend: local
controls:
- state_files
platforms:
- name: ubuntu
suites:
- name: example
test/integration/example/
versions.tf
in the root of the project.touch versions.tf
# Set the required provider and versions
terraform {
required_version = ">= 0.14.0, < 2.0.0"
required_providers {
# We recommend pinning to the specific version of the Docker Provider you're using
# since new versions are released frequently
docker = {
source = "kreuzwerker/docker"
version = "2.23.1"
}
}
}
main.tf
in the root of the project.touch main.tf
provider "docker" {
host = "unix:///var/run/docker.sock"
}
data "docker_registry_image" "ubuntu_sshd" {
name = "rastasheep/ubuntu-sshd:latest"
}
resource "docker_image" "ubuntu_sshd" {
keep_locally = true
name = data.docker_registry_image.ubuntu_sshd.name
pull_triggers = [data.docker_registry_image.ubuntu_sshd.sha256_digest]
}
resource "docker_container" "ubuntu" {
image = docker_image.ubuntu_sshd.latest
must_run = true
name = "ubuntu_container"
ports {
external = 2222
internal = 22
}
}
output.tf
touch output.tf
output "terraform state" {
description = "The path to the backend state file"
value = "${path.module}/terraform.tfstate.d/${terraform.workspace}/terraform.tfstate"
}
output "container_host" {
description = "The container's host name"
value = "localhost"
}
.kitchen.yml
file and in the verifier section you will see a reference to the above container host output.test/integration/example/inspec.yml
touch test/integration/examples/inspec.yml
---
name: default
.kitchen.yml
file and inside the verifier section there is an operating_system control which we need to create.test/integration/example/controls/operating_system.rb
# frozen_string_literal: true
control 'operating_system' do
describe command("lsb_release -a") do
its('stderr') { should match /lsb_release: command not found/ }
end
describe command('uname -ar') do
its('stdout') { should match(/Linux/) }
end
describe command("env -i bash -c '. /etc/os-release; echo $NAME'") do
its('stdout') { should match /Ubuntu/ }
end
end
test/integration/example/controls/state_file.rb
# frozen_string_literal: true
terraform_state = input('terraform_state', {})
control 'state_files' do
describe 'the terraform state file' do
subject do
file terraform_state
end
it do
is_expected.to exist
end
end
end
bundle exec kitchen converge
-----> Starting Test Kitchen (v3.4.0)
-----> Creating <example-ubuntu>...
$$$$$$ Reading the Terraform client version...
Terraform v0.14.0
+ provider registry.terraform.io/kreuzwerker/docker v2.23.1
Your version of Terraform is out of date! The latest version
is 1.3.6. You can update by downloading from https://www.terraform.io/downloads.html
$$$$$$ Finished reading the Terraform client version.
$$$$$$ Verifying the Terraform client version is in the supported interval of >= 0.11.4, < 2.0.0...
$$$$$$ Finished verifying the Terraform client version.
$$$$$$ Initializing the Terraform working directory...
Initializing the backend...
Initializing provider plugins...
- Finding kreuzwerker/docker versions matching "2.23.1"...
- Installing kreuzwerker/docker v2.23.1...
- Installed kreuzwerker/docker v2.23.1 (self-signed, key ID BD080C4571C6104C)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/plugins/signing.html
Terraform has been successfully initialized!
$$$$$$ Finished initializing the Terraform working directory.
$$$$$$ Creating the kitchen-terraform-example-ubuntu Terraform workspace...
Created and switched to workspace "kitchen-terraform-example-ubuntu"!
You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
$$$$$$ Finished creating the kitchen-terraform-example-ubuntu Terraform workspace.
Finished creating <example-ubuntu> (0m3.36s).
-----> Converging <example-ubuntu>...
$$$$$$ Reading the Terraform client version...
Terraform v0.14.0
+ provider registry.terraform.io/kreuzwerker/docker v2.23.1
Your version of Terraform is out of date! The latest version
is 1.3.6. You can update by downloading from https://www.terraform.io/downloads.html
$$$$$$ Finished reading the Terraform client version.
$$$$$$ Verifying the Terraform client version is in the supported interval of >= 0.11.4, < 2.0.0...
$$$$$$ Finished verifying the Terraform client version.
$$$$$$ Selecting the kitchen-terraform-example-ubuntu Terraform workspace...
$$$$$$ Finished selecting the kitchen-terraform-example-ubuntu Terraform workspace.
$$$$$$ Downloading the modules needed for the Terraform configuration...
$$$$$$ Finished downloading the modules needed for the Terraform configuration.
$$$$$$ Validating the Terraform configuration files...
Success! The configuration is valid.
$$$$$$ Finished validating the Terraform configuration files.
$$$$$$ Building the infrastructure based on the Terraform configuration...
docker_image.ubuntu_sshd: Creating...
docker_image.ubuntu_sshd: Creation complete after 0s [id=sha256:49533628fb371c9f1952c06cedf912c78a81fbe3914901334673c369376e077erastasheep/ubuntu-sshd:latest]
docker_container.ubuntu: Creating...
docker_container.ubuntu: Creation complete after 1s [id=b2766cde74c528e46638f5ab273476431402f7053668499145bc03a12e07291f]
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
Outputs:
container_host = "localhost"
terraform_state = "./terraform.tfstate.d/kitchen-terraform-example-ubuntu/terraform.tfstate"
$$$$$$ Finished building the infrastructure based on the Terraform configuration.
$$$$$$ Reading the output variables from the Terraform state...
$$$$$$ Finished reading the output variables from the Terraform state.
$$$$$$ Parsing the Terraform output variables as JSON...
$$$$$$ Finished parsing the Terraform output variables as JSON.
$$$$$$ Writing the output variables to the Kitchen instance state...
$$$$$$ Finished writing the output variables to the Kitchen instance state.
$$$$$$ Writing the input variables to the Kitchen instance state...
$$$$$$ Finished writing the input variables to the Kitchen instance state.
Finished converging <example-ubuntu> (0m3.32s).
-----> Test Kitchen is finished. (0m7.86s)
bundle exec kitchen verify
-----> Starting Test Kitchen (v3.4.0)
-----> Setting up <example-ubuntu>...
Finished setting up <example-ubuntu> (0m0.00s).
-----> Verifying <example-ubuntu>...
$$$$$$ Reading the Terraform input variables from the Kitchen instance state...
$$$$$$ Finished reading the Terraform input variables from the Kitchen instance state.
$$$$$$ Reading the Terraform output variables from the Kitchen instance state...
$$$$$$ Finished reading the Terraform output variables from the Kitchen instance state.
$$$$$$ Verifying the systems...
$$$$$$ Verifying the 'docker container' system...
Profile: default
Version: (not specified)
Target: ssh://root@localhost:2222
✔ operating_system: Command: `lsb_release -a`
✔ Command: `lsb_release -a` stderr is expected to match /lsb_release: command not found/
✔ Command: `uname -ar` stdout is expected to match /Linux/
✔ Command: `env -i bash -c '. /etc/os-release; echo $NAME'` stdout is expected to match /Ubuntu/
Profile Summary: 1 successful control, 0 control failures, 0 controls skipped
Test Summary: 3 successful, 0 failures, 0 skipped
$$$$$$ Finished verifying the 'docker container' system.
$$$$$$ Verifying the 'terraform state' system...
Profile: default
Version: (not specified)
Target: local://
✔ state_files: the terraform state file
✔ the terraform state file is expected to exist
Profile Summary: 1 successful control, 0 control failures, 0 controls skipped
Test Summary: 1 successful, 0 failures, 0 skipped
$$$$$$ Finished verifying the 'terraform state' system.
$$$$$$ Finished verifying the systems.
Finished verifying <example-ubuntu> (0m6.20s).
-----> Test Kitchen is finished. (0m7.62s)
bundle exec kitchen verify
bundle exec kitchen destroy