This is an example of how to utilize Kitchen-Terraform to test OpenStack resources configured with the Terraform OpenStack Provider.
mkdir -p openstack_provider_example/test/integration/example/controls
mkdir -p openstack_provider_example/dummy_keypair
ssh-keygen -f openstack_provider_example/dummy_keypair/cloud.key
cd openstack_provider_example
Gemfile
to install our dependencies.source "https://rubygems.org/" do
gem "kitchen-terraform"
end
gem install bundler
bundle install
.kitchen.yml
which brings together the Terraform module code and Inspec controls.---
driver:
name: terraform
command_timeout: 1000
variable_files:
- ./my-variables.tfvars
provisioner:
name: terraform
transport:
name: ssh
ssh_key: ./dummy_keypair/cloud.key
username: ubuntu
verifier:
name: terraform
groups:
- name: master
controls:
- nano_installed
hostnames: master_address
- name: workers
controls:
- curl_installed
hostnames: workers_addresses
platforms:
- name: ubuntu
suites:
- name: example
test/integration/example/
main.tf
and add each block of code into it.terraform {
required_version = "~> 0.10.2"
}
provider "openstack" {
auth_url = "${var.provider_auth_url}"
password = "${var.provider_password}"
region = "${var.provider_region}"
tenant_name = "${var.provider_tenant_name}"
user_name = "${var.provider_user_name}"
}
resource "openstack_compute_keypair_v2" "kitchen-terraform" {
name = "kitchen-terraform-example"
public_key = "${file("./dummy_keypair/cloud.key.pub")}"
}
resource "openstack_networking_floatingip_v2" "master" {
pool = "${var.networking_floatingips_pool}"
}
resource "openstack_compute_instance_v2" "master" {
flavor_name = "v.c1.m1024.d5.e0"
floating_ip = "${element(openstack_networking_floatingip_v2.master.*.address, 0)}"
image_name = "ubuntu-16.04"
key_pair = "${openstack_compute_keypair_v2.kitchen-terraform.name}"
name = "kitchen-terraform-example-master"
connection {
host = "${self.floating_ip}"
private_key = "${file("./dummy/cloud.key")}"
type = "ssh"
user = "ubuntu"
}
metadata = {
ssh_user = "ubuntu"
}
network {
name = "${var.compute_instances_network_name}"
}
provisioner "remote-exec" {
inline = ["sudo apt-get install --no-install-recommends --yes nano"]
}
}
resource "openstack_networking_floatingip_v2" "workers" {
count = 2
pool = "${var.networking_floatingips_pool}"
}
resource "openstack_compute_instance_v2" "worker" {
count = 2
flavor_name = "v.c1.m1024.d5.e0"
floating_ip = "${element(openstack_networking_floatingip_v2.workers.*.address, count.index)}"
image_name = "ubuntu-16.04"
key_pair = "${openstack_compute_keypair_v2.kitchen-terraform.name}"
name = "kitchen-terraform-example-worker-${count.index+1}"
connection {
host = "${self.floating_ip}"
private_key = "${file("./dummy/cloud.key")}"
type = "ssh"
user = "ubuntu"
}
metadata = {
ssh_user = "ubuntu"
}
network {
name = "${var.compute_instances_network_name}"
}
provisioner "remote-exec" {
inline = ["sudo apt-get install --no-install-recommends --yes curl"]
}
}
variable.tf
and add the below block of code into it.variable "compute_instances_network_name" {
description = "The human-readable name of the network of the compute instances"
type = "string"
}
variable "networking_floatingips_pool" {
description = "The name of the pool from which to obtain the floating IP addresses"
type = "string"
}
variable "provider_auth_url" {
description = "The identity authentication URL"
type = "string"
}
variable "provider_password" {
description = "The password to login with"
type = "string"
}
variable "provider_region" {
description = "The cloud region to use"
type = "string"
}
variable "provider_tenant_name" {
description = "The name of the tenant to login with"
type = "string"
}
variable "provider_user_name" {
description = "The user ID to login with"
type = "string"
}
output.tf
and add each block of code into it.output "master_address" {
value = "${openstack_networking_floatingip_v2.master.address}"
}
output "workers_addresses" {
value = ["${openstack_networking_floatingip_v2.workers.*.address}"]
}
my-variables.tfvars
and add the block of code into it.compute_instances_network_name = "<VALUE>"
networking_floatingips_pool = "<VALUE>"
provider_auth_url = "<VALUE>"
provider_password = "<VALUE>"
provider_region = "<VALUE>"
provider_tenant_name = "<VALUE>"
provider_user_name = "<VALUE>"
test/integration/example/inspec.yml
---
name: default
test/integration/example/controls/nano_installed_spec.rb
# frozen_string_literal: true
control "nano_installed" do
describe package "nano" do
it do
is_expected.to be_installed
end
end
end
test/integration/example/controls/curl_installed_spec.rb
# frozen_string_literal: true
control "curl_installed" do
describe package "curl" do
it do
is_expected.to be_installed
end
end
end
# Create resources from the Terraform code in main.tf
bundle exec kitchen converge
# Run the Inspec controls from the .kitchen.yml verifier section
bundle exec kitchen verify