terraform-compliance / cli

a lightweight, security focused, BDD test framework against terraform.
https://terraform-compliance.com
MIT License
1.36k stars 152 forks source link

Count the length of value using terraform-compliance #593

Open craigthackerx opened 2 years ago

craigthackerx commented 2 years ago

Hello :wave:

I am trying to test the length of a value using terraform-compliance. I am completely open to how to do this or if its even possible, but I think I am misunderstanding the count action. From my testing, I can only use it to count the amount of occurrences of something, rather than test a values length.

Feature: Test

  Scenario Outline: Naming Standard For Resource Groups
    Given I have output <resource_name> defined
    When it has <name_key>
    Then it must have name
    Then its value must match the "rg-.*-(dev|tst|prd).*" regex
    When I count it
    Then its value must be less to 24

    Examples:
      | resource_name                | name_key |
      | azurerm_resource_group | name        |

This just checks that there is less than 24 groups defined, which of course, there is. But I want to count the value, not the occurences

Another way round this is testing if a variable exists which is enough for me:

variable "test" {
default = 26
}
Feature: Test
  Scenario: Test Length
    Given I have any variables defined
    When it has default
    Then it must have value to be less to 24

If I could swing this, or even using an output, I could perform a length() check using Terraform and check that it matches a value.

Is it possible?

eerkunt commented 2 years ago

Hi @craigthackerx ,

Thanks for creating this issue.

Implementing length instead of count should be very easy, let me have a look on this.

eerkunt commented 2 years ago

Happy to hear suggestions about an understandable BDD directive for using length instead of count

   When I count it
   When I measured it
   When I measured the length of it
   ...
   suggestions :)
craigthackerx commented 2 years ago

Yeah when I measure the length sounds good to me.

Can you @ me on the finished code when it's done so I can read and try and learn? I'd like to be able to contribute more but wouldn't know where to look!