Commit Graph

152 Commits

Author SHA1 Message Date
Martin Atkins
068df07d11 various: Remove legacy "+build" comments
Go 1.17 and earlier used a different syntax for build constraint comments,
starting with "+build". Go 1.18 changed this to the modern "go:build" form
as part of standardizing the structure of toolchain directive comments,
and so for a while it was convention to include comments in both styles
to allow building with both old and new Go compilers.

However, Go 1.17 is no longer supported, and regardless of that we only
expect OpenTofu to be built with the specific version we have selected
in "go.mod" and ".go-version" anyway, so we no longer need the legacy form
of these comments: the all supported Go toolchains now support the new
form, which this commit retains.

golangci-lint v2.6.0 includes a check for this legacy form, so removing
this will also allow us to upgrade to a newer version of that linter
aggregator in a future commit.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-11-01 08:00:01 -03:00
Diógenes Fernandes
3a8506b14e fix: TestPlan_generatedConfigPath on Windows by standardizing line breaks (#3274)
Signed-off-by: Diogenes Fernandes <diofeher@gmail.com>
2025-09-16 16:11:52 -03:00
Diógenes Fernandes
22910f2b01 Adapt statelocker usage to Windows (#3206)
Signed-off-by: Diogenes Fernandes <diofeher@gmail.com>
2025-09-06 10:47:17 -03:00
Christian Mesh
fd4e426a12 Fix test crash when using deprecated outputs in the root module (#3249)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2025-09-05 14:41:32 -04:00
Ilia Gogotchuri
2c68afe753 CLI import target validation for incorrect for_each keys (#3106)
Signed-off-by: Ilia Gogotchuri <ilia.gogotchuri0@gmail.com>
2025-09-02 13:21:19 +04:00
Diógenes Fernandes
11d416edf9 add for_each attribute to the mock_provider block (#3087)
Signed-off-by: Diogenes Fernandes <diofeher@gmail.com>
2025-08-18 10:36:04 -03:00
Christian Mesh
3c76c5f419 Use the correct data when mocking a resource refresh (#3068)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Signed-off-by: Diogenes Fernandes <diofeher@gmail.com>
Co-authored-by: Diogenes Fernandes <diofeher@gmail.com>
2025-07-25 22:00:43 -03:00
Ilia Gogotchuri
3cce64d223 Import block ID validation (#2973)
Signed-off-by: Ilia Gogotchuri <ilia.gogotchuri0@gmail.com>
2025-07-17 22:54:34 +04:00
Martin Atkins
cbfeb0fdc8 jsonconfig: Additional details about input variables
The JSON object describing an input variable can now include two additional
properties:

- "type" provides a JSON representation of the variable's type constraint,
  if one is set. Omitted if either there is no constraint declared at all
  or if it's set to "any", which are equivalent and both mean that the
  type is completely unconstrained.

  This uses the standard cty representation of a type constraint, which
  matches how OpenTofu already describes types in the provider protocol,
  in state snapshots, and in saved plan files.
- "required" directly represents whether callers are required to provide
  a value for the variable. This is technically redundant since it is
  set to true unless "default" is also set, but this avoids the need for
  consuming software to reimplement this rule and potentially allows us to
  make this rule more complicated/subtle in future if needed.

For some reason the documentation about the JSON configuration
representation did not previously mention the "variables" property at all,
so this adds documentation for both the new properties and the pre-existing
properties.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-07-11 09:25:30 -07:00
Martin Atkins
6a27c82bb4 tofu show: -module=DIR mode, for showing just a single module
We previously added the -config mode for showing the entire assembled
configuration tree, including the content of any descendent modules, but
that mode requires first running "tofu init" to install all of the
provider and module dependencies of the configuration.

This new -module=DIR mode returns a subset of the same JSON representation
for only a single module that can be generated without first installing
any dependencies, making this mode more appropriate for situations like
generating documentation for a single module when importing it into the
OpenTofu Registry. The registry generation process does not want to endure
the overhead of installing other providers and modules when all it actually
needs is metadata about the top-level declarations in the module.

To minimize the risk to the already-working full-config JSON representation
while still reusing most of its code, the implementation details of package
jsonconfig are a little awkward here. Since this code changes relatively
infrequently and is implementing an external interface subject to
compatibility constraints, and since this new behavior is relatively
marginal and intended primarily for our own OpenTofu Registry purposes,
this is a pragmatic tradeoff that is hopefully compensated for well enough
by the code comments that aim to explain what's going on for the benefit
of future maintainers. If we _do_ find ourselves making substantial changes
to this code at a later date then we can consider a more significant
restructure of the code at that point; the weird stuff is intentionally
encapsulated inside package jsonconfig so it can change later without
changing any callers.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-07-10 13:18:26 -07:00
Martin Atkins
67a5cd0911 statemgr+remote: context.Context parameters
This extends statemgr.Persistent, statemgr.Locker and remote.Client to
all expect context.Context parameters, and then updates all of the existing
implementations of those interfaces to support them.

All of the calls to statemgr.Persistent and statemgr.Locker methods outside
of tests are consistently context.TODO() for now, because the caller
landscape of these interfaces has some complications:

1. statemgr.Locker is also used by the clistate package for its state
   implementation that was derived from statemgr.Filesystem's predecessor,
   even though what clistate manages is not actually "state" in the sense
   of package statemgr. The callers of that are not yet ready to provide
   real contexts.

   In a future commit we'll either need to plumb context through to all of
   the clistate callers, or continue the effort to separate statemgr from
   clistate by introducing a clistate-specific "locker" API for it
   to use instead.

2. We call statemgr.Persistent and statemgr.Locker methods in situations
   where the active context might have already been cancelled, and so we'll
   need to make sure to ignore cancellation when calling those.

   This is mainly limited to PersistState and Unlock, since both need to
   be able to complete after a cancellation, but there are various
   codepaths that perform a Lock, Refresh, Persist, Unlock sequence and so
   it isn't yet clear where is the best place to enforce the invariant that
   Persist and Unlock must not be called with a cancelable context. We'll
   deal with that more in subsequent commits.

Within the various state manager and remote client implementations the
contexts _are_ wired together as best as possible with how these subsystems
are already laid out, and so once we deal with the problems above and make
callers provide suitable contexts they should be able to reach all of the
leaf API clients that might want to generate OpenTelemetry traces.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-07-10 08:11:39 -07:00
kasulani
1af1512705 Fix: Allow function calls in test variable blocks (#2947) (#2990) 2025-07-08 08:26:25 -04:00
Martin Atkins
647ea6a645 jsonformat: Tolerate incorrect paths in plan relevant_attributes
The code for matching relevant_attributes against resource_drift entries
(a part of the heuristic for deciding whether to show "changes outside of
OpenTofu" in the human-oriented plan UI) was previously assuming that paths
in resource_drift would always be valid for the associated resource
instance object values because in most cases the language runtime will
detect invalid references and so fail to generate a plan at all.

However, when the reference is to something within a dynamically-typed
argument (such as the manifest in kubernetes_manifest) and when it appears
only as an argument to either the "try" or "can" functions (so the dynamic
error is intentionally suppressed) the language runtime can't catch it
and so the incorrect reference will leak out into relevant_attributes,
thereby violating assumptions made by the path matcher.

Instead then, we'll continue the existing precedent that this "relevant
attributes" mechanism is a best-effort heuristic that prefers to succeed
with an incomplete result rather than to fail, extending that to the
traversals in the plan renderer which will now treat incorrectly-typed
steps as not matching rather than causing OpenTofu to crash completely.

Since a reference to something that doesn't exist cannot succeed it also
cannot possibly _actually_ contribute directly to the final result of the
expression it appeared in, so in practice it should be okay to disregard
these invalid references for the purposes of deciding which changes outside
of OpenTofu seem likely to have caused the actions that OpenTofu is
proposing to make during the apply phase.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-07-03 08:50:12 -07:00
baa-ableton
3bdd0073a5 command: tofu show -config (#2820)
Signed-off-by: Babur Ayanlar <babur.ayanlar@ableton.com>
2025-06-02 10:15:46 -07:00
Martin Atkins
da1c39260b command: Improve reliability of module install cancel tests
We previously had two tests of how the module installer responds to
cancellation (e.g. SIGINT) which were flakey because they tried to rely
on the cancellation being detected at some arbitrary point before the
module installer attempted to make a request, which isn't guaranteed in
practice because our interrupt mechanism only aims to cause OpenTofu to
exit "soon", with no guarantee about how much ongoing progress it will
make before it does.

To make these tests more robust, we'll now instead tell the module
installer to install from a real HTTP server that is intentionally designed
to stall the client by accepting its request but then just leaving the
connection open without responding.

This means that we can now test the more realistic situation of the cancel
signal being triggered after a slow request is already in progress, and
be sure that we're definitely sending the cancel signal at a moment that
matches that intention.

This is similar to a strategy we previously took to improve the reliability
of the tests for cancellation of the _provider_ installer, in
TestInit_cancelProviders. However, our provider installer version of this
used an intentionally-stalling implementation of getproviders.Source
instead of running a real server because the provider installer is designed
to support configurable installation methods, while the module installer
is not: its policy about what module source types are accepted is
hard-coded in package getproviders, at least for now.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2025-04-18 09:11:22 -07:00
Ilia Gogotchuri
baed1f2df5 Adds support for run block outputs in the test provider block (#2543)
Signed-off-by: Ilia Gogotchuri <ilia.gogotchuri0@gmail.com>
2025-03-18 12:29:37 +04:00
Diógenes Fernandes
832310fb74 fix: remove elapsed_seconds check on the golden reference (#2565)
Signed-off-by: Diógenes Fernandes <diofeher@gmail.com>
2025-03-06 09:03:46 -03:00
Jarrett Duskey
ecd4dc5c61 Add count of forgotten resources to plan and apply outputs. (#2010)
Signed-off-by: Jarrett Duskey <jarrett@duskey.io>
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Co-authored-by: Christian Mesh <christianmesh1@gmail.com>
2025-02-24 14:53:32 -05:00
Oleksandr Levchenkov
be5b14625d fix randomly failing destroyed_mod_outputs test (#2315)
Signed-off-by: ollevche <ollevche@gmail.com>
2024-12-30 16:38:27 +02:00
Oleksandr Levchenkov
c5b43b9f1a fix: unused config's create_before_destroy on resource change with no refresh (#2248)
Signed-off-by: ollevche <ollevche@gmail.com>
2024-12-20 10:47:00 +02:00
Sekiranda Hamza
211ec55a30 Skip reading encryption keys on tofu init with -backend=false flag set (#2293)
Signed-off-by: Sekiranda <sekirandahamza@gmail.com>
2024-12-20 10:45:12 +02:00
Oleksandr Levchenkov
5f8eee4708 add simulated state serialization between tofu test runs (#2274)
Signed-off-by: ollevche <ollevche@gmail.com>
2024-12-10 16:34:25 +02:00
Oleksandr Levchenkov
32ca523689 fix: type defaults for variables in tests (#2244)
Signed-off-by: ollevche <ollevche@gmail.com>
2024-12-04 16:48:08 +02:00
Oleksandr Levchenkov
2758f2cfbf fix mock provider validation (#2140)
Signed-off-by: ollevche <ollevche@gmail.com>
2024-11-20 16:45:17 +02:00
adenhuen
0e189b33f8 test: ensure test vars are not used in tf plan (#2128)
Signed-off-by: Aden Huen <aden.huen@gmail.com>
Signed-off-by: adenhuen <aden.huen@gmail.com>
Signed-off-by: Ados <aden.huen@gmail.com>
Co-authored-by: Oleksandr Levchenkov <ollevche@gmail.com>
2024-11-12 17:23:54 +02:00
Arel Rabinowitz
3d4bf29c56 Add exclude flag support (#1900)
Signed-off-by: RLRabinowitz <rlrabinowitz2@gmail.com>
2024-11-05 10:16:00 -05:00
Martin Atkins
6707ef6ca3 Fix regression of backend reinit detection when backend schema has required arguments (#2119)
Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2024-11-04 14:23:32 -05:00
Andy Hayes
7215ee2ed8 Adds warning if tests don't provide valid variable (#2057)
Signed-off-by: Andrew Hayes <andrew.hayes@harness.io>
2024-10-15 09:20:11 +01:00
Martin Atkins
8b0b5b271b command/init: Support static eval for backend config migration check
The "backendConfigNeedsMigration" helper evaluates the backend
configuration inline to compare it with the object previously saved in the
.terraform/terraform.tfstate file.

However, this wasn't updated to use the new "static eval" functionality
and so was treating any references to variables or function calls as
invalid, causing a spurious "backend configuration changed" error when
re-initializing the working directory with identical backend configuration
settings.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
2024-10-09 09:27:11 -07:00
Andy Hayes
de69070b02 Adds prompt for missing static variables (#2047)
Signed-off-by: Andrew Hayes <andrew.hayes@harness.io>
2024-10-07 18:30:42 +01:00
Viktor Szépe
bb63574f09 Fix typos (#1954)
Signed-off-by: Viktor Szépe <viktor@szepe.net>
2024-09-17 12:04:30 +02:00
Nathan Baulch
9b7bec31b4 Another batch of minor typos (#1953)
Signed-off-by: Nathan Baulch <nathan.baulch@gmail.com>
2024-09-09 07:51:39 -04:00
Burak Şen
7a02fad996 Fix repetitive diagnosis output in init response (#1890)
Signed-off-by: buraksenn <buraksenb@gmail.com>
2024-09-05 07:36:18 -04:00
Nathan Baulch
ea558d9d4b Fix typos (#1905)
Signed-off-by: Nathan Baulch <nathan.baulch@gmail.com>
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Co-authored-by: Christian Mesh <christianmesh1@gmail.com>
2024-08-29 13:20:33 -04:00
Andrew Hayes
131c2cadda Adds multi line support to console (#1875)
Signed-off-by: Andrew Hayes <andrew.hayes@harness.io>
2024-08-02 10:21:44 +01:00
Syasusu
1c0cb13bf7 feat: Add support for tofu.workspace which will be resolved in the same way as terraform.workspace (#1305)
Signed-off-by: Syasusu <syasusu@163.com>
2024-08-01 08:14:34 -04:00
Siddhartha Sonker
579d74c409 Add -show-sensitive flag to tofu plan, apply, state-show and output commands (#1554)
Signed-off-by: siddharthasonker95 <158144589+siddharthasonker95@users.noreply.github.com>
2024-07-22 10:58:57 +01:00
James Humphries
12d9380982 Improve comparison of sensitive marks on resources, and propagate the sensitive_attributes correctly (#1640)
Signed-off-by: James Humphries <james@james-humphries.co.uk>
2024-07-09 08:42:02 -04:00
Andrew Hayes
7e706fa1a7 Adds provider function to tofu scheme/metadata commands (#1753)
Signed-off-by: Andrew Hayes <andrew.hayes@harness.io>
2024-07-01 16:41:27 +01:00
Christian Mesh
8f8e0aa4aa Static Evaluation Base, Module Sources, Backend Config (#1718)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Signed-off-by: Christian Mesh <cristianmesh1@gmail.com>
Co-authored-by: James Humphries <James@james-humphries.co.uk>
Co-authored-by: Oleksandr Levchenkov <ollevche@gmail.com>
2024-06-24 09:13:07 -04:00
Siddhartha Sonker
9138470a67 Allowed variable to pass inside variables block (#1488)
Signed-off-by: siddharthasonker95 <158144589+siddharthasonker95@users.noreply.github.com>
2024-06-03 18:44:05 +05:30
Alex Ott
522a859cf0 Write state using compact JSON representation (#1647)
Signed-off-by: Alex Ott <alexott@gmail.com>
Co-authored-by: Oleksandr Levchenkov <ollevche@gmail.com>
2024-05-21 15:04:10 -04:00
Siddhartha Sonker
6065bc593f Fixed tofu test when module has no resource (#1409)
Signed-off-by: siddharthasonker95 <158144589+siddharthasonker95@users.noreply.github.com>
2024-05-06 14:49:42 +02:00
Siddhartha Sonker
de30707b6b Fix for tofu init failure when test have spaces in their name (#1489)
Signed-off-by: siddharthasonker95 <158144589+siddharthasonker95@users.noreply.github.com>
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
Co-authored-by: Christian Mesh <christianmesh1@gmail.com>
2024-04-25 11:11:38 -04:00
Sanskruti Shahu
08f9a740ac Added support to use .tfvars files from tests folder (#1386)
Signed-off-by: sanskruti-shahu <sanskruti.shahu@harness.io>
2024-03-29 08:44:27 -04:00
Siddhartha Sonker
accfe1c412 Allow referencing output from test run in local variables block (tofu test) (#1254)
Signed-off-by: siddharthasonker95 <158144589+siddharthasonker95@users.noreply.github.com>
2024-02-19 10:18:56 +00:00
Christian Mesh
249ed42fb1 Run block variable references (#1129)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2024-01-17 06:57:14 -05:00
Ronny Orot
1aa92856b1 Fix test command teardown order (#1043)
Signed-off-by: Ronny Orot <ronny.orot@gmail.com>
2023-12-20 16:38:42 +02:00
Christian Mesh
78464f251a Add a warning when multiple likely forks of a provider are detected (#1009)
Signed-off-by: Christian Mesh <christianmesh1@gmail.com>
2023-12-14 12:21:16 -05:00
Christian Mesh
abd324ea7c Revert "Fixes #898: Replace hashicorp and terraform references" (#995) 2023-12-11 15:10:03 -05:00