Skip to content

Commit 8f8bf62

Browse files
authored
Feature enable example tests (#804)
* improve azapi example tests * update ci pipeline * go mod tidy * add skip on the tests * support external providers * update example * fix lint * add docs * go mod tidy * format * fix unit test * change log level * skip non-exist config file
1 parent 45705fc commit 8f8bf62

File tree

20 files changed

+3184
-29
lines changed

20 files changed

+3184
-29
lines changed

.azdo/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,5 @@ jobs:
7575
ARM_TEST_LOCATION_ALT: $(ARM_TEST_LOCATION_ALT)
7676
ARM_TEST_LOCATION_ALT2: $(ARM_TEST_LOCATION_ALT2)
7777
ARM_TEST_EXAMPLES: $(ARM_TEST_EXAMPLES)
78+
ARM_TEST_EXAMPLES_ALL: $(ARM_TEST_EXAMPLES_ALL)
7879
TESTARGS: $(TESTARGS)

GNUmakefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,8 @@ teamcity-test:
118118
@$(MAKE) -C .teamcity tools
119119
@$(MAKE) -C .teamcity test
120120

121+
example-test:
122+
TF_ACC=1 ARM_TEST_EXAMPLES=${TARGET} go test -v ./internal/services/... -run TestAccExamples_Selected -timeout $(TESTTIMEOUT) -ldflags="-X=github.com/Azure/terraform-provider-azapi/version.ProviderVersion=acc"
121123

122-
.PHONY: docs build build-docker test test-docker testacc vet fmt fmtcheck errcheck scaffold-website tools test-compile website website-test
124+
125+
.PHONY: docs build build-docker test test-docker testacc vet fmt fmtcheck errcheck scaffold-website tools test-compile website website-test example-test

examples/Microsoft.Storage_storageAccounts@2021-09-01/with_private_endpoint/main.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ terraform {
33
azapi = {
44
source = "Azure/azapi"
55
}
6+
azurerm = {
7+
source = "hashicorp/azurerm"
8+
version = "=4.20.0"
9+
}
610
}
711
}
812

examples/README.md

Lines changed: 158 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,164 @@
1-
# Examples
1+
# Introduction
22

