Coder Social home page Coder Social logo

Comments (9)

nhurel avatar nhurel commented on August 23, 2024

I understand the need of being able to test with specific resource attributes values but I don't think those values should be in the assert block. The assert block should only describe the expected result of applying the terraform config under certain circumstances (the input values and the mocked data source).

Do you have other example in mind where you'd like more control on the resource attributes ? That would help to design this feature

from terraspec.

tomelliot16 avatar tomelliot16 commented on August 23, 2024

A real case I'm dealing with which came up with the thought is

resource "aws_kms_key" "key" {
  description = "KMS key 1"
}

resource "aws_iam_role_policy" "policy" {
  role = aws_iam_role.role.id
  policy = <<-EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "kms:Decrypt",
                "kms:DescribeKey",
                "kms:Encrypt",
                "kms:GenerateDataKey*",
                "kms:ReEncrypt*"
            ],
            "Resource": [
                "${aws_kms_key.key.id}"
            ]
        }
  EOF
}

resource "aws_iam_role" "role" {
  name = "role"

  assume_role_policy = <<-EOF
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Principal": {
            "AWS": "arn:aws:iam::${data.aws_caller_identity.current.account_id}:root"
          },
          "Action": "sts:AssumeRole",
          "Condition": {
            "Bool": {
              "aws:MultiFactorAuthPresent": "true"
            }
          }
        }
      ]
    }
    EOF
}

Where I want to assert each one of the above however when ids are not set they are only set as nil

aws_iam_role_policy.policy.role : <nil> != at-least-something-unique-for-role-arn

I also was thinking of asserting policies.

from terraspec.

tomelliot16 avatar tomelliot16 commented on August 23, 2024

Also I tend to disagree that is doesn't belong in the assert block because it seems the natural place when comparing it with rspec or other mocking systems
expect(thing).to receive(:call_name).with(args).and_return(something)
basically we are expecting the provider to fill in some data that will be used for other things. I'm not sure I understand why you don't want to add the return to the assert block.

from terraspec.

tomelliot16 avatar tomelliot16 commented on August 23, 2024

perhaps this would be fine

assert "aws_s3_bucket" "c" {
  bucket = "my-tf-test-bucket"
}
mock_resource "aws_s3_bucket" "c" {
  return {
    id = "arn:aws:s3::0000000000:my-tf-test-bucket"
  }
}

from terraspec.

tomelliot16 avatar tomelliot16 commented on August 23, 2024

if its language you could change it to

expect "aws_s3_bucket" "c" {
  bucket = "my-tf-test-bucket"
  return {
    id = "arn:aws:s3::0000000000:my-tf-test-bucket"
  }
}

from terraspec.

nhurel avatar nhurel commented on August 23, 2024

indeed, that's better with expect rather than assert. And it's easier to read (and write) with a single expect block rather than splitting it betwen an assert block and a mock one 👍

from terraspec.

nhurel avatar nhurel commented on August 23, 2024

Hello @tomelliot16
I've started implemented support for mocking return values or resources in the mock-return branch.
It's still work in progress but you can start testing it if you want

from terraspec.

tomelliot16 avatar tomelliot16 commented on August 23, 2024

@nhurel I've gotten my team to work on this from Acquia acquia#1 I will open a PR to this repo

from terraspec.

tomelliot16 avatar tomelliot16 commented on August 23, 2024

https://github.com/nhurel/terraspec/pull/10/files

from terraspec.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.