Add unit tests under tests directory for backend

This commit is contained in:
manojvazirani 2019-03-27 15:34:56 -04:00
Родитель d0cc919ad3
Коммит a3c370c65e
6 изменённых файлов: 198 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,36 @@
# Backend Dev Testing
## Description
- main.sh is the parent file that contains all the tests
- assert.sh is the code used for the base functions needed to test the backend.
## Requirements:
Fill the values required below in test_backend.tfvars
- Storage Account Name
- Storage Account Access Key
- Storage Account Container Name
- Key Name to store for Terraform State for Test Environment
#### Folder test1 contains test1.tf
1. Deploy Resource Group
2. Deploy KeyVault1
- Should deploy above resources
#### Folder test2 contains test2.tf
1. Deploy Resource Group
2. Deploy KeyVault1
3. Deploy KeyVault2
- Should not change/deploy resource 1 and 2. Only deploy resource 3.
#### Folder test3 contains test3.tf
1. Deploy Resource Group
2. Deploy KeyVault1
3. Deploy KeyVault2
- Should not deploy any resource since above 3 resources are already deployed.
## Usage
``` bash
./main.sh
```

50
tests/backend_test/main.sh Executable file
Просмотреть файл

@ -0,0 +1,50 @@
#!/bin/bash
set -euo pipefail
. ../assert.sh continue_on_error # load test functions with continue_on_error/stop_on_error
#Cleanup
terraform init -backend-config=./test_backend.tfvars test3
terraform destroy -auto-approve test3
#Check if resource group exists
#Resource group does not exists #Fail
printf "\e[0;31mIf below test 'Failed' then test passed \n"
assertRG rg-test-cblt-msft
#Create Resource group and Keyvault 1
terraform init -backend-config=./test_backend.tfvars test1
terraform apply -auto-approve test1
#Check if resource group exists
assertRG rg-test-cblt-msft
assertResource rg-test-cblt-msft "Microsoft.KeyVault/vaults" kv1-test-cblt-msft
#Key Vault 2 does not exists #Fail
printf "\e[0;31mIf below test 'Failed' then test passed \n"
assertResource rg-test-cblt-msft "Microsoft.KeyVault/vaults" kv2-test-cblt-msft
terraform init -backend-config=./test_backend.tfvars test2
terraform apply -auto-approve test2
assertRG rg-test-cblt-msft
assertResource rg-test-cblt-msft "Microsoft.KeyVault/vaults" kv1-test-cblt-msft
assertResource rg-test-cblt-msft "Microsoft.KeyVault/vaults" kv2-test-cblt-msft
before=`az resource list -g rg-test-cblt-msft --resource-type "Microsoft.KeyVault/vaults"|jq .[].name| wc -l`
terraform init -backend-config=./test_backend.tfvars test3
terraform apply -auto-approve test3
after=`az resource list -g rg-test-cblt-msft --resource-type "Microsoft.KeyVault/vaults"|jq .[].name| wc -l`
assertRG rg-test-cblt-msft
assertResource rg-test-cblt-msft "Microsoft.KeyVault/vaults" kv1-test-cblt-msft
assertResource rg-test-cblt-msft "Microsoft.KeyVault/vaults" kv2-test-cblt-msft
if [ $before -eq $after ]
then
printf "\e[0;32mTest Passed: Number of resources in resource group are the same before and after the current test \n"
else
printf "\e[0;31mTest Failed... \n"
fi
terraform destroy -auto-approve test3

Просмотреть файл

@ -0,0 +1,28 @@
provider "azurerm" {
version = "~>1.21.0"
}
terraform {
required_version = "~> 0.11.11"
backend "azurerm" {
}
}
resource "azurerm_resource_group" "rg_core" {
name = "rg-test-cblt-msft"
location = "eastus"
}
data "azurerm_client_config" "current" {}
resource "azurerm_key_vault" "keyvault1" {
name = "kv1-test-cblt-msft"
location = "eastus"
resource_group_name = "rg-test-cblt-msft"
tenant_id = "${data.azurerm_client_config.current.tenant_id}"
depends_on = ["azurerm_resource_group.rg_core"]
sku {
name = "standard"
}
}

Просмотреть файл

@ -0,0 +1,40 @@
provider "azurerm" {
version = "~>1.21.0"
}
terraform {
required_version = "~> 0.11.11"
backend "azurerm" {
}
}
resource "azurerm_resource_group" "rg_core" {
name = "rg-test-cblt-msft"
location = "eastus"
}
data "azurerm_client_config" "current" {}
resource "azurerm_key_vault" "keyvault1" {
name = "kv1-test-cblt-msft"
location = "eastus"
resource_group_name = "rg-test-cblt-msft"
tenant_id = "${data.azurerm_client_config.current.tenant_id}"
depends_on = ["azurerm_resource_group.rg_core"]
sku {
name = "standard"
}
}
resource "azurerm_key_vault" "keyvault2" {
name = "kv2-test-cblt-msft"
location = "eastus"
resource_group_name = "rg-test-cblt-msft"
tenant_id = "${data.azurerm_client_config.current.tenant_id}"
depends_on = ["azurerm_resource_group.rg_core"]
sku {
name = "standard"
}
}

Просмотреть файл

@ -0,0 +1,40 @@
provider "azurerm" {
version = "~>1.21.0"
}
terraform {
required_version = "~> 0.11.11"
backend "azurerm" {
}
}
resource "azurerm_resource_group" "rg_core" {
name = "rg-test-cblt-msft"
location = "eastus"
}
data "azurerm_client_config" "current" {}
resource "azurerm_key_vault" "keyvault1" {
name = "kv1-test-cblt-msft"
location = "eastus"
resource_group_name = "rg-test-cblt-msft"
tenant_id = "${data.azurerm_client_config.current.tenant_id}"
depends_on = ["azurerm_resource_group.rg_core"]
sku {
name = "standard"
}
}
resource "azurerm_key_vault" "keyvault2" {
name = "kv2-test-cblt-msft"
location = "eastus"
resource_group_name = "rg-test-cblt-msft"
tenant_id = "${data.azurerm_client_config.current.tenant_id}"
depends_on = ["azurerm_resource_group.rg_core"]
sku {
name = "standard"
}
}

Просмотреть файл

@ -0,0 +1,4 @@
storage_account_name="storage-account-name"
access_key="storage-account-access-key"
container_name="storage-account-container"
key="test-tfstate-key"