github.com/Azure/tflint-ruleset-azurerm-ext@v0.6.0/rules/azurerm_resource_tag_test.go (about)

     1  package rules
     2  
     3  import (
     4  	"github.com/terraform-linters/tflint-plugin-sdk/helper"
     5  	"testing"
     6  )
     7  
     8  func Test_AzurermResourceTagRule(t *testing.T) {
     9  
    10  	cases := []struct {
    11  		Name     string
    12  		Content  string
    13  		Expected helper.Issues
    14  	}{
    15  		{
    16  			Name: "1. simple block",
    17  			Content: `
    18  resource "azurerm_container_group" "example" {
    19    location            = azurerm_resource_group.example.location
    20    name                = "example-continst"
    21    ip_address_type     = "Public"
    22    dns_name_label      = "aci-label"
    23    os_type             = "Linux"
    24    resource_group_name = azurerm_resource_group.example.name
    25    
    26    container {
    27      cpu    = "0.5"
    28      image  = "mcr.microsoft.com/azuredocs/aci-tutorial-sidecar"
    29      memory = "1.5"
    30      name   = "sidecar"
    31    }
    32  }`,
    33  			Expected: helper.Issues{
    34  				{
    35  					Rule:    NewAzurermResourceTagRule(),
    36  					Message: "`tags` argument is not set but supported in resource `azurerm_container_group`",
    37  				},
    38  			},
    39  		},
    40  		{
    41  			Name: "2. tags are set anywhere if supported",
    42  			Content: `
    43  resource "azurerm_resource_group" "rg" {
    44    name     = "myTFResourceGroup"
    45    location = "westus2"
    46    tags = {
    47      Team = "DevOps"
    48    }
    49  }
    50  
    51  resource "azurerm_container_registry" "acr" {
    52    name                = "containerRegistry1"
    53    resource_group_name = azurerm_resource_group.example.name
    54    location            = azurerm_resource_group.example.location
    55    sku                 = "Premium"
    56    admin_enabled       = false
    57    tags = {
    58      environment = "testing"
    59    }
    60  
    61    georeplications {
    62      location                = "westeurope"
    63      zone_redundancy_enabled = true
    64      tags = {
    65        environment = "testing"
    66      }
    67    }
    68    dynamic "georeplications" {
    69      for_each = var.georeplications
    70      content {
    71        location                = georeplications.location
    72        zone_redundancy_enabled = georeplications.zone_redundancy_enabled
    73        tags = georeplications.tags
    74      }
    75    }
    76  }`,
    77  			Expected: helper.Issues{},
    78  		},
    79  	}
    80  
    81  	rule := NewAzurermResourceTagRule()
    82  
    83  	for _, tc := range cases {
    84  		runner := helper.TestRunner(t, map[string]string{"config.tf": tc.Content})
    85  		t.Run(tc.Name, func(t *testing.T) {
    86  			if err := rule.Check(runner); err != nil {
    87  				t.Fatalf("Unexpected error occurred: %s", err)
    88  			}
    89  			AssertIssues(t, tc.Expected, runner.Issues)
    90  		})
    91  	}
    92  }