3-
This directory contains examples that are mostly used for documentation, but can also be run/tested manually via the Terraform CLI.
3+
This directory contains examples for the Azure provider. The examples are categorized by Azure resource type and scenario. Each example contains a `main.tf` file that demonstrates how to use the resource type in a specific scenario. The `README.md` file in each example directory provides additional information about the example.
4+
5+
6+
## File Structure
7+
8+
The file structure is as follows:
9+
10+
```
11+
AzureResourceType@APIVersion
12+
├── senario_name
13+
│ ├── main.tf (required)
14+
│ ├── README.md (optional)
15+
16+
17+
# Example
18+
# Notice: The `/` in the directory name is replaced with `_` in the resource type name
19+
Microsoft.Storage_storageAccounts@2021-04-01
20+
├── basic
21+
│ ├── main.tf
22+
├── with_private_endpoint
23+
│ ├── main.tf
24+
│ ├── README.md
25+
```
26+
27+
28+
## Contributing
29+
30+
If you have an example that you would like to contribute, please follow the guidelines below:
31+
32+
1. Create a new directory for the resource type and API version if it does not already exist. The directory name should be in the format `AzureResourceType@APIVersion` and `/` in the resource type name should be replaced with `_`. For example, `Microsoft.Storage_storageAccounts@2021-04-01`. Please try to use the latest API version available.
33+
34+
2. Create a new directory for the scenario if it does not already exist. The directory name should be descriptive of the scenario. For example, `basic`, `with_private_endpoint`.
35+
36+
3. Create a `main.tf` file in the scenario directory that demonstrates how to use the resource type in the scenario.
37+
38+
1. Template: Here is a template for the `main.tf` file:
39+
40+
```hcl
41+
terraform {
42+
required_providers {
43+
azapi = {
44+
source = "Azure/azapi"
45+
}
46+
}
47+
}
48+
49+
provider "azapi" {
50+
skip_provider_registration = false
51+
}
52+
53+
variable "resource_name" {
54+
type = string
55+
default = "acctest0001"
56+
}
57+
58+
variable "location" {
59+
type = string
60+
default = "westeurope"
61+
}
62+
63+
resource "azapi_resource" "resourceGroup" {
64+
type = "Microsoft.Resources/resourceGroups@2020-06-01"
65+
name = var.resource_name
66+
location = var.location
67+
}
68+
69+
resource "azapi_resource" "automationAccount" {
70+
type = "Microsoft.Automation/automationAccounts@2021-06-22"
71+
parent_id = azapi_resource.resourceGroup.id
72+
name = var.resource_name
73+
location = var.location
74+
body = {
75+
properties = {
76+
encryption = {
77+
keySource = "Microsoft.Automation"
78+
}
79+
publicNetworkAccess = true
80+
sku = {
81+
name = "Basic"
82+
}
83+
}
84+
}
85+
}
86+
87+
```
88+
89+
2. Resource label: The resource label should be the singular form of the resource type name. For example, for type `Microsoft.Storage/storageAccounts@2021-04-01`, the resource label should be `storageAccount`; for type `Microsoft.Compute/virtualMachines@2021-03-01`, the resource label should be `virtualMachine`. You can add suffixes to the resource label if there are multiple resources of the same type in the scenario. For example, `storageAccount1`, `storageAccount2`.
90+
91+
3. Resource name: The resource name should be set to the `resource_name` variable or computed based on the `resource_name` variable for example `name = "${var.resource_name}-ip"`. One exception is when the resource name is required to be a specific value, for example, when creating a subnet for bastion host, the subnet name should be `AzureBastionSubnet`. During the acceptance test, the variable `resource_name` will be replaced with a unique name.
92+
93+
4. Location: The location should be set to the `location` variable. The location should be a location that is supported by the resource type.
94+
95+
5. Identity: If the resource type supports identity, you should set the identity block in the resource. For example, `identity { type = "SystemAssigned" }`.
96+
97+
6. Dependencies: If the resource has dependencies on other resources, you should create the dependent resources in the same `main.tf` file and use `azapi` resources to represent the dependent resources. You could find examples of how to create dependent resources in the existing examples.
98+
1. `subscription_id`: `azapi_client_config` data source is used to get the subscription ID, for example, `subscription_id = data.azapi_client_config.current.subscription_id`.
99+
100+
5. Other providers: If other providers are required to create the resource, you should add the required providers block in the `main.tf` file. For example, if the resource requires the `azurerm` provider, you should add the following block to the `main.tf` file:
101+
102+
```hcl
103+
terraform {
104+
required_providers {
105+
azapi = {
106+
source = "Azure/azapi"
107+
}
108+
azurerm = {
109+
source = "hashicorp/azurerm"
110+
version = "=4.20.0"
111+
}
112+
}
113+
}
114+
```
115+
116+
117+
4. (Optional) Create a `README.md` file in the scenario directory that provides additional information about the example if the example is complex or requires additional context.
118+
119+
5. Verify that the example works by running the acceptance tests. You can run the acceptance tests by running the following command:
120+
121+
```shell
122+
make example-test TARGET=AzureResourceType@APIVersion
123+
```
124+
125+
It will run acceptance tests for all scenarios in the specified example.
126+
127+
For example, to run the acceptance tests for the `basic` scenario of the `Microsoft.Storage_storageAccounts@2021-04-01` example, you can run the following command:
128+
129+
```shell
130+
make example-test TARGET=Microsoft_Storage_storageAccounts@2021-04-01
131+
```
132+
133+
For example, to run multiple examples, you can run the following command:
134+
135+
```shell
136+
make example-test TARGET=Microsoft_Storage_storageAccounts@2021-04-01,Microsoft_Compute_virtualMachines@2021-03-01
137+
```
138+
139+
Behind the scenes, it will run the unit test `TestAccExamples_Selected` with environment variable `ARM_TEST_EXAMPLES` set to the target example. The test will create the resources defined in the example and verify that the resources are created successfully, then it will destroy the resources.
140+
141+
It's also acceptable to run the below terraform commands to test the examples:
142+
143+
```shell
144+
cd examples/AzureResourceType@APIVersion/scenario_name
145+
terraform init
146+
terraform apply
147+
terraform plan # it's expected to have no changes
148+
terraform destroy
149+
```
150+
151+
6. Create a pull request with the changes. Please also include the output of the acceptance tests in the pull request description.
152+
153+
154+
## Documentation Examples
155+
156+
These examples are mostly used for documentation, but can also be run/tested manually via the Terraform CLI.
4157
5158
The document generation tool looks for files in the following locations by default. All other *.tf files besides the ones mentioned below are ignored by the documentation tool. This is useful for creating examples that can run and/or ar testable even if some parts are not relevant for the documentation.
6159
7160
* **provider/provider.tf** example file for the provider index page
8161
* **data-sources/`full data source name`/data-source.tf** example file for the named data source page
9-
* **resources/`full resource name`/resource.tf** example file for the named data source page
162+
* **resources/`full resource name`/resource.tf** example file for the named resource page
163+
* **ephemeral-resources/`full resource name`/ephemeral-resource.tf** example file for the named ephemeral resource page
164+

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/google/go-cmp v0.7.0
88
github.com/hashicorp/go-azure-helpers v0.70.1
99
github.com/hashicorp/go-uuid v1.0.3
10+
github.com/hashicorp/hcl/v2 v2.22.0
1011
github.com/hashicorp/terraform-plugin-docs v0.20.1
1112
github.com/hashicorp/terraform-plugin-framework v1.13.0
1213
github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1
@@ -16,6 +17,7 @@ require (
1617
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0
1718
github.com/jmespath/go-jmespath v0.4.0
1819
github.com/stretchr/testify v1.10.0
20+
github.com/zclconf/go-cty v1.15.0
1921
)
2022

2123
require (
@@ -51,7 +53,6 @@ require (
5153
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
5254
github.com/hashicorp/go-version v1.7.0 // indirect
5355
github.com/hashicorp/hc-install v0.9.0 // indirect
54-
github.com/hashicorp/hcl/v2 v2.22.0 // indirect
5556
github.com/hashicorp/logutils v1.0.0 // indirect
5657
github.com/hashicorp/terraform-exec v0.21.0 // indirect
5758
github.com/hashicorp/terraform-json v0.23.0 // indirect
@@ -81,7 +82,6 @@ require (
8182
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
8283
github.com/yuin/goldmark v1.7.7 // indirect
8384
github.com/yuin/goldmark-meta v1.1.0 // indirect
84-
github.com/zclconf/go-cty v1.15.0 // indirect
8585
go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect
8686
golang.org/x/crypto v0.35.0 // indirect
8787
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect

0 commit comments

Comments
 (0)