Skip to content

Commit

Permalink
Feature enable example tests (#804)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ms-henglu authored Mar 7, 2025
1 parent 45705fc commit 8f8bf62
Show file tree
Hide file tree
Showing 20 changed files with 3,184 additions and 29 deletions.
1 change: 1 addition & 0 deletions .azdo/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ jobs:
ARM_TEST_LOCATION_ALT: $(ARM_TEST_LOCATION_ALT)
ARM_TEST_LOCATION_ALT2: $(ARM_TEST_LOCATION_ALT2)
ARM_TEST_EXAMPLES: $(ARM_TEST_EXAMPLES)
ARM_TEST_EXAMPLES_ALL: $(ARM_TEST_EXAMPLES_ALL)
TESTARGS: $(TESTARGS)
5 changes: 4 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,8 @@ teamcity-test:
@$(MAKE) -C .teamcity tools
@$(MAKE) -C .teamcity test

example-test:
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"

.PHONY: docs build build-docker test test-docker testacc vet fmt fmtcheck errcheck scaffold-website tools test-compile website website-test

.PHONY: docs build build-docker test test-docker testacc vet fmt fmtcheck errcheck scaffold-website tools test-compile website website-test example-test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ terraform {
azapi = {
source = "Azure/azapi"
}
azurerm = {
source = "hashicorp/azurerm"
version = "=4.20.0"
}
}
}

Expand Down
161 changes: 158 additions & 3 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,164 @@
# Examples
# Introduction

This directory contains examples that are mostly used for documentation, but can also be run/tested manually via the Terraform CLI.
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.


## File Structure

The file structure is as follows:

```
AzureResourceType@APIVersion
├── senario_name
│ ├── main.tf (required)
│ ├── README.md (optional)
# Example
# Notice: The `/` in the directory name is replaced with `_` in the resource type name
Microsoft.Storage_storageAccounts@2021-04-01
├── basic
│ ├── main.tf
├── with_private_endpoint
│ ├── main.tf
│ ├── README.md
```


## Contributing

If you have an example that you would like to contribute, please follow the guidelines below:

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.

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`.

3. Create a `main.tf` file in the scenario directory that demonstrates how to use the resource type in the scenario.

1. Template: Here is a template for the `main.tf` file:

```hcl
terraform {
required_providers {
azapi = {
source = "Azure/azapi"
}
}
}
provider "azapi" {
skip_provider_registration = false
}
variable "resource_name" {
type = string
default = "acctest0001"
}
variable "location" {
type = string
default = "westeurope"
}
resource "azapi_resource" "resourceGroup" {
type = "Microsoft.Resources/resourceGroups@2020-06-01"
name = var.resource_name
location = var.location
}
resource "azapi_resource" "automationAccount" {
type = "Microsoft.Automation/automationAccounts@2021-06-22"
parent_id = azapi_resource.resourceGroup.id
name = var.resource_name
location = var.location
body = {
properties = {
encryption = {
keySource = "Microsoft.Automation"
}
publicNetworkAccess = true
sku = {
name = "Basic"
}
}
}
}
```
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`.
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.
4. Location: The location should be set to the `location` variable. The location should be a location that is supported by the resource type.
5. Identity: If the resource type supports identity, you should set the identity block in the resource. For example, `identity { type = "SystemAssigned" }`.
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.
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`.
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:
```hcl
terraform {
required_providers {
azapi = {
source = "Azure/azapi"
}
azurerm = {
source = "hashicorp/azurerm"
version = "=4.20.0"
}
}
}
```
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.
5. Verify that the example works by running the acceptance tests. You can run the acceptance tests by running the following command:
```shell
make example-test TARGET=AzureResourceType@APIVersion
```
It will run acceptance tests for all scenarios in the specified example.
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:
```shell
make example-test TARGET=Microsoft_Storage_storageAccounts@2021-04-01
```
For example, to run multiple examples, you can run the following command:
```shell
make example-test TARGET=Microsoft_Storage_storageAccounts@2021-04-01,Microsoft_Compute_virtualMachines@2021-03-01
```
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.
It's also acceptable to run the below terraform commands to test the examples:
```shell
cd examples/AzureResourceType@APIVersion/scenario_name
terraform init
terraform apply
terraform plan # it's expected to have no changes
terraform destroy
```
6. Create a pull request with the changes. Please also include the output of the acceptance tests in the pull request description.
## Documentation Examples
These examples are mostly used for documentation, but can also be run/tested manually via the Terraform CLI.
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.
* **provider/provider.tf** example file for the provider index page
* **data-sources/`full data source name`/data-source.tf** example file for the named data source page
* **resources/`full resource name`/resource.tf** example file for the named data source page
* **resources/`full resource name`/resource.tf** example file for the named resource page
* **ephemeral-resources/`full resource name`/ephemeral-resource.tf** example file for the named ephemeral resource page
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/google/go-cmp v0.7.0
github.com/hashicorp/go-azure-helpers v0.70.1
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/hcl/v2 v2.22.0
github.com/hashicorp/terraform-plugin-docs v0.20.1
github.com/hashicorp/terraform-plugin-framework v1.13.0
github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1
Expand All @@ -16,6 +17,7 @@ require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0
github.com/jmespath/go-jmespath v0.4.0
github.com/stretchr/testify v1.10.0
github.com/zclconf/go-cty v1.15.0
)

require (
Expand Down Expand Up @@ -51,7 +53,6 @@ require (
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/hashicorp/hc-install v0.9.0 // indirect
github.com/hashicorp/hcl/v2 v2.22.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.21.0 // indirect
github.com/hashicorp/terraform-json v0.23.0 // indirect
Expand Down Expand Up @@ -81,7 +82,6 @@ require (
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yuin/goldmark v1.7.7 // indirect
github.com/yuin/goldmark-meta v1.1.0 // indirect
github.com/zclconf/go-cty v1.15.0 // indirect
go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect
golang.org/x/crypto v0.35.0 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
Expand Down
Loading

0 comments on commit 8f8bf62

Please sign in to comment.