Add Sample: Integration Testing (#43)

add samples for integration testing
This commit is contained in:
Julien Corioland
2020-06-11 10:35:53 +02:00
committed by GitHub
parent 7d4ee2d346
commit 3328af0982
14 changed files with 211 additions and 0 deletions

View File

@ -0,0 +1,56 @@
# Azure Pipeline that run basic continuous integration on a Terraform project
# This makes sure the pipeline is triggered every time code is pushed in the validation-testing example source, on all branches.
trigger:
branches:
include:
- '*'
paths:
include:
- 'examples/validation-testing/src/*'
variables:
# There must be an Azure Service Connection with that name defined in your Azure DevOps settings. See https://docs.microsoft.com/en-us/azure/devops/pipelines/library/connect-to-azure?view=azure-devops
serviceConnection: 'terraform-basic-testing-azure-connection'
azureLocation: 'westeurope'
# Terraform settings
terraformWorkingDirectory: '$(System.DefaultWorkingDirectory)/examples/basic-testing/src'
terraformVersion: '0.12.25'
stages:
- stage: TerraformContinuousIntegration
displayName: Terraform Module - CI
jobs:
- job: TerraformContinuousIntegrationJob
displayName: TerraformContinuousIntegration - CI Job
pool:
vmImage: ubuntu-20.04
steps:
# Step 1: run the Checkov Static Code Analysis
- bash: $(terraformWorkingDirectory)/checkov.sh $(terraformWorkingDirectory)
displayName: Checkov Static Code Analysis
# Step 2: install Terraform on the Azure Pipelines agent
- task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-installer.TerraformInstaller@0
displayName: 'Install Terraform'
inputs:
terraformVersion: $(terraformVersion)
# Step 3: run Terraform init to initialize the workspace
- task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
displayName: 'Run terraform init'
inputs:
command: init
workingDirectory: $(terraformWorkingDirectory)
# Step 4: run Terraform validate to validate HCL syntax
- task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
displayName: 'Run terraform validate'
inputs:
command: validate
workingDirectory: $(terraformWorkingDirectory)
# Step 5: run Terraform plan to validate HCL syntax
- task: charleszipp.azure-pipelines-tasks-terraform.azure-pipelines-tasks-terraform-cli.TerraformCLI@0
displayName: 'Run terraform plan'
inputs:
command: plan
workingDirectory: $(terraformWorkingDirectory)
environmentServiceName: $(serviceConnection)
commandOptions: -var location=$(azureLocation)

View File

@ -0,0 +1,57 @@
#!/bin/bash
#title :run_checkov.sh
#description :Runs the Checkov static analysis tool on all subdirectories of the target.
#author :andreas.heuamier@microsoft.com
#date :20200510
#version :0.1
#usage :./checkov.sh {WORKk_DIR}
#bash_version :5.0.16(1)-release
#
set -eo pipefail
# The target directory for scanning.
WORK_DIR=${1-$(pwd)}
#######################################
# run_checkov() docker command
# Arguments:
# test_dir folder
# Outputs:
# Writes test command outputs to stdout
# Exits on $? != 0
#######################################
run_checkov() {
local test_dir=$1
docker run -t -v "${test_dir}":/tf bridgecrew/checkov:release-1.0.235 -d /tf
}
#######################################
# find_folders_by() file pattern
# Globals:
# WORK_DIR -path
# Arguments:
# pattern - regex
# Outputs:
# Writes folders list to stdout
#######################################
find_folders_by() {
local pattern=${1:-"main.tf"}
find "${WORK_DIR}" -type f -name "${pattern}" -printf '%h\n' | sort -u
}
#######################################
# Runs the Checkov static analysis tool on all subdirectories
#######################################
run_main() {
for folder in $(find_folders_by "main.tf"); do
run_checkov "${folder}" &
done
wait
}
#######################################
# Be able to run this one either as standalone or import as lib
#######################################
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
run_main
fi

View File

@ -0,0 +1,6 @@
resource "random_uuid" "uuid" {}
resource "azurerm_resource_group" "rg" {
name = "rg-hello-tf-${random_uuid.uuid.result}"
location = var.location
}

View File

@ -0,0 +1,3 @@
output "resource_group_name" {
value = azurerm_resource_group.rg.name
}

View File

@ -0,0 +1,3 @@
provider "azurerm" {
features {}
}

View File

@ -0,0 +1,5 @@
variable location {
type = string
description = "The Azure location where the resources will be created"
default = "westeurope"
}