github.com/terraform-linters/tflint-ruleset-azurerm@v0.26.0/rules/apispec/generated_rules_test.go (about)

     1  package apispec
     2  
     3  import (
     4  	"testing"
     5  
     6  	hcl "github.com/hashicorp/hcl/v2"
     7  	"github.com/terraform-linters/tflint-plugin-sdk/helper"
     8  	"github.com/terraform-linters/tflint-plugin-sdk/tflint"
     9  )
    10  
    11  func Test_generatedEnumRule(t *testing.T) {
    12  	cases := []struct {
    13  		Name     string
    14  		Rule     tflint.Rule
    15  		Content  string
    16  		Expected helper.Issues
    17  	}{
    18  		{
    19  			Name: "invalid",
    20  			Rule: NewAzurermAnalysisServicesServerInvalidQuerypoolConnectionModeRule(),
    21  			Content: `
    22  resource "azurerm_analysis_services_server" "main" {
    23      querypool_connection_mode = "Write"
    24  }`,
    25  			Expected: helper.Issues{
    26  				{
    27  					Rule:    NewAzurermAnalysisServicesServerInvalidQuerypoolConnectionModeRule(),
    28  					Message: `"Write" is an invalid value as querypool_connection_mode`,
    29  					Range: hcl.Range{
    30  						Filename: "resource.tf",
    31  						Start:    hcl.Pos{Line: 3, Column: 33},
    32  						End:      hcl.Pos{Line: 3, Column: 40},
    33  					},
    34  				},
    35  			},
    36  		},
    37  		{
    38  			Name: "valid",
    39  			Rule: NewAzurermAnalysisServicesServerInvalidQuerypoolConnectionModeRule(),
    40  			Content: `
    41  resource "azurerm_analysis_services_server" "main" {
    42      querypool_connection_mode = "ReadOnly"
    43  }`,
    44  			Expected: helper.Issues{},
    45  		},
    46  	}
    47  
    48  	for _, tc := range cases {
    49  		runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
    50  
    51  		if err := tc.Rule.Check(runner); err != nil {
    52  			t.Fatalf("Unexpected error occurred: %s", err)
    53  		}
    54  
    55  		helper.AssertIssues(t, tc.Expected, runner.Issues)
    56  	}
    57  }
    58  
    59  func Test_generatedPatternRule(t *testing.T) {
    60  	cases := []struct {
    61  		Name     string
    62  		Content  string
    63  		Expected helper.Issues
    64  	}{
    65  		{
    66  			Name: "invalid",
    67  			Content: `
    68  resource "azurerm_mysql_firewall_rule" "main" {
    69      start_ip_address = "192.168.0.256"
    70  }`,
    71  			Expected: helper.Issues{
    72  				{
    73  					Rule:    NewAzurermMysqlFirewallRuleInvalidStartIPAddressRule(),
    74  					Message: `"192.168.0.256" does not match valid pattern ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`,
    75  					Range: hcl.Range{
    76  						Filename: "resource.tf",
    77  						Start:    hcl.Pos{Line: 3, Column: 24},
    78  						End:      hcl.Pos{Line: 3, Column: 39},
    79  					},
    80  				},
    81  			},
    82  		},
    83  		{
    84  			Name: "valid",
    85  			Content: `
    86  resource "azurerm_mysql_firewall_rule" "main" {
    87      start_ip_address = "192.168.0.1"
    88  }`,
    89  			Expected: helper.Issues{},
    90  		},
    91  	}
    92  
    93  	rule := NewAzurermMysqlFirewallRuleInvalidStartIPAddressRule()
    94  
    95  	for _, tc := range cases {
    96  		runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
    97  
    98  		if err := rule.Check(runner); err != nil {
    99  			t.Fatalf("Unexpected error occurred: %s", err)
   100  		}
   101  
   102  		helper.AssertIssues(t, tc.Expected, runner.Issues)
   103  	}
   104  }
   105  
   106  func Test_generatedMinimumRule(t *testing.T) {
   107  	cases := []struct {
   108  		Name     string
   109  		Content  string
   110  		Expected helper.Issues
   111  	}{
   112  		{
   113  			Name: "invalid",
   114  			Content: `
   115  resource "azurerm_search_service" "main" {
   116      partition_count = -1
   117  }`,
   118  			Expected: helper.Issues{
   119  				{
   120  					Rule:    NewAzurermSearchServiceInvalidPartitionCountRule(),
   121  					Message: `partition_count must be 1 or higher`,
   122  					Range: hcl.Range{
   123  						Filename: "resource.tf",
   124  						Start:    hcl.Pos{Line: 3, Column: 23},
   125  						End:      hcl.Pos{Line: 3, Column: 25},
   126  					},
   127  				},
   128  			},
   129  		},
   130  		{
   131  			Name: "valid",
   132  			Content: `
   133  resource "azurerm_search_service" "main" {
   134      partition_count = 1
   135  }`,
   136  			Expected: helper.Issues{},
   137  		},
   138  	}
   139  
   140  	rule := NewAzurermSearchServiceInvalidPartitionCountRule()
   141  
   142  	for _, tc := range cases {
   143  		runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
   144  
   145  		if err := rule.Check(runner); err != nil {
   146  			t.Fatalf("Unexpected error occurred: %s", err)
   147  		}
   148  
   149  		helper.AssertIssues(t, tc.Expected, runner.Issues)
   150  	}
   151  }
   152  
   153  func Test_generatedMaximumRule(t *testing.T) {
   154  	cases := []struct {
   155  		Name     string
   156  		Content  string
   157  		Expected helper.Issues
   158  	}{
   159  		{
   160  			Name: "invalid",
   161  			Content: `
   162  resource "azurerm_search_service" "main" {
   163      partition_count = 100
   164  }`,
   165  			Expected: helper.Issues{
   166  				{
   167  					Rule:    NewAzurermSearchServiceInvalidPartitionCountRule(),
   168  					Message: `partition_count must be 12 or less`,
   169  					Range: hcl.Range{
   170  						Filename: "resource.tf",
   171  						Start:    hcl.Pos{Line: 3, Column: 23},
   172  						End:      hcl.Pos{Line: 3, Column: 26},
   173  					},
   174  				},
   175  			},
   176  		},
   177  		{
   178  			Name: "valid",
   179  			Content: `
   180  resource "azurerm_search_service" "main" {
   181      partition_count = 10
   182  }`,
   183  			Expected: helper.Issues{},
   184  		},
   185  	}
   186  
   187  	rule := NewAzurermSearchServiceInvalidPartitionCountRule()
   188  
   189  	for _, tc := range cases {
   190  		runner := helper.TestRunner(t, map[string]string{"resource.tf": tc.Content})
   191  
   192  		if err := rule.Check(runner); err != nil {
   193  			t.Fatalf("Unexpected error occurred: %s", err)
   194  		}
   195  
   196  		helper.AssertIssues(t, tc.Expected, runner.Issues)
   197  	}
   198  }