With the changes to S3 a few months ago, I decided that it was time to update the S3 module we use at my current employer. Since I did not write the original module, and the changes were pretty significant, I decided to add some tests to the module using Terratest. This way, I could test the changes as I refactored. As I started writing the tests I ran in to an interesting problems.

Within the S3 resources and options, there are a couple that are exclusive. The one that I came across while I was testing was a conflict between object_lock_enabled parameter in the aws_s3_bucket resource, and the aws_s3_bucket_versioning resource. When you enable the object lock on a bucket, it automatically turns on versioning. Since I setup my S3 bucket first, I had to be able to test enabling the object lock and wrote a test for it. When it came time to test versioning, I had to disable the object lock and comment out the test.

I started to wonder if there was a way that I could use the variables in a TFVars file to conditionally test my resources so that I could quickly changes different configurations without having to comment/uncomment tests in my terratest file. It took a little digging around, but it turns out to be rather simple. If I wanted to pull a variable (in this case object_lock_enabled) out of the TFVars file, I could add use the `GetVariavleAsStringFromVarFile function from the terratest Terraform module:

ObjectLockEnabled := terraform.GetVariableAsStringFromVarFile(t, "../examples/terratest/terratest.tfvars", "object_lock_enabled")

Then you can test with a conditional:

if ObjectLockEnabled == "true" {
		assert.Equal(t, s3ObjectLockConfigurationId, s3Id)
}

Now I can change whatever variable I want in the TFVars file without also having to go and comment out tests in the go file.