|
| 1 | +package tflint |
| 2 | + |
| 3 | +import rego.v1 |
| 4 | + |
| 5 | +invalid_resources(type, schema, options) := terraform.mock_resources(type, schema, options, { |
| 6 | + "main.tf": ` |
| 7 | +resource "aws_instance" "invalid" { |
| 8 | + ami = get_ami_id("service1", "v0.9") |
| 9 | +
|
| 10 | + lifecycle { |
| 11 | + ignore_changes = [ami] |
| 12 | + } |
| 13 | +} |
| 14 | +`, |
| 15 | +}) |
| 16 | + |
| 17 | +valid_resources(type, schema, options) := terraform.mock_resources(type, schema, options, { |
| 18 | + "main.tf": ` |
| 19 | +resource "aws_instance" "valid" { |
| 20 | + ami = get_ami_id("service1", "v1") |
| 21 | +
|
| 22 | + lifecycle { |
| 23 | + ignore_changes = [tags] |
| 24 | + } |
| 25 | +} |
| 26 | +`, |
| 27 | +}) |
| 28 | + |
| 29 | +test_wrong_ignore_passed if { |
| 30 | + issues := deny_require_lifecycle_ignore_tags with terraform.resources as invalid_resources |
| 31 | + |
| 32 | + count(issues) == 1 |
| 33 | + issues[_].msg == "Resource aws_instance.invalid must have lifecycle.ignore_changes include \"tags\"" |
| 34 | +} |
| 35 | + |
| 36 | +test_wrong_ignore_failed if { |
| 37 | + issues := deny_require_lifecycle_ignore_tags with terraform.resources as invalid_resources |
| 38 | + |
| 39 | + count(issues) == 0 |
| 40 | +} |
| 41 | + |
| 42 | +test_correct_ignore_passed if { |
| 43 | + issues := deny_require_lifecycle_ignore_tags with terraform.resources as valid_resources |
| 44 | + |
| 45 | + count(issues) == 0 |
| 46 | +} |
| 47 | + |
| 48 | +test_correct_ignore_failed if { |
| 49 | + issues := deny_require_lifecycle_ignore_tags with terraform.resources as valid_resources |
| 50 | + |
| 51 | + count(issues) == 1 |
| 52 | +} |
| 53 | + |
| 54 | +test_wrong_ami_passed if { |
| 55 | + issues := deny_deprecated_ami_version with terraform.resources as invalid_resources |
| 56 | + |
| 57 | + count(issues) == 1 |
| 58 | + issues[_].msg == "get_ami_id function should be called with v1" |
| 59 | +} |
| 60 | + |
| 61 | +test_wrong_ami_failed if { |
| 62 | + issues := deny_deprecated_ami_version with terraform.resources as invalid_resources |
| 63 | + |
| 64 | + count(issues) == 0 |
| 65 | +} |
| 66 | + |
| 67 | +test_correct_ami_passed if { |
| 68 | + issues := deny_deprecated_ami_version with terraform.resources as valid_resources |
| 69 | + |
| 70 | + count(issues) == 0 |
| 71 | +} |
| 72 | + |
| 73 | +test_correct_ami_failed if { |
| 74 | + issues := deny_deprecated_ami_version with terraform.resources as valid_resources |
| 75 | + |
| 76 | + count(issues) == 1 |
| 77 | +} |
| 78 | + |
| 79 | +invalid_calls(schema, options) := terraform.mock_module_calls(schema, options, { |
| 80 | + "main.tf": ` |
| 81 | +module "invalid" { |
| 82 | + providers = { |
| 83 | + aws = aws.usw2 |
| 84 | + } |
| 85 | +} |
| 86 | +`, |
| 87 | +}) |
| 88 | + |
| 89 | +valid_calls(schema, options) := terraform.mock_module_calls(schema, options, { |
| 90 | + "main.tf": ` |
| 91 | +module "valid" { |
| 92 | + providers = { |
| 93 | + aws = aws.usw1 |
| 94 | + } |
| 95 | +} |
| 96 | +`, |
| 97 | +}) |
| 98 | + |
| 99 | +test_wrong_region_passed if { |
| 100 | + issues := deny_deprecated_regions with terraform.module_calls as invalid_calls |
| 101 | + |
| 102 | + count(issues) == 1 |
| 103 | + issues[_].msg == "deprecated region reference found" |
| 104 | +} |
| 105 | + |
| 106 | +test_wrong_region_failed if { |
| 107 | + issues := deny_deprecated_regions with terraform.module_calls as invalid_calls |
| 108 | + |
| 109 | + count(issues) == 0 |
| 110 | +} |
| 111 | + |
| 112 | +test_correct_region_passed if { |
| 113 | + issues := deny_deprecated_regions with terraform.module_calls as valid_calls |
| 114 | + |
| 115 | + count(issues) == 0 |
| 116 | +} |
| 117 | + |
| 118 | +test_correct_region_failed if { |
| 119 | + issues := deny_deprecated_regions with terraform.module_calls as valid_calls |
| 120 | + |
| 121 | + count(issues) == 1 |
| 122 | +} |
0 commit comments