Skip to content

Commit 7c436c7

Browse files
authored
feat: add unit testing (#40)
1 parent a4540cb commit 7c436c7

File tree

6 files changed

+241
-4
lines changed

6 files changed

+241
-4
lines changed

.github/workflows/unit.yml

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
name: unit test
3+
4+
on:
5+
pull_request:
6+
types: ['opened', 'reopened', 'synchronize']
7+
merge_group:
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: read
12+
id-token: write
13+
14+
jobs:
15+
testunit:
16+
runs-on: [ self-hosted, 1ES.Pool=terraform-azurerm-avm-ptn-vnetgateway ]
17+
env:
18+
TF_IN_AUTOMATION: 1
19+
TF_VAR_enable_telemetry: false
20+
steps:
21+
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 #v4.1.2
22+
- name: Run Unit Tests
23+
shell: bash
24+
run: |
25+
set -e
26+
az login --identity --username $MSI_ID > /dev/null
27+
export ARM_SUBSCRIPTION_ID=$(az login --identity --username $MSI_ID | jq -r '.[0] | .id')
28+
export ARM_TENANT_ID=$(az login --identity --username $MSI_ID | jq -r '.[0] | .tenantId')
29+
export ARM_CLIENT_ID=$(az identity list | jq -r --arg MSI_ID "$MSI_ID" '.[] | select(.principalId == $MSI_ID) | .clientId')
30+
docker run --rm \
31+
-v $(pwd):/src \
32+
-v /var/run/docker.sock:/var/run/docker.sock \
33+
-w /src \
34+
--network=host \
35+
-e TF_IN_AUTOMATION \
36+
-e TF_VAR_enable_telemetry \
37+
-e MSI_ID \
38+
-e ARM_SUBSCRIPTION_ID \
39+
-e ARM_TENANT_ID \
40+
-e ARM_CLIENT_ID \
41+
-e ARM_USE_MSI=true \
42+
mcr.microsoft.com/azterraform:latest \
43+
sh -c "terraform init && terraform test -verbose"
44+
45+

.gitignore

-4
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,3 @@ terraform.rc
8686
avm.tflint.merged.hcl
8787
avm.tflint_example.merged.hcl
8888
*.md.tmp
89-
90-
# ignore tftest whilst developing
91-
**.tftest.*
92-
**/tests/vnet-with-subnet/**

tests/.gitkeep

Whitespace-only changes.

tests/examples.tftest.hcl

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
provider "azurerm" {
2+
features {}
3+
}
4+
5+
run "examples_default" {
6+
command = plan
7+
8+
module {
9+
source = "./examples/default"
10+
}
11+
}
12+
13+
run "examples_vnet_with_subnet" {
14+
command = plan
15+
16+
module {
17+
source = "./examples/vnet-with-subnet"
18+
}
19+
}
20+

tests/expressroute.tftest.hcl

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
provider "azurerm" {
2+
features {}
3+
}
4+
5+
variables {
6+
location = "uksouth"
7+
name = "vgw-test"
8+
subnet_address_prefix = "10.0.0.0/24"
9+
enable_telemetry = false
10+
virtual_network_id = join("/", [
11+
"",
12+
"subscriptions",
13+
"00000000-0000-0000-0000-000000000000",
14+
"resourceGroups",
15+
"rg-test",
16+
"providers",
17+
"Microsoft.Network",
18+
"virtualNetworks",
19+
"vnet-test"
20+
])
21+
}
22+
23+
run "expressroute" {
24+
command = plan
25+
26+
variables {
27+
sku = "HighPerformance"
28+
type = "ExpressRoute"
29+
30+
ip_configurations = {
31+
ip_config_0 = {
32+
public_ip = {
33+
allocation_method = "Static"
34+
sku = "Standard"
35+
}
36+
}
37+
}
38+
express_route_circuits = {
39+
erc = {
40+
id = join("/", [
41+
"",
42+
"subscriptions",
43+
"00000000-0000-0000-0000-000000000000",
44+
"resourceGroups",
45+
"rg-erc-test",
46+
"providers",
47+
"Microsoft.Network",
48+
"expressRouteCircuits",
49+
"erc-test"
50+
])
51+
connection = {
52+
express_route_gateway_bypass = true
53+
authorization_key = "ABDNDBEHF"
54+
name = "conn-test"
55+
routing_weight = 10
56+
}
57+
peering = {
58+
peering_type = "AzurePrivatePeering"
59+
vlan_id = 100
60+
resource_group_name = "rg-test"
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
68+
run "expressroute_route_table_creation" {
69+
command = plan
70+
71+
variables {
72+
route_table_creation_enabled = true
73+
}
74+
}

tests/vpn.tftest.hcl

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
provider "azurerm" {
2+
features {}
3+
}
4+
5+
variables {
6+
location = "uksouth"
7+
name = "vgw-test"
8+
subnet_address_prefix = "10.0.0.0/24"
9+
sku = "VpnGw1AZ"
10+
type = "Vpn"
11+
enable_telemetry = false
12+
virtual_network_id = join("/", [
13+
"",
14+
"subscriptions",
15+
"00000000-0000-0000-0000-000000000000",
16+
"resourceGroups",
17+
"rg-test", "providers",
18+
"Microsoft.Network",
19+
"virtualNetworks",
20+
"vnet-test"
21+
])
22+
}
23+
24+
run "vpn_active_active" {
25+
command = plan
26+
27+
variables {
28+
vpn_active_active_enabled = true
29+
}
30+
}
31+
32+
run "vpn_active_active_custom_ip_config" {
33+
command = plan
34+
35+
variables {
36+
vpn_active_active_enabled = true
37+
ip_configurations = {
38+
ip_config_0 = {
39+
public_ip = {
40+
allocation_method = "Static"
41+
sku = "Standard"
42+
}
43+
}
44+
ip_config_2 = {
45+
public_ip = {
46+
allocation_method = "Static"
47+
sku = "Standard"
48+
}
49+
}
50+
}
51+
}
52+
}
53+
54+
run "vpn_active_active_custom_ip_config_fail" {
55+
command = plan
56+
57+
variables {
58+
vpn_active_active_enabled = true
59+
ip_configurations = {
60+
ip_config_0 = {
61+
public_ip = {
62+
allocation_method = "Static"
63+
sku = "Standard"
64+
}
65+
}
66+
}
67+
}
68+
69+
expect_failures = [
70+
azurerm_virtual_network_gateway.vgw
71+
]
72+
}
73+
74+
run "vpn_local_network_gateway" {
75+
command = plan
76+
77+
variables {
78+
ip_configurations = {
79+
ip_config_0 = {
80+
public_ip = {
81+
allocation_method = "Static"
82+
sku = "Standard"
83+
}
84+
}
85+
}
86+
local_network_gateways = {
87+
lgn-1 = {
88+
name = "lgn-test"
89+
gateway_address = "0.0.0.0"
90+
bgp_settings = {
91+
asn = 65515
92+
bgp_peering_address = "0.0.0.0"
93+
}
94+
connection = {
95+
name = "conn-test"
96+
type = "IPsec"
97+
98+
}
99+
}
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)