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

Add ability to specify a state alias in a run block #35080

Open
freakinhippie opened this issue Apr 25, 2024 · 1 comment
Open

Add ability to specify a state alias in a run block #35080

freakinhippie opened this issue Apr 25, 2024 · 1 comment
Labels
enhancement new new issue not yet triaged terraform test

Comments

@freakinhippie
Copy link

Terraform Version

Terraform v1.8.2
on linux_amd64

Use Cases

I would like to be able to exercise the module under test with multiple distinct executions, all within a single tftest.hcl file. For example, consider the following:

./tests/multiple_tests.tftest.hcl:

# this setup module may take a significant period of time
# to launch, but can be used by multiple instances of the
# module under test
run "setup_backend" {
  module {
    source = "./tests/modules/setup"
  }

  variables {
    cluster_members = 4
  }
}

run "first" {
  variables {
    input1 = "first"
  }

  assert { ... }
}

run "second" {
  variables {
    input1 = "second"
  }

  assert { ... }
}

In the above scenario, the "second" run block modifies the "first" run block's state. While that is desirable in many circumstances, I would like to be able to instruct the second run block to use it's own state configuration, rather than using the same as from the "first" run block. If I could tell subsequent run blocks to use their own state configuration then I could write a variety of test cases which all utilized the resources created by the "setup_backend" module.

Another use case is when module invocations can be chained, such that the output of one module is used as input for another. This requires that tests maintain separate state for each.

Attempted Solutions

I can work-around this limitation with the following, less than ideal solution.

> mkdir -p tests/modules
> cd tests/modules
# create a symlink to the module under test
> ln -s ../../ loopback01
> ln -s ../../ loopback02

Then, I can modify the original config, like so:

./tests/multiple_tests.tftest.hcl:

# this setup module may take a significant period of time
# to launch, but can be used by multiple instances of the
# module under test
run "setup_backend" {
  module {
    source = "./tests/modules/setup"
  }

  variables {
    cluster_members = 4
  }
}

run "first" {
  variables {
    input1 = "first"
  }

  assert { ... }
}

run "second" {
  module {
    source = "./tests/modules/loopback01"
  }

  variables {
    input1 = "second"
  }

  assert { ... }
}

run "third" {
  module {
    source = "./tests/modules/loopback02"
  }

  variables {
    input1 = "third"
  }

  assert { ... }
}

This does work, but it requires creating a symlink back to the module root directory for each individual state configuration that is needed. It isn't difficult, but it clutters the module repository unnecessarily.

Proposal

Add a state_alias or other keyword to the terraform test syntax to allow something like:

./tests/multiple_tests.tftest.hcl:

# this setup module may take a significant period of time
# to launch, but can be used by multiple instances of the
# module under test
run "setup_backend" {
  module {
    source = "./tests/modules/setup"
  }

  variables {
    cluster_members = 4
  }
}

run "first" {
  variables {
    input1 = "first"
  }

  assert { ... }
}

run "second" {
  state_alias = "second"

  variables {
    input1 = "second"
  }

  assert { ... }
}

run "third" {
  state_alias = "third"

  variables {
    input1 = "third"
  }

  assert { ... }
}

Where each run block that shared the same state_alias value would operate in the same way as the normal run block but with its own dedicated state.

References

No response

@freakinhippie freakinhippie added enhancement new new issue not yet triaged labels Apr 25, 2024
@crw
Copy link
Collaborator

crw commented Apr 25, 2024

Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new new issue not yet triaged terraform test
Projects
None yet
Development

No branches or pull requests

3 participants