Merge pull request #263 from imnitin28/testcases-for-IaC-codes
This commit is contained in:
commit
0e28d1b8cc
12
2022/Days/IaC/Terratest/examples/instance.tf
Normal file
12
2022/Days/IaC/Terratest/examples/instance.tf
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
resource "aws_instance" "example" {
|
||||||
|
ami = var.AMIS[var.AWS_REGION]
|
||||||
|
instance_type = "t2.micro"
|
||||||
|
vpc_security_group_ids = [aws_security_group.instance.id]
|
||||||
|
|
||||||
|
# When the instance boots, start a web server on port 8080 that responds with "Hello, World!".
|
||||||
|
user_data = <<EOF
|
||||||
|
#!/bin/bash
|
||||||
|
echo "Hello, World!" > index.html
|
||||||
|
nohup busybox httpd -f -p 8080 &
|
||||||
|
EOF
|
||||||
|
}
|
4
2022/Days/IaC/Terratest/examples/output.tf
Normal file
4
2022/Days/IaC/Terratest/examples/output.tf
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# Output the instance's public IP address.
|
||||||
|
output "public_ip" {
|
||||||
|
value = aws_instance.example.public_ip
|
||||||
|
}
|
5
2022/Days/IaC/Terratest/examples/provider.tf
Normal file
5
2022/Days/IaC/Terratest/examples/provider.tf
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
provider "aws" {
|
||||||
|
access_key = var.AWS_ACCESS_KEY
|
||||||
|
secret_key = var.AWS_SECRET_KEY
|
||||||
|
region = var.AWS_REGION
|
||||||
|
}
|
9
2022/Days/IaC/Terratest/examples/securitygroup.tf
Normal file
9
2022/Days/IaC/Terratest/examples/securitygroup.tf
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# Allow the instance to receive requests on port 8080.
|
||||||
|
resource "aws_security_group" "instance" {
|
||||||
|
ingress {
|
||||||
|
from_port = 8080
|
||||||
|
to_port = 8080
|
||||||
|
protocol = "tcp"
|
||||||
|
cidr_blocks = ["0.0.0.0/0"]
|
||||||
|
}
|
||||||
|
}
|
3
2022/Days/IaC/Terratest/examples/terraform.tfvars
Normal file
3
2022/Days/IaC/Terratest/examples/terraform.tfvars
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
AWS_ACCESS_KEY = "XXXX"
|
||||||
|
AWS_SECRET_KEY = "XXXXXXXX"
|
||||||
|
AWS_REGION="ap-south-1"
|
16
2022/Days/IaC/Terratest/examples/vars.tf
Normal file
16
2022/Days/IaC/Terratest/examples/vars.tf
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
variable "AWS_ACCESS_KEY" {
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "AWS_SECRET_KEY" {
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "AWS_REGION" {
|
||||||
|
default = "ap-south-1"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "AMIS" {
|
||||||
|
type = map(string)
|
||||||
|
default = {
|
||||||
|
ap-south-1 = "ami-0860c9429baba6ad2"
|
||||||
|
}
|
||||||
|
}
|
3
2022/Days/IaC/Terratest/examples/versions.tf
Normal file
3
2022/Days/IaC/Terratest/examples/versions.tf
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
terraform {
|
||||||
|
required_version = ">= 0.12.26"
|
||||||
|
}
|
34
2022/Days/IaC/Terratest/test/terraform_test.go
Normal file
34
2022/Days/IaC/Terratest/test/terraform_test.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
http_helper "github.com/gruntwork-io/terratest/modules/http-helper"
|
||||||
|
|
||||||
|
"github.com/gruntwork-io/terratest/modules/terraform"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTerraformAwsHelloWorldExample(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
/*Construct the terraform options with default retryable errors to handle the most common retryable errors in terraform testing. */
|
||||||
|
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
|
||||||
|
/* The path to where our Terraform code is located */
|
||||||
|
TerraformDir: "../examples",
|
||||||
|
})
|
||||||
|
|
||||||
|
/* At the end of the test, run `terraform destroy` to clean up any resources that were created.*/
|
||||||
|
defer terraform.Destroy(t, terraformOptions)
|
||||||
|
|
||||||
|
/* Run `terraform init` and `terraform apply`. Fail the test if there are any errors. */
|
||||||
|
terraform.InitAndApply(t, terraformOptions)
|
||||||
|
|
||||||
|
/* Run `terraform output` to get the IP of the instance */
|
||||||
|
publicIp := terraform.Output(t, terraformOptions, "public_ip")
|
||||||
|
|
||||||
|
/* Make an HTTP request to the instance and make sure we get back a 200 OK with the body "Hello, World!" */
|
||||||
|
url := fmt.Sprintf("http://%s:8080", publicIp)
|
||||||
|
http_helper.HttpGetWithRetry(t, url, nil, 200, "Hello, World!", 30, 5*time.Second)
|
||||||
|
}
|
@ -56,9 +56,32 @@ Managed Cloud offering
|
|||||||
|
|
||||||
- [Terraform Sentinel](https://www.terraform.io/cloud-docs/sentinel) - embedded policy-as-code framework integrated with the HashiCorp Enterprise products. It enables fine-grained, logic-based policy decisions, and can be extended to use information from external sources.
|
- [Terraform Sentinel](https://www.terraform.io/cloud-docs/sentinel) - embedded policy-as-code framework integrated with the HashiCorp Enterprise products. It enables fine-grained, logic-based policy decisions, and can be extended to use information from external sources.
|
||||||
|
|
||||||
Automated testing
|
### Automated testing
|
||||||
|
|
||||||
- [Terratest](https://terratest.gruntwork.io/) - Terratest is a Go library that provides patterns and helper functions for testing infrastructure
|
- [Terratest](https://terratest.gruntwork.io/) - Terratest is a Go library that provides patterns and helper functions for testing infrastructure
|
||||||
|
- Terratest makes it easier to write automated tests for our infrastructure code. It provides a variety of helper functions and patterns for common infrastructure testing.
|
||||||
|
- find code at 2022/Days/IaC/Terratest
|
||||||
|
- To Run this application
|
||||||
|
* git clone #repo_url# <br />
|
||||||
|
* cd test <br />
|
||||||
|
* go mod init "<MODULE_NAME>" <br />
|
||||||
|
**MODULE_NAME would be github.com/<YOUR_USERNAME>/<YOUR_REPO_NAME>** <br />
|
||||||
|
* go mod init github.com/<FOLDER-PATH> <br/>
|
||||||
|
* go run
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
go mod init "<MODULE_NAME>" would create go.mod file into test folder. <br />
|
||||||
|
* The go.mod file is the root of dependency management in GoLang.
|
||||||
|
* All the modules which are needed or to be used in the project are maintained here in go.mod file.
|
||||||
|
* It creates entry for all the packages we are going to use/import in our project.
|
||||||
|
* It reduces effort for getting each dependencies manually.
|
||||||
|
|
||||||
|
On running **go test** for the first time you would get go.sum file created. <br />
|
||||||
|
* go.sum file is created when **go test** or **go build** is executed for the first time.
|
||||||
|
* It installs all the packages with specific version(latest)
|
||||||
|
* we do not need to edit or modify this file.
|
||||||
|
|
||||||
|
|
||||||
Worth a mention
|
Worth a mention
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user