In my last two articles, I've talked conceptually and theoretically about the need for DevOps testers.
In this article, I will provide practical examples of unit testing.
Since public cloud storage seems to be a common problem, I will begin with an example unit test for a terraform project which creates a simple S3 bucket.
First, we need to install localstack, so we can test AWS locally.
In a new console/terminal and new directory, create a simple terraform project. The provider.tf file should point to the localstack ports.
Deploy the terraform project.
Create a test.py file with the following code to test the deployment of the S3 bucket.
Test that the bucket was created.
Now, let’s destroy the S3 bucket.
Next, we will install the terraform-compliance python module.
Next, we will set up the directory for our test.
Next, make a file named s3.features inside the features directory with the following content.
Now, we will return to the root directory for the project and run a terraform plan to get the plans output in json format.
Lastly, we will test the terraform project against the feature file to see if the project is compliant.
As you will notice from the results, all tests passed because the S3 bucket deployed is private.
While these are just basic examples, they are intended to demonstrate the concept of unit testing infrastructure-as-code, and testing for various rules.