-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_impl.go
83 lines (70 loc) · 2.57 KB
/
test_impl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package common
import (
"context"
"os"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
armNetwork "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v5"
"github.com/gruntwork-io/terratest/modules/terraform"
"github.com/launchbynttdata/lcaf-component-terratest/types"
"github.com/stretchr/testify/assert"
)
func TestNsgSubnetAssociation(t *testing.T, ctx types.TestContext) {
subscriptionID := os.Getenv("ARM_SUBSCRIPTION_ID")
if len(subscriptionID) == 0 {
t.Fatal("ARM_SUBSCRIPTION_ID is not set in the environment variables ")
}
credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
t.Fatalf("Unable to get credentials: %e\n", err)
}
options := arm.ClientOptions{
ClientOptions: azcore.ClientOptions{
Cloud: cloud.AzurePublic,
},
}
// Create network security group client
nsgClient, err := armNetwork.NewSecurityGroupsClient(subscriptionID, credential, &options)
if err != nil {
t.Fatalf("Error getting NSG client: %v", err)
}
subnetsClient, err := armNetwork.NewSubnetsClient(subscriptionID, credential, &options)
if err != nil {
t.Fatalf("Error getting subnets client: %v", err)
}
t.Run("IsNsgSubnetAssociated", func(t *testing.T) {
resourceGroupName := terraform.Output(t, ctx.TerratestTerraformOptions(), "resource_group_name")
nsgName := terraform.Output(t, ctx.TerratestTerraformOptions(), "name")
vnetNames := terraform.OutputMap(t, ctx.TerratestTerraformOptions(), "vnet_names")
subnetNames := terraform.OutputMap(t, ctx.TerratestTerraformOptions(), "vnet_subnets")
nsg, err := nsgClient.Get(context.Background(), resourceGroupName, nsgName, nil)
if err != nil {
t.Fatalf("Error getting nsg: %v", err)
}
if nsg.Name == nil {
t.Fatalf("nsg does not exist")
}
for _, vnetName := range vnetNames {
for _, subnetName := range subnetNames {
inputSubnetName := strings.Trim(getSubstring(subnetName), "[]")
subnet, err := subnetsClient.Get(context.Background(), resourceGroupName, vnetName, inputSubnetName, nil)
if err != nil {
t.Fatalf("Error getting subnet: %v", err)
}
if subnet.Name == nil {
t.Fatalf("Subnet does not exist")
}
subnetNsg := subnet.Properties.NetworkSecurityGroup
assert.NotEmpty(t, subnetNsg, "Subnet does not have a nsg associated.")
}
}
})
}
func getSubstring(input string) string {
parts := strings.Split(input, "/")
return parts[len(parts)-1]
}