Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
helayoty committed Jan 26, 2021
2 parents 713ff9d + d000797 commit b379da6
Show file tree
Hide file tree
Showing 17 changed files with 279 additions and 147 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![CircleCI](https://circleci.com/gh/gruntwork-io/terratest.svg?style=svg&circle-token=e48019e09fc3b8bf6e0315a84048501c87c4157c)](https://circleci.com/gh/gruntwork-io/terratest)
[![Go Report Card](https://goreportcard.com/badge/github.com/gruntwork-io/terratest)](https://goreportcard.com/report/github.com/gruntwork-io/terratest)
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/mod/github.com/gruntwork-io/terratest?tab=overview)
![go.mod version](https://img.shields.io/github/go-mod/go-version/gruntwork-io/terratest)


Terratest is a Go library that makes it easier to write automated tests for your infrastructure code. It provides a
Expand Down
16 changes: 8 additions & 8 deletions examples/azure/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Terratest Configuration and Setup

Terratest uses Go to make calls to Azure through the azure-sdk-for-go library and independently confirm the actual Azure resource property matches the expected state provided by Terraform output variables.
Terratest uses Go to make calls to Azure through the azure-sdk-for-go library and independently confirm the actual Azure resource property matches the expected state provided by Terraform output variables.

* Instructions for running each Azure Terratest module are included in each Terraform example sub-folder:
* examples/azure/terraform-azure-*-example/README.md
* Tests wich assert against expected Terraform output values are located in the the respective go files of the folder:
* [test/azure/terraform-azure-*-example_test.go](../../test/azure)
* Test APIs which provide the actual Azure resource property values via the azure-sdk-for-go are located in the folder:
* [modules/azure](../../modules/azure)
- Instructions for running each Azure Terratest module are included in each Terraform example sub-folder:
- examples/azure/terraform-azure-\*-example/README.md
- Tests which assert against expected Terraform output values are located in the the respective go files of the folder:
- [test/azure/terraform-azure-\*-example_test.go](../../test/azure)
- Test APIs which provide the actual Azure resource property values via the azure-sdk-for-go are located in the folder:
- [modules/azure](../../modules/azure)

## Go Dependencies

Expand Down Expand Up @@ -44,7 +44,7 @@ export ARM_SUBSCRIPTION_ID=your_subscription_id
export ARM_TENANT_ID=your_tenant_id
```

Note, in a Windows environment, these should be set as **system environment variables**. We can use a PowerShell console with administrative rights to update these environment variables:
Note, in a Windows environment, these should be set as **system environment variables**. We can use a PowerShell console with administrative rights to update these environment variables:

```powershell
[System.Environment]::SetEnvironmentVariable("ARM_CLIENT_ID",$your_app_id,[System.EnvironmentVariableTarget]::Machine)
Expand Down
10 changes: 3 additions & 7 deletions examples/azure/terraform-azure-resourcegroup-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ how you can use TerraTest to write automated tests for your Azure Terraform code

- A [Resource Group](https://docs.microsoft.com/azure/azure-resource-manager/management/overview) with no other resources.


* A [Resource Group](https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/overview) that gives the module the following:
* [Resource Group](https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/overview) with the value specified in the `resource_group_name` output variable.

Check out [test/azure/terraform_azure_@module@_test.go](/test/azure/terraform_azure_@module@_test.go) to see how you can write
Check out [test/azure/terraform_azure_resourcegroup_example_test.go](/test/azure/terraform_azure_resourcegroup_example_test.go) to see how you can write
automated tests for this module.

Note that the Resource Group this module creates does not actually do anything; it just runs the resources for
Expand All @@ -21,7 +17,7 @@ it should be free, but you are completely responsible for all Azure charges.

## Running this module manually

1. Sign up for [Azure](https://azure.microsoft.com/).
1. Sign up for [Azure](https://azure.microsoft.com/)
1. Configure your Azure credentials using one of the [supported methods for Azure CLI
tools](https://docs.microsoft.com/cli/azure/azure-cli-configuration?view=azure-cli-latest)
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH`
Expand All @@ -32,7 +28,7 @@ it should be free, but you are completely responsible for all Azure charges.

## Running automated tests against this module

1. Sign up for [Azure](https://azure.microsoft.com/).
1. Sign up for [Azure](https://azure.microsoft.com/)
1. Configure your Azure credentials using one of the [supported methods for Azure CLI
tools](https://docs.microsoft.com/cli/azure/azure-cli-configuration?view=azure-cli-latest)
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# ---------------------------------------------------------------------------------------------------------------------
# ENVIRONMENT VARIABLES
# Define these secrets as environment variables
# ---------------------------------------------------------------------------------------------------------------------

# ARM_CLIENT_ID
# ARM_CLIENT_SECRET
# ARM_SUBSCRIPTION_ID
# ARM_TENANT_ID

# ---------------------------------------------------------------------------------------------------------------------
# REQUIRED PARAMETERS
# You must provide a value for each of these parameters.
# ---------------------------------------------------------------------------------------------------------------------

# ---------------------------------------------------------------------------------------------------------------------
# OPTIONAL PARAMETERS
# These parameters have reasonable defaults.
# ---------------------------------------------------------------------------------------------------------------------

variable "location" {
description = "The location to set for the resource group."
type = string
Expand All @@ -7,5 +27,8 @@ variable "location" {
variable "postfix" {
description = "A postfix string to centrally mitigate resource name collisions."
type = string
default = "terratest-resource-group"
default = "resource"
}



1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/vdemeester/k8s-pkg-credentialprovider v0.0.0-20200107171650-7c61ffa44238/go.mod h1:JwQJCMWpUDqjZrB5jpw0f5VbN7U95zxFy1ZDpoEarGo=
github.com/vmware/govmomi v0.20.3/go.mod h1:URlwyTFZX72RmxtxuaFL2Uj3fD1JTvZdx59bHWk6aFU=
Expand Down
67 changes: 44 additions & 23 deletions modules/azure/availabilityset.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,72 @@ import (
"strings"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
"github.com/gruntwork-io/terratest/modules/collections"
"github.com/gruntwork-io/terratest/modules/testing"
"github.com/stretchr/testify/require"
)

// AvailabilitySetExists indicates whether the speficied Azure Availability Set exists
// AvailabilitySetExists indicates whether the specified Azure Availability Set exists.
// This function would fail the test if there is an error.
func AvailabilitySetExists(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) bool {
exists, err := AvailabilitySetExistsE(t, avsName, resGroupName, subscriptionID)
require.NoError(t, err)
return exists
}

// AvailabilitySetExistsE indicates whether the speficied Azure Availability Set exists
// AvailabilitySetExistsE indicates whether the specified Azure Availability Set exists
func AvailabilitySetExistsE(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) (bool, error) {
_, err := GetAvailabilitySetE(t, avsName, resGroupName, subscriptionID)
if err != nil {
if strings.Contains(err.Error(), "ResourceNotFound") {
if ResourceNotFoundErrorExists(err) {
return false, nil
}
return false, err
}
return true, nil
}

// CheckAvailabilitySetContainsVM checks if the Virtual Machine is contained in the Availability Set VMs
// CheckAvailabilitySetContainsVM checks if the Virtual Machine is contained in the Availability Set VMs.
// This function would fail the test if there is an error.
func CheckAvailabilitySetContainsVM(t testing.TestingT, vmName string, avsName string, resGroupName string, subscriptionID string) bool {
avsVMs, err := GetAvailabilitySetVMsE(t, avsName, resGroupName, subscriptionID)
success, err := CheckAvailabilitySetContainsVME(t, vmName, avsName, resGroupName, subscriptionID)
require.NoError(t, err)
return success
}

// CheckAvailabilitySetContainsVME checks if the Virtual Machine is contained in the Availability Set VMs
func CheckAvailabilitySetContainsVME(t testing.TestingT, vmName string, avsName string, resGroupName string, subscriptionID string) (bool, error) {
client, err := GetAvailabilitySetClientE(subscriptionID)
if err != nil {
return false, err
}

// Get the Availability Set
avs, err := client.Get(context.Background(), resGroupName, avsName)
if err != nil {
return false
return false, err
}

// Check if the VM is found in the AVS VM collection and return true
for _, vm := range *avs.VirtualMachines {
// VM IDs are always ALL CAPS in this property so ignoring case
if strings.EqualFold(vmName, GetNameFromResourceID(*vm.ID)) {
return true, nil
}
}

return collections.ListContains(avsVMs, strings.ToLower(vmName))
return false, NewNotFoundError("Virtual Machine", vmName, avsName)
}

// GetAvailabilitySetVMs gets a list of VM names in the specified Azure Availability Set
func GetAvailabilitySetVMs(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) []string {
vms, err := GetAvailabilitySetVMsE(t, avsName, resGroupName, subscriptionID)
// GetAvailabilitySetVMNamesInCaps gets a list of VM names in the specified Azure Availability Set.
// This function would fail the test if there is an error.
func GetAvailabilitySetVMNamesInCaps(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) []string {
vms, err := GetAvailabilitySetVMNamesInCapsE(t, avsName, resGroupName, subscriptionID)
require.NoError(t, err)
return vms
}

// GetAvailabilitySetVMsE gets a list of VM names in the specified Azure Availability Set
func GetAvailabilitySetVMsE(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) ([]string, error) {
// GetAvailabilitySetVMNamesInCapsE gets a list of VM names in the specified Azure Availability Set
func GetAvailabilitySetVMNamesInCapsE(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) ([]string, error) {
client, err := GetAvailabilitySetClientE(subscriptionID)
if err != nil {
return nil, err
Expand All @@ -60,33 +83,31 @@ func GetAvailabilitySetVMsE(t testing.TestingT, avsName string, resGroupName str

vms := []string{}

// Get the names for all VMs in the Availability Set
for _, vm := range *avs.VirtualMachines {
vms = append(vms, strings.ToLower(GetNameFromResourceID(*vm.ID)))
// IDs are returned in ALL CAPS for this property
if vmName := GetNameFromResourceID(*vm.ID); len(vmName) > 0 {
vms = append(vms, vmName)
}
}

return vms, nil
}

// GetAvailabilitySetFaultDomainCount gets the Fault Domain Count for the specified Azure Availability Set
// GetAvailabilitySetFaultDomainCount gets the Fault Domain Count for the specified Azure Availability Set.
// This function would fail the test if there is an error.
func GetAvailabilitySetFaultDomainCount(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) int32 {
avsFaultDomainCount, err := GetAvailabilitySetFaultDomainCountE(t, avsName, resGroupName, subscriptionID)
require.NoError(t, err)

return avsFaultDomainCount
}

// GetAvailabilitySetFaultDomainCountE gets the Fault Domain Count for the specified Azure Availability Set
func GetAvailabilitySetFaultDomainCountE(t testing.TestingT, avsName string, resGroupName string, subscriptionID string) (int32, error) {
client, err := GetAvailabilitySetClientE(subscriptionID)
avs, err := GetAvailabilitySetE(t, avsName, resGroupName, subscriptionID)
if err != nil {
return -1, err
}

avs, err := client.Get(context.Background(), resGroupName, avsName)
if err != nil {
return -1, err
}

return *avs.PlatformFaultDomainCount, nil
}

Expand Down
16 changes: 16 additions & 0 deletions modules/azure/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,19 @@ func getTargetAzureResourceGroupName(resourceGroupName string) (string, error) {

return resourceGroupName, nil
}

// safePtrToString converts a string pointer to a non-pointer string value, or to "" if the pointer is nil.
func safePtrToString(raw *string) string {
if raw == nil {
return ""
}
return *raw
}

// safePtrToInt32 converts a int32 pointer to a non-pointer int32 value, or to 0 if the pointer is nil.
func safePtrToInt32(raw *int32) int32 {
if raw == nil {
return 0
}
return *raw
}
1 change: 1 addition & 0 deletions modules/azure/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down
Loading

0 comments on commit b379da6

Please sign in to comment.