Skip to content

Commit

Permalink
Merge pull request #1 from helayoty/dev/service-bus-terratest
Browse files Browse the repository at this point in the history
Add tests and terraform module for service bus
  • Loading branch information
helayoty authored Jan 27, 2021
2 parents 1cd9381 + cd10ea7 commit 230f411
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 1 deletion.
1 change: 0 additions & 1 deletion examples/azure/terraform-azure-servicebus-example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ resource "azurerm_resource_group" "seervicebus" {
location = var.location
}


# ---------------------------------------------------------------------------------------------------------------------
# DDefine locals variables
# ---------------------------------------------------------------------------------------------------------------------
Expand Down
79 changes: 79 additions & 0 deletions modules/azure/servicebus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// +build azure

// NOTE: We use build tags to differentiate azure testing because we currently do not have azure access setup for
// CircleCI.
package azure

import (
"testing"

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

/*
The below tests are currently stubbed out, with the expectation that they will throw errors. These tests can be extended.
*/
func TestListServiceBusNamespaceNamesE(t *testing.T) {
t.Parallel()

subscriptionID := ""

_, err := ListServiceBusNamespaceNamesE(subscriptionID)
require.Error(t, err)
}

func TestListServiceBusNamespaceIDsByResourceGroupE(t *testing.T) {
t.Parallel()

subscriptionID := ""
resourceGroup := ""

_, err := ListServiceBusNamespaceIDsByResourceGroupE(subscriptionID, resourceGroup)
require.Error(t, err)
}

func TestListNamespaceAuthRulesE(t *testing.T) {
t.Parallel()

subscriptionID := ""
namespace := ""
resourceGroup := ""

_, err := ListNamespaceAuthRulesE(subscriptionID, namespace, resourceGroup)
require.Error(t, err)
}

func TestListNamespaceTopicsE(t *testing.T) {
t.Parallel()

subscriptionID := ""
namespace := ""
resourceGroup := ""

_, err := ListNamespaceTopicsE(subscriptionID, namespace, resourceGroup)
require.Error(t, err)
}

func TestListTopicAuthRulesE(t *testing.T) {
t.Parallel()

subscriptionID := ""
namespace := ""
resourceGroup := ""
topicName := ""

_, err := ListTopicAuthRulesE(subscriptionID, namespace, resourceGroup, topicName)
require.Error(t, err)
}

func TestListTopicSubscriptionsNameE(t *testing.T) {
t.Parallel()

subscriptionID := ""
namespace := ""
resourceGroup := ""
topicName := ""

_, err := ListTopicSubscriptionsNameE(subscriptionID, namespace, resourceGroup, topicName)
require.Error(t, err)
}
71 changes: 71 additions & 0 deletions test/azure/terraform_azure_servicebus_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// +build azure

// NOTE: We use build tags to differentiate azure testing because we currently do not have azure access setup for
// CircleCI.

package test

import (
"strings"
"testing"
"os"

"github.com/gruntwork-io/terratest/modules/azure"
"github.com/gruntwork-io/terratest/modules/random"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/stretchr/testify/assert"
)

func TestTerraformAzureServiceBusExample(t *testing.T) {
t.Parallel()

uniquePostfix := strings.ToLower(random.UniqueId())

// website::tag::1:: Configure Terraform setting up a path to Terraform code.
terraformOptions := &terraform.Options{
// The path to where our Terraform code is located
TerraformDir: "../../examples/azure/terraform-azure-servicebus-example",
Vars: map[string]interface{}{
"postfix": uniquePostfix,
},
}

// website::tag::4:: At the end of the test, run `terraform destroy` to clean up any resources that were created
defer terraform.Destroy(t, terraformOptions)

// website::tag::2:: Run `terraform init` and `terraform apply`. Fail the test if there are any errors.
terraform.InitAndApply(t, terraformOptions)

// website::tag::3:: Run `terraform output` to get the values of output variables
expectedTopicSubscriptionsMap := terraform.OutputMapOfObjects(t, terraformOptions, "topics")
expectedNamespaceName := terraform.Output(t, terraformOptions, "namespace_name")
expectedResourceGroup := terraform.Output(t, terraformOptions, "resource_group")

for topicName, topicsMap := range expectedTopicSubscriptionsMap {
subscriptionNamesFromAzure, err := azure.ListTopicSubscriptionsNameE(
os.Getenv("ARM_SUBSCRIPTION_ID"),
expectedNamespaceName,
expectedResourceGroup,
topicName)

if err != nil {
t.Fatal(err)
}

subscriptionsMap := topicsMap.(map[string]interface{})["subscriptions"].(map[string]interface{})
subscriptionNamesFromOutput := getMapKeylist(subscriptionsMap)
// each subscription from the output should also exist in Azure
assert.Equal(t, len(*subscriptionNamesFromOutput), len(*subscriptionNamesFromAzure))
for _, subscrptionName := range *subscriptionNamesFromOutput {
assert.Contains(t, *subscriptionNamesFromAzure, subscrptionName)
}
}
}

func getMapKeylist(mapList map[string]interface{}) *[]string {
names := make([]string, 0)
for key := range mapList {
names = append(names, key)
}
return &names
}

0 comments on commit 230f411

Please sign in to comment.