Image for post
Image for post

Test Driven Infrastructure on GCP

Testing Google Cloud using InSpec

Many by now have heard of Test Driven Development, where tests are written first, and then the code. Some may even know Infrastructure As Code, where we can craft our entire infrastructure using code.

Installing The Tools

For this tutorial you will need to fetch the following tools:

cat <<-"EOF" > Brewfile
tap 'chef/chef'
cask 'inspec'
cask 'google-cloud-sdk'
brew 'terraform'
EOF
brew bundle --verbose
inspec detect -t gcp://== Platform DetailsName:      gcp
Families: cloud, api
Release: google-cloud-v

Creating the Tests

Now we need to create a small code structure for our tests for a future cluster we’ll call guestbook:

inspec init profile guestbook-profile
touch attributes.yml
mv guestbook-profile/default.rb \
guestbook-profile/cluster_test.rb
.
├── attributes.yml
└── guestbook-profile
├── README.md
├── controls
│ └── cluster_test.rb
├── inspec.lock
├── inspec.yml
└── libraries
cat <<-"EOF" > attributes.yml
project_name: $(gcloud config list --format 'value(core.project)')
cluster_region: us-east1
cluster_name: guestbook
EOF
cat <<-"EOF" > guestbook-profile/inspec.yml
name: guestbook-profile
title: InSpec Profile
maintainer: MuadDib
copyright: MuadDib
copyright_email: khaderach@spice.arrakis
license: MIT
summary: InSpec GCP Cluster Demo
version: 0.1.0
depends:
- name: gcp-resources
url: https://github.com/inspec/inspec-gcp/archive/master.tar.gz
supports:
- platform: gcp
EOF
cluster_test.rb
inspec exec guestbook-profile -t gcp:// --attrs attributes.yml
Profile: InSpec Profile (guestbook-profile)
Version: 0.1.0
Target: gcp://764086051850-6gn4p6qla6lp506wb8ikvt83di341hur.apps.googleusercontent.com
× gcp-1: Check Guestbook Cluster
× Cluster guestbook
The resource "projects/arrakis-dune" was not found.
Profile: Google Cloud Platform Resource Pack (inspec-gcp)
Version: 0.2.0
Target: gcp://764086051850-6gn4p6qla6lp506wb8ikvt83di341hur.apps.googleusercontent.com
No tests executed.Profile Summary: 0 successful controls, 1 control failure, 0 controls skipped
Test Summary: 0 successful, 1 failure, 0 skipped

Creating the Cluster

Now let’s create a cluster using Terraform. We can do this with terraform. Create a file called cluster.tf with this content:

cluster.tf
export TF_VAR_project="$(gcloud config list\
--format 'value(core.project)'
)"
export TF_VAR_region="us-east1"
export TF_VAR_user="admin"
export TF_VAR_password="m8XBWrg2zt8R8JoH"
terraform init
terraform plan  # inspect changes that will be made
terraform apply # apply these changes to create cloud resources

Running the tests

Now we can run our tests:

inspec exec guestbook-profile -t gcp:// --attrs attributes.yml
Profile: InSpec Profile (guestbook-profile)
Version: 0.1.0
Target: gcp://764086051850-6gn4p6qla6lp506wb8ikvt83di341hur.apps.googleusercontent.com
✔ gcp-1: Check Guestbook Cluster
✔ Cluster guestbook should exist
✔ Cluster guestbook name should eq "guestbook"
✔ Cluster guestbook status should eq "RUNNING"
✔ Cluster guestbook master_auth.username should eq "admin"
✔ Cluster guestbook network should eq "default"
✔ Cluster guestbook subnetwork should eq "default"
✔ Cluster guestbook initial_node_count should eq 3
Profile: Google Cloud Platform Resource Pack (inspec-gcp)
Version: 0.2.0
Target: gcp://764086051850-6gn4p6qla6lp506wb8ikvt83di341hur.apps.googleusercontent.com
No tests executed.Profile Summary: 1 successful control, 0 control failures, 0 controls skipped
Test Summary: 7 successful, 0 failures, 0 skipped

References

Google Resouce Pack

Source code and Blog.

Test Driven Infrastructure Articles

Early articles during Iron Age. Recent current articles for Cloud Age yet to be created.

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store