Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I count and Then <expectation> does not work as expected #187

Closed
MisterFreeze opened this issue Dec 5, 2019 · 7 comments
Closed

When I count and Then <expectation> does not work as expected #187

MisterFreeze opened this issue Dec 5, 2019 · 7 comments
Assignees
Labels
bug waiting for confirmation Workaround/Fix applied, waiting for confirmation

Comments

@MisterFreeze
Copy link
Contributor

MisterFreeze commented Dec 5, 2019

Description :
Using terraform-compliance version 1.0.57 and loving it!
When I count them and Then clauses for the counter do not work as expected, raising:

    Scenario: There is exactly one VPC
        Given I have any resource defined
        When its type is aws_vpc
        And I count them
        Then I expect the result is equal to 1
          AttributeError: 'Context' object has no attribute 'property_name'

To Reproduce
1.
main.tf:

resource "aws_vpc" "web_vpc" {
  cidr_block = "10.0.0.0/16" #var.web_vpc_cidr_block
  tags = merge(
    { "Name" = "vpc-testbla-13" },
    {} # var.common_tags
  )
}
  1. <Used terraform-compliance parameters>
    terraform plan -out=plan.out && terraform-compliance -f test/bdd/ -p plan.out

python

    Scenario: There is exactly one VPC
        Given I have any resource defined
        When its type is aws_vpc
        And I count them
        Then I expect the result is equal to 1
          *AttributeError: 'Context' object has no attribute 'property_name'*
  1. <Your feature/scenario/steps>
    (file test/bdd/webvpc.features):
Scenario: There is exactly one VPC
   Given I have any resource defined
   When its type is aws_vpc
   And I count them
   Then I expect the result is equal to 1

Expected behavior :
The scenario should pass. No exception should be raised.

Tested versions :

  • 1.0.57
  • 0.12.17
  • 3.6.9

Additional context
The code does not reflect the test / specs that use a dict {'values': } as the code expects an integer instead of a dict.

The When I count them directive creates a {'values': <int>} dict instead of a plain int valueand assigns it to stash in [1]. Counter accesses stash and then evaluates based on the type, resulting [2] to be banced into instead of [3].

Hint:
The MockedStepContext object defines a property_name attribute that does not get assigned by default in the Given evaluation function ([4]). This lets the according test pass. ([5]).

1: https://github.com/eerkunt/terraform-compliance/blob/02be1d2c4c98754f41998ccb2e9557708eac141b/terraform_compliance/steps/steps.py#L397
2: https://github.com/eerkunt/terraform-compliance/blob/02be1d2c4c98754f41998ccb2e9557708eac141b/terraform_compliance/steps/steps.py#L423
3: https://github.com/eerkunt/terraform-compliance/blob/02be1d2c4c98754f41998ccb2e9557708eac141b/terraform_compliance/steps/steps.py#L432
4: https://github.com/eerkunt/terraform-compliance/blob/02be1d2c4c98754f41998ccb2e9557708eac141b/terraform_compliance/steps/steps.py#L44
5: https://github.com/eerkunt/terraform-compliance/blob/02be1d2c4c98754f41998ccb2e9557708eac141b/tests/mocks.py#L269

@MisterFreeze
Copy link
Contributor Author

I figured out that a more graceful handling of property_name not being defined in fail in i_expect_the_result_is_operator_than_number ([1]) would also do it.

Not even sure if the count is meant to be used in the way I did.

1: https://github.com/eerkunt/terraform-compliance/blob/02be1d2c4c98754f41998ccb2e9557708eac141b/terraform_compliance/steps/steps.py#L414

@eerkunt
Copy link
Member

eerkunt commented Dec 5, 2019

Great to hear you loved it! 🎉

Yes, normally it requires a property to be defined, not just the resource itself, but I think this is quite a valid case. I will incorporate this into the tool! Thanks for the issue 🎉

@MisterFreeze
Copy link
Contributor Author

Thanks @eerkunt , many thanks for the effort!
As I needed this to work, I adapted the test accordingly and tried a more graceful handling of property_name attribute not being defined. 127 tests passed. hth

@MisterFreeze
Copy link
Contributor Author

This does not work as expected as after counting as the count function returns a dict.

Cannot count set stash to an integer or would this break any behavior?

@eerkunt
Copy link
Member

eerkunt commented Dec 6, 2019

Having a look now, sorry couldn't had a look tomorrow was quite hectic.

@eerkunt
Copy link
Member

eerkunt commented Dec 6, 2019

1.0.58 will be released in few minutes. Could you please have a try ?

@eerkunt eerkunt added the waiting for confirmation Workaround/Fix applied, waiting for confirmation label Dec 6, 2019
@MisterFreeze
Copy link
Contributor Author

Hi eerkunt, that works like charm! Thank you for the effort :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug waiting for confirmation Workaround/Fix applied, waiting for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants