diff --git a/.github/workflows/dependency-review.yml b/.github/workflows/dependency-review.yml index 0c6c7292afb..c8e8e0bc508 100644 --- a/.github/workflows/dependency-review.yml +++ b/.github/workflows/dependency-review.yml @@ -54,11 +54,15 @@ jobs: # sax: BlueOak-1.0.0 (verified at https://github.com/isaacs/sax-js/blob/main/LICENSE.md) - # Blue Oak Model License, a permissive OSS license. # Transitive dependency of svgo (website SVG optimization). + # github.com/opencontainers/image-spec: Apache-2.0 code; "LicenseRef-scancode-dco-1.1" + # is the contributor DCO text and "MIT" covers bundled schema tooling (see + # https://github.com/opencontainers/image-spec/blob/main/LICENSE). + # Transitive dependency of Terragrunt in plugins/terragrunt (and of the root module). # minimatch and @isaacs/nonrecursive-glob-match: BlueOak-1.0.0 is a permissive license, # approved for these two packages only (BlueOak-1.0.0 is deliberately not added to # allow-licenses). They come from the website docs tooling (pnpm override of minimatch # to v11) and the cdk-terraform example lockfile; neither ships in the atmos binary. - allow-dependencies-licenses: "pkg:golang/github.com/antlr4-go/antlr/v4, pkg:golang/github.com/google/cel-go, pkg:golang/golang.org/x/image, pkg:golang/modernc.org/libc, pkg:golang/github.com/opencontainers/go-digest, pkg:npm/pako, pkg:npm/sax, pkg:npm/minimatch, pkg:npm/%40isaacs/nonrecursive-glob-match" + allow-dependencies-licenses: "pkg:golang/github.com/antlr4-go/antlr/v4, pkg:golang/github.com/google/cel-go, pkg:golang/golang.org/x/image, pkg:golang/modernc.org/libc, pkg:golang/github.com/opencontainers/go-digest, pkg:golang/github.com/opencontainers/image-spec, pkg:npm/pako, pkg:npm/sax, pkg:npm/minimatch, pkg:npm/%40isaacs/nonrecursive-glob-match" # Fail on moderate or higher severity vulnerabilities fail-on-severity: moderate diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6ea3821ae37..6df5844de5b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1290,6 +1290,9 @@ jobs: env: CGO_ENABLED: "0" GOWORK: "off" + # Each package module pins its own Go version; plugins/terragrunt needs a newer + # toolchain than the root go.mod this job installs. + GOTOOLCHAIN: auto run: | # Each official package is a separate module with an independent version. printf 'mode: set\n' > plugins-coverage.out diff --git a/docs/prd/plugins/extensions/component-engines.md b/docs/prd/plugins/extensions/component-engines.md index 1fa4329850e..70ce5d95258 100644 --- a/docs/prd/plugins/extensions/component-engines.md +++ b/docs/prd/plugins/extensions/component-engines.md @@ -1,60 +1,111 @@ -# Component engine extensions +# Component configuration and engine plugins -**Status:** Follow-on adapter; contracts retained in the first-release design. [All extension types](README.md). +**Status:** Planned. The component protocol, SDK handlers, host adapters, and CLI integration described here are not available on this branch. -A component extension adds an execution engine for an infrastructure tool. It is distinct from distributing modules, charts, playbooks, or other component content. +**Decision:** Separate component type, configuration provider, and execution engine. Negotiate optional capabilities independently. A configuration provider must not need an executor. -## Consumer and author experience +See the [Terragrunt requirements](../../terragrunt-support.md) for the first consumer and [all extension types](README.md) for the platform scope. -```shell -atmos plugin create acme-pulumi --capability component --capability ci.component --runtime go -atmos plugin install acme/pulumi --as pulumi --version '~1.0' -atmos pulumi preview app -s production -``` +## Required behavior -These are proposed plugin commands. The engine declares its component kind, command metadata, configuration schema, artifact types, and lifecycle events. Engine command shortcuts follow command collision rules; qualified package invocation remains available for declared command exports. +A configuration plugin supplies ordinary component instances from another format. The host must use these instances in selection, inventory, dependencies, affected detection, state reads, and execution. -## Contract +For native Terragrunt adoption, the type is `terraform`, the configuration provider is the Terragrunt plugin, and the engine is Terraform or OpenTofu. Atmos owns execution, scheduling, saved plans, UI, and native Terraform CI reporting. Executable mode is separate and uses engine `terragrunt`. Never select it as an automatic fallback. -Adapt the [component registry](../../../../pkg/component/registry.go) for discovery, resolved stack inputs, validation, artifact generation, execution, and lifecycle dispatch. API services provide working directories, identity environments, pinned tools, streams, deadlines, and provenance. +## Reuse and ownership -Dynamic component kinds must work throughout stack resolution, validation, selection, dependency graphs, and affected-component discovery, not just a command wrapper. Replace closed lists/fallbacks at these integration points with registered metadata while preserving built-in behavior. +Terraform is registered through [`pkg/component/terraform`](../../../../pkg/component/terraform/provider.go). Its implementation has moved out of `internal/exec`; the remaining execution adapter supplies existing host services. Reuse this lifecycle, source provisioning, backend access, scoped authentication, toolchain, dependency graph, bulk executor, and CI reporting. -The engine returns structured operation outcomes and artifact references. It declares command-specific success semantics, including “changes detected” statuses; Atmos cannot assume every nonzero tool exit means failure. Artifact contracts distinguish source configuration, a generated plan, and the exact artifact applied. +Reuse the merged plugin SDK, static manifest validation, OCI releases, lock files, policy, brokered APIs, and process lifecycle. Extend the descriptor registry when its host adapter becomes available. A protobuf service alone must not advertise an installable capability. -Component-specific CI extensions interpret operation results and emit reporting requests through the selected [CI provider](ci-providers.md). They can ship with the engine. A new engine must not need core changes for plan summaries, checks, comments, or validator findings. +The component registry currently allows replacement registrations. External plugins must not use that behavior to replace built-in Terraform. Add explicit ownership and collision checks at the adapter boundary. Duplicate provider identities or component coordinates must fail before any registration becomes visible. -## Integration +## Independent contracts -| Aspect | Today | -|---|---| -| Registry | [`component.Register(provider ComponentProvider) error`](../../../../pkg/component/registry.go) with `GetProvider`, `MustGetProvider`, `ListTypes`, and `ListAllComponents`. Ansible, aws/cloudformation, container, emulator, helm, kubernetes, and mock register in `init()`. [`custom/provider.go`](../../../../pkg/component/custom/provider.go) registers at runtime. | -| Duplicate policy | Replaces: "last registration wins, allowing plugin override". | -| Context on the interface | Yes, through `ExecutionContext.Context`. | -| Reachable from outside the module | Yes. [`ComponentProvider`](../../../../pkg/component/provider.go) is public. | -| Closed lists to open | `schema.Components.GetComponentConfig`; `autoDetectComponentTypes` in `internal/exec/describe_component.go`; `typeEntries` in `internal/exec/describe_stacks_component_processor.go`; `getComponentBasePath` in `internal/exec/describe_stacks.go`; the three type lists in `pkg/list/extract/components.go`; the hardcoded `terraform, helmfile, packer` lists in `pkg/utils/component_reverse_path_utils.go` and `cmd/list/sources.go`. The component-section whitelist in the stack processor (`vars`, `settings`, `env`, `hooks`, `secrets`, and others) also applies; custom types survive only through the raw-map fallback. | -| Boundary leaks | `Execute` and `GenerateArtifacts` take an `ExecutionContext` struct carrying `*schema.AtmosConfiguration`, `schema.ConfigAndStacksInfo`, and `Flags map[string]any`. | +The checked-in [protocol](../../../../proto/atmos/plugin/v1/plugin.proto) is authoritative for available services. The proposed component services use contract version 1. -Terraform, helmfile, and packer are not registered: they never call `component.Register` and run through legacy paths in `internal/exec`. The component CI plugin types (`Plugin`, `HookBinding`, `HookContext`) are Go-internal; see [CI providers](ci-providers.md). +| Capability | Service | Responsibility | +|---|---|---| +| `component.discovery` | `ComponentDiscovery.Discover` | Resolve imports or discover logical instances, source watches, and dependency edges in the requested root/revision. | +| `component.validation` | `ComponentValidation.Validate` | Reject unsupported operations, modes, and provider options before effects. | +| `component.preparation` | `ComponentPreparation.Prepare` | Reevaluate one selected unit before its operation; return inputs, module, state identity, and literal generated files. | +| `component.state` | `ComponentState.Resolve` | Resolve backend/workspace identity without preparation or execution. | -**Adapter:** `pkg/plugin/adapter/component` implements `ComponentProvider` and calls the `Component` wire service, plus `CIComponent` for an engine's `ci.component` extension. It consumes the `api.Process`, `api.Toolchain`, `api.FS`, `api.Credentials`, and `api.Log` API services. +The following contracts still require implementation: -**Injection points:** -- `component.GetProvider`: a miss consults plugin engines. -- Every closed list above reads `component.ListTypes()` plus plugin metadata. -- Stack processing treats a registered external type like a custom type, including `metadata.inherits`. -- Dependency graphs and `describe affected` resolve types through the registry. -- `ci.GetPluginForEvent`: accepts the engine's `ci.component` extension. +- Static metadata for modes, commands, and typed options. Component family, provider export name, package-local settings schema, and service version are planned. +- Optional execution with stdout, stderr, diagnostics/logs, lifecycle events, and one authoritative outcome. +- Scoped host process, filesystem, toolchain, and logging APIs. The existing broker does not provide these services yet. +- External engine CI reporting. Native Terraform adaptation retains the existing Terraform CI path. -**Prerequisites:** P0.3 Duplicate policy; P0.6 Component registry completeness; P0.5 Public aliases for CI component types (for `ci.component`); Phase 3 work package WP3.6 (components and `ci.component`). See the [gap analysis](../platform/gap-analysis.md) and [delivery plan](../platform/delivery-plan.md). +Do not transmit Go callbacks, writers, `ExecutionContext`, `AtmosConfiguration`, or `ConfigAndStacksInfo`. Send explicit coordinates, values, paths, operation context, and granted effects. -**Must not cross the wire:** -- `ExecutionContext` struct: replaced by resolved component values, stack coordinates, working directory, and a scoped environment. -- `*schema.AtmosConfiguration` and `schema.ConfigAndStacksInfo`: replaced by the same resolved values. -- `HookHandler func(*HookContext)`: replaced by the `CIComponent` service with a structured outcome and reporting requests. +### Values and artifacts -## Acceptance +`ComponentValue` carries cty type JSON, cty MessagePack, and sensitivity paths. Preserve exact numbers, collection types, nulls, nested unknowns, and marks. Never use `protobuf.Struct` for infrastructure values. Reject unsupported marks and invalid sensitivity paths. Error messages must not contain payloads. -A fixture engine is discovered from stack configuration, resolves imports and dependencies, authenticates through Atmos, generates artifacts, executes pinned tools, fires lifecycle events, and reports through both built-in and external CI providers. Test cancellation, partial failure, unsupported artifacts, required plan validation, command/kind collisions, and unchanged built-ins. +The SDK is planned to provide `component.Encode` and `component.Decode`. Translate language-specific sensitivity marks deliberately. The host must decode to its own typed values and masking rules. A byte round trip does not establish secret-safe rendering. -The [migration direction](../builtin-migration.md) applies these contracts to future Terraform, Helm, Ansible, and Kubernetes packages. +Generated content is evaluated bytes, not a second template. Validate destination paths, ownership policies, and write scope. Reject unknown ownership policies. Saved plans must bind exact artifact bytes, provider version, source revision, effective options, state identity, and engine. Verify these before apply; never replace reviewed inputs by reevaluating them. + +### Discovery and state + +Installation must not enable discovery. Disabled providers must cause no startup, downloads, or directory scans during ordinary commands. + +Add discovered definitions before commands reject projects without YAML stacks. Feed imports and discovery into the same resolved stack map. Reuse naming precedence and stable logical coordinates. Cache paths must not become instance names. + +Discovery defaults to no commands, network, secret decryption, writes, or state reads. Host API grants must enforce the policy. These restrictions do not sandbox trusted native code at the operating-system level. Report denied effects; never omit instances or substitute empty values. + +State resolution must work before plan/apply without hooks or generated files. Return the upstream working directory because relative backend paths can refer to a location other than Atmos's execution directory. + +### Flags and command ownership + +Use static metadata for help without starting plugins. Use `pkg/flags.NewStandardParser` and CLI > environment > configuration > default precedence. + +- Declare option types, defaults, aliases, applicable modes/commands, deprecation, and sensitivity. +- Preserve absent versus explicit false, zero, empty string, and empty list. +- Atmos owns selection, `--affected`, `--ui`, and native scheduling. Reject collisions. +- Keep provider options, Terraform argv, and explicit external-engine argv separate. +- Validate plugin semantics before discovery/preparation effects. +- Apply discovery-relevant options consistently to both revisions, preparation, and applicable state resolution. +- Include effective options in cache and saved-plan identity. Never log sensitive values. +- Reject unsupported native options. Executable passthrough must be explicit and must not create a second scheduler. + +The proposed `ComponentOperation` transmits resolved provider options. It does not implement parsing or authorize arbitrary passthrough. + +## Host integration + +The planned `pkg/plugin/adapter/component` will supply discovery, validation, preparation, and state clients. Its immutable registry is private to the invocation and does not register or replace execution engines. Compose these providers with registered engines in the stack and execution adapters. Keep business logic out of `internal/exec`. + +Open remaining closed type lists through registry metadata: `schema.Components.GetComponentConfig`, component auto-detection, stack component processing, component/reverse base paths, list extraction, `list sources`, stack-section validation/inheritance, dependency/affected dispatch, and CI dispatch. + +The planned `bootstrap.LoadComponents` accepts explicit capability-qualified provider references. It uses the existing package policy, OCI cache, lock, and restoration paths. An empty reference list does not inspect package manifests. Disabled packages cannot satisfy references. Schema validation and ownership checks precede native startup; a failed batch returns no registry. Activation does not call discovery or execute the engine. + +Declare each service under `spec.capabilities.components.discovery`, `.validation`, `.preparation`, or `.state`. Each export declares `component_type` and a package-local `schema`. All capabilities for one provider must agree on package identity, executable, schema, type, and negotiated description. Keep provider settings separate from resolved component inputs in every request. + +Connect this explicit loader to configuration and command contexts. Preserve lazy activation for unrelated operations. Do not add a process-global discovery callback or cache plugin results without provider/options/revision identity. + +Distinguish file/directory/glob watches, output dependencies, and ordering-only edges. Compare each Git revision in its own root. Include additions, removals, and renames; evaluating only current HCL is insufficient. + +Cancellation must stop invocations, processes, callbacks, and descendants. Test deadlines, caller cancellation, interrupts, crashes, and bounded output/backpressure. Never retry apply automatically after a lost connection. + +## Delivery and acceptance + +| Layer | Required result | State | +|---|---|---| +| Protocol and SDK | Independent services, lossless values, optional registration, native transport tests. | Planned. | +| Host activation | Static descriptors, provider settings schemas, private registry, package selection, and service adapters. | Planned. | +| Host composition | Flags, scoped services, discovery/import and engine adapters. | Planned. | +| Terragrunt plugin | Separately built SDK package runs the advanced native fixture on stock Atmos. | Planned. | +| Integration | Lists, state functions, affected, dependencies, UI, saved plans, and Pro share instances. | Planned. | +| Compatibility | Differential tests for supported constructs and explicit failures for unsupported behavior. | Required. | + +Require at least 90% patch coverage per layer. Test failures as well as successful calls. A protocol fixture does not prove installed-plugin or Terragrunt compatibility. + +The first product milestone is an installed Terragrunt plugin running the advanced native fixture through stock Atmos without snapshot patches or temporary callbacks. Port the pilot's field regressions before removing its temporary hooks. + +## Execution-context acceptance gate + +A configuration provider must receive the host execution context before it performs preparation effects. Test sanitized environments, explicit empty environments, identity selection, authentication failure, toolchain executable selection, PATH precedence, managed RC files, registry trust, cache policy, cancellation, and isolation between invocations. + +The provider does not choose the host execution directory. Atmos source/workdir provisioning owns that directory. Test that evaluated source changes preserve provision and metadata settings, that literal artifacts reach the resolved directory, and that backend identity does not change with workdir placement. Test repeated runs and two instances that use the same module. diff --git a/docs/prd/plugins/overview.md b/docs/prd/plugins/overview.md index 4d50f309414..10daa24ecae 100644 --- a/docs/prd/plugins/overview.md +++ b/docs/prd/plugins/overview.md @@ -38,7 +38,7 @@ A **plugin** is an installable, independently versioned package managed by `atmo ## Foundation and architectural direction -Atmos's existing registries are the integration points, but they are uneven. The [gap analysis](platform/gap-analysis.md) inventories each one: most replace silently on duplicate registration, two live in Go-internal packages, three interfaces carry no `context.Context`, and several surfaces still dispatch through closed lists rather than a registry. Terraform, Helmfile, and Packer are not registered as component engines; validation has no engine registry; credentials have no versioned type. +Atmos's existing registries are the integration points, but they are uneven. The [gap analysis](platform/gap-analysis.md) inventories each one: most replace silently on duplicate registration, two live in Go-internal packages, three interfaces carry no `context.Context`, and several surfaces still dispatch through closed lists rather than a registry. Terraform now uses the component registry. Helmfile and Packer still use legacy execution paths. The [component extension](extensions/component-engines.md) separates configuration providers from engines; its protocol and explicit host activation API are implemented, while CLI stack and execution integration remain pending. Other entries in the gap analysis describe the platform design baseline and must be checked against the implemented adapters. Auth now dispatches built-in and plugin provider and identity constructors through `pkg/auth/registry`. Preserve existing configuration and behavior as additional auth adapters are introduced. diff --git a/docs/prd/plugins/platform/gap-analysis.md b/docs/prd/plugins/platform/gap-analysis.md index 5975ce11105..0d9c0da27ab 100644 --- a/docs/prd/plugins/platform/gap-analysis.md +++ b/docs/prd/plugins/platform/gap-analysis.md @@ -61,7 +61,7 @@ Prerequisites: P0.1 (registry completion), P0.2 (credential types). Follow-on: [ | Aspect | Finding | |---|---| | Registry | `component.Register(provider ComponentProvider) error`; `GetProvider`, `MustGetProvider`, `ListTypes`, `ListAllComponents`. Built-ins register in `init()` through blank imports in `cmd/root.go`: ansible, aws/cloudformation, container, emulator, helm, kubernetes, mock. [`custom/provider.go`](../../../../pkg/component/custom/provider.go) registers at runtime. | -| Not registered | **terraform, helmfile, and packer never call `component.Register`.** They run through legacy paths in `internal/exec`. | +| Registry migration | Terraform now registers through `pkg/component/terraform`; its runtime is extracted from `internal/exec`. Helmfile and Packer remain on legacy paths. External configuration providers must compose with Terraform, not replace it. See [component contracts and remaining adapters](../extensions/component-engines.md). | | Duplicate policy | "Last registration wins, allowing plugin override." | | Closed lists | `schema.Components.GetComponentConfig` switches on built-in types; dynamic types land in `Components.Plugins map[string]any` (`mapstructure:",remain"`). `internal/exec/describe_component.go` `autoDetectComponentTypes`; `internal/exec/describe_stacks_component_processor.go` `typeEntries`; `internal/exec/describe_stacks.go` `getComponentBasePath`; three type lists in `pkg/list/extract/components.go`; `pkg/utils/component_reverse_path_utils.go` and `cmd/list/sources.go` hardcode `terraform, helmfile, packer`. Component sections (`vars`, `settings`, `env`, `hooks`, `secrets`, …) are whitelisted in the stack processor (`pkg/config/const.go`, `internal/exec/stack_processor_process_stacks_helpers_extraction.go`, `internal/exec/stack_processor_merge.go`); custom types survive only through the raw-map fallback in `internal/exec/stack_processor_process_stacks.go`. | | Boundary leaks | `Execute(ctx *ExecutionContext)` and `GenerateArtifacts` take a struct carrying `*schema.AtmosConfiguration`, `*schema.ConfigAndStacksInfo`, and `Flags map[string]any`. | diff --git a/docs/prd/plugins/platform/interfaces.md b/docs/prd/plugins/platform/interfaces.md index 45bf1132652..d198b8ed366 100644 --- a/docs/prd/plugins/platform/interfaces.md +++ b/docs/prd/plugins/platform/interfaces.md @@ -173,7 +173,7 @@ Every row is checked across built-ins, YAML configuration, and all selected pack | Namespace | Key | Built-in or YAML claimant | Outcome | |---|---|---|---| | Auth provider and identity kinds | `kind` | `factory` kinds | Error | -| Component types | `type` | [`component.Register`](../../../../pkg/component/registry.go) plus legacy terraform, helmfile, packer | Error | +| Component types | `type` | [`component.Register`](../../../../pkg/component/registry.go) including Terraform, plus legacy helmfile and packer | Error | | Command names | top-level name and aliases | [`internal.Register`](../../../../cmd/internal/registry.go) and YAML `commands:` | Shortcut unavailable; `atmos plugin run ` still works | | Hook kinds | `kind` | [`hooks.RegisterKind`](../../../../pkg/hooks/kind.go) | Error | | Step types and aliases | `type`, `aliases` (one namespace) | [`step.Register`](../../../../pkg/runner/step/registry.go) | Error | @@ -373,7 +373,8 @@ An adapter implements one existing Atmos interface by calling a wire service, so | Extension | Adapter (`pkg/plugin/adapter/...`) | Existing interface implemented | Wire service | API services | Must not cross | Phase 0 | |---|---|---|---|---|---|---| | Auth | `auth.ProviderAdapter`, `auth.IdentityAdapter` | [`types.Provider`](../../../../pkg/auth/types/interfaces.go): `Kind`, `Name`, `PreAuthenticate`, `Authenticate`, `Validate`, `Environment`, `Paths`, `PrepareEnvironment`, `Logout`, `GetFilesDisplayPath`, `SetRealm`. `types.Identity`: `Kind`, `GetProviderName`, `Authenticate`, `Validate`, `Environment`, `Paths`, `PrepareEnvironment`, `PostAuthenticate`, `Logout`, `CredentialsExist`, `LoadCredentials`, `SetRealm`. Optional `Provisioner`, `JustificationConsumer`. | `AuthProvider`, `AuthIdentity` | `api.Credentials`, `api.Prompt`, `api.Browser`, `api.HTTP`, `api.Log` | `types.AuthManager`, `*schema.Provider`, `*schema.Identity`, `*schema.AuthContext` | P0.1, P0.2 | -| Component | `component.Adapter` | [`ComponentProvider`](../../../../pkg/component/provider.go): `GetType`, `GetGroup`, `GetBasePath`, `ListComponents`, `ValidateComponent`, `Execute`, `GenerateArtifacts`, `GetAvailableCommands`. | `Component` | `api.Process`, `api.Toolchain`, `api.FS`, `api.Credentials`, `api.Log` | `ExecutionContext` struct, `*schema.AtmosConfiguration`, `schema.ConfigAndStacksInfo` | P0.3, P0.6 | +| Component configuration | `component.Provider`, private `component.Registry` | Planned discovery, validation, preparation, and state clients; composition with existing engines remains required. | `ComponentDiscovery`, `ComponentValidation`, `ComponentPreparation`, `ComponentState` | No process, filesystem, network, or private-credential grants yet. | `ExecutionContext`, `*schema.AtmosConfiguration`, `schema.ConfigAndStacksInfo` | P0.3, P0.6 | +| Component execution (planned) | Engine adapter | [`ComponentProvider`](../../../../pkg/component/provider.go); external engines must not replace built-in providers. | Optional execution contract | Scoped process, toolchain, filesystem, credentials, and logging services | Host execution structs and callbacks | P0.3, P0.6 | | CI provider | `ci.Adapter` | [`provider.Provider`](../../../../pkg/ci/internal/provider/types.go): `Name`, `Detect`, `Context`, `GetStatus`, `CreateCheckRun`, `UpdateCheckRun`, `PostComment`, `OutputWriter`, `ResolveBase`. Optional `LabelReader`, `DebugModeDetector`, `LogGrouper`. | `CIProvider` | `api.HTTP`, `api.Secrets`, `api.Log` | `OutputWriter`, `io.Writer` | P0.3, P0.7 | | Component CI | `cicomponent.Adapter` | [`plugin.Plugin`](../../../../pkg/ci/internal/plugin/types.go): `GetType`, `GetHookBindings` (`HookBinding{Event, Handler}`). | `CIComponent` | `api.CIReporter`, `api.FS`, `api.Log` | `HookHandler` func, `HookContext.Provider`, `TemplateLoader`, `CreatePlanfileStore` | P0.5 | | Command | `command.Adapter` (lives behind `cmd/plugin`) | [`internal.CommandProvider`](../../../../cmd/internal/command.go): `GetCommand`, `GetName`, `GetGroup`, `GetFlagsBuilder`, `GetPositionalArgsBuilder`, `GetCompatibilityFlags`, `GetAliases`, `IsExperimental`. | `Commands.Run` for native plugins; in-process binding proposed for Starlark | Native command bootstrap currently grants `api.HTTP`; output uses RPC events, not inherited stdio | `*cobra.Command` | P0.3 | @@ -392,7 +393,11 @@ An adapter implements one existing Atmos interface by calling a wire service, so ### Component -`ExecutionContext` is flattened into an `ExecuteRequest`: component and stack names, resolved `ComponentConfig` map, command, subcommand, args, flags, working directory, and a scoped environment. Output is not written to `io.Writer`s; the plugin streams output chunks that Atmos writes through its own masked writers. `GenerateArtifacts` returns file contents and relative paths, and Atmos writes them, so a plugin cannot write outside the working-directory grant. `ListComponents` and `ValidateComponent` run from manifest-declared schemas where possible and call the plugin only when the manifest declares dynamic discovery. +Separate configuration providers from component types and execution engines. `ComponentDiscovery`, `ComponentValidation`, `ComponentPreparation`, and `ComponentState` are independent versioned services. Native Terragrunt configuration composes with the registered Terraform provider; it does not replace it. These services, SDK and runtime support, package activation, and host adapters are planned. + +Requests flatten host context into explicit invocation, coordinates, source/root, operation/options, effects, and typed configuration. `ComponentValue` preserves cty types, exact numbers, unknowns, nulls, and sensitivity. Generated files return literal bytes and ownership policies for host validation. State resolution is separate from preparation. Optional execution must stream through masked host I/O and provide an authoritative outcome. + +Use the [component plugin PRD](../extensions/component-engines.md) for static flag metadata, collision rules, discovery/import integration, host service grants, and acceptance. The current protocol does not grant filesystem or process access merely by declaring an effect policy. ### CI provider and component CI diff --git a/docs/prd/plugins/platform/protocol.md b/docs/prd/plugins/platform/protocol.md index f6641986ff3..3a1c8784a76 100644 --- a/docs/prd/plugins/platform/protocol.md +++ b/docs/prd/plugins/platform/protocol.md @@ -254,29 +254,22 @@ Atmos selects the command export from manifest metadata before starting the nati The runtime writes events to caller-provided stdout/stderr writers; callers supply masked writers for terminal output. The plugin process's own stdout is reserved for the go-plugin transport, not command data. Cancellation and deadlines follow the native invocation lifecycle. There is no stdin stream, inherited interactive terminal, or TTY negotiation in this contract. The current command bootstrap starts the process in the package directory, leaves `Invocation.working_directory` unset, and grants `api.HTTP` with no auth provider's private credential namespace. -### Component +### Component configuration services -```proto -service Component { - rpc Describe(ComponentDescribeRequest) returns (ComponentDescription); rpc ListComponents(ListComponentsRequest) returns (ListComponentsResponse); - rpc Validate(ComponentRequest) returns (ValidateResponse); rpc GenerateArtifacts(ComponentRequest) returns (GenerateArtifactsResponse); - rpc Execute(ComponentRequest) returns (stream ExecuteEvent); rpc Events(ComponentRequest) returns (EventsResponse); -} -message ComponentDescription { string type = 1; string group = 2; string base_path = 3; repeated CommandInfo commands = 4; string schema = 5; } -message ComponentRequest { Invocation invocation = 1; string command = 2; string subcommand = 3; google.protobuf.Struct component_config = 4; - repeated string args = 5; google.protobuf.Struct flags = 6; } -message ListComponentsRequest { Invocation invocation = 1; string stack = 2; google.protobuf.Struct stack_config = 3; } -message ExecuteEvent { oneof event { OutputChunk chunk = 1; LifecycleEvent lifecycle = 2; OperationOutcome outcome = 3; } } -``` +The component configuration protocol, generated CLI and SDK bindings, and component-specific host descriptors, package loading, and adapters are planned. CLI stack and execution integration are also unavailable. These proposed independently negotiated version-1 services replace the earlier proposed monolithic `Component` service: -`Execute` streams `OutputChunk` and optional `LifecycleEvent` messages, then exactly one `OperationOutcome`. The engine declares which statuses mean success, so a nonzero tool exit can be `CHANGES`. `Events` returns the lifecycle events a command emits so Atmos can validate hook bindings. `GenerateArtifacts` returns `ArtifactRef` entries. +| Capability | RPC | Result | +|---|---|---| +| `component.discovery` | `ComponentDiscovery.Discover` | Logical instances, source watches, and dependency edges for an explicit root/revision. | +| `component.validation` | `ComponentValidation.Validate` | Unsupported mode/command/options diagnostics before effects. | +| `component.preparation` | `ComponentPreparation.Prepare` | Fresh inputs, module, backend identity, and literal generated files. | +| `component.state` | `ComponentState.Resolve` | Backend/workspace identity without preparation. | -| RPC | Replaces on `component.ComponentProvider` | -|---|---| -| `Describe` | `GetType`, `GetGroup`, `GetAvailableCommands`, `GetBasePath` (relative to the Atmos base path). | -| `ListComponents`, `Validate`, `GenerateArtifacts`, `Execute` | `ListComponents`, `ValidateComponent`, `GenerateArtifacts`, `Execute`. | +`ComponentRequest` carries explicit invocation, provider, coordinates, root/source, operation/options, effects, resolved component configuration, and separate provider settings. It does not carry host Go structs or callbacks. `ComponentValue` uses cty type JSON plus MessagePack and sensitivity paths, preserving exact numbers, unknowns, nulls, collection types, and sensitive values. Do not use `protobuf.Struct` for these values. The SDK rejects unsupported marks and sensitivity paths that cannot be applied. + +Configuration providers do not replace execution engines. Native Terragrunt units remain Terraform components executed by Atmos. Optional external execution and component CI services remain follow-on contracts: they must stream output, diagnostics/logs, lifecycle events, then exactly one authoritative outcome. Preserve detailed plan exit 2 as changes. Saved-plan identity must bind the exact applied artifact and effective configuration. -`ExecutionContext.AtmosConfig` and `ConfigAndStacksInfo` become `Invocation` plus `ComponentRequest`: resolved values, stack coordinates, working directory, scoped environment. +Static command/flag metadata, effect enforcement, host services, and CLI activation remain required. See the [component plugin PRD](../extensions/component-engines.md) for ownership, integration points, and acceptance. Protocol availability alone does not mean an installed component plugin works. ### CIProvider diff --git a/docs/prd/terragrunt-support.md b/docs/prd/terragrunt-support.md new file mode 100644 index 00000000000..2be1fbad38e --- /dev/null +++ b/docs/prd/terragrunt-support.md @@ -0,0 +1,189 @@ +# Terragrunt component plugin + +**Status:** Implementation in progress. CLI discovery and native execution are wired to the SDK plugin on this branch. Validation covers local modules and local state. The plugin is not published. The requirements below define the complete target, not a compatibility claim. + +**Decision:** Ship Terragrunt as a separately versioned native plugin. Use Terragrunt Go modules to interpret HCL and Atmos to execute native Terraform components. Provide executable mode as a separate compatibility path. + +This carries forward the checkpointed pilot. The [component plugin PRD](plugins/extensions/component-engines.md) defines shared contracts. Keep pilot evidence in engineering reports, not in the migration guide. + +## User outcome + +Users retain Terragrunt units, explicit stacks, HCL, module sources, and state. With the plugin installed and discovery enabled, they can list instances and run Atmos without generating YAML for every unit. Conversion to native Atmos YAML remains optional. + +Native mode can use Terragrunt Go dependencies but must not require its executable. Do not translate Terragrunt functions into Atmos functions or maintain another evaluator. + +Do not infer a compatibility percentage from block or fixture counts. Measure unchanged adoption against a documented repository corpus and pinned Terragrunt version. Report language evaluation, execution semantics, and Atmos integration separately. + +## Modes + +| Mode | Configuration | Executor | Scheduling | +|---|---|---|---| +| Native, primary | Terragrunt Go evaluator in the plugin. | Atmos Terraform/OpenTofu engine. | Atmos selection and dependency graph. | +| Terragrunt executable, optional | Terragrunt. | Explicitly configured Terragrunt binary. | One declared owner; never nest all-unit Terragrunt scheduling inside per-unit Atmos scheduling. | + +Reject unsupported native behavior before execution. Never fall back silently. Test executable mode separately; passing it does not prove native compatibility. + +## Reuse existing capabilities + +| Need | Reuse | Preserve or add | +|---|---|---| +| Execution | `pkg/component/terraform`, registry, lifecycle. | Per-operation plugin preparation; no global override. | +| Generated files | `pkg/terraform/generate`. | Literal bytes, ownership signatures, `if_exists`, disable behavior. | +| Sources | `pkg/provisioner/source`, vendoring, toolchain. | Upstream path/auth semantics and cache state safety. | +| Dependencies | Dependency graph, bulk executor, deferred values. | Ordering/output edges and fresh outputs per operation. | +| State/output | Native state reader and Terraform output handling. | Plugin backend/workspace identity; test both functions. | +| Authentication | Scoped identities and environments. | Equivalent upstream cloud and credential semantics. | +| Hooks, mocks, retries | Existing lifecycle and services. | Terragrunt ordering, allowlists, merge rules, errors. | +| Discovery/affected | Resolved stack map, naming, watches, dependency expansion. | HCL/stack provenance for both revisions. | +| UI/reporting | Masked I/O, `AtmosLogger`, `ui.*`, Terraform CI. | HCL diagnostics and consistent instance/engine identity. | +| Delivery | Merged SDK, OCI, locks, policy, local builds, catalog. | Component capabilities and adapters. | + +Generation, locals, hooks, mocks, retries, and caching already exist. Matching feature names do not establish matching semantics. + +## Explicit configuration + +Use a structured section, not a boolean compatibility list. Target configuration: + +```yaml +components: + terraform: + terragrunt: + enabled: true + mode: native + discovery: + enabled: true + included_paths: [live/**] + excluded_paths: [vendor/**] +``` + +Enablement and automatic discovery default to false. Installation alone starts no process and scans no monorepo. Scope traversal to literal prefixes of included paths; prune exclusions and caches. Allow explicit `terragrunt://` imports without automatic discovery. + +Provide settings for the executable/version in executable mode, project/discovery paths, cache location, and supported provider options. Keep package source/version/path in plugin package configuration. Native Terraform/OpenTofu selection stays with Atmos toolchain configuration. Resolve paths against the Atmos base directory. Document schema and precedence; preserve explicit false and empty values. + +## Discovery and inventory + +- Discover standalone units and explicit `terragrunt.stack.hcl` definitions, including nested stacks and values. +- Expand stacks without treating generated cache directories as separate projects. +- Dispatch `terragrunt://` through a generic import interface. Share the evaluator with discovery. +- Add definitions before commands require YAML stacks. Reuse naming precedence and path fallback. +- Keep logical coordinates stable across imports, discovery, generated directories, and machines. +- Deduplicate imports/discovery; fail on conflicting definitions, including YAML collisions. +- Leave source configuration and state unchanged during listing. Report denied effects. + +Imported units have type `terraform`. Native mode reports engine `terraform` or `opentofu`; executable mode reports `terragrunt`. Do not infer the engine from HCL. + +The default instance table shows `Component`, `Stack`, `Type`, `Engine`. Expose type/engine in structured output and custom columns. Preserve user column overrides. + +Lists, describe, selection, affected, execution, and Pro must use the same resolved instances. No separate Terragrunt inventory is acceptable. + +## Native evaluation and preparation + +- Reuse Terragrunt functions, includes, locals, and merge behavior through its Go evaluator. +- Preserve exact cty values, unknowns, sensitivity, and generated bytes across the wire. +- Discover graphs without dependency state or mocks. Reevaluate selected units before execution with the real command and fresh outputs. +- Map `dependencies.paths` to ordering edges. Map enabled `dependency` blocks to ordering and output edges. +- Preserve mock command allowlists and merge rules. Report dependencies outside adoption scope until explicit mapping exists. +- Use Atmos forward order for apply and reverse order for destroy. +- Resolve sources and materialize declared backends even when Atmos backend generation is disabled. +- Preserve relative paths, upstream working-directory functions, workspace identity, and inputs. +- Check Terragrunt and Terraform/OpenTofu version constraints before execution. +- Apply the exact reviewed saved plan. Bind provider/options/source/state identity to the artifact. +- Ordinary plan exit 0 is success without an asserted change count. Detailed plan exit 2 means changes, not failure. + +Discovery defaults to denying commands, network, secret decryption, writes, and state reads. Fail with the source when an effect is required. Do not omit units or substitute empty values. Stack-generation writes are limited to their declared workspace and do not authorize hooks or infrastructure operations. + +Native plugins are trusted code. Broker grants and evaluator controls are not an OS sandbox. Test actual effect interception through nested includes and stack expansion. + +## Atmos execution contracts + +Treat environment, identity, toolchain, and workdir behavior as release gates. + +- Resolve the selected Atmos identity before native preparation can read outputs, decrypt data, or run commands. Honor explicit, default, and disabled identity selection. Stop on authentication failure. +- Pass the sanitized subprocess environment to the evaluator. Do not merge ambient credentials back into it. Distinguish a missing environment from an explicitly empty environment. +- Use the same component environment, toolchain PATH, managed Terraform RC, registry trust, and cache policy for preparation and Terraform execution. Preserve empty overrides and last-value precedence. +- Pass an absolute executable path after toolchain resolution. HCL commands must use the invocation PATH. Never mutate the plugin process environment to switch identities or tools. +- Resolve explicit relative HCL commands from their Terragrunt unit directory. Keep `get_terragrunt_dir()` independent of the Atmos execution workdir. +- Keep credentials and temporary RC files scoped to the operation. Clean temporary files after preparation, provisioning, or execution fails. +- Test two identities and two tool versions in consecutive and concurrent invocations. Test cancellation and auth/tool failures before evaluator effects. +- Preserve configured workdir and source settings when replacing the evaluated module URI. Place generated files, variable files, and plans in the resolved Atmos working directory. +- Isolate discovered instances by stack and component. Do not let two stacks overwrite the same module's generated backend. +- Keep state identity independent of execution workdir. Relative Terragrunt backend paths must retain their upstream base. Workdir refresh must not move, delete, or recreate state. +- Test ordinary placement, automatic workdirs, absolute and project-relative working directory overrides, repeated runs, and conflicting directory ownership. Check both generated bytes and the backend used by Terraform. A rejected second owner must leave the first owner's artifacts and state unchanged. + +Read-only discovery precedes component authentication. If discovery requires an identity-dependent effect, report the unsupported requirement. Do not use ambient credentials as an implicit substitute for the selected identity. + +## State and cache ownership + +Resolve state independently of preparation. `!terraform.state` must work before a Terraform command runs for the target. Do not run hooks, write files, or read unrelated outputs to locate state. Reject backend expressions requiring those effects. + +Reuse Atmos state reading, sensitivity, and invalidation after successful upstream operations. Test `!terraform.output` separately. Preserve default/named workspace paths and freshness across mixed YAML/HCL updates. + +`.terragrunt-cache` can contain local state. Distinguish state, downloaded modules, output-read scratch directories, and generated `.terragrunt-stack` units. Adoption and refresh must not move/delete state. Preserve `get_working_dir()` or reject the affected configuration. + +The pilot found a separate named-workspace dependency gap: Terragrunt output reads can require its initialized backend directory while Atmos initializes another. Resolve this before accepting those graphs. Standalone workspace handoff and state-function tests do not prove HCL-to-HCL dependency behavior. + +## Affected detection + +`describe affected` and `terraform --affected` must share identities and source relationships. + +The current native implementation creates separate discovery sessions for HEAD and BASE. It retains the invocation's trusted plugin, scope, environment, and cancellation. It restores HEAD before dependent expansion and execution. Acceptance covers local HCL and module watches, nested stack provenance, changed dependency targets, additions, removals, renames, mixed YAML/HCL graphs, stack filters, and dependent depth. Reads in unevaluated input expressions and arbitrary dynamic glob membership remain open requirements below. + +- Track primary HCL, includes, cross-directory reads, `file`, `mark_as_read`, modules, and optional reads. +- Watch directories and glob membership to detect new/deleted files. +- Track root/nested stacks, values, catalog sources, generated origins, and changed remote references. +- Distinguish file watches, output edges, and ordering edges. Define dependent expansion. +- Evaluate each revision in its own root. Do not read current HCL or live state for an older revision. +- Test additions, removals, renames, globs, filters, mixed graphs, and selection reasons. +- Preserve `--include-dependents` and existing stack/component filters. + +Hierarchy helps provenance but does not prove dependencies stay within an ancestor subtree. + +## Flags, I/O, and UI + +Use shared static metadata and the unified Atmos flag parser. Declare modes, commands, types, defaults, aliases, sensitivity, and deprecations. Separate provider options from Terraform argv. Validate unsupported options before effects. Bind discovery-relevant options to graph/cache/plan identity. + +Atmos owns selection, `--affected`, `--ui`, and native scheduling. Explicit executable passthrough must reject ownership conflicts. Never silently forward flags or fall back. + +Preserve stdout, stderr, logs, diagnostics, lifecycle, and outcomes as distinct channels. Stream while running. Adapt Terragrunt logs through `AtmosLogger`, with explicit severity conversion, timestamp, source, stack, and component. Mask before presentation. + +Optionally map selected known messages to `ui.*`; unmatched messages stay in normal logging. Logs never establish operation success. Define stdin, TTY, backpressure, cancellation, deadlines, and crashes. Preserve native Terraform `--ui`. Never automatically replay apply after a lost connection. + +## Compatibility gaps requiring acceptance + +| Area | Requirement | +|---|---| +| Public Go API | Upstream public environment/stack APIs or an explicitly maintained bridge. The pilot facade wraps internal APIs and is not directly importable from an independent module. | +| GCS backend creation | Equivalent provisioning or an existing bucket. S3/Azure support does not prove GCS parity. | +| Generate ownership | Signatures, overwrite/skip/error policies, and disable behavior. | +| `errors.ignore` and signals | Define/test native semantics; generic retries are insufficient. | +| `prevent_destroy`, exclusions, feature flags | Enforce before effects or reject. | +| Hooks and extra arguments | Preserve order, command, environment, failure behavior, and paths. | +| Remote sources/cloud state | Real source/auth/backend tests before parity claims. | +| SOPS/credential-dependent evaluation | Phase-specific effects and scoped credentials. | +| Named-workspace dependencies | Resolve the separate directory/backend gap. | +| Explicit stacks | Remote catalogs, values, generation policies, nested provenance. | +| UI/platforms | Masking, cancellation, terminal and path tests on Linux/macOS/Windows. | + +## Pro and CI + +Reuse Terraform CI for native units. Preserve inherited Pro settings and enablement. Test inventory/affected uploads, locking, drift, and runner plan/apply with the same artifact. Restore pinned plugins, HCL, sources, and scoped credentials on the runner. + +Local contracts do not establish live Pro compatibility. Test the configured end-to-end flow before claiming it. External engine reporting needs separate outcomes and adapters. + +## Optional setup and migration guide + +Provide `atmos migrate terragrunt ` for custom grouping/scope. Discovery must not require it. Accept directories, units, and explicit stacks. Preview writes/installs nothing. Preserve existing Atmos configuration, allow a stack name, detect collisions, and make repeated setup a no-op. Import stacks once, not also their generated units. + +Setup must not evaluate HCL functions, run hooks, read outputs, generate stacks, or run plan/apply/destroy. Mark evaluation-dependent checks pending and print describe/plan commands. Do not silently widen scope for dependencies. + +Write the migration guide for the completed experience: adopt HCL unchanged through the plugin, or optionally convert to Atmos YAML. Present executable mode separately. Keep pilot results/status in engineering docs. Verify state identity and a plan before apply. + +## Delivery gates + +1. Complete independent contracts, typed values, static metadata, flags, and host adapters. Preserve existing plugins and built-ins. +2. Run the advanced native fixture through an installed SDK plugin and stock Atmos, without temporary host patches. +3. Extend acceptance to nested stacks, discovery, mixed YAML/HCL, state functions, affected, saved plans, reverse destroy, cancellation, and crashes. +4. Port field regressions into the default runner. Add differential and explicit unsupported-behavior tests. +5. Verify Pro/CI, supported platforms, and release installation. Require at least 90% patch coverage per layer. + +Do not report completion while host wiring, compatibility tests, or required product flows remain unfinished. diff --git a/errors/errors.go b/errors/errors.go index cc179229494..a50acdeeff5 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -317,6 +317,7 @@ var ( ErrS3BucketAccessDenied = errors.New("access denied to S3 bucket") ErrInvalidSSECustomerKey = errors.New("invalid SSE-C customer encryption key") ErrCreateGCSClient = errors.New("failed to create GCS client") + ErrGCSIdentityTokenExpired = errors.New("GCP identity access token is expired and the identity has no credentials file that can refresh it") ErrGetObjectFromGCS = errors.New("failed to get object from GCS") ErrReadGCSObjectBody = errors.New("failed to read GCS object body") ErrGCSBucketRequired = errors.New("bucket is required for gcs backend") diff --git a/internal/exec/describe_stacks_static.go b/internal/exec/describe_stacks_static.go index fda3ddbfa2d..fdb382cb727 100644 --- a/internal/exec/describe_stacks_static.go +++ b/internal/exec/describe_stacks_static.go @@ -22,7 +22,20 @@ func findDescribeStacksMap(config *schema.AtmosConfiguration, stack string, igno _, stacks, raw, deferred, err := processYAMLConfigFilesWithOptions(config, config.StacksBaseAbsolutePath, config.TerraformDirAbsolutePath, config.HelmfileDirAbsolutePath, config.PackerDirAbsolutePath, config.AnsibleDirAbsolutePath, files, false, true, ignoreMissing, &StackLoadOptions{SkipTemplates: true, StaticOnly: true}) - return stacks, raw, deferred, err + if err != nil { + return nil, nil, nil, err + } + if err := mergeStaticStacks(config, stacks, raw, deferred); err != nil { + return nil, nil, nil, err + } + return stacks, raw, deferred, nil +} + +// mergeStaticStacks adds stacks that come from outside YAML manifests, such as provider +// discovery, to static inspection. Sources must contribute literal data only: static +// inspection never authenticates or renders templates. YAML-only builds leave it a no-op. +var mergeStaticStacks = func(*schema.AtmosConfiguration, map[string]any, map[string]map[string]any, AllStacksDeferredContexts) error { + return nil } func (p *describeStacksProcessor) resolveStackName(file, manifest string, info schema.ConfigAndStacksInfo, vars map[string]any) (string, schema.Context, error) { //nolint:gocritic // Mirrors the existing read-only name resolver. diff --git a/internal/exec/describe_stacks_static_test.go b/internal/exec/describe_stacks_static_test.go index 8f8e70e3ff2..7060ff89225 100644 --- a/internal/exec/describe_stacks_static_test.go +++ b/internal/exec/describe_stacks_static_test.go @@ -224,3 +224,28 @@ func TestDescribeStacksStaticEmptyStacks(t *testing.T) { }) } } + +var errStaticMerge = errors.New("static merge failed") + +func TestFindDescribeStacksMapStaticMergesExtraStacks(t *testing.T) { + root := t.TempDir() + config := &schema.AtmosConfiguration{BasePath: root, BasePathAbsolute: root, StacksBaseAbsolutePath: filepath.Join(root, "stacks")} + original := mergeStaticStacks + t.Cleanup(func() { mergeStaticStacks = original }) + + mergeStaticStacks = func(_ *schema.AtmosConfiguration, stacks map[string]any, raw map[string]map[string]any, _ AllStacksDeferredContexts) error { + stacks["extra"] = map[string]any{"components": map[string]any{}} + raw["extra"] = map[string]any{"name": "extra"} + return nil + } + stacks, raw, _, err := findDescribeStacksMap(config, "extra", false, true) + require.NoError(t, err) + require.Contains(t, stacks, "extra") + require.Equal(t, "extra", raw["extra"]["name"]) + + mergeStaticStacks = func(*schema.AtmosConfiguration, map[string]any, map[string]map[string]any, AllStacksDeferredContexts) error { + return errStaticMerge + } + _, _, _, err = findDescribeStacksMap(config, "", false, true) + require.ErrorIs(t, err, errStaticMerge) +} diff --git a/internal/terraform_backend/terraform_backend_gcs.go b/internal/terraform_backend/terraform_backend_gcs.go index 32c06c1f570..5e3c83b2853 100644 --- a/internal/terraform_backend/terraform_backend_gcs.go +++ b/internal/terraform_backend/terraform_backend_gcs.go @@ -4,9 +4,11 @@ import ( "context" "crypto/sha256" "encoding/hex" + "encoding/json" "errors" "fmt" "io" + "os" "path" "strings" "sync" @@ -109,13 +111,12 @@ func getCachedGCSClient(backend *map[string]any, authContext *schema.AuthContext // Create a proper hash to avoid collisions. Append the identity endpoint only when // present so an emulator-backed read and a real-GCS read (same credentials) never // alias to the same cached client; the real-cloud key format is preserved unchanged. - h := sha256.Sum256([]byte(credentials)) - cacheKey := fmt.Sprintf("credentials_hash=%s;impersonate=%s", - hex.EncodeToString(h[:8]), // Use first 8 bytes for brevity. - impersonateServiceAccount) - if endpoint != "" { - cacheKey += ";endpoint=" + endpoint + opts, err := authOptions(credentials, authContext) + if err != nil && endpoint == "" { + // An emulator-backed read never authenticates, so only a real-GCS read must fail here. + return nil, err } + cacheKey := gcsClientCacheKey(opts, impersonateServiceAccount, endpoint) // Check the cache. if cached, ok := gcsClientCache.Load(cacheKey); ok { @@ -136,6 +137,78 @@ func getCachedGCSClient(backend *map[string]any, authContext *schema.AuthContext return gcsClient, nil } +// gcsAuthOptions selects the credentials the GCS reader authenticates with. Explicit backend +// credentials win. Otherwise the target identity's unexpired access token is used, then its +// credentials file, so a state read uses the target's credentials instead of the ambient ones. +// Nothing is read from or written to the process environment. An expired identity token is an +// error unless the credentials file can mint a new token on its own (service account key, +// external account, or a refresh token), so the read never runs as the ambient identity or with +// a file that only replays the expired token. +func gcsAuthOptions(credentials string, authContext *schema.AuthContext, now time.Time) (gcp.AuthOptions, error) { + opts := gcp.AuthOptions{Credentials: credentials} + if credentials != "" || authContext == nil || authContext.GCP == nil { + return opts, nil + } + identity := authContext.GCP + if identity.AccessToken != "" { + if identity.TokenExpiry.IsZero() || identity.TokenExpiry.After(now) { + opts.AccessToken, opts.TokenExpiry = identity.AccessToken, identity.TokenExpiry + return opts, nil + } + if !gcsCredentialsFileCanRefresh(identity.CredentialsFile) { + return gcp.AuthOptions{}, fmt.Errorf("%w: expired at %s", errUtils.ErrGCSIdentityTokenExpired, identity.TokenExpiry.Format(time.RFC3339)) + } + } + opts.Credentials = identity.CredentialsFile + return opts, nil +} + +// gcsCredentialsFileCanRefresh reports whether the credentials file can obtain a fresh access +// token without the identity's stored one. An unreadable or unrecognized file cannot. +func gcsCredentialsFileCanRefresh(path string) bool { + if path == "" { + return false + } + data, err := os.ReadFile(path) + if err != nil { + return false + } + var content struct { + Type string `json:"type"` + RefreshToken string `json:"refresh_token"` + } + if err := json.Unmarshal(data, &content); err != nil { + return false + } + switch content.Type { + case "service_account", "external_account", "impersonated_service_account": + return true + } + return content.RefreshToken != "" +} + +// gcsClientCacheKey builds a deterministic cache key. Credentials and tokens are hashed so no +// secret appears in the key, and the token key part is added only when a token is in use so the +// real-cloud key format is preserved unchanged. +func gcsClientCacheKey(opts *gcp.AuthOptions, impersonateServiceAccount, endpoint string) string { + h := sha256.Sum256([]byte(opts.Credentials)) + key := fmt.Sprintf("credentials_hash=%s;impersonate=%s", hex.EncodeToString(h[:8]), impersonateServiceAccount) + if opts.AccessToken != "" { + t := sha256.Sum256([]byte(opts.AccessToken)) + key += ";token_hash=" + hex.EncodeToString(t[:8]) + } + if endpoint != "" { + key += ";endpoint=" + endpoint + } + return key +} + +// authOptions resolves the effective credentials at the current time. +func authOptions(credentials string, authContext *schema.AuthContext) (*gcp.AuthOptions, error) { + opts, err := gcsAuthOptions(credentials, authContext, time.Now()) + return &opts, err +} + // identityGCSEndpoint returns the active GCP identity's GCS emulator endpoint // (STORAGE_EMULATOR_HOST), or "" when no identity endpoint is set. GCP is per-service, // so only the storage endpoint is relevant to the GCS state backend reader. @@ -204,7 +277,11 @@ func createGCSClient(ctx context.Context, backend *map[string]any, authContext * opts = append(opts, option.WithoutAuthentication()) } else { // Use unified GCP authentication. - opts = gcp.GetClientOptions(gcp.AuthOptions{Credentials: credentials}) + authOpts, err := gcsAuthOptions(credentials, authContext, time.Now()) + if err != nil { + return nil, err + } + opts = gcp.GetClientOptions(authOpts) } if credentials != "" { diff --git a/internal/terraform_backend/terraform_backend_identity_endpoint_test.go b/internal/terraform_backend/terraform_backend_identity_endpoint_test.go index d46da0a13c9..066f68634a9 100644 --- a/internal/terraform_backend/terraform_backend_identity_endpoint_test.go +++ b/internal/terraform_backend/terraform_backend_identity_endpoint_test.go @@ -6,13 +6,18 @@ import ( "encoding/hex" "net/http" "net/http/httptest" + "os" + "path/filepath" "strings" "testing" + "time" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/internal/gcp" "github.com/cloudposse/atmos/pkg/schema" ) @@ -144,3 +149,159 @@ func TestGetCachedAzureBlobClient_ConnectionStringIsolatesCache(t *testing.T) { assert.Same(t, got2, cachedVal.(AzureBlobAPI), "value stored in cache must be the same object returned by getCachedAzureBlobClient") } + +// TestGCSAuthOptions verifies the GCS reader authenticates with the target identity's +// credentials, and never silently falls back to ambient credentials in their place. +func TestGCSAuthOptions(t *testing.T) { + now := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) + identity := func(g *schema.GCPAuthContext) *schema.AuthContext { return &schema.AuthContext{GCP: g} } + credsFile := filepath.Join(t.TempDir(), "target-credentials.json") + refreshableFile := writeGCSCredsFile(t, `{"type":"service_account"}`) + + tests := []struct { + name string + credentials string + authContext *schema.AuthContext + want gcp.AuthOptions + }{ + {name: "no identity uses ambient", want: gcp.AuthOptions{}}, + {name: "nil gcp identity uses ambient", authContext: &schema.AuthContext{}, want: gcp.AuthOptions{}}, + { + name: "explicit backend credentials take precedence over the identity", + credentials: "/backend/creds.json", + authContext: identity(&schema.GCPAuthContext{AccessToken: "target-token", CredentialsFile: credsFile}), + want: gcp.AuthOptions{Credentials: "/backend/creds.json"}, + }, + { + name: "identity access token is used", + authContext: identity(&schema.GCPAuthContext{AccessToken: "target-token", TokenExpiry: now.Add(time.Hour), CredentialsFile: credsFile}), + want: gcp.AuthOptions{AccessToken: "target-token", TokenExpiry: now.Add(time.Hour)}, + }, + { + name: "identity token without expiry is used", + authContext: identity(&schema.GCPAuthContext{AccessToken: "target-token"}), + want: gcp.AuthOptions{AccessToken: "target-token"}, + }, + { + name: "expired identity token falls back to a refreshable credentials file", + authContext: identity(&schema.GCPAuthContext{AccessToken: "stale", TokenExpiry: now.Add(-time.Minute), CredentialsFile: refreshableFile}), + want: gcp.AuthOptions{Credentials: refreshableFile}, + }, + { + name: "identity credentials file is used without a token", + authContext: identity(&schema.GCPAuthContext{CredentialsFile: credsFile}), + want: gcp.AuthOptions{Credentials: credsFile}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := gcsAuthOptions(tt.credentials, tt.authContext, now) + require.NoError(t, err) + assert.Equal(t, tt.want, got) + }) + } +} + +// writeGCSCredsFile writes a credentials file with the given JSON content and returns its path. +func writeGCSCredsFile(t *testing.T, content string) string { + t.Helper() + path := filepath.Join(t.TempDir(), "creds.json") + require.NoError(t, os.WriteFile(path, []byte(content), 0o600)) + return path +} + +// TestGCSAuthOptions_ExpiredTokenWithUnrefreshableCredentialsFileFails verifies an expired token +// is an error when the credentials file only replays that token, and that refreshable files are kept. +func TestGCSAuthOptions_ExpiredTokenWithUnrefreshableCredentialsFileFails(t *testing.T) { + now := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) + expired := func(file string) *schema.AuthContext { + return &schema.AuthContext{GCP: &schema.GCPAuthContext{AccessToken: "stale", TokenExpiry: now.Add(-time.Minute), CredentialsFile: file}} + } + + tests := []struct { + name string + file string + wantErr bool + }{ + {name: "authorized_user with only an access token", file: writeGCSCredsFile(t, `{"type":"authorized_user","access_token":"stale","client_id":"c","client_secret":"s"}`), wantErr: true}, + {name: "missing file", file: filepath.Join(t.TempDir(), "missing.json"), wantErr: true}, + {name: "malformed file", file: writeGCSCredsFile(t, `not json`), wantErr: true}, + {name: "authorized_user with a refresh token", file: writeGCSCredsFile(t, `{"type":"authorized_user","refresh_token":"r"}`)}, + {name: "service account key", file: writeGCSCredsFile(t, `{"type":"service_account"}`)}, + {name: "external account", file: writeGCSCredsFile(t, `{"type":"external_account"}`)}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + opts, err := gcsAuthOptions("", expired(tt.file), now) + if tt.wantErr { + require.ErrorIs(t, err, errUtils.ErrGCSIdentityTokenExpired) + assert.Equal(t, gcp.AuthOptions{}, opts) + return + } + require.NoError(t, err) + assert.Equal(t, gcp.AuthOptions{Credentials: tt.file}, opts) + }) + } + + t.Run("valid token is unaffected by an unrefreshable file", func(t *testing.T) { + file := writeGCSCredsFile(t, `{"type":"authorized_user","access_token":"t"}`) + ac := &schema.AuthContext{GCP: &schema.GCPAuthContext{AccessToken: "t", TokenExpiry: now.Add(time.Hour), CredentialsFile: file}} + opts, err := gcsAuthOptions("", ac, now) + require.NoError(t, err) + assert.Equal(t, gcp.AuthOptions{AccessToken: "t", TokenExpiry: now.Add(time.Hour)}, opts) + }) +} + +// TestGCSAuthOptions_ExpiredTokenWithoutCredentialsFileFails verifies an expired identity token +// with no credentials file is an error rather than empty options that would use ambient credentials. +func TestGCSAuthOptions_ExpiredTokenWithoutCredentialsFileFails(t *testing.T) { + now := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) + ac := &schema.AuthContext{GCP: &schema.GCPAuthContext{AccessToken: "stale", TokenExpiry: now.Add(-time.Minute)}} + + opts, err := gcsAuthOptions("", ac, now) + require.ErrorIs(t, err, errUtils.ErrGCSIdentityTokenExpired) + assert.Equal(t, gcp.AuthOptions{}, opts) + + t.Run("explicit backend credentials still win", func(t *testing.T) { + opts, err := gcsAuthOptions("/backend/creds.json", ac, now) + require.NoError(t, err) + assert.Equal(t, gcp.AuthOptions{Credentials: "/backend/creds.json"}, opts) + }) + + t.Run("client creation fails instead of using ambient credentials", func(t *testing.T) { + client, err := createGCSClient(context.Background(), &map[string]any{}, ac) + require.ErrorIs(t, err, errUtils.ErrGCSIdentityTokenExpired) + assert.Nil(t, client) + + client, err = getCachedGCSClient(&map[string]any{}, ac) + require.ErrorIs(t, err, errUtils.ErrGCSIdentityTokenExpired) + assert.Nil(t, client) + }) + + t.Run("emulator identity is not affected", func(t *testing.T) { + emu := &schema.AuthContext{GCP: &schema.GCPAuthContext{ + AccessToken: "stale", TokenExpiry: now.Add(-time.Minute), StorageEmulatorHost: "localhost:9000", WithoutAuthentication: true, + }} + client, err := createGCSClient(context.Background(), &map[string]any{}, emu) + require.NoError(t, err) + assert.NotNil(t, client) + }) +} + +// TestGCSClientCacheKey_IsolatesTargetCredentials verifies two identities never share a client. +func TestGCSClientCacheKey_IsolatesTargetCredentials(t *testing.T) { + now := time.Now() + key := func(ac *schema.AuthContext) string { + opts, err := gcsAuthOptions("", ac, now) + require.NoError(t, err) + return gcsClientCacheKey(&opts, "", "") + } + ambient := key(nil) + a := key(&schema.AuthContext{GCP: &schema.GCPAuthContext{AccessToken: "token-a"}}) + b := key(&schema.AuthContext{GCP: &schema.GCPAuthContext{AccessToken: "token-b"}}) + file := key(&schema.AuthContext{GCP: &schema.GCPAuthContext{CredentialsFile: filepath.Join(t.TempDir(), "c.json")}}) + + assert.Len(t, map[string]struct{}{ambient: {}, a: {}, b: {}, file: {}}, 4, "ambient and every target identity need distinct keys") + assert.NotContains(t, a, "token-a", "the token must not appear in the cache key") + assert.Equal(t, a, key(&schema.AuthContext{GCP: &schema.GCPAuthContext{AccessToken: "token-a"}}), "the same identity reuses its client") +} diff --git a/pkg/retry/retry.go b/pkg/retry/retry.go index 4240ab9b97a..27892bb5b60 100644 --- a/pkg/retry/retry.go +++ b/pkg/retry/retry.go @@ -8,6 +8,7 @@ import ( "math/rand/v2" "time" + log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/schema" ) @@ -198,11 +199,13 @@ func (e *Executor) ExecuteWithPredicate(ctx context.Context, fn Func, shouldRetr return delayErr } - // Wait for the calculated delay or until context is cancelled + // Start the timer before announcing backoff so observers can synchronize cancellation. + wait := e.clock.After(delay) + log.Debug("Entering retry backoff", "attempt", attempt, "delay", delay) select { case <-ctx.Done(): return fmt.Errorf("context cancelled during retry: %w", ctx.Err()) - case <-e.clock.After(delay): + case <-wait: // Continue to next attempt } } diff --git a/pkg/runner/step/process_cleanup_test.go b/pkg/runner/step/process_cleanup_test.go index 8b04ef23a12..7b6215bd09b 100644 --- a/pkg/runner/step/process_cleanup_test.go +++ b/pkg/runner/step/process_cleanup_test.go @@ -14,9 +14,13 @@ import ( "github.com/cloudposse/atmos/pkg/api" ) -// processTreeTimeout allows both race-instrumented subprocesses to start on a loaded CI runner. -// Cleanup latency is checked separately after the deadline rather than including startup time. -const processTreeTimeout = "30s" +// Each helper re-executes this test binary, including all package initialization under -race. +// Give startup its own budget before the step deadline; a short deadline can kill the helpers +// before they record their pids and never exercise descendant cleanup at all. +const ( + processTreeStartupBudget = 20 * time.Second + processTreeTimeout = 30 * time.Second +) // startedStepHelpers waits until both helpers of the spawn-grandchild tree recorded their pids. func startedStepHelpers(t *testing.T, dir string) (int, int) { @@ -25,7 +29,7 @@ func startedStepHelpers(t *testing.T, dir string) (int, int) { require.Eventually(t, func() bool { parent, child = stepHelperPID(dir, "parent"), stepHelperPID(dir, "child") return parent > 0 && child > 0 - }, 25*time.Second, 25*time.Millisecond, "the helper process tree never started; the step timeout may be too short for this machine") + }, processTreeStartupBudget, 25*time.Millisecond, "the helper process tree never started within its startup budget") return parent, child } @@ -67,7 +71,7 @@ func TestStepTimeoutEndsTheWholeProcessTree(t *testing.T) { killStepHelpersAtCleanup(t, dir) marker := "wp5-orphan-marker-" + time.Now().Format("150405.000000000") fields := tc.fields(marker) - fields["timeout"] = processTreeTimeout + fields["timeout"] = processTreeTimeout.String() fields["env"] = map[string]string{ "_ATMOS_STEP_FAKE": "spawn-grandchild", stepHelperPIDDirEnv: dir, @@ -76,25 +80,32 @@ func TestStepTimeoutEndsTheWholeProcessTree(t *testing.T) { library := NewAutomationLibrary(NewVariables(), nil) ctx, cancel := context.WithCancel(t.Context()) - result := make(chan error, 1) + returnTimer := time.NewTimer(processTreeTimeout + stepTimeoutBudget) + defer returnTimer.Stop() done := make(chan struct{}) - defer func() { - cancel() - <-done - }() + var runErr error go func() { defer close(done) - _, runErr := library.Run(ctx, &api.StepCall{Type: tc.stepType, Configuration: fields, WorkingDirectory: dir}) - result <- runErr + _, runErr = library.Run(ctx, &api.StepCall{Type: tc.stepType, Configuration: fields, WorkingDirectory: dir}) }() - + t.Cleanup(func() { + cancel() + select { + case <-done: + case <-time.After(stepTimeoutBudget): + t.Error("step did not return after cleanup cancellation") + } + }) + + // Observe readiness while the step is alive, rather than waiting for pid files after + // the deadline has already killed a potentially half-initialized process tree. parent, child := startedStepHelpers(t, dir) select { - case runErr := <-result: - t.Fatalf("step ended before the helper tree was observed: %v", runErr) - default: + case <-done: + case <-returnTimer.C: + t.Fatal("step did not return after its deadline") } - runErr := <-result + require.ErrorIs(t, runErr, errUtils.ErrStepTimeout, "a step that outlives its timeout reports the step timeout") requireStepProcessesGone(t, map[string]int{"command started by the step": parent, "grandchild of that command": child}) }) diff --git a/plugins/auth-secrets/README.md b/plugins/auth-secrets/README.md index 55e0d3ea3a3..d2d4979121f 100644 --- a/plugins/auth-secrets/README.md +++ b/plugins/auth-secrets/README.md @@ -11,7 +11,7 @@ plugins: packages: secrets: source: oci://ghcr.io/cloudposse/atmos/plugins/auth-secrets - version: 1.1.1 + version: 1.1.2 stores: team: diff --git a/plugins/auth-secrets/auth_test.go b/plugins/auth-secrets/auth_test.go index 6bdd42198bf..734961868d5 100644 --- a/plugins/auth-secrets/auth_test.go +++ b/plugins/auth-secrets/auth_test.go @@ -197,7 +197,7 @@ func TestNilRequestsFailWithoutPanicking(t *testing.T) { func TestDescriptionUsesManifestIdentityAndExports(t *testing.T) { info := description() require.Equal(t, "cloudposse/auth-secrets", info.PluginName) - require.Equal(t, "1.1.1", info.PluginVersion) + require.Equal(t, "1.1.2", info.PluginVersion) require.Len(t, info.Services, 2) require.Equal(t, "secrets/store", info.Exports[0].Kind) require.Equal(t, "secrets/env", info.Exports[1].Kind) diff --git a/plugins/auth-secrets/plugin.yaml b/plugins/auth-secrets/plugin.yaml index f6897a3accd..3a6e532f814 100644 --- a/plugins/auth-secrets/plugin.yaml +++ b/plugins/auth-secrets/plugin.yaml @@ -2,7 +2,7 @@ apiVersion: atmos/v1 kind: AtmosPluginConfig metadata: name: cloudposse/auth-secrets - version: 1.1.1 + version: 1.1.2 display_name: Secret store authentication description: Read declared Atmos secret values and map them to an identity environment author: Cloud Posse diff --git a/sdk/README.md b/sdk/README.md index c2b6704d765..8ec26da0adb 100644 --- a/sdk/README.md +++ b/sdk/README.md @@ -15,3 +15,13 @@ The SDK release tag is `sdk/vX.Y.Z` for Atmos `vX.Y.Z`. During unreleased develo `ReadSecret(ctx, name)` calls `atmos.api.v1.Secrets.Read` for a logical credential name granted by the host. The `auth-secrets` provider receives grants from its configured `spec.credentials`; identity and command invocations have no secret-read grant. The host resolves store references and masks returned text. This API is separate from private session `Get`/`Put`/`Delete` and cannot enumerate stores or accept arbitrary backend paths. See `../plugins/auth-secrets` for a complete native provider and environment identity. Official monorepo packages under `../plugins` are an exception to external SDK version pinning: their module replacement selects this SDK at the exact recorded source commit, with shared inputs declared in the release inventory. Consumers download precompiled OCI artifacts and never resolve that local replacement. + +## Component configuration contracts (in development) + +The planned `plugin.Services` extension will accept optional `Discovery`, `Validation`, `Preparation`, and `State` handlers. Negotiate these separately as `component.discovery`, `component.validation`, `component.preparation`, and `component.state`, each at version 1. A configuration provider does not need to supply an executor. Matching host clients and an explicit package loader with private configuration-provider registries are planned. CLI stack and execution integration are not available yet. + +The planned `component.Encode` and `component.Decode` helpers will handle infrastructure values; the component package is not yet available. They retain exact cty numbers, types, nulls, unknowns, and nested sensitivity. Translate evaluator-specific sensitivity to `component.Sensitive` before encoding. Other marks are rejected. The payload and its sensitivity paths can contain secrets; never log them. Invalid wire values produce a payload-free `component.ErrInvalidValue`. + +Component exports in `spec.capabilities.components` are planned; validation and packing do not yet support nonempty exports. Each export will declare `component_type` and a package-local settings `schema`. Use the same export name and metadata across its supported services. Provider settings travel in `provider_configuration`; resolved component values travel separately in `configuration`. Installing a package does not enable discovery. + +Discovery supplies logical coordinates, file/directory/glob provenance, and separate ordering/output dependencies. Preparation supplies fresh inputs and literal file bytes for the selected operation. State resolution returns backend/workspace identity independently. Effect policy defaults to denial and must be enforced by the evaluator and host grants; native code is not sandboxed. See `../docs/prd/plugins/extensions/component-engines.md` for remaining implementation requirements. diff --git a/tests/fixtures/scenarios/scaffold-computed-field/generated/multi-region/atmos.yaml b/tests/fixtures/scenarios/scaffold-computed-field/generated/multi-region/atmos.yaml new file mode 100644 index 00000000000..8b60e094919 --- /dev/null +++ b/tests/fixtures/scenarios/scaffold-computed-field/generated/multi-region/atmos.yaml @@ -0,0 +1,34 @@ +# Atmos CLI configuration for this test fixture. +# https://atmos.tools/cli/configuration + +# Base path for components, stacks and workflows. +base_path: "." + +# Stack configuration. +stacks: + # Path to stack files (relative to base_path). + base_path: "stacks" + # Stack naming template. + name_template: "{{.vars.project_name}}" + +# Template configuration. +templates: + settings: + enabled: true + sprig: + enabled: true + gomplate: + enabled: true + +# Register the local `scaffold.yaml` in this directory as the "example" +# template, so `atmos scaffold generate example ` resolves it by +# name instead of requiring a local path argument. +scaffold: + templates: + example: + description: Test fixture exercising a type:computed field + source: "." + +# Logging. +logs: + level: info diff --git a/tests/fixtures/scenarios/scaffold-computed-field/generated/multi-region/config.yaml b/tests/fixtures/scenarios/scaffold-computed-field/generated/multi-region/config.yaml new file mode 100644 index 00000000000..8ea901a5204 --- /dev/null +++ b/tests/fixtures/scenarios/scaffold-computed-field/generated/multi-region/config.yaml @@ -0,0 +1 @@ +primary_region: "us-west-2" diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/README.md b/tests/fixtures/scenarios/terraform-registry-field-test/README.md new file mode 100644 index 00000000000..7c32cec6ea0 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/README.md @@ -0,0 +1,71 @@ +# Terraform component-registry field fixtures + +Disposable real-engine comparisons for baseline `512e6e5c32cc46e5d7fffee77b901a8847bb0dcf` +and candidate `c041bea2109bfd35ad8f7589eaf7351000a0da74` (PR #3545). +The field pass used Terraform **1.15.6**, OpenTofu **1.12.5**, and Go **1.27.1**. +These are investigation fixtures, not golden snapshots or a claim of complete coverage. + +Always copy fixtures into a new disposable directory with its own Git repository, +state, HOME/XDG caches, and synthetic credentials. Lifecycle, output lookup, +vendoring, cache maintenance, and even dry-run commands can mutate files or backends. +Create linked worktrees only from those disposable repositories. Do not point these +fixtures at a production account, existing state bucket, or shared cache. + +| Fixture | Purpose | Dependencies | +|---|---|---| +| `core/` | Existing-state upgrade, saved plans, policies, dependency freshness, selectors, hooks, editions, cancellation, streaming UI | Both pinned engines, Python 3 + PyYAML, Git; Helm/Helmfile for shared-caller check | +| `cache/` | Provider plugin cache, provider/module registry cache, filesystem mirrors, smart init | null provider 3.2.4; registry module cloudposse/label/null 0.25.0; Docker for trusted Linux proxy/offline tests | +| `sources/` | Directory/tagged local Git sources, aliases, workdirs, nested lookups and linked checkouts | Git, both engines; built-in terraform_data | +| `backend/` | S3 provisioning, emulator identity, SSM hooks and source/cache/backend combinations | Isolated Floci endpoint, dummy credentials; null 3.2.4 for combined variant | + +## Core reproducers + +Build the two immutable revisions with the same Go toolchain/settings in independent +source archives. Then run from the repository root, choosing a **new** output directory +outside the fixture for each command (the harness refuses to overwrite existing case roots +and rejects output inside `core/`, including through relative paths or symlinks): + +```sh +python3 tests/fixtures/scenarios/terraform-registry-field-test/core/run.py \ + .context/my-field-run/core /absolute/path/atmos-baseline /absolute/path/atmos-candidate +python3 tests/fixtures/scenarios/terraform-registry-field-test/core/run_extra.py \ + .context/my-field-run/extra /absolute/path/atmos-baseline /absolute/path/atmos-candidate +python3 tests/fixtures/scenarios/terraform-registry-field-test/core/run_contracts.py \ + .context/my-field-run/contracts /absolute/path/atmos-baseline /absolute/path/atmos-candidate +python3 tests/fixtures/scenarios/terraform-registry-field-test/core/run_retry.py \ + .context/my-field-run/retry /absolute/path/atmos-baseline /absolute/path/atmos-candidate +``` + +The runners execute both engines separately. `results.json` contains assertions; +`commands/` contains argv, controlled environment overrides, stdout, stderr, status, +engine subprocess traces and hook events. A failed assertion does not stop later cases; +after the last case the runner lists failed assertions on stderr and exits nonzero. A +failed initial fixture plan still aborts the run immediately. Inspect the assertions for +details. The runner self-tests need no Atmos or engine binaries: + +```sh +python3 -m unittest discover -p test_run.py \ + -s tests/fixtures/scenarios/terraform-registry-field-test/core +``` + +Controlled Packer dispatch is explicitly a stub; real Packer validation +is recorded separately in the retained field report. Shared callers never deploy Helm +releases or publish images. The local stored-plan store has CI checks/comments disabled. + +The PTY runner uses an actual controlling terminal and the real engine path, because +streaming UI subprocesses do not preserve the instrumentation wrapper's custom variable. +Cancellation preserves the configuration for the recovery command: changing HCL after +interrupting would test backend reconfiguration as well as cancellation. + +## Retained evidence + +The completed pass lives in +`.context/regression-test/terraform-registry-c041-20261027-01/`. +Its `REPORT.md` links the coverage ledger, reduced reproducers, raw observations, +paired-check JSON, builds, and ten-sample Mage benchmark comparisons. The cache, +source, and backend directories retain their additional runtime harnesses. They use +run-specific paths and must be configured/copied to a fresh run before reuse. + +Fixture origins include existing lifecycle/dependency examples, source/workdir +scenarios, `terraform-component-mocks-fallback`, and `aws-store-hooks-floci`. +No product implementation, API, or golden snapshots were changed by this pass. diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/backend/README.md b/tests/fixtures/scenarios/terraform-registry-field-test/backend/README.md new file mode 100644 index 00000000000..e51771c5434 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/backend/README.md @@ -0,0 +1,45 @@ +# Terraform registry backend field-test fixture + +Derived from `terraform-component-mocks-fallback` and `aws-store-hooks-floci`. +Requires an isolated AWS emulator and dummy credentials. Set `FIELD_ENGINE`, +`FIELD_BUCKET`, `FIELD_STORE_PREFIX`, and `AWS_ENDPOINT_URL`. Run a copied fixture. +The fixture uses real `terraform_data` state in S3, distinct dev/prod workspaces, +state/output references, fallback mocks, an AWS user identity, and an SSM output hook. + +The field pass also copies `modules/source-marker` into a source-provisioned variant: +`source: {uri: ./modules/source-marker, ttl: 1h}` plus +`provision.workdir.enabled: true`. That variant uses null provider 3.2.4 from an +isolated filesystem mirror and an absolute `components.terraform.plugin_cache_dir`. +Refresh source explicitly with `terraform source pull producer -s dev --force`. + +## Reproduce dry-run backend mutation + +Use a new unique bucket name for each repetition. With a copied fixture configured +against the disposable emulator: + +```shell +aws s3api head-bucket --bucket "$FIELD_BUCKET" +atmos terraform plan producer -s dev --dry-run +aws s3api head-bucket --bucket "$FIELD_BUCKET" +``` + +The first command returns 404. Both tested revisions create the bucket during +`plan --dry-run`, print `Provisioned S3 backend`, and return 0. The final command +returns 0. This behavior is inherited, not introduced by the registry extraction. + +## Reproduce source refresh losing a saved plan + +In the source variant, change `source.ttl` to `0s`, then run: + +```shell +atmos terraform plan producer -s dev -detailed-exitcode +atmos terraform apply producer -s dev --from-plan +``` + +Both tested revisions return 2 from plan and 1 from apply, reporting a missing +`dev-producer.planfile` after source refresh. TTL `1h` preserves the saved plan; +force-refreshing source before a new plan preserves the apply workflow. + +Manual backend CRUD currently requires literal bucket/endpoint values: its host +lookup disables YAML function processing. This is separate from lifecycle commands, +which resolve the `!env` values correctly. Do not use real cloud credentials. diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/backend/atmos.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/backend/atmos.yaml new file mode 100644 index 00000000000..a6d532ee037 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/backend/atmos.yaml @@ -0,0 +1,31 @@ +base_path: . +components: + terraform: + base_path: components/terraform + command: !env FIELD_ENGINE + auto_generate_backend_file: true + plugin_cache: false +stacks: + base_path: stacks + included_paths: ["*.yaml"] + name_template: "{{ .vars.stage }}" +logs: + level: Info +stores: + results: + kind: aws/ssm + identity: sandbox + options: + region: us-east-1 + prefix: !env FIELD_STORE_PREFIX +auth: + identities: + sandbox: + kind: aws/user + credentials: + access_key_id: test + secret_access_key: test + region: us-east-1 + spec: + endpoint_url: !env AWS_ENDPOINT_URL + default: true diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/backend/components/terraform/marker/main.tf b/tests/fixtures/scenarios/terraform-registry-field-test/backend/components/terraform/marker/main.tf new file mode 100644 index 00000000000..a64222efe67 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/backend/components/terraform/marker/main.tf @@ -0,0 +1,8 @@ +variable "value" { type = string } +variable "inputs" { + type = any + default = {} +} +resource "terraform_data" "marker" { input = var.value } +output "value" { value = terraform_data.marker.output } +output "inputs" { value = var.inputs } diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/backend/modules/source-marker/main.tf b/tests/fixtures/scenarios/terraform-registry-field-test/backend/modules/source-marker/main.tf new file mode 100644 index 00000000000..f0021e2ccdb --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/backend/modules/source-marker/main.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + null = { source = "hashicorp/null", version = "3.2.4" } + } +} +variable "value" { type = string } +resource "terraform_data" "marker" { input = "${var.value}-source-v1" } +resource "null_resource" "cache_probe" { triggers = { value = var.value } } +output "value" { value = terraform_data.marker.output } diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/backend/stacks/dev.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/backend/stacks/dev.yaml new file mode 100644 index 00000000000..6a25b4cbaba --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/backend/stacks/dev.yaml @@ -0,0 +1,46 @@ +vars: + stage: dev +terraform: + backend_type: s3 + backend: + s3: + bucket: !env FIELD_BUCKET + key: terraform.tfstate + region: us-east-1 + endpoints: + s3: !env AWS_ENDPOINT_URL + use_path_style: true + skip_credentials_validation: true + skip_requesting_account_id: true + skip_metadata_api_check: true + skip_region_validation: true + skip_s3_checksum: true + provision: + backend: + enabled: true +components: + terraform: + producer: + metadata: {component: marker} + vars: {value: dev-real} + mocks: {value: mock-value} + hooks: + store-value: + events: [after.terraform.apply] + kind: store + name: results + outputs: {value: .value} + # The consumer reads producer state and outputs, so apply producer first. + # For plan or describe without producer state, pass --use-mocks=always. + consumer: + metadata: {component: marker} + vars: + value: consumer + inputs: + state: !terraform.state producer dev value + output: !terraform.output producer dev value + disabled: + metadata: {component: marker} + vars: {value: disabled} + provision: + backend: {enabled: false} diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/backend/stacks/prod.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/backend/stacks/prod.yaml new file mode 100644 index 00000000000..58b9c36b1fb --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/backend/stacks/prod.yaml @@ -0,0 +1,44 @@ +vars: + stage: prod +terraform: + backend_type: s3 + backend: + s3: + bucket: !env FIELD_BUCKET + key: terraform.tfstate + region: us-east-1 + endpoints: + s3: !env AWS_ENDPOINT_URL + use_path_style: true + skip_credentials_validation: true + skip_requesting_account_id: true + skip_metadata_api_check: true + skip_region_validation: true + skip_s3_checksum: true + provision: + backend: + enabled: true +components: + terraform: + producer: + metadata: {component: marker} + vars: {value: prod-real} + mocks: {value: mock-value} + hooks: + store-value: + events: [after.terraform.apply] + kind: store + name: results + outputs: {value: .value} + consumer: + metadata: {component: marker} + vars: + value: consumer + inputs: + state: !terraform.state producer prod value + output: !terraform.output producer prod value + disabled: + metadata: {component: marker} + vars: {value: disabled} + provision: + backend: {enabled: false} diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/cache/README.md b/tests/fixtures/scenarios/terraform-registry-field-test/cache/README.md new file mode 100644 index 00000000000..ff67bb65fd3 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/cache/README.md @@ -0,0 +1,54 @@ +# Terraform registry field test: caches and smart init + +Derived from `tests/fixtures/scenarios/plugin-cache` and the pinned label module in `complete/modules/label`. Local null resources and a registry naming module only; no cloud resources. The investigation harness copies this fixture into an isolated Git root, replaces the engine command with a transparent invocation recorder, and scopes every cache to that copy. Null 3.2.4 and label 0.25.0 are pinned for paired Terraform/OpenTofu execution. Runtime mutations and logs belong under `.context/regression-test/`. + +The 2026-10-27 paired field run used Terraform 1.15.6/OpenTofu 1.12.5 on native macOS and disposable Linux containers. Harness and detailed evidence live in `.context/regression-test/terraform-registry-c041-20261027-01/cache/`. It exercised all four plugin/registry cache combinations, public module downloads, offline filesystem mirrors/corruption, RC overrides, smart-init recovery/opt-outs, concurrent fleets, and multi-platform provider locking. These components use only local `null_resource` state; they create no cloud resources. + +The completeness addendum additionally exercises scoped provider/module cache inspection/deletion/pruning, offline registry-module reuse, isolated fingerprint changes, and failed/interrupted real-engine initialization recovery. + +The retained `.context` harness is not part of the repository. Reproduce each scenario from a fresh checkout with the steps below. + +## Prepare a disposable copy + +Copy this directory into a new temporary directory, run `git init` there, and point every cache at that copy so no shared cache is read or written: + +```shell +export HOME="$PWD/home" XDG_CACHE_HOME="$PWD/xdg-cache" +export ATMOS_XDG_CACHE_HOME="$PWD/xdg-cache" +export ATMOS_COMPONENTS_TERRAFORM_CACHE_LOCATION="$PWD/xdg-cache/atmos/terraform/registry" +export ATMOS_COMPONENTS_TERRAFORM_PLUGIN_CACHE_DIR="$PWD/plugin-cache" +export ATMOS_COMPONENTS_TERRAFORM_COMMAND=terraform # Repeat every scenario with tofu. +``` + +On macOS and Windows, enable the registry cache and run a Terraform command first to generate its certificate. The command may fail the trust check; run `atmos terraform cache trust`, then retry it. Linux needs no trust step. + +## Cache combinations + +Run `atmos terraform apply provider-a -s dev`, `provider-b`, and `modules` for each combination of `ATMOS_COMPONENTS_TERRAFORM_PLUGIN_CACHE` and `ATMOS_COMPONENTS_TERRAFORM_CACHE_ENABLED` (`true`/`false` each). Use a fresh copy per combination, then repeat the applies in the same copy. When the registry cache is enabled, remove each component's `.terraform/` directory before its repeated apply. + +- Every apply exits 0 and `provider-a` and `provider-b` report outputs `alpha` and `beta`. +- With the plugin cache enabled, `plugin-cache/registry.terraform.io/hashicorp/null/3.2.4/` exists once and serves both provider components. +- With the registry cache enabled, `atmos terraform cache list` shows the `hashicorp/null` provider and the `cloudposse/label/null` 0.25.0 module. A repeated run that serves cached artifacts prints a savings report. +- With both disabled, nothing is written under `plugin-cache/` or `xdg-cache/atmos/terraform/registry`. + +## Offline reuse and corruption + +After a warm registry-cache run, block outbound network access (for example, run the copy in a container with `--network none`), delete each component's `.terraform/` directory, and apply again. The applies must succeed from the cache alone. Next, overwrite one cached provider archive under `xdg-cache/atmos/terraform/registry/providers/` with garbage bytes. Disable the plugin cache and remove the affected provider component's `.terraform/` directory before applying it again. The apply must fail with an integrity error, never install the corrupted archive, and recover after `atmos terraform cache delete ` with network access restored. + +## Smart init + +With `init.mode: auto` (the fixture default), a second `atmos terraform plan provider-a -s dev` must not run `terraform init`. Then check each case: + +- Changing `components/terraform/provider-a/main.tf` (for example, the provider version constraint) must cause one init for `provider-a` only. +- `--init-mode=always` initializes on every command. +- `--init-mode=never` and `--skip-init` skip initialization. +- After interrupting `atmos terraform init provider-a -s dev` with Ctrl+C, the next ordinary `plan` must reinitialize rather than reuse a partial `.terraform/`. + +## Cache maintenance and platforms + +- `atmos terraform cache list --format=json` and `atmos terraform cache stats` report the cached provider and module. +- `atmos terraform cache delete ` removes only that key. +- `atmos terraform cache prune --older-than=0s --dry-run` lists entries without deleting them. +- With `components.terraform.platforms: [linux_amd64, darwin_arm64]`, `atmos terraform cache mirror --all` stores both platforms of `hashicorp/null` 3.2.4. + +For concurrency, start the three applies in parallel against one cold registry cache. All must succeed, and each provider and module must be downloaded once. diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/cache/atmos.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/cache/atmos.yaml new file mode 100644 index 00000000000..678ab3ded10 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/cache/atmos.yaml @@ -0,0 +1,37 @@ +{ + "base_path": ".", + "components": { + "terraform": { + "base_path": "components/terraform", + "auto_generate_backend_file": false, + "apply_auto_approve": true, + "deploy_run_init": true, + "plugin_cache": false, + "init": { + "mode": "auto", + "reconfigure": "auto", + "upgrade": "auto" + } + } + }, + "stacks": { + "base_path": "stacks", + "included_paths": [ + "*.yaml" + ], + "name_template": "{{.vars.stage}}" + }, + "logs": { + "level": "Debug" + }, + "settings": { + "experimental": "silence" + }, + "telemetry": { + "enabled": false + }, + "auth": {}, + "ci": { + "enabled": false + } +} diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/cache/components/terraform/modules/main.tf b/tests/fixtures/scenarios/terraform-registry-field-test/cache/components/terraform/modules/main.tf new file mode 100644 index 00000000000..1e2a0fdc8f8 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/cache/components/terraform/modules/main.tf @@ -0,0 +1,9 @@ +variable "name" { type = string } +variable "stage" { type = string } +module "label" { + source = "cloudposse/label/null" + version = "0.25.0" + name = var.name + stage = var.stage +} +output "id" { value = module.label.id } diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/cache/components/terraform/provider-a/main.tf b/tests/fixtures/scenarios/terraform-registry-field-test/cache/components/terraform/provider-a/main.tf new file mode 100644 index 00000000000..e916489b4b3 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/cache/components/terraform/provider-a/main.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + null = { source = "hashicorp/null", version = "3.2.4" } + } +} +variable "value" { type = string } +variable "stage" { type = string } +resource "null_resource" "example" { triggers = { value = var.value } } +output "value" { value = null_resource.example.triggers.value } diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/cache/components/terraform/provider-b/main.tf b/tests/fixtures/scenarios/terraform-registry-field-test/cache/components/terraform/provider-b/main.tf new file mode 100644 index 00000000000..e916489b4b3 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/cache/components/terraform/provider-b/main.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + null = { source = "hashicorp/null", version = "3.2.4" } + } +} +variable "value" { type = string } +variable "stage" { type = string } +resource "null_resource" "example" { triggers = { value = var.value } } +output "value" { value = null_resource.example.triggers.value } diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/cache/stacks/dev.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/cache/stacks/dev.yaml new file mode 100644 index 00000000000..d3bae7870a0 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/cache/stacks/dev.yaml @@ -0,0 +1,24 @@ +{ + "vars": { + "stage": "dev" + }, + "components": { + "terraform": { + "provider-a": { + "vars": { + "value": "alpha" + } + }, + "provider-b": { + "vars": { + "value": "beta" + } + }, + "modules": { + "vars": { + "name": "fieldtest" + } + } + } + } +} diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/.gitignore b/tests/fixtures/scenarios/terraform-registry-field-test/core/.gitignore new file mode 100644 index 00000000000..60997ad97ef --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/.gitignore @@ -0,0 +1,9 @@ +.terraform/ +*.tfstate* +*.tfplan +*.planfile +*.terraform.tfvars.json +backend.tf.json +.workdir/ + +__pycache__/ diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/atmos.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/core/atmos.yaml new file mode 100644 index 00000000000..680213772f7 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/atmos.yaml @@ -0,0 +1,26 @@ +base_path: . +components: + terraform: + base_path: components/terraform + command: terraform + auto_generate_backend_file: false + apply_auto_approve: false + plugin_cache: false +stacks: + base_path: stacks + included_paths: ["deploy/**/*"] + name_template: "{{ .vars.stage }}" +logs: + level: Warning + file: /dev/stderr +telemetry: + enabled: false +settings: + terminal: + pager: false + pro: + enabled: false + errors: + enabled: false +toolchain: + install: never diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/components/terraform/marker/main.tf b/tests/fixtures/scenarios/terraform-registry-field-test/core/components/terraform/marker/main.tf new file mode 100644 index 00000000000..22e8fd067e0 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/components/terraform/marker/main.tf @@ -0,0 +1,30 @@ +terraform { + required_version = ">= 1.4.0" + backend "local" {} +} + +variable "name" { type = string } +variable "stage" { type = string } +variable "upstream" { + type = string + default = "" +} + +resource "terraform_data" "marker" { + input = { + name = var.name + upstream = var.upstream + } + + provisioner "local-exec" { + command = "echo 'create ${self.input.name}' >> \"$ORDER_LOG\"" + } + + provisioner "local-exec" { + when = destroy + command = "echo 'destroy ${self.input.name}' >> \"$ORDER_LOG\"" + } +} + +output "name" { value = terraform_data.marker.output.name } +output "upstream" { value = terraform_data.marker.output.upstream } diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/components/terraform/marker/marker.tftest.hcl b/tests/fixtures/scenarios/terraform-registry-field-test/core/components/terraform/marker/marker.tftest.hcl new file mode 100644 index 00000000000..5d3b1350f92 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/components/terraform/marker/marker.tftest.hcl @@ -0,0 +1,7 @@ +run "name_is_set" { + command = plan + assert { + condition = length(var.name) > 0 + error_message = "A fixture name is required." + } +} diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/policy/policy.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/core/policy/policy.yaml new file mode 100644 index 00000000000..d10fe102a32 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/policy/policy.yaml @@ -0,0 +1,9 @@ +apiVersion: atmos/v1 +kind: Policy +metadata: {name: field-test} +spec: + policies: + no-delete: + on: terraform.plan + action: deny + rule: "has(input.resource_changes) && input.resource_changes.exists(r, 'delete' in r.change.actions)" diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/run.py b/tests/fixtures/scenarios/terraform-registry-field-test/core/run.py new file mode 100644 index 00000000000..aa9e8ae1681 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/run.py @@ -0,0 +1,232 @@ +"""Paired real-CLI field harness. Requires Python 3, PyYAML, Git and both engines. + +All runtime changes are confined to a new output directory; the fixture is copied. +The output directory must be outside this fixture. The runner exits nonzero when any check fails. +Usage: python3 run.py RUN_DIRECTORY BASELINE_BINARY CANDIDATE_BINARY +""" +import concurrent.futures +import hashlib +import json +import os +from pathlib import Path +import shutil +import signal +import subprocess +import sys +import time + +import yaml + + +SOURCE = Path(__file__).parent.resolve() + + +def reject_nested_output(root): + """Return the resolved case root, or fail when it lies inside the fixture source. + + copytree() would otherwise copy the growing case into itself. + """ + resolved = Path(root).resolve() + if resolved == SOURCE or SOURCE in resolved.parents: + raise ValueError(f"output directory {resolved} must be outside the fixture source {SOURCE}") + return resolved + + +def exit_status(cases): + """Report failed checks across all cases and return the process exit status.""" + failed = [(case.root.name, check) for case in cases for check in case.checks if not check["passed"]] + for name, check in failed: + print(f"FAIL {name}: {check['name']}", file=sys.stderr, flush=True) + total = sum(len(case.checks) for case in cases) + print(f"{total - len(failed)}/{total} checks passed across {len(cases)} cases", file=sys.stderr, flush=True) + return 1 if failed else 0 + + +class Case: + def __init__(self, root, revision, engine, binary): + self.root = reject_nested_output(root) + self.revision, self.engine, self.binary = revision, engine, str(Path(binary).resolve()) + self.project = self.root / "project" + self.root.mkdir(parents=True, exist_ok=False) + shutil.copytree(SOURCE, self.project, ignore=shutil.ignore_patterns("run.py", "test_run.py", "__pycache__", ".terraform", "*.tfstate*", "terraform.tfstate.d")) + self.logs = self.root / "commands" + self.logs.mkdir() + self.records, self.checks = [], [] + self.env = {k: os.environ[k] for k in ("PATH", "TMPDIR", "LANG") if k in os.environ} + # Each case gets a disposable HOME so caller dotfiles and credentials never leak in. + home = self.root / "home" + home.mkdir() + self.env["HOME"] = str(home) + self.env.update({"ATMOS_TELEMETRY_ENABLED": "false", "ATMOS_PRO_ERRORS_ENABLED": "false", + "ATMOS_CI": "false", "CI": "false", "CHECKPOINT_DISABLE": "1", + "TF_INPUT": "0", "AWS_EC2_METADATA_DISABLED": "true", "NO_COLOR": "1", + "XDG_CONFIG_HOME": str(self.root / "config"), "XDG_CACHE_HOME": str(self.root / "cache"), + "ATMOS_XDG_CACHE_HOME": str(self.root / "cache"), + "ORDER_LOG": str(self.root / "order.log"), "HOOK_LOG": str(self.root / "hooks.log"), + "TRACE_LOG": str(self.root / "trace.jsonl")}) + rc = self.root / "terraform.rc" + rc.write_text("") + self.env.update(TF_CLI_CONFIG_FILE=str(rc), TOFU_CLI_CONFIG_FILE=str(rc)) + wrapper = self.root / engine + wrapper.write_text("#!" + sys.executable + "\nimport os,sys,json,time\n" + "with open(os.environ['TRACE_LOG'],'a') as f: f.write(json.dumps({'argv':sys.argv[1:],'cwd':os.getcwd(),'pid':os.getpid(),'time':time.time()})+'\\n')\n" + f"os.execv({shutil.which(engine)!r},[{shutil.which(engine)!r}]+sys.argv[1:])\n") + wrapper.chmod(0o755) + config = yaml.safe_load((self.project / "atmos.yaml").read_text()) + config["components"]["terraform"]["command"] = str(wrapper) + (self.project / "atmos.yaml").write_text(yaml.safe_dump(config, sort_keys=False)) + hooks = {} + for verb in ("init", "plan", "apply", "deploy", "destroy", "output", "refresh", "test"): + for when in ("before", "after"): + key = f"{when}-{verb}" + hooks[key] = {"events": [f"{when}.terraform.{verb}"], "kind": "step", "type": "shell", + "with": {"command": f'echo "{key} $ATMOS_STACK/$ATMOS_COMPONENT status=$ATMOS_HOOK_STATUS" >> "$HOOK_LOG"'}} + # Root hooks are inherited by both stacks. + for path in (self.project / "stacks/deploy").glob("*.yaml"): + path.write_text(yaml.safe_dump({"hooks": hooks}, sort_keys=False) + path.read_text()) + for args in (["init", "-q"], ["add", "."], ["-c", "commit.gpgsign=false", "-c", "user.name=Field Test", "-c", "user.email=field@example.invalid", "commit", "-qm", "fixture"]): + subprocess.run(["git", *args], cwd=self.project, env=self.env, check=True, capture_output=True) + + def command(self, label, args, expect=None, env=None, timeout=120, cwd=None): + index = len(self.records) + 1 + trace = self.root / "trace.jsonl" + hooks = self.root / "hooks.log" + before_trace = trace.read_text().splitlines() if trace.exists() else [] + before_hooks = hooks.read_text().splitlines() if hooks.exists() else [] + command = [self.binary, *args] + actual_env = dict(self.env) + actual_env.update(env or {}) + started = time.time() + process = subprocess.Popen(command, cwd=cwd or self.project, env=actual_env, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True) + timed_out = False + try: + stdout, stderr = process.communicate(timeout=timeout) + except subprocess.TimeoutExpired: + timed_out = True + os.killpg(process.pid, signal.SIGTERM) + try: + stdout, stderr = process.communicate(timeout=5) + except subprocess.TimeoutExpired: + os.killpg(process.pid, signal.SIGKILL) + stdout, stderr = process.communicate() + prefix = self.logs / f"{index:03d}-{label}" + prefix.with_suffix(".stdout").write_bytes(stdout) + prefix.with_suffix(".stderr").write_bytes(stderr) + after_trace = trace.read_text().splitlines() if trace.exists() else [] + after_hooks = hooks.read_text().splitlines() if hooks.exists() else [] + result = {"label": label, "command": command, "cwd": str(cwd or self.project), + "environment_overrides": actual_env, "exit_code": process.returncode, + "seconds": time.time() - started, "timeout": timed_out, + "trace": [json.loads(x) for x in after_trace[len(before_trace):]], + "hooks": after_hooks[len(before_hooks):], "stdout": str(prefix.with_suffix('.stdout')), + "stderr": str(prefix.with_suffix('.stderr'))} + self.records.append(result) + prefix.with_suffix(".json").write_text(json.dumps(result, indent=2)) + if expect is not None: + self.check(label + " exit", process.returncode in expect, {"actual": process.returncode, "expected": expect}) + self.save() + print(self.root.name, label, process.returncode, round(result["seconds"], 2), flush=True) + return result, stdout.decode(errors="replace"), stderr.decode(errors="replace") + + def check(self, name, passed, detail=None): + self.checks.append({"name": name, "passed": bool(passed), "detail": detail}) + + def save(self): + (self.root / "results.json").write_text(json.dumps({"revision": self.revision, "engine": self.engine, + "commands": self.records, "checks": self.checks}, indent=2)) + + def output(self, label, component="producer", stack="dev"): + result, stdout, _ = self.command(label, ["terraform", "output", component, "-s", stack, "-json"], [0]) + try: + parsed = json.loads(stdout) + self.check(label + " JSON", True) + return parsed + except ValueError: + self.check(label + " JSON", False, stdout[:250]) + return {} + + def state_snapshot(self): + return {str(p.relative_to(self.project)): hashlib.sha256(p.read_bytes()).hexdigest() + for p in self.project.rglob("*.tfstate") if ".terraform" not in p.parts} + + +def lifecycle(case): + c = case + c.command("config", ["describe", "config", "-f", "json"], [0]) + c.command("explicit-init", ["terraform", "init", "producer", "-s", "dev"], [0]) + initial, _, _ = c.command("initial-plan", ["terraform", "plan", "producer", "-s", "dev", "-detailed-exitcode"], [2]) + c.check("initial plan records before and after hooks", all( + any(event.startswith(name + " ") for event in initial["hooks"]) + for name in ("before-plan", "after-plan")), initial["hooks"]) + if initial["exit_code"] != 2: + raise RuntimeError("Initial fixture plan failed; inspect evidence before continuing") + c.command("apply-saved", ["terraform", "apply", "producer", "-s", "dev", "--from-plan"], [0]) + first = c.output("first-output") + c.check("initial output value", first.get("name", {}).get("value") == "dev-producer-v1", first) + c.command("warm-plan", ["terraform", "plan", "producer", "-s", "dev", "-detailed-exitcode"], [0]) + c.command("state-list", ["terraform", "state", "list", "producer", "-s", "dev"], [0]) + c.command("prod-apply", ["terraform", "apply", "producer", "-s", "prod", "-auto-approve"], [0]) + c.output("prod-output", stack="prod") + c.output("dev-after-prod") + c.command("bulk-apply", ["terraform", "apply", "--all", "-s", "dev", "-auto-approve", "--max-concurrency=1"], [0]) + for component in ("consumer", "state-consumer"): + out = c.output(component + "-output", component) + c.check(component + " initial upstream", out.get("upstream", {}).get("value") == "dev-producer-v1", out) + dev = c.project / "stacks/deploy/dev.yaml" + dev.write_text(dev.read_text().replace("dev-producer-v1", "dev-producer-v2")) + c.command("bulk-update", ["terraform", "apply", "--all", "-s", "dev", "-auto-approve", "--max-concurrency=1"], [0]) + for component in ("consumer", "state-consumer"): + out = c.output(component + "-updated", component) + c.check(component + " fresh upstream", out.get("upstream", {}).get("value") == "dev-producer-v2", out) + selectors = {"tags": ["--tags=other"], "labels": ["--labels=team=platform"], + "query": ["--query", '.metadata.labels.tier == "data"'], + "components": ["--components=consumer", "--include-dependencies"], + "tags-labels": ["--tags=other", "--labels=team=application"], + "tags-dependencies": ["--tags=data", "--include-dependencies"], + "all-tags": ["--all", "--tags=other"], "stack-only": [], + "all-parallel": ["--all", "--max-concurrency=2", "--log-order=grouped"]} + expected_selectors = { + "tags": {"independent"}, "labels": {"producer", "consumer", "state-consumer"}, + "query": {"consumer", "state-consumer"}, "components": {"consumer", "producer"}, + "tags-labels": {"independent"}, "tags-dependencies": {"consumer", "state-consumer", "producer"}, + "all-tags": {"independent"}, "stack-only": {"producer", "consumer", "state-consumer", "independent"}, + "all-parallel": {"producer", "consumer", "state-consumer", "independent"}} + for label, flags in selectors.items(): + result, _, _ = c.command("select-" + label, ["terraform", "plan", "-s", "dev", *flags], [0]) + plans = [entry["argv"] for entry in result["trace"] if entry["argv"] and entry["argv"][0] == "plan"] + selected = {component for component in expected_selectors["stack-only"] + if any(any("dev-" + component + ".terraform.tfvars.json" in arg for arg in argv) for argv in plans)} + c.check("select-" + label + " components", selected == expected_selectors[label] and len(plans) == len(selected), plans) + c.command("saved-v2", ["terraform", "plan", "producer", "-s", "dev"], [0]) + dev.write_text(dev.read_text().replace("dev-producer-v2", "dev-producer-v3")) + c.command("apply-old-saved", ["terraform", "apply", "producer", "-s", "dev", "--from-plan"], [0]) + out = c.output("saved-input-output") + c.check("saved plan preserves old inputs", out.get("name", {}).get("value") == "dev-producer-v2", out) + c.command("hooks-disabled", ["terraform", "output", "producer", "-s", "dev", "-json", "--skip-hooks"], [0]) + c.check("skip-hooks silent", not c.records[-1]["hooks"], c.records[-1]["hooks"]) + c.command("test", ["terraform", "test", "producer", "-s", "dev"], [0]) + c.command("refresh", ["terraform", "refresh", "producer", "-s", "dev"], [0]) + c.command("deploy", ["terraform", "deploy", "producer", "-s", "dev"], [0]) + before = c.state_snapshot() + c.command("policy-denied-destroy-plan", ["terraform", "plan", "producer", "-s", "dev", "-destroy"], [77], + env={"ATMOS_POLICY_DIR": str(c.project / "policy")}) + c.check("denied plan preserves state", c.state_snapshot() == before) + c.command("policy-denied-apply", ["terraform", "apply", "producer", "-s", "dev", "-auto-approve"], [1], + env={"ATMOS_POLICY_DIR": str(c.project / "policy")}) + c.check("denied apply preserves state", c.state_snapshot() == before) + c.command("bulk-destroy", ["terraform", "destroy", "--all", "-s", "dev", "-auto-approve", "--max-concurrency=1"], [0]) + out = c.output("prod-after-dev-destroy", stack="prod") + c.check("other stack survives destroy", out.get("name", {}).get("value") == "prod-producer-v1", out) + c.command("prod-destroy", ["terraform", "destroy", "producer", "-s", "prod", "-auto-approve"], [0]) + c.save() + + +if __name__ == "__main__": + root, baseline, candidate = sys.argv[1:] + cases = [] + for engine in ("terraform", "tofu"): + for revision, binary in (("baseline", baseline), ("candidate", candidate)): + cases.append(Case(Path(root) / f"{engine}-{revision}", revision, engine, binary)) + lifecycle(cases[-1]) + sys.exit(exit_status(cases)) diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/run_contracts.py b/tests/fixtures/scenarios/terraform-registry-field-test/core/run_contracts.py new file mode 100644 index 00000000000..b466f2b6d16 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/run_contracts.py @@ -0,0 +1,157 @@ +"""Generation, policy application, scheduling failures and shared-caller checks.""" +import json +from pathlib import Path +import sys + +import yaml + +from run import Case, exit_status +from run_extra import simple + + +def generation(c): + dev = simple(c) + doc = yaml.safe_load(dev.read_text()) + doc["components"]["terraform"]["producer"].update(backend_type="local", backend={"local": {}}) + dev.write_text(yaml.safe_dump(doc, sort_keys=False)) + main = c.project / "components/terraform/marker/main.tf" + main.write_text(main.read_text().replace('backend "local" {}', '')) + c.command("generate-varfile", ["terraform", "generate", "varfile", "producer", "-s", "dev", "-f", str(c.root / "vars.json")], [0]) + varfile = c.root / "vars.json" + try: + variables = json.loads(varfile.read_text()) + except (OSError, ValueError) as error: + variables = {} + c.check("generated varfile readable JSON", False, str(error)) + c.check("generated varfile contains stack variables", variables.get("name") == "dev-producer-v1" and variables.get("stage") == "dev", variables) + c.command("generate-backend", ["terraform", "generate", "backend", "producer", "-s", "dev"], [0]) + c.command("plan", ["terraform", "plan", "producer", "-s", "dev", "-detailed-exitcode"], [2]) + plan = c.project / "components/terraform/marker/dev-producer.planfile" + original = c.root / "original.planfile" + if plan.exists(): + original.write_bytes(plan.read_bytes()) + c.command("generate-plan-json", ["terraform", "generate", "planfile", "producer", "-s", "dev", "--file=" + str(plan)], [0]) + c.command("show-json", ["terraform", "show", "producer", "-s", "dev", "-json", str(original)], [0]) + c.command("plan-diff-same", ["terraform", "plan-diff", "producer", "-s", "dev", "--orig=" + str(original), "--new=" + str(original)], [0]) + dev.write_text(dev.read_text().replace("dev-producer-v1", "dev-producer-v2")) + c.command("changed-plan", ["terraform", "plan", "producer", "-s", "dev"], [0]) + changed = c.root / "changed.planfile" + if plan.exists(): + changed.write_bytes(plan.read_bytes()) + c.command("plan-diff-changed", ["terraform", "plan-diff", "producer", "-s", "dev", "--orig=" + str(original), "--new=" + str(changed)], [2]) + c.command("apply", ["terraform", "apply", "producer", "-s", "dev", "--from-plan=" + str(changed)], [0]) + c.command("destructive-plan", ["terraform", "plan", "producer", "-s", "dev", "-destroy"], [0]) + before = c.state_snapshot() + c.command("deny-saved-destructive-apply", ["terraform", "apply", "producer", "-s", "dev", "--from-plan"], [77], + env={"ATMOS_POLICY_DIR": str(c.project / "policy")}) + c.check("policy rejects saved destructive plan without state mutation", before == c.state_snapshot()) + result, _, _ = c.command("skip-planfile", ["terraform", "plan", "producer", "-s", "dev", "--skip-planfile"], [0]) + plans = [entry["argv"] for entry in result["trace"] if entry["argv"] and entry["argv"][0] == "plan"] + c.check("skip-planfile omits plan output argument", bool(plans) and all( + arg != "-out" and not arg.startswith("-out=") for argv in plans for arg in argv), plans) + c.save() + + +def failure(c): + dev = simple(c) + doc = yaml.safe_load(dev.read_text()) + doc["components"]["terraform"]["bad"] = {"metadata": {"component": "marker"}, "vars": {"name": "fail"}} + dev.write_text(yaml.safe_dump(doc, sort_keys=False)) + main = c.project / "components/terraform/marker/main.tf" + main.write_text(main.read_text().replace('resource "terraform_data" "marker" {', '''resource "terraform_data" "marker" { + lifecycle { + precondition { + condition = var.name != "fail" + error_message = "FIELD_TEST_EXPECTED_FAILURE" + } + } +''')) + for mode in ("fail-fast", "keep-going"): + c.command(mode, ["terraform", "plan", "--all", "-s", "dev", "--failure-mode=" + mode, + "--max-concurrency=1", "--execution-summary-file=" + str(c.root / (mode + ".json"))], [1]) + try: + summary = json.loads((c.root / (mode + ".json")).read_text()) + statuses = {entry["component"]: entry["status"] for entry in summary["results"]} + except (OSError, ValueError, KeyError, TypeError) as error: + statuses = {} + c.check(mode + " summary readable", False, str(error)) + c.check(mode + " bad fails", statuses.get("bad") == "failed", statuses) + expected = "skipped" if mode == "fail-fast" else "succeeded" + c.check(mode + " producer status", statuses.get("producer") == expected, statuses) + c.save() + + +def shared_callers(c): + dev = simple(c) + doc = yaml.safe_load(dev.read_text()) + doc["components"]["helmfile"] = {"smoke": {"vars": {"field_marker": "helmfile-scope"}}} + doc["components"]["packer"] = {"smoke": {"vars": {"field_marker": "packer-scope"}}} + dev.write_text(yaml.safe_dump(doc, sort_keys=False)) + config = yaml.safe_load((c.project / "atmos.yaml").read_text()) + config["components"]["helmfile"] = {"base_path": "components/helmfile", "use_eks": False} + stub = c.root / "packer-controlled" + stub.write_text("#!" + sys.executable + "\nimport json,os,sys\n" + "args=sys.argv[1:]\n" + "if '-var-file' in args:\n" + " open(os.environ['PACKER_VARFILE_CAPTURE'],'w').write(open(args[args.index('-var-file')+1]).read())\n" + "print(json.dumps({'argv':args,'cwd':os.getcwd()}))\n") + stub.chmod(0o755) + config["components"]["packer"] = {"base_path": "components/packer", "command": str(stub)} + (c.project / "atmos.yaml").write_text(yaml.safe_dump(config, sort_keys=False)) + helm = c.project / "components/helmfile/smoke" + helm.mkdir(parents=True) + (helm / "helmfile.yaml.gotmpl").write_text('releases:\n - name: smoke\n namespace: field-test\n chart: ./chart\n values:\n - marker: {{ .Values.field_marker | quote }}\n') + chart = helm / "chart" + (chart / "templates").mkdir(parents=True) + (chart / "Chart.yaml").write_text("apiVersion: v2\nname: field-test\nversion: 0.1.0\n") + (chart / "templates/marker.yaml").write_text('apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: field-test\ndata:\n marker: {{ .Values.marker | quote }}\n') + packer = c.project / "components/packer/smoke" + packer.mkdir(parents=True) + (packer / "main.pkr.hcl").write_text("variable \"field_marker\" { type = string }\n") + _, stdout, _ = c.command("helmfile-template", ["helmfile", "template", "smoke", "-s", "dev"], [0]) + c.check("helmfile renders scope marker", "helmfile-scope" in stdout, stdout) + capture = c.root / "packer-varfile.json" + c.command("packer-controlled-validate", ["packer", "validate", "smoke", "-s", "dev"], [0], + env={"PACKER_VARFILE_CAPTURE": str(capture)}) + try: + packer_vars = json.loads(capture.read_text()) + except (OSError, ValueError) as error: + packer_vars = {} + c.check("packer varfile readable JSON", False, str(error)) + c.check("packer receives scope marker", packer_vars.get("field_marker") == "packer-scope", packer_vars) + c.save() + + +def stored_plan(c): + dev = simple(c) + config = yaml.safe_load((c.project / "atmos.yaml").read_text()) + config["components"]["terraform"]["planfiles"] = { + "default": "local", "priority": ["local"], "verify": "fail", + "stores": {"local": {"type": "local/dir", "options": {"path": str(c.root / "artifacts")}}}} + config["ci"] = {"enabled": True, "checks": {"enabled": False}, "comments": {"enabled": False}} + (c.project / "atmos.yaml").write_text(yaml.safe_dump(config, sort_keys=False)) + c.command("reviewed-plan", ["terraform", "plan", "producer", "-s", "dev"], [0]) + c.command("upload-reviewed-plan", ["terraform", "planfile", "upload", "producer", "-s", "dev", "--store=local"], [0]) + dev.write_text(dev.read_text().replace("dev-producer-v1", "dev-producer-v2")) + before = c.state_snapshot() + c.command("reject-drift", ["terraform", "deploy", "producer", "-s", "dev", "--verify-plan", "--ci"], [1]) + c.check("drift rejection preserves state", c.state_snapshot() == before) + dev.write_text(dev.read_text().replace("dev-producer-v2", "dev-producer-v1")) + c.command("deploy-reviewed-plan", ["terraform", "deploy", "producer", "-s", "dev", "--verify-plan", "--ci"], [0]) + c.output("verified-output") + c.save() + + +if __name__ == "__main__": + root, baseline, candidate = sys.argv[1:] + root = Path(root) + cases = [] + for engine in ("terraform", "tofu"): + for revision, binary in (("baseline", baseline), ("candidate", candidate)): + for name, operation in (("generation", generation), ("failure", failure), ("stored-plan", stored_plan)): + cases.append(Case(root / f"{engine}-{revision}-{name}", revision, engine, binary)) + operation(cases[-1]) + for revision, binary in (("baseline", baseline), ("candidate", candidate)): + cases.append(Case(root / f"{revision}-shared", revision, "terraform", binary)) + shared_callers(cases[-1]) + sys.exit(exit_status(cases)) diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/run_extra.py b/tests/fixtures/scenarios/terraform-registry-field-test/core/run_extra.py new file mode 100644 index 00000000000..22b0c0f0a2c --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/run_extra.py @@ -0,0 +1,225 @@ +"""Upgrade, edition, affected, failure, cancellation and PTY field experiments.""" +import json +import os +from pathlib import Path +import pty +import select +import shutil +import signal +import subprocess +import sys +import time + +import yaml + +from run import Case, exit_status + + +def simple(c): + path = c.project / "stacks/deploy/dev.yaml" + # Keep only the independent producer for interruption and edition experiments. + text = path.read_text() + path.write_text(text.split(" consumer:\n")[0]) + return path + + +def upgrade(root, engine, baseline, candidate): + c = Case(root / (engine + "-upgrade"), "baseline-to-candidate", engine, baseline) + dev = simple(c) + c.command("baseline-apply", ["terraform", "apply", "producer", "-s", "dev", "-auto-approve"], [0]) + before = c.state_snapshot() + c.output("baseline-output") + c.binary = candidate + c.command("candidate-no-change", ["terraform", "plan", "producer", "-s", "dev", "-detailed-exitcode"], [0]) + c.check("candidate plan does not mutate baseline state", c.state_snapshot() == before) + c.binary = baseline + dev.write_text(dev.read_text().replace("dev-producer-v1", "dev-producer-v2")) + c.command("baseline-reviewed-plan", ["terraform", "plan", "producer", "-s", "dev", "-detailed-exitcode"], [2]) + dev.write_text(dev.read_text().replace("dev-producer-v2", "dev-producer-v3")) + c.binary = candidate + c.command("candidate-apply-baseline-plan", ["terraform", "apply", "producer", "-s", "dev", "--from-plan"], [0]) + output = c.output("candidate-reviewed-output") + c.check("candidate honors baseline saved input", output.get("name", {}).get("value") == "dev-producer-v2", output) + dev.write_text(dev.read_text().replace("dev-producer-v3", "dev-producer-v2")) + c.command("candidate-stable-plan", ["terraform", "plan", "producer", "-s", "dev", "-detailed-exitcode"], [0]) + c.save() + return c + + +def affected(c): + dev = simple(c) + subprocess.run(["git", "add", "."], cwd=c.project, env=c.env, check=True, capture_output=True) + subprocess.run(["git", "-c", "commit.gpgsign=false", "-c", "user.name=Field Test", "-c", "user.email=field@example.invalid", "commit", "-qm", "simple baseline"], + cwd=c.project, env=c.env, check=True, capture_output=True) + base = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=c.project, text=True).strip() + dev.write_text(dev.read_text().replace("dev-producer-v1", "dev-producer-v2")) + subprocess.run(["git", "add", "."], cwd=c.project, env=c.env, check=True, capture_output=True) + subprocess.run(["git", "-c", "commit.gpgsign=false", "-c", "user.name=Field Test", "-c", "user.email=field@example.invalid", "commit", "-qm", "producer change"], + cwd=c.project, env=c.env, check=True, capture_output=True) + _, stdout, _ = c.command("describe-affected", ["describe", "affected", "--ref=" + base, "--format=json"], [0]) + try: + affected_components = json.loads(stdout[stdout.index("["):]) + except ValueError: + affected_components = [] + c.check("affected includes dev producer", isinstance(affected_components, list) and any( + item.get("component") == "producer" and item.get("stack") == "dev" for item in affected_components), affected_components) + for label, flags in (("plan-affected", []), ("plan-affected-tags", ["--tags=foundation"])): + result, _, _ = c.command(label, ["terraform", "plan", "--affected", "--ref=" + base, *flags, "-s", "dev"], [0]) + plans = [entry["argv"] for entry in result["trace"] if entry["argv"] and entry["argv"][0] == "plan"] + c.check(label + " plans producer", any(any("dev-producer.terraform.tfvars.json" in arg for arg in argv) for argv in plans), plans) + c.command("invalid-all-affected", ["terraform", "plan", "--affected", "--all", "--ref=" + base, "-s", "dev"], [1]) + c.save() + + +def editions(c): + simple(c) + c.command("apply", ["terraform", "apply", "producer", "-s", "dev", "-auto-approve"], [0]) + for date in ("2026-09-11", "2026-09-12", "2026-09-30", "2026-10-01", "2026-10-12", "2026-10-13", + "2026-10-18", "2026-10-19", "2026-10-20", "2026-10-25", "2026-10-26"): + c.command("config-" + date, ["describe", "config", "-f", "json", "--edition=" + date], [0]) + c.command("plan-" + date, ["terraform", "plan", "producer", "-s", "dev", "--edition=" + date], [0]) + c.command("old-edition-explicit-auto", ["terraform", "plan", "producer", "-s", "dev", "--edition=2026-09-11", "--init-mode=auto"], [0]) + _, stdout, _ = c.command("flag-over-env", ["describe", "edition", "--edition=2026-10-26"], [0], env={"ATMOS_EDITION": "2026-09-11"}) + try: + edition = yaml.safe_load(stdout) + except yaml.YAMLError: + edition = None + c.check("edition flag overrides environment", isinstance(edition, dict) and edition.get("edition") == "2026-10-26", edition) + c.command("invalid-edition", ["describe", "edition", "--edition=not-a-date"], [1]) + c.save() + + +def interrupted(c): + simple(c) + main = c.project / "components/terraform/marker/main.tf" + marker = c.root / "provisioner-started" + c.env["PROVISIONER_STARTED"] = str(marker) + main.write_text(main.read_text().replace("echo 'create ${self.input.name}'", "touch \\\"$PROVISIONER_STARTED\\\"; sleep 30; echo 'create ${self.input.name}'")) + cmd = [c.binary, "terraform", "apply", "producer", "-s", "dev", "-auto-approve"] + started = time.time() + p = subprocess.Popen(cmd, cwd=c.project, env=c.env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True) + while time.time() - started < 30 and p.poll() is None and not marker.exists(): + time.sleep(.1) + provisioner_running = marker.exists() and p.poll() is None + c.check("delayed provisioner started and is running", provisioner_running) + if provisioner_running: + p.send_signal(signal.SIGINT) + elif p.poll() is None: + os.killpg(p.pid, signal.SIGKILL) + signal_time = time.time() + forced = False + try: + out, err = p.communicate(timeout=10) + except subprocess.TimeoutExpired: + forced = True + os.killpg(p.pid, signal.SIGKILL) + out, err = p.communicate() + live = [] + for row in subprocess.check_output(["ps", "ax", "-o", "pid=,pgid=,command="], text=True).splitlines(): + fields = row.strip().split(None, 2) + if len(fields) == 3 and fields[1] == str(p.pid): + live.append(row) + (c.root / "interrupt.stdout").write_bytes(out) + (c.root / "interrupt.stderr").write_bytes(err) + (c.root / "interrupt.json").write_text(json.dumps({"command": cmd, "exit": p.returncode, "forced_cleanup": forced, + "seconds_after_signal": time.time()-signal_time, "remaining_group": live}, indent=2)) + if live: + os.killpg(p.pid, signal.SIGKILL) + c.check("interruption exit status", provisioner_running and p.returncode == 130, p.returncode) + c.check("interruption exits without forced kill", provisioner_running and not forced) + c.check("interruption leaves no child group", not live, live) + c.command("plan-after-cancellation", ["terraform", "plan", "producer", "-s", "dev"], [0]) + c.save() + + +def terminal(c): + simple(c) + config_path = c.project / "atmos.yaml" + config = yaml.safe_load(config_path.read_text()) + engine = shutil.which(c.engine) + gate = c.root / "plan-gate" + wrapper = c.root / "terminal-engine" + wrapper.write_text("#!" + sys.executable + "\nimport os,sys,time\n" + "if len(sys.argv) > 1 and sys.argv[1] == 'plan':\n" + f" open({str(c.root / 'plan-started')!r}, 'w').close()\n" + f" while not os.path.exists({str(gate)!r}): time.sleep(.05)\n" + f"os.execv({engine!r}, [{engine!r}] + sys.argv[1:])\n") + wrapper.chmod(0o755) + config["components"]["terraform"]["command"] = str(wrapper) + config_path.write_text(yaml.safe_dump(config, sort_keys=False)) + cmd = [c.binary, "terraform", "plan", "producer", "-s", "dev", "--ui", "--force-tty", "--force-color"] + pid, master = pty.fork() + if pid == 0: + os.chdir(c.project) + os.execve(c.binary, cmd, {**c.env, "TERM": "xterm-256color"}) + chunks, start = [], time.time() + status = None + progress_before_completion = False + while time.time() - start < 30: + ready, _, _ = select.select([master], [], [], .1) + if ready: + try: + data = os.read(master, 65536) + except OSError: + break + if not data: + break + chunks.append((time.time()-start, data)) + if (c.root / "plan-started").exists() and b"plan dev/marker" in b"".join(data for _, data in chunks).lower(): + progress_before_completion = True + gate.touch() + exited, value = os.waitpid(pid, os.WNOHANG) + if exited: + status = value + break + if status is None: + # EOF on the PTY can arrive before the child is reapable; give it a short grace + # period so a clean exit is not misrecorded as a forced kill. + grace = time.time() + 5 + exited, value = os.waitpid(pid, os.WNOHANG) + while not exited and time.time() < grace: + time.sleep(.05) + exited, value = os.waitpid(pid, os.WNOHANG) + if not exited: + # pty.fork makes the child a session leader, so its pid is the process group of + # Atmos and the engine processes it started; kill the whole group, then reap. + try: + os.killpg(pid, signal.SIGKILL) + except ProcessLookupError: + pass + _, value = os.waitpid(pid, 0) + status = value + # Drain any output buffered after the loop stopped so chunks are not undercounted. + while True: + ready, _, _ = select.select([master], [], [], 0) + if not ready: + break + try: + data = os.read(master, 65536) + except OSError: + break + if not data: + break + chunks.append((time.time()-start, data)) + os.close(master) + code = os.waitstatus_to_exitcode(status) + (c.root / "terminal.raw").write_bytes(b"".join(data for _, data in chunks)) + (c.root / "terminal.json").write_text(json.dumps({"command": cmd, "exit": code, + "chunks": [{"seconds": t, "bytes": len(data)} for t,data in chunks]}, indent=2)) + c.check("PTY plan succeeds", code == 0) + c.check("PTY emits progress before completion", progress_before_completion) + c.save() + + +if __name__ == "__main__": + root, baseline, candidate = sys.argv[1:] + root = Path(root).resolve() + baseline, candidate = str(Path(baseline).resolve()), str(Path(candidate).resolve()) + cases = [] + for engine in ("terraform", "tofu"): + cases.append(upgrade(root, engine, baseline, candidate)) + for revision, binary in (("baseline", baseline), ("candidate", candidate)): + for name, operation in (("affected", affected), ("editions", editions), ("interrupt", interrupted), ("terminal", terminal)): + cases.append(Case(root / f"{engine}-{revision}-{name}", revision, engine, binary)) + operation(cases[-1]) + sys.exit(exit_status(cases)) diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/run_retry.py b/tests/fixtures/scenarios/terraform-registry-field-test/core/run_retry.py new file mode 100644 index 00000000000..1366f39ebbe --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/run_retry.py @@ -0,0 +1,77 @@ +"""Interrupt retry backoff with exact command/environment evidence; no cloud calls.""" +import json +import os +from pathlib import Path +import shutil +import signal +import subprocess +import sys +import time + +import yaml + +from run import Case, exit_status +from run_extra import simple + + +def retry(c): + dev = simple(c) + doc = yaml.safe_load(dev.read_text()) + doc["components"]["terraform"]["producer"]["retry"] = { + "max_attempts": 3, "initial_delay": "30s", "max_delay": "30s", + "random_jitter": 0, "conditions": ["FIELD_RETRY"]} + dev.write_text(yaml.safe_dump(doc, sort_keys=False)) + wrapper = c.root / c.engine + wrapper.write_text("#!" + sys.executable + "\nimport os,sys,json,time\n" + "with open(os.environ['TRACE_LOG'],'a') as f: f.write(json.dumps({'argv':sys.argv[1:],'cwd':os.getcwd(),'pid':os.getpid(),'time':time.time()})+'\\n')\n" + "if sys.argv[1]=='plan':\n print('FIELD_RETRY',flush=True)\n sys.exit(1)\n" + f"os.execv({shutil.which(c.engine)!r},[{shutil.which(c.engine)!r}]+sys.argv[1:])\n") + wrapper.chmod(0o755) + command = [c.binary, "terraform", "plan", "producer", "-s", "dev"] + env = dict(c.env, ATMOS_LOGS_LEVEL="Debug") + trace = c.root / "trace.jsonl" + stdout = c.root / "retry.stdout" + stderr = c.root / "retry.stderr" + backoff = False + forced = False + with stdout.open("wb") as out, stderr.open("wb") as err: + proc = subprocess.Popen(command, cwd=c.project, env=env, stdout=out, + stderr=err, start_new_session=True) + deadline = time.monotonic() + 15 + while proc.poll() is None and time.monotonic() < deadline: + if "Entering retry backoff" in stderr.read_text(errors="replace"): + backoff = True + break + time.sleep(.05) + signal_at = time.monotonic() + if backoff and proc.poll() is None: + proc.send_signal(signal.SIGINT) + elif proc.poll() is None: + forced = True + os.killpg(proc.pid, signal.SIGKILL) + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + forced = True + os.killpg(proc.pid, signal.SIGKILL) + proc.wait() + calls = [json.loads(line) for line in trace.read_text().splitlines()] if trace.exists() else [] + attempts = sum(call["argv"][0] == "plan" for call in calls) + record = {"command": command, "cwd": str(c.project), "environment_overrides": env, + "exit": proc.returncode, "seconds_after_signal": time.monotonic()-signal_at, + "backoff_confirmed": backoff, "forced_cleanup": forced, "plan_attempts": attempts, "engine_calls": calls} + (c.root / "retry.json").write_text(json.dumps(record, indent=2) + "\n") + c.check("retry entered backoff before SIGINT", backoff) + c.check("SIGINT interrupts backoff without another attempt", backoff and proc.returncode == 130 and not forced and attempts == 1) + c.save() + print(c.engine, c.revision, record["exit"], "attempts", attempts, flush=True) + + +if __name__ == "__main__": + root, baseline, candidate = sys.argv[1:] + cases = [] + for engine in ("terraform", "tofu"): + for revision, binary in (("baseline", baseline), ("candidate", candidate)): + cases.append(Case(Path(root) / f"{engine}-{revision}", revision, engine, binary)) + retry(cases[-1]) + sys.exit(exit_status(cases)) diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/stacks/deploy/dev.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/core/stacks/deploy/dev.yaml new file mode 100644 index 00000000000..003e10f871f --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/stacks/deploy/dev.yaml @@ -0,0 +1,38 @@ +vars: + stage: dev +components: + terraform: + producer: + metadata: + component: marker + tags: [foundation] + labels: {team: platform, tier: foundation} + vars: + name: dev-producer-v1 + consumer: + metadata: + component: marker + tags: [data] + labels: {team: platform, tier: data} + dependencies: + components: [{component: producer}] + vars: + name: dev-consumer + upstream: !terraform.output producer name + state-consumer: + metadata: + component: marker + tags: [data] + labels: {team: platform, tier: data} + dependencies: + components: [{component: producer}] + vars: + name: dev-state-consumer + upstream: !terraform.state producer name + independent: + metadata: + component: marker + tags: [other] + labels: {team: application, tier: other} + vars: + name: dev-independent diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/stacks/deploy/prod.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/core/stacks/deploy/prod.yaml new file mode 100644 index 00000000000..68443f4369d --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/stacks/deploy/prod.yaml @@ -0,0 +1,9 @@ +vars: + stage: prod +components: + terraform: + producer: + metadata: + component: marker + vars: + name: prod-producer-v1 diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/test_retry.py b/tests/fixtures/scenarios/terraform-registry-field-test/core/test_retry.py new file mode 100644 index 00000000000..26319287f06 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/test_retry.py @@ -0,0 +1,51 @@ +"""Retry runner synchronization and early-exit evidence regressions.""" +import json +from pathlib import Path +import tempfile +from types import SimpleNamespace +import unittest +from unittest.mock import Mock, patch + +from run_retry import retry + + +class RetryEvidenceTest(unittest.TestCase): + def run_case(self, backoff): + with tempfile.TemporaryDirectory() as scratch: + root = Path(scratch) + dev = root / "dev.yaml" + dev.write_text("components: {terraform: {producer: {}}}\n") + c = SimpleNamespace(root=root, project=root, engine="terraform", binary="atmos", + revision="candidate", env={}, check=Mock(), save=Mock()) + proc = Mock(returncode=None if backoff else 2) + proc.poll.side_effect = lambda: proc.returncode + proc.send_signal.side_effect = lambda sig: setattr(proc, "returncode", 130) + + def launch(*args, **kwargs): + kwargs["stdout"].write(b"process evidence\n") + kwargs["stderr"].write(b"Entering retry backoff\n" if backoff else b"bad configuration\n") + kwargs["stderr"].flush() + if backoff: + (root / "trace.jsonl").write_text('{"argv":["plan"]}\n') + return proc + + with patch("run_retry.simple", return_value=dev), patch("run_retry.subprocess.Popen", side_effect=launch): + retry(c) + record = json.loads((root / "retry.json").read_text()) + self.assertEqual(record["backoff_confirmed"], backoff) + self.assertEqual(record["plan_attempts"], int(backoff)) + self.assertEqual((root / "retry.stdout").read_bytes(), b"process evidence\n") + self.assertTrue((root / "retry.stderr").read_bytes()) + self.assertEqual(c.check.call_args_list[0].args[1], backoff) + self.assertEqual(c.check.call_args_list[1].args[1], backoff) + c.save.assert_called_once() + if backoff: + proc.send_signal.assert_called_once() + else: + proc.send_signal.assert_not_called() + + def test_missing_trace_preserves_failure_evidence(self): + self.run_case(False) + + def test_signal_requires_explicit_backoff_marker(self): + self.run_case(True) diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/core/test_run.py b/tests/fixtures/scenarios/terraform-registry-field-test/core/test_run.py new file mode 100644 index 00000000000..81a3f36032f --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/core/test_run.py @@ -0,0 +1,75 @@ +"""Runner self-tests that need no Atmos or engine binaries. + +Usage: python3 -m unittest discover -s tests/fixtures/scenarios/terraform-registry-field-test/core -p "test_run.py" +""" +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + +from run import SOURCE, Case, exit_status, reject_nested_output + + +class FakeCase: + def __init__(self, name, *results): + self.root = Path(name) + self.checks = [{"name": f"check-{i}", "passed": passed, "detail": None} for i, passed in enumerate(results)] + + +class ExitStatusTest(unittest.TestCase): + def test_all_checks_pass(self): + self.assertEqual(exit_status([FakeCase("a", True, True), FakeCase("b", True)]), 0) + + def test_any_failed_check_in_any_case_fails(self): + self.assertEqual(exit_status([FakeCase("a", True), FakeCase("b", True, False, True)]), 1) + + def test_no_checks_passes(self): + self.assertEqual(exit_status([FakeCase("a")]), 0) + + +class NestedOutputTest(unittest.TestCase): + def assert_rejected(self, root): + with self.assertRaises(ValueError): + Case(root, "baseline", "terraform", sys.executable) + + def test_rejects_source_itself_and_descendants(self): + self.assert_rejected(SOURCE) + self.assert_rejected(SOURCE / "runs" / "terraform-baseline") + self.assertFalse((SOURCE / "runs").exists(), "rejection must happen before the case root is created") + + def test_rejects_relative_path_inside_source(self): + cwd = os.getcwd() + os.chdir(SOURCE) + try: + self.assert_rejected(Path("runs") / "terraform-baseline") + self.assert_rejected(Path("components") / ".." / "runs") + finally: + os.chdir(cwd) + self.assertFalse((SOURCE / "runs").exists()) + + def test_rejects_symlink_into_source(self): + with tempfile.TemporaryDirectory() as scratch: + link = Path(scratch) / "fixture" + link.symlink_to(SOURCE, target_is_directory=True) + self.assert_rejected(link / "runs" / "terraform-baseline") + self.assertFalse((SOURCE / "runs").exists()) + + def test_allows_sibling_with_shared_prefix(self): + sibling = SOURCE.parent / (SOURCE.name + "-runs") / "case" + self.assertEqual(reject_nested_output(sibling), sibling) + + def test_copies_fixture_outside_source(self): + with tempfile.TemporaryDirectory() as scratch: + case = Case(Path(scratch) / "runs" / "terraform-baseline", "baseline", "terraform", sys.executable) + self.assertTrue((case.project / "atmos.yaml").is_file()) + self.assertTrue((case.project / "stacks" / "deploy" / "dev.yaml").is_file()) + self.assertFalse((case.project / "run.py").exists()) + self.assertFalse((case.project / "test_run.py").exists()) + log = subprocess.run(["git", "log", "--oneline"], cwd=case.project, capture_output=True, text=True, check=True) + self.assertIn("fixture", log.stdout) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/README.md b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/README.md new file mode 100644 index 00000000000..72083a9717e --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/README.md @@ -0,0 +1,40 @@ +# Relative source context + +This provider-free fixture compares direct execution and an isolated Atmos workdir +for the same nested Terraform component. The component reads a sibling local module +and a template outside its own directory. Both plans should report the markers +from the nested copy, rather than the outer copy. + +Create an outer copy and a nested copy with different module and template markers. +The nested copy makes an accidental read from the outer tree observable. Run each +plan from the nested copy and check both values in its output: + +```sh +fixture="$(pwd)/tests/fixtures/scenarios/terraform-registry-field-test/relative-context" +run="$(mktemp -d)" +cp -R "$fixture/." "$run/" +mkdir -p "$run/nested" +cp -R "$fixture/." "$run/nested/" +printf 'output "value" { value = "outer-module" }\n' > "$run/modules/marker/main.tf" +printf 'outer-template\n' > "$run/templates/message.txt" +printf 'output "value" { value = "nested-module" }\n' > "$run/nested/modules/marker/main.tf" +printf 'nested-template\n' > "$run/nested/templates/message.txt" +cd "$run/nested" +for component in direct isolated; do + atmos terraform plan "$component" -s dev --identity=false -no-color > "$run/$component.plan" || exit 1 + grep -F 'nested-module' "$run/$component.plan" >/dev/null || exit 1 + grep -F 'nested-template' "$run/$component.plan" >/dev/null || exit 1 +done +``` + +Choose the engine with `ATMOS_COMPONENTS_TERRAFORM_COMMAND` (Terraform 1.15.6 or +OpenTofu 1.12.5 for the repair comparison). Use a new run directory for each engine. +No remote provider, cloud credentials, apply, or backend provisioning is required. +No symlinks or platform-specific paths are used. + +The existing workdir layout flattens the nested component path. Retaining a fixed +workdir depth does not preserve the four parent-directory steps in this fixture. +The desired repair must preserve both output values without rewriting Terraform +source, reading another checkout's sibling files, or requiring symbolic links. + +This is a correctness reproducer, not a DAG performance or Windows certification. diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/atmos.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/atmos.yaml new file mode 100644 index 00000000000..73f95e96a79 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/atmos.yaml @@ -0,0 +1,21 @@ +base_path: . +components: + terraform: + base_path: components/terraform + auto_generate_backend_file: false + plugin_cache: false +stacks: + base_path: stacks + included_paths: ["dev"] + name_template: "{{ .vars.stage }}" +logs: + level: Warning +telemetry: + enabled: false +version: + check: + enabled: false +ci: + enabled: false +toolchain: + install: never diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/components/terraform/platform/app/main.tf b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/components/terraform/platform/app/main.tf new file mode 100644 index 00000000000..8709212850b --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/components/terraform/platform/app/main.tf @@ -0,0 +1,15 @@ +terraform { + required_version = ">= 1.0" +} + +module "sibling" { + source = "../../../../modules/marker" +} + +output "sibling" { + value = module.sibling.value +} + +output "template" { + value = trimspace(file("${path.module}/../../../../templates/message.txt")) +} diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/modules/marker/main.tf b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/modules/marker/main.tf new file mode 100644 index 00000000000..a0bbce3e681 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/modules/marker/main.tf @@ -0,0 +1,3 @@ +output "value" { + value = "sibling-module" +} diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/stacks/dev.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/stacks/dev.yaml new file mode 100644 index 00000000000..2b2e4bcbde1 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/stacks/dev.yaml @@ -0,0 +1,16 @@ +vars: + stage: dev +components: + terraform: + direct: + metadata: + component: platform/app + provision: + workdir: + enabled: false + isolated: + metadata: + component: platform/app + provision: + workdir: + enabled: true diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/templates/message.txt b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/templates/message.txt new file mode 100644 index 00000000000..36903f142b7 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/relative-context/templates/message.txt @@ -0,0 +1 @@ +sibling-template diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/sources/.gitignore b/tests/fixtures/scenarios/terraform-registry-field-test/sources/.gitignore new file mode 100644 index 00000000000..7245d3086db --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/sources/.gitignore @@ -0,0 +1,7 @@ +.workdir/ +state/ +.terraform/ +*.tfvars.json +*.planfile +backend.tf.json +generated.txt diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/sources/README.md b/tests/fixtures/scenarios/terraform-registry-field-test/sources/README.md new file mode 100644 index 00000000000..7449a14825a --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/sources/README.md @@ -0,0 +1,45 @@ +# Terraform registry source and workdir field fixture + +Derived from the existing `source-provisioner-workdir-nested`, `workdir-global-default`, +and `terraform-output-jit-workdir` scenarios. Uses real `terraform_data` resources, +local state, generated files, and local module sources; no cloud credentials or +external providers are needed. + +Copy this directory into a disposable Git repository before running. +`terraform apply source-local -s dev` exercises source provisioning; +`terraform apply local-alias -s dev` exercises local workdir provisioning. +Run each with Terraform and OpenTofu, then repeat to verify warm reuse. + +The field harness expands copies with nested/colliding names, component-level +workdir opt-outs, local Git tags/subdirectories, zero-TTL sources, output/state +consumers, and linked Git worktrees. Keep state outside runtime workdirs so source +refresh can be observed without discarding the real local state. Evidence and +the runnable harness live in the investigation run directory. + +```shell +atmos terraform apply source-local -s dev -auto-approve --identity=false +atmos terraform plan source-local -s dev -detailed-exitcode --identity=false +atmos terraform apply local-alias -s dev -auto-approve --identity=false +atmos terraform output local-alias -s dev -json --identity=false +atmos terraform source pull source-local -s dev --force +``` + +For the output command, capture stdout separately and parse the entire stream; +do not strip initialization messages before deciding whether the output is valid +JSON. State lives in `state/` outside the runtime workdir in this fixture. The +investigation also tests the separate case of state inside a refreshed source +workdir; that case must use an independently disposable copy. + +The separate vendor interface is exercised with `atmos vendor pull`, followed by +`atmos terraform apply vendor-alias -s dev -auto-approve --identity=false`. +After modifying the module, a normal pull preserves the materialized dependency; +use `atmos vendor pull --refresh-lock` to fetch the modified local source and +apply again to verify its output. The `vendor.yaml` excludes README files. After the first pull, verify that +`components/terraform/vendor-marker/main.tf` exists and +`components/terraform/vendor-marker/README.md` does not; both checks must pass. + +Run `atmos terraform shell source-local -s dev` in a terminal, inspect `pwd`, +`ATMOS_COMPONENT`, `ATMOS_STACK`, and `TF_CLI_ARGS_plan`, then execute the engine's +native output command and exit. Repeat with `--skip-init`. The field run also +tests explicit `--identity=false`, which currently fails in an auth-free project +on both compared revisions, and tagged Git sources whose runtime is a subdirectory. diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/sources/atmos.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/sources/atmos.yaml new file mode 100644 index 00000000000..da05bbcb37b --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/sources/atmos.yaml @@ -0,0 +1,18 @@ +base_path: . +components: + terraform: + base_path: components/terraform + auto_generate_backend_file: true + auto_generate_files: true + auto_provision_workdir_for_outputs: true + workspaces_enabled: false + apply_auto_approve: true +stacks: + base_path: stacks + included_paths: ["*.yaml"] + name_template: "{{ .vars.stage }}" +templates: + settings: + enabled: true +logs: + level: Warning diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/sources/components/terraform/marker/main.tf b/tests/fixtures/scenarios/terraform-registry-field-test/sources/components/terraform/marker/main.tf new file mode 100644 index 00000000000..34b18090c99 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/sources/components/terraform/marker/main.tf @@ -0,0 +1,12 @@ +variable "stage" { type = string } +variable "name" { type = string } +variable "upstream" { + type = string + default = "" +} +resource "terraform_data" "marker" { + input = { name = var.name, upstream = var.upstream, source = "v1" } +} +output "name" { value = terraform_data.marker.output.name } +output "upstream" { value = terraform_data.marker.output.upstream } +output "source" { value = terraform_data.marker.output.source } diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/sources/modules/marker/README.md b/tests/fixtures/scenarios/terraform-registry-field-test/sources/modules/marker/README.md new file mode 100644 index 00000000000..e600f42d7f7 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/sources/modules/marker/README.md @@ -0,0 +1,3 @@ +# Marker module + +This file must be excluded by the vendor pull. diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/sources/modules/marker/main.tf b/tests/fixtures/scenarios/terraform-registry-field-test/sources/modules/marker/main.tf new file mode 100644 index 00000000000..34b18090c99 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/sources/modules/marker/main.tf @@ -0,0 +1,12 @@ +variable "stage" { type = string } +variable "name" { type = string } +variable "upstream" { + type = string + default = "" +} +resource "terraform_data" "marker" { + input = { name = var.name, upstream = var.upstream, source = "v1" } +} +output "name" { value = terraform_data.marker.output.name } +output "upstream" { value = terraform_data.marker.output.upstream } +output "source" { value = terraform_data.marker.output.source } diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/sources/stacks/dev.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/sources/stacks/dev.yaml new file mode 100644 index 00000000000..cbf283fa53f --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/sources/stacks/dev.yaml @@ -0,0 +1,37 @@ +vars: + stage: dev +terraform: + provision: + workdir: + enabled: true +components: + terraform: + source-local: + source: + uri: ./modules/marker + backend_type: local + backend: + local: + path: ../../../state/source-local.tfstate + vars: + name: source-local + generate: + generated.txt: "generated for {{ .atmos_component }}" + local-alias: + metadata: + component: marker + backend_type: local + backend: + local: + path: ../../../state/local-alias.tfstate + vars: + name: local-alias + vendor-alias: + metadata: + component: vendor-marker + backend_type: local + backend: + local: + path: ../../../state/vendor-alias.tfstate + vars: + name: vendor-alias diff --git a/tests/fixtures/scenarios/terraform-registry-field-test/sources/vendor.yaml b/tests/fixtures/scenarios/terraform-registry-field-test/sources/vendor.yaml new file mode 100644 index 00000000000..43d160a4042 --- /dev/null +++ b/tests/fixtures/scenarios/terraform-registry-field-test/sources/vendor.yaml @@ -0,0 +1,14 @@ +apiVersion: atmos/v1 +kind: AtmosVendorConfig +metadata: + name: terraform-registry-field-test +spec: + sources: + - component: vendor-marker + source: ./modules/marker + targets: + - components/terraform/vendor-marker + included_paths: + - "**/*" + excluded_paths: + - README.md