diff --git a/docs/user/how-to/build-component.md b/docs/user/how-to/build-component.md index 8cb5701ef..881780e4a 100644 --- a/docs/user/how-to/build-component.md +++ b/docs/user/how-to/build-component.md @@ -32,6 +32,7 @@ Built packages are placed in structured subdirectories under the project's outpu | `--srpm-only` | Build only the SRPM, not the binary RPMs | | `--preserve-buildenv on-failure` | Keep the mock chroot on build failure for debugging | | `--continue-on-error` / `-k` | Continue building remaining components if one fails | +| `--unique-mockroot` | Use distinct mock roots instead of the shared default root | | `--local-repo ` | Add a local RPM repository for build dependencies | | `--local-repo-with-publish ` | Use a local repo and publish built RPMs into it | @@ -49,6 +50,51 @@ azldev comp build --local-repo-with-publish ./base/out -p dep-package -p main-pa 2. Enter the chroot with `azldev adv mock shell` to inspect the build environment 3. Check build logs in the project's log directory +## Isolate Mock Roots + +Use the global `--unique-mockroot` flag to avoid sharing mock roots with other +azldev operations: + +```bash +azldev --unique-mockroot comp build -p curl +azldev --unique-mockroot comp render -p curl -p zlib +``` + +Each mock runner passes `--uniqueext` with an extension of at most 24 ASCII +characters: an abbreviated action, a component label of up to 12 characters, +and six random characters, for example `bld-curl-a2b3c4`. +An extension such as `rndr-abcdefghijkl-a2b3c4` uses the full 24-character limit. +Action codes are `bld` for builds (including advanced mock builds), `rndr` for +rendering, `qry` for queries, and `shl` for mock shells with or without packages. +These codes are never shortened further. Other actions use an abbreviated +command name. + +Batch roots label the first component and include the component count when it +fits; otherwise they use the abbreviated first component without the count. +Labels use letters, digits, `_`, and `-`, never spaces. +The extension stays the same for initialization, commands, and cleanup of that +root; separate roots receive separate random suffixes. Without the flag, +mock's existing root naming is unchanged. + +Unique-root commands also pass `--disable-plugin yum_cache` to disable mock's +shared yum cache. Commands without `--unique-mockroot` retain the configured cache +behavior. + +Cleanup removes both the unique build root and its bootstrap root while preserving +shared caches. azldev checks `mock --help` for `bootstrap-cache` support before +cleanup: newer Mock versions can safely use `--scrub=bootstrap`; older versions +require `sudo` to remove only the bootstrap directory with the matching extension +after Mock finishes scrubbing the build root. +With `--dry-run`, cleanup skips capability and path discovery and previews the +scrub command without executing it. + +The flag also applies to queries, custom source generation, and +`azldev advanced mock` commands. A unique `mock shell` session does not reuse a +previous session's root. To inspect a preserved unique build root, take its +`--uniqueext` value from the verbose command log and run +`mock -r --uniqueext --shell` directly; a new azldev +invocation generates a new extension. + ## Related Resources - [Components Reference](../reference/config/components.md) — component definition format diff --git a/docs/user/reference/cli/azldev.md b/docs/user/reference/cli/azldev.md index c5de33703..3c4f94887 100644 --- a/docs/user/reference/cli/azldev.md +++ b/docs/user/reference/cli/azldev.md @@ -29,6 +29,7 @@ lives), or use -C to point to one. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_advanced.md b/docs/user/reference/cli/azldev_advanced.md index f76116d52..2342a086a 100644 --- a/docs/user/reference/cli/azldev_advanced.md +++ b/docs/user/reference/cli/azldev_advanced.md @@ -32,6 +32,7 @@ output but fully supported. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_advanced_download-sources.md b/docs/user/reference/cli/azldev_advanced_download-sources.md index 7758ecd08..f055a4822 100644 --- a/docs/user/reference/cli/azldev_advanced_download-sources.md +++ b/docs/user/reference/cli/azldev_advanced_download-sources.md @@ -70,6 +70,7 @@ azldev advanced download-sources [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_advanced_mcp.md b/docs/user/reference/cli/azldev_advanced_mcp.md index 1f62c5e0c..a38d2dc1a 100644 --- a/docs/user/reference/cli/azldev_advanced_mcp.md +++ b/docs/user/reference/cli/azldev_advanced_mcp.md @@ -43,6 +43,7 @@ azldev advanced mcp [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_advanced_mock.md b/docs/user/reference/cli/azldev_advanced_mock.md index 984278eea..919e6217f 100644 --- a/docs/user/reference/cli/azldev_advanced_mock.md +++ b/docs/user/reference/cli/azldev_advanced_mock.md @@ -31,6 +31,7 @@ starting interactive shell sessions in mock chroot environments. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_advanced_mock_build-rpms.md b/docs/user/reference/cli/azldev_advanced_mock_build-rpms.md index 18e1be1a3..82fb0fa02 100644 --- a/docs/user/reference/cli/azldev_advanced_mock_build-rpms.md +++ b/docs/user/reference/cli/azldev_advanced_mock_build-rpms.md @@ -50,6 +50,7 @@ azldev advanced mock build-rpms [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_advanced_mock_shell.md b/docs/user/reference/cli/azldev_advanced_mock_shell.md index 54d6ebc76..92e54bbff 100644 --- a/docs/user/reference/cli/azldev_advanced_mock_shell.md +++ b/docs/user/reference/cli/azldev_advanced_mock_shell.md @@ -55,6 +55,7 @@ azldev advanced mock shell [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_advanced_wget.md b/docs/user/reference/cli/azldev_advanced_wget.md index aa23ba04a..520f01533 100644 --- a/docs/user/reference/cli/azldev_advanced_wget.md +++ b/docs/user/reference/cli/azldev_advanced_wget.md @@ -44,6 +44,7 @@ azldev advanced wget URI [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_completion.md b/docs/user/reference/cli/azldev_completion.md index 109d7858e..dcfde1a97 100644 --- a/docs/user/reference/cli/azldev_completion.md +++ b/docs/user/reference/cli/azldev_completion.md @@ -30,6 +30,7 @@ See each sub-command's help for details on how to use the generated script. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_completion_bash.md b/docs/user/reference/cli/azldev_completion_bash.md index f39580feb..179f83df4 100644 --- a/docs/user/reference/cli/azldev_completion_bash.md +++ b/docs/user/reference/cli/azldev_completion_bash.md @@ -53,6 +53,7 @@ azldev completion bash --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_completion_fish.md b/docs/user/reference/cli/azldev_completion_fish.md index 42eaf1dd5..d493f8153 100644 --- a/docs/user/reference/cli/azldev_completion_fish.md +++ b/docs/user/reference/cli/azldev_completion_fish.md @@ -44,6 +44,7 @@ azldev completion fish [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_completion_powershell.md b/docs/user/reference/cli/azldev_completion_powershell.md index 6565209e0..af5b066e2 100644 --- a/docs/user/reference/cli/azldev_completion_powershell.md +++ b/docs/user/reference/cli/azldev_completion_powershell.md @@ -41,6 +41,7 @@ azldev completion powershell [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_completion_zsh.md b/docs/user/reference/cli/azldev_completion_zsh.md index aa7d5d7a4..3ec225a07 100644 --- a/docs/user/reference/cli/azldev_completion_zsh.md +++ b/docs/user/reference/cli/azldev_completion_zsh.md @@ -55,6 +55,7 @@ azldev completion zsh [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component.md b/docs/user/reference/cli/azldev_component.md index df0e9622b..7126c74f2 100644 --- a/docs/user/reference/cli/azldev_component.md +++ b/docs/user/reference/cli/azldev_component.md @@ -34,6 +34,7 @@ components defined in the project configuration. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_add.md b/docs/user/reference/cli/azldev_component_add.md index 3b0b66fd0..8e3a82f7e 100644 --- a/docs/user/reference/cli/azldev_component_add.md +++ b/docs/user/reference/cli/azldev_component_add.md @@ -47,6 +47,7 @@ azldev component add [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_build.md b/docs/user/reference/cli/azldev_component_build.md index 278ab3f6c..e51b73a20 100644 --- a/docs/user/reference/cli/azldev_component_build.md +++ b/docs/user/reference/cli/azldev_component_build.md @@ -79,6 +79,7 @@ azldev component build [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_changed.md b/docs/user/reference/cli/azldev_component_changed.md index 2752c4ea2..9371218f4 100644 --- a/docs/user/reference/cli/azldev_component_changed.md +++ b/docs/user/reference/cli/azldev_component_changed.md @@ -69,6 +69,7 @@ azldev component changed [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_diff-sources.md b/docs/user/reference/cli/azldev_component_diff-sources.md index 4bcb11e90..83580b9e3 100644 --- a/docs/user/reference/cli/azldev_component_diff-sources.md +++ b/docs/user/reference/cli/azldev_component_diff-sources.md @@ -41,6 +41,7 @@ azldev component diff-sources [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_history.md b/docs/user/reference/cli/azldev_component_history.md index b269a82d5..48f70d71d 100644 --- a/docs/user/reference/cli/azldev_component_history.md +++ b/docs/user/reference/cli/azldev_component_history.md @@ -67,6 +67,7 @@ azldev component history [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_list.md b/docs/user/reference/cli/azldev_component_list.md index f0cdcfd7c..1d121f8a4 100644 --- a/docs/user/reference/cli/azldev_component_list.md +++ b/docs/user/reference/cli/azldev_component_list.md @@ -56,6 +56,7 @@ azldev component list [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_metadata.md b/docs/user/reference/cli/azldev_component_metadata.md index 35b0dc208..6f9fd82e4 100644 --- a/docs/user/reference/cli/azldev_component_metadata.md +++ b/docs/user/reference/cli/azldev_component_metadata.md @@ -82,6 +82,7 @@ azldev component metadata [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_prepare-sources.md b/docs/user/reference/cli/azldev_component_prepare-sources.md index 45f9f8cb7..5426ca569 100644 --- a/docs/user/reference/cli/azldev_component_prepare-sources.md +++ b/docs/user/reference/cli/azldev_component_prepare-sources.md @@ -62,6 +62,7 @@ azldev component prepare-sources [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_query.md b/docs/user/reference/cli/azldev_component_query.md index f840ca4cd..194929f92 100644 --- a/docs/user/reference/cli/azldev_component_query.md +++ b/docs/user/reference/cli/azldev_component_query.md @@ -59,6 +59,7 @@ azldev component query [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_render.md b/docs/user/reference/cli/azldev_component_render.md index 88a9d5157..7c3c3acee 100644 --- a/docs/user/reference/cli/azldev_component_render.md +++ b/docs/user/reference/cli/azldev_component_render.md @@ -82,6 +82,7 @@ azldev component render [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_test.md b/docs/user/reference/cli/azldev_component_test.md index e316666e3..a1c3d7406 100644 --- a/docs/user/reference/cli/azldev_component_test.md +++ b/docs/user/reference/cli/azldev_component_test.md @@ -108,6 +108,7 @@ azldev component test COMPONENT [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_component_update.md b/docs/user/reference/cli/azldev_component_update.md index 024d3a153..5db10cdb7 100644 --- a/docs/user/reference/cli/azldev_component_update.md +++ b/docs/user/reference/cli/azldev_component_update.md @@ -83,6 +83,7 @@ azldev component update [flags] -C, --project string path to Azure Linux project -q, --quiet only enable minimal output --spec-editor editor Select the RPM spec editor (legacy or experimental) (default legacy) + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_config.md b/docs/user/reference/cli/azldev_config.md index 6eed54b63..a1f09a356 100644 --- a/docs/user/reference/cli/azldev_config.md +++ b/docs/user/reference/cli/azldev_config.md @@ -31,6 +31,7 @@ JSON schema used for validating TOML config files. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_config_dump.md b/docs/user/reference/cli/azldev_config_dump.md index 7136efc12..e4c74b1ef 100644 --- a/docs/user/reference/cli/azldev_config_dump.md +++ b/docs/user/reference/cli/azldev_config_dump.md @@ -51,6 +51,7 @@ azldev config dump [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_config_generate-schema.md b/docs/user/reference/cli/azldev_config_generate-schema.md index 2f4eece5a..d8ea8fe77 100644 --- a/docs/user/reference/cli/azldev_config_generate-schema.md +++ b/docs/user/reference/cli/azldev_config_generate-schema.md @@ -45,6 +45,7 @@ azldev config generate-schema [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_docs.md b/docs/user/reference/cli/azldev_docs.md index 787033cbb..4a0d52176 100644 --- a/docs/user/reference/cli/azldev_docs.md +++ b/docs/user/reference/cli/azldev_docs.md @@ -31,6 +31,7 @@ command tree, suitable for inclusion in the user guide. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_docs_agent.md b/docs/user/reference/cli/azldev_docs_agent.md index fd1c785b0..0b7b0f2f3 100644 --- a/docs/user/reference/cli/azldev_docs_agent.md +++ b/docs/user/reference/cli/azldev_docs_agent.md @@ -33,6 +33,7 @@ reference so that agents always load the guidance that ships with the binary. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_docs_agent_install.md b/docs/user/reference/cli/azldev_docs_agent_install.md index c8c8853a5..127f5ef6e 100644 --- a/docs/user/reference/cli/azldev_docs_agent_install.md +++ b/docs/user/reference/cli/azldev_docs_agent_install.md @@ -73,6 +73,7 @@ azldev docs agent install [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_docs_agent_show.md b/docs/user/reference/cli/azldev_docs_agent_show.md index 11d32383c..289100501 100644 --- a/docs/user/reference/cli/azldev_docs_agent_show.md +++ b/docs/user/reference/cli/azldev_docs_agent_show.md @@ -50,6 +50,7 @@ azldev docs agent show [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_docs_markdown.md b/docs/user/reference/cli/azldev_docs_markdown.md index a86b59b4d..38d5da719 100644 --- a/docs/user/reference/cli/azldev_docs_markdown.md +++ b/docs/user/reference/cli/azldev_docs_markdown.md @@ -52,6 +52,7 @@ azldev docs markdown [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_image.md b/docs/user/reference/cli/azldev_image.md index ea678cf94..b09e8a497 100644 --- a/docs/user/reference/cli/azldev_image.md +++ b/docs/user/reference/cli/azldev_image.md @@ -32,6 +32,7 @@ can be customized using Azure Linux Image Customizer. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_image_boot.md b/docs/user/reference/cli/azldev_image_boot.md index 143536805..4941a6ad8 100644 --- a/docs/user/reference/cli/azldev_image_boot.md +++ b/docs/user/reference/cli/azldev_image_boot.md @@ -96,6 +96,7 @@ azldev image boot [IMAGE_NAME] [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_image_build.md b/docs/user/reference/cli/azldev_image_build.md index 3c89f6b7a..3c9ca710d 100644 --- a/docs/user/reference/cli/azldev_image_build.md +++ b/docs/user/reference/cli/azldev_image_build.md @@ -57,6 +57,7 @@ azldev image build [image-name] [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_image_customize.md b/docs/user/reference/cli/azldev_image_customize.md index 929c4f10b..6f97e43cb 100644 --- a/docs/user/reference/cli/azldev_image_customize.md +++ b/docs/user/reference/cli/azldev_image_customize.md @@ -55,6 +55,7 @@ azldev image customize [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_image_inject-files.md b/docs/user/reference/cli/azldev_image_inject-files.md index 748bde7f4..707f6adf2 100644 --- a/docs/user/reference/cli/azldev_image_inject-files.md +++ b/docs/user/reference/cli/azldev_image_inject-files.md @@ -46,6 +46,7 @@ azldev image inject-files [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_image_list.md b/docs/user/reference/cli/azldev_image_list.md index a1270bbd3..db6053b18 100644 --- a/docs/user/reference/cli/azldev_image_list.md +++ b/docs/user/reference/cli/azldev_image_list.md @@ -48,6 +48,7 @@ azldev image list [image-name-pattern...] [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_image_test.md b/docs/user/reference/cli/azldev_image_test.md index e2c1ca54e..fe3bacf48 100644 --- a/docs/user/reference/cli/azldev_image_test.md +++ b/docs/user/reference/cli/azldev_image_test.md @@ -81,6 +81,7 @@ azldev image test IMAGE_NAME [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_package.md b/docs/user/reference/cli/azldev_package.md index 678421bcc..10534a12a 100644 --- a/docs/user/reference/cli/azldev_package.md +++ b/docs/user/reference/cli/azldev_package.md @@ -32,6 +32,7 @@ publish channel assignments derived from package groups and component overrides. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_package_list.md b/docs/user/reference/cli/azldev_package_list.md index e6165273e..39574b56e 100644 --- a/docs/user/reference/cli/azldev_package_list.md +++ b/docs/user/reference/cli/azldev_package_list.md @@ -81,6 +81,7 @@ azldev package list [package-name...] [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_project.md b/docs/user/reference/cli/azldev_project.md index fef53ace7..41ed95190 100644 --- a/docs/user/reference/cli/azldev_project.md +++ b/docs/user/reference/cli/azldev_project.md @@ -31,6 +31,7 @@ as an Azure Linux project with a basic configuration. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_project_init.md b/docs/user/reference/cli/azldev_project_init.md index 13be79456..c80d18b63 100644 --- a/docs/user/reference/cli/azldev_project_init.md +++ b/docs/user/reference/cli/azldev_project_init.md @@ -42,6 +42,7 @@ azldev project init [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_project_new.md b/docs/user/reference/cli/azldev_project_new.md index b494e46ee..e273ad314 100644 --- a/docs/user/reference/cli/azldev_project_new.md +++ b/docs/user/reference/cli/azldev_project_new.md @@ -45,6 +45,7 @@ azldev project new PATH [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_repo.md b/docs/user/reference/cli/azldev_repo.md index ccbc3f5ee..efc106ef1 100644 --- a/docs/user/reference/cli/azldev_repo.md +++ b/docs/user/reference/cli/azldev_repo.md @@ -32,6 +32,7 @@ under one or more URL prefixes. --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_repo_compare.md b/docs/user/reference/cli/azldev_repo_compare.md index 7053a3f19..9ceb3df5a 100644 --- a/docs/user/reference/cli/azldev_repo_compare.md +++ b/docs/user/reference/cli/azldev_repo_compare.md @@ -50,6 +50,7 @@ azldev repo compare [flags] --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_repo_query.md b/docs/user/reference/cli/azldev_repo_query.md index 9086aa1c9..d6c1e0d4f 100644 --- a/docs/user/reference/cli/azldev_repo_query.md +++ b/docs/user/reference/cli/azldev_repo_query.md @@ -73,6 +73,7 @@ azldev repo query [flags] -- --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/docs/user/reference/cli/azldev_version.md b/docs/user/reference/cli/azldev_version.md index 5ebddbcdd..309d83172 100644 --- a/docs/user/reference/cli/azldev_version.md +++ b/docs/user/reference/cli/azldev_version.md @@ -36,6 +36,7 @@ azldev version -O json --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --without-lockfile preview: track resolved upstream commits in generated config instead of lock files ``` diff --git a/internal/app/azldev/agentskill/agentskill_test.go b/internal/app/azldev/agentskill/agentskill_test.go index a263a100f..e53c985a8 100644 --- a/internal/app/azldev/agentskill/agentskill_test.go +++ b/internal/app/azldev/agentskill/agentskill_test.go @@ -153,6 +153,31 @@ func TestBuildComponentSkillDocumentsRPMDevBumpspecPreview(t *testing.T) { assert.NotContains(t, doc, "rpmdev-bumpspec` or RPM macros") } +func TestSkillsDocumentUniqueMockRoots(t *testing.T) { + for _, name := range []string{"azldev-build-component", "azldev-mock"} { + t.Run(name, func(t *testing.T) { + doc, err := agentskill.SkillDocument(name, testParams()) + require.NoError(t, err) + assert.Contains(t, doc, "--unique-mockroot") + assert.Contains(t, doc, "at most 24 characters") + assert.Contains(t, doc, "--uniqueext --shell") + assert.Contains(t, doc, "--disable-plugin yum_cache") + + withoutLockfileDoc, err := agentskill.NewCatalog(true).SkillDocument(name, testParams()) + require.NoError(t, err) + assert.Contains(t, withoutLockfileDoc, "--disable-plugin yum_cache") + }) + } +} + +func TestMockSkillDocumentsBootstrapCleanup(t *testing.T) { + doc, err := agentskill.SkillDocument("azldev-mock", testParams()) + require.NoError(t, err) + assert.Contains(t, doc, "Cleanup preserves shared caches.") + assert.Contains(t, doc, "`mock --help` does not list `bootstrap-cache`") + assert.Contains(t, doc, "matching unique bootstrap directory") +} + func TestComponentTomlSkillDocumentsRPMDevBumpspecPreview(t *testing.T) { doc, err := agentskill.SkillDocument("azldev-comp-toml", testParams()) require.NoError(t, err) diff --git a/internal/app/azldev/agentskill/content/build-component.md.tmpl b/internal/app/azldev/agentskill/content/build-component.md.tmpl index 9375d5e22..4930e1ff3 100644 --- a/internal/app/azldev/agentskill/content/build-component.md.tmpl +++ b/internal/app/azldev/agentskill/content/build-component.md.tmpl @@ -21,6 +21,15 @@ Build foundational packages before their dependents. RPMs land in the project's configured output directory (`out` by default). `-q` quiets output but hides build progress — use it only for inner-loop builds you expect to succeed. +Use the global `--unique-mockroot` flag to isolate mock roots from other azldev +operations. Extensions contain abbreviated action/component labels and a random +suffix, use at most 24 characters total (up to 12 for the component label), and +remain stable through each root's cleanup. +azldev also passes `--disable-plugin yum_cache` for unique-root commands. +To inspect a preserved unique root, use +`mock -r --uniqueext --shell` with the extension from +the verbose command log; a new azldev shell invocation creates a different root. + ## The inner loop investigate → modify → render → build → test → inspect diff --git a/internal/app/azldev/agentskill/content/mock.md.tmpl b/internal/app/azldev/agentskill/content/mock.md.tmpl index e42d63615..8bf8a36ba 100644 --- a/internal/app/azldev/agentskill/content/mock.md.tmpl +++ b/internal/app/azldev/agentskill/content/mock.md.tmpl @@ -73,6 +73,7 @@ react to output between commands. | --- | --- | | `-p`, `--add-package ` | Install a package into the chroot before entering. Accepts an RPM file path or a package name. May be repeated. | | `--enable-network` | Allow network access (dependency resolution, downloads). | +| `--unique-mockroot` | Use a distinct root for this session, passing mock an action/package label (up to 12 characters for the package label) and random suffix via `--uniqueext` (at most 24 characters total). | > **`-p` means `--add-package`, not a component name.** Unlike `azldev comp build -p `, > `mock shell` has no component selector. `-p ` installs a package *by name* from the @@ -82,10 +83,16 @@ react to output between commands. - **Don't mix `-p ` with `--add-package /path/to/.rpm` for the same package** — that installs both the repo build and your local build, which conflict. Use one or the other. -- **The mock chroot is persistent** across `azldev adv mock shell` sessions — installed packages and +- **The mock chroot is persistent by default** across `azldev adv mock shell` sessions — installed packages and created files survive. Handy (install once, re-enter later), but stale state can confuse. azldev has no built-in reset; clear it with `mock` directly (`--scrub=chroot`, or `--scrub=all` to also drop the bootstrap), pointing at the project's distro mock config. +- **`--unique-mockroot` starts a separate root each time.** To re-enter a preserved unique root, + use `mock -r --uniqueext --shell` with the extension from the + verbose command log, not another azldev invocation. + azldev also passes `--disable-plugin yum_cache` for unique-root commands. + Cleanup preserves shared caches. If `mock --help` does not list `bootstrap-cache`, + azldev uses `sudo` to remove only the matching unique bootstrap directory after Mock exits. - **One mock session per terminal.** Running `azldev adv mock shell` while a terminal is already inside a mock shell fails — run `exit` first. diff --git a/internal/app/azldev/agentskill/content/withoutlockfile/build-component.md.tmpl b/internal/app/azldev/agentskill/content/withoutlockfile/build-component.md.tmpl index cac327d99..e476ef94c 100644 --- a/internal/app/azldev/agentskill/content/withoutlockfile/build-component.md.tmpl +++ b/internal/app/azldev/agentskill/content/withoutlockfile/build-component.md.tmpl @@ -21,6 +21,9 @@ Build foundational packages before their dependents. RPMs land in the project's configured output directory (`out` by default). `-q` quiets output but hides build progress — use it only for inner-loop builds you expect to succeed. +Use `--unique-mockroot` to isolate mock roots. azldev also passes +`--disable-plugin yum_cache` for unique-root commands. + ## The inner loop investigate → modify → render → build → test → inspect diff --git a/internal/app/azldev/app.go b/internal/app/azldev/app.go index f9cfda1ae..ef442a60a 100644 --- a/internal/app/azldev/app.go +++ b/internal/app/azldev/app.go @@ -55,6 +55,7 @@ type App struct { configFiles []string colorMode ColorMode withoutLockfile bool + uniqueMockRoot bool // Root command for the CLI. cmd cobra.Command @@ -141,6 +142,7 @@ lives), or use -C to point to one.`, env.SetConcurrency(app.concurrency) env.SetNetworkRetries(app.networkRetries) env.SetPermissiveConfigParsing(app.permissiveConfigEnabled()) + env.SetUniqueMockRoot(app.uniqueMockRoot, command.Name()) return nil }, @@ -193,6 +195,9 @@ func (app *App) registerGlobalFlags() { false, "do not fail on unknown fields in TOML config files") app.cmd.PersistentFlags().BoolVar(&app.withoutLockfile, "without-lockfile", false, "preview: track resolved upstream commits in generated config instead of lock files") + app.cmd.PersistentFlags().BoolVar(&app.uniqueMockRoot, "unique-mockroot", false, + "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); "+ + "allows 'component build' to build multiple components concurrently") } // addAdvancedCommandHint embeds a hint about the hidden "advanced" command group diff --git a/internal/app/azldev/app_test.go b/internal/app/azldev/app_test.go index 64ac1397b..ab7421b19 100644 --- a/internal/app/azldev/app_test.go +++ b/internal/app/azldev/app_test.go @@ -9,6 +9,7 @@ import ( "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev" "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/testutils" + "github.com/microsoft/azure-linux-dev-tools/internal/rpm/mock" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -67,6 +68,41 @@ func TestApp_Execute(t *testing.T) { assert.True(t, ran) } +func TestApp_UniqueMockRoot(t *testing.T) { + for name, enabled := range map[string]bool{"default": false, "enabled": true} { + t.Run(name, func(t *testing.T) { + app := createTestApp(t) + ran := false + cmd := &cobra.Command{ + Use: "build", + RunE: func(cmd *cobra.Command, _ []string) error { + ran = true + env, err := azldev.GetEnvFromCommand(cmd) + require.NoError(t, err) + + runner := mock.NewRunner(env.WithMockRootComponents("curl"), "/mock.cfg") + if enabled { + assert.Regexp(t, `^bld-curl-[a-z2-7]{6}$`, runner.UniqueExtension()) + } else { + assert.Empty(t, runner.UniqueExtension()) + } + + return nil + }, + } + app.AddTopLevelCommand(cmd) + + args := []string{"build"} + if enabled { + args = append(args, "--unique-mockroot") + } + + assert.Zero(t, app.Execute(args)) + assert.True(t, ran) + }) + } +} + func TestApp_RegisterPostInitCallback(t *testing.T) { app := createTestApp(t) diff --git a/internal/app/azldev/cmds/advanced/mock.go b/internal/app/azldev/cmds/advanced/mock.go index 18e4f8060..fad7da562 100644 --- a/internal/app/azldev/cmds/advanced/mock.go +++ b/internal/app/azldev/cmds/advanced/mock.go @@ -6,6 +6,7 @@ package advanced import ( "fmt" "os" + "path/filepath" "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev" "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/buildenvfactory" @@ -142,6 +143,8 @@ running smoke tests. Packages can be pre-installed into the chroot using // Builds RPMs from sources, using options. func BuildRPMS(env *azldev.Env, options *BuildRPMOptions) (results interface{}, err error) { + env = env.WithMockRootComponents(filepath.Base(options.SRPMPath)) + runner, err := makeMockRunner(env, &options.MockCmdOptions) if err != nil { return results, err @@ -167,6 +170,13 @@ func BuildRPMS(env *azldev.Env, options *BuildRPMOptions) (results interface{}, // Executes an interactive shell within a mock root. Uses the provided [ShellOptions] to configure the shell. func RunShell(env *azldev.Env, options *ShellOptions, extraArgs []string) error { + packages := make([]string, len(options.PackagesToAdd)) + for idx, pkg := range options.PackagesToAdd { + packages[idx] = filepath.Base(pkg) + } + + env = env.WithMockRootComponents(packages...) + runner, err := makeMockRunner(env, &options.MockCmdOptions) if err != nil { return err @@ -181,6 +191,12 @@ func RunShell(env *azldev.Env, options *ShellOptions, extraArgs []string) error if err != nil { return fmt.Errorf("failed to install packages in mock root (%v): %w", options.PackagesToAdd, err) } + } else if runner.UniqueExtension() != "" { + // A unique root is brand new, and mock requires an initialized root for shell mode. + err = runner.InitRoot(env) + if err != nil { + return fmt.Errorf("failed to initialize mock root: %w", err) + } } cmd, err := runner.CmdInChroot(env, extraArgs, true /*interactive*/) diff --git a/internal/app/azldev/cmds/advanced/mock_test.go b/internal/app/azldev/cmds/advanced/mock_test.go index 61a295f19..79e295210 100644 --- a/internal/app/azldev/cmds/advanced/mock_test.go +++ b/internal/app/azldev/cmds/advanced/mock_test.go @@ -35,6 +35,58 @@ func TestNewShellCmd(t *testing.T) { assert.Equal(t, "shell", cmd.Use) } +func TestAdvancedUniqueMockRoots(t *testing.T) { + tests := []struct { + name string + action string + configPath string + prefix string + }{ + {name: "shell explicit config", action: "shell", configPath: "/mock.cfg", prefix: "shl-curl-1-0-rpm-"}, + {name: "shell project config", action: "shell", prefix: "shl-curl-1-0-rpm-"}, + {name: "build explicit config", action: "build-rpms", configPath: "/mock.cfg", prefix: "bld-curl-1-0-src-"}, + {name: "build project config", action: "build-rpms", prefix: "bld-curl-1-0-src-"}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + env := testutils.NewTestEnv(t) + env.Env.SetUniqueMockRoot(true, test.action) + env.CmdFactory.RegisterCommandInSearchPath("mock") + + var extensions []string + + env.CmdFactory.RunHandler = func(cmd *exec.Cmd) error { + idx := slices.Index(cmd.Args, "--uniqueext") + require.Positive(t, idx) + require.Less(t, idx+1, len(cmd.Args)) + extensions = append(extensions, cmd.Args[idx+1]) + + return nil + } + + common := advanced.MockCmdOptions{MockConfigPath: test.configPath} + if test.action == "shell" { + require.NoError(t, advanced.RunShell(env.Env, &advanced.ShellOptions{ + MockCmdOptions: common, + PackagesToAdd: []string{"/rpms/curl-1.0.rpm"}, + }, []string{"true"})) + require.Len(t, extensions, 2) + assert.Equal(t, extensions[0], extensions[1]) + } else { + _, err := advanced.BuildRPMS(env.Env, &advanced.BuildRPMOptions{ + MockCmdOptions: common, + SRPMPath: "/srpms/curl-1.0.src.rpm", + OutputDirPath: "/rpms", + }) + require.NoError(t, err) + require.Len(t, extensions, 1) + } + + assert.Regexp(t, "^"+test.prefix+`[a-z2-7]{6}$`, extensions[0]) + }) + } +} + func TestBuildRPMS(t *testing.T) { const ( testMockConfigPath = "/mock/mock.cfg" diff --git a/internal/app/azldev/cmds/component/build.go b/internal/app/azldev/cmds/component/build.go index 2c9f99171..26a3b308e 100644 --- a/internal/app/azldev/cmds/component/build.go +++ b/internal/app/azldev/cmds/component/build.go @@ -4,6 +4,7 @@ package component import ( + "context" "errors" "fmt" "path/filepath" @@ -21,6 +22,7 @@ import ( "github.com/microsoft/azure-linux-dev-tools/internal/utils/defers" "github.com/microsoft/azure-linux-dev-tools/internal/utils/fileutils" "github.com/microsoft/azure-linux-dev-tools/internal/utils/localrepo" + "github.com/microsoft/azure-linux-dev-tools/internal/utils/parmap" "github.com/spf13/cobra" ) @@ -201,6 +203,15 @@ func BuildComponents( return nil, fmt.Errorf("failed to create work dir factory:\n%w", err) } + // Unique mock roots make concurrent builds safe. Publishing to a local repo requires + // builds to complete in order (later components may depend on earlier ones, and + // publishing mutates shared repo metadata), so it stays sequential. + if env.UniqueMockRootEnabled() && options.LocalRepoWithPublishPath == "" && components.Len() > 1 { + return buildComponentsConcurrently(env, components, workDirFactory, options) + } + + env = env.WithMockRootComponents(components.Names()...) + mockProcessor := createBuildMockProcessor(env) defer destroyMockProcessor(env, mockProcessor) @@ -223,12 +234,86 @@ func BuildComponents( return results, err } +// buildComponentsConcurrently builds the components in parallel, each with its own +// mock root and mock processor. Results are returned in input order. +func buildComponentsConcurrently( + env *azldev.Env, + comps *components.ComponentSet, + workDirFactory *workdir.Factory, + options *ComponentBuildOptions, +) ([]ComponentBuildResults, error) { + workerEnv, cancel := env.WithCancel() + defer cancel() + + progressEvent := env.StartEvent("Building components", "count", comps.Len()) + defer progressEvent.End() + + type buildOutcome struct { + results ComponentBuildResults + err error + } + + allComponents := comps.Components() + total := int64(len(allComponents)) + + outcomes := parmap.Map( + workerEnv, + env.CPUBoundConcurrency(), + allComponents, + func(done, _ int) { progressEvent.SetProgress(int64(done), total) }, + func(_ context.Context, component components.Component) buildOutcome { + // workerEnv (captured) carries the cancellation context. + //nolint:contextcheck // env carries the ctx + componentResults, buildErr := BuildComponent(workerEnv, component, workDirFactory, options) + if buildErr != nil { + buildErr = fmt.Errorf("failed to build %q:\n%w", component.GetName(), buildErr) + + if !options.ContinueOnError { + cancel() + } + } + + return buildOutcome{results: componentResults, err: buildErr} + }, + ) + + results := make([]ComponentBuildResults, 0, len(outcomes)) + + var err error + + cancelled := false + + for _, outcome := range outcomes { + if outcome.Cancelled { + cancelled = true + + continue + } + + err = errors.Join(err, outcome.Value.err) + + results = append(results, outcome.Value.results) + } + + if ctxErr := env.Err(); ctxErr != nil { + err = errors.Join(err, ctxErr) + } + + if cancelled && err == nil { + err = errors.New("component builds cancelled before completion") + } + + return results, err +} + func BuildComponent( env *azldev.Env, component components.Component, workDirFactory *workdir.Factory, options *ComponentBuildOptions, ) (ComponentBuildResults, error) { + env = env.WithMockRootComponents(component.GetName()) + mockProcessor := createBuildMockProcessor(env) defer destroyMockProcessor(env, mockProcessor) @@ -242,6 +327,7 @@ func buildComponent( mockProcessor *sources.MockProcessor, options *ComponentBuildOptions, ) (results ComponentBuildResults, err error) { + env = env.WithMockRootComponents(component.GetName()) // Resolve the effective distro for this component before creating the source manager. distro, err := sourceproviders.ResolveDistro(env, component) if err != nil { @@ -267,7 +353,10 @@ func buildComponent( // Clean up the build environment before we return (unless we were asked not to do so). defer defers.HandleDeferError(func() error { if !options.BuildEnvPolicy.ShouldPreserve(err == nil) { - return buildEnv.Destroy(env) + ctx, cancel := context.WithTimeout(context.WithoutCancel(env), mockProcessorCleanupTimeout) + defer cancel() + + return buildEnv.Destroy(env.WithContext(ctx)) } return nil diff --git a/internal/app/azldev/cmds/component/diffsources.go b/internal/app/azldev/cmds/component/diffsources.go index 7fd728050..e021e01f5 100644 --- a/internal/app/azldev/cmds/component/diffsources.go +++ b/internal/app/azldev/cmds/component/diffsources.go @@ -96,7 +96,7 @@ func DiffComponentSources(env *azldev.Env, options *DiffSourcesOptions) (interfa return nil, fmt.Errorf("failed to resolve distro for component %q:\n%w", component.GetName(), err) } - sourceManager, err := sourceproviders.NewSourceManager(env, distro) + sourceManager, err := sourceproviders.NewSourceManager(env.WithMockRootComponents(component.GetName()), distro) if err != nil { return nil, fmt.Errorf("failed to create source manager:\n%w", err) } diff --git a/internal/app/azldev/cmds/component/preparesources.go b/internal/app/azldev/cmds/component/preparesources.go index 317b7fb38..4be62ae43 100644 --- a/internal/app/azldev/cmds/component/preparesources.go +++ b/internal/app/azldev/cmds/component/preparesources.go @@ -126,7 +126,7 @@ func PrepareComponentSources(env *azldev.Env, options *PrepareSourcesOptions) er } // Create source manager to handle all source fetching, both local and upstream. - sourceManager, err = sourceproviders.NewSourceManager(env, distro) + sourceManager, err = sourceproviders.NewSourceManager(env.WithMockRootComponents(component.GetName()), distro) if err != nil { return fmt.Errorf("failed to create source manager:\n%w", err) } diff --git a/internal/app/azldev/cmds/component/query.go b/internal/app/azldev/cmds/component/query.go index 83014b12c..8c09e6563 100644 --- a/internal/app/azldev/cmds/component/query.go +++ b/internal/app/azldev/cmds/component/query.go @@ -154,6 +154,8 @@ func QueryComponents( comps.Len()) } + env = env.WithMockRootComponents(comps.Names()...) + mockProcessor := createQueryMockProcessor(env) if mockProcessor == nil { return nil, errors.New( diff --git a/internal/app/azldev/cmds/component/refreshupstreamcommit.go b/internal/app/azldev/cmds/component/refreshupstreamcommit.go index 02da3bddf..a5c7dc30b 100644 --- a/internal/app/azldev/cmds/component/refreshupstreamcommit.go +++ b/internal/app/azldev/cmds/component/refreshupstreamcommit.go @@ -15,6 +15,7 @@ import ( "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/components" "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" "github.com/microsoft/azure-linux-dev-tools/internal/providers/sourceproviders" + "github.com/microsoft/azure-linux-dev-tools/internal/rpm/mock" "github.com/microsoft/azure-linux-dev-tools/internal/upstreamcommit" "github.com/microsoft/azure-linux-dev-tools/internal/utils/parmap" "github.com/spf13/cobra" @@ -619,7 +620,9 @@ func resolveUpstreamCommit( return "", fmt.Errorf("resolving distro for %#q:\n%w", componentName, err) } - sourceManager, err := sourceproviders.NewSourceManager(env, distro) + //nolint:contextcheck // The cloned operational Env carries ctx and component-specific root labels. + sourceManager, err := sourceproviders.NewSourceManager( + env.WithContext(mock.WithRootComponents(ctx, componentName)), distro) if err != nil { return "", fmt.Errorf("creating source manager for %#q:\n%w", componentName, err) } diff --git a/internal/app/azldev/cmds/component/render.go b/internal/app/azldev/cmds/component/render.go index ff99942bd..afe1e95cc 100644 --- a/internal/app/azldev/cmds/component/render.go +++ b/internal/app/azldev/cmds/component/render.go @@ -177,6 +177,8 @@ func RenderComponents(env *azldev.Env, options *RenderOptions) ([]*RenderResult, } // Create mock processor for rpmautospec/spectool. + env = env.WithMockRootComponents(comps.Names()...) + mockProcessor := createMockProcessor(env) if mockProcessor == nil { return nil, errors.New( @@ -516,7 +518,7 @@ func prepareComponentSources( } // Create source manager. - sourceManager, err := sourceproviders.NewSourceManager(env, distro) + sourceManager, err := sourceproviders.NewSourceManager(env.WithMockRootComponents(componentName), distro) if err != nil { return nil, fmt.Errorf("creating source manager for %#q:\n%w", componentName, err) } diff --git a/internal/app/azldev/cmds/component/update.go b/internal/app/azldev/cmds/component/update.go index 9da1b4f75..32a027477 100644 --- a/internal/app/azldev/cmds/component/update.go +++ b/internal/app/azldev/cmds/component/update.go @@ -17,6 +17,7 @@ import ( "github.com/microsoft/azure-linux-dev-tools/internal/lockfile" "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" "github.com/microsoft/azure-linux-dev-tools/internal/providers/sourceproviders" + "github.com/microsoft/azure-linux-dev-tools/internal/rpm/mock" "github.com/microsoft/azure-linux-dev-tools/internal/utils/parmap" "github.com/spf13/cobra" ) @@ -886,7 +887,9 @@ func resolveOneSourceIdentity( return "", fmt.Errorf("resolving distro for %#q:\n%w", componentName, err) } - sourceManager, err := sourceproviders.NewSourceManager(env, distro) + //nolint:contextcheck // The cloned operational Env carries ctx and component-specific root labels. + sourceManager, err := sourceproviders.NewSourceManager( + env.WithContext(mock.WithRootComponents(ctx, componentName)), distro) if err != nil { return "", fmt.Errorf("creating source manager for %#q:\n%w", componentName, err) } diff --git a/internal/app/azldev/core/workdir/workdir.go b/internal/app/azldev/core/workdir/workdir.go index f611485e5..8af71c2fc 100644 --- a/internal/app/azldev/core/workdir/workdir.go +++ b/internal/app/azldev/core/workdir/workdir.go @@ -92,7 +92,7 @@ func MkComponentBuildEnvironment( // We'll nest the build environment in a subdirectory under the work directory. buildEnvDir := filepath.Join(workDir, "buildenv") - buildEnvFactory, err := buildenvfactory.NewMockRootFactoryForEnv(env) + buildEnvFactory, err := buildenvfactory.NewMockRootFactoryForEnv(env.WithMockRootComponents(component.Name)) if err != nil { return nil, fmt.Errorf("failed to create mock root factory:\n%w", err) } diff --git a/internal/app/azldev/core/workdir/workdir_test.go b/internal/app/azldev/core/workdir/workdir_test.go index fdc6d53b5..f61df3f59 100644 --- a/internal/app/azldev/core/workdir/workdir_test.go +++ b/internal/app/azldev/core/workdir/workdir_test.go @@ -9,6 +9,7 @@ import ( "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/testutils" "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/workdir" + "github.com/microsoft/azure-linux-dev-tools/internal/buildenv" "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -59,3 +60,28 @@ func Test_Create(t *testing.T) { require.NoError(t, statErr) assert.True(t, stat.IsDir()) } + +func TestMkComponentBuildEnvironmentUniqueRoots(t *testing.T) { + env := testutils.NewTestEnv(t) + env.Env.SetUniqueMockRoot(true, "build") + factory, err := workdir.NewFactory(env.FS(), env.Env.WorkDir(), env.Env.ConstructionTime()) + require.NoError(t, err) + + names := []string{"curl", "zlib", "curl"} + + extensions := make([]string, 0, len(names)) + for _, name := range names { + root, createErr := workdir.MkComponentBuildEnvironment(env.Env, factory, + &projectconfig.ComponentConfig{Name: name}, "build", nil) + require.NoError(t, createErr) + + mockRoot, ok := root.(*buildenv.MockRoot) + require.True(t, ok) + + extension := mockRoot.GetRunner().UniqueExtension() + assert.Regexp(t, `^bld-`+name+`-[a-z2-7]{6}$`, extension) + assert.Equal(t, extension, mockRoot.GetRunner().UniqueExtension()) + assert.NotContains(t, extensions, extension) + extensions = append(extensions, extension) + } +} diff --git a/internal/app/azldev/env.go b/internal/app/azldev/env.go index 9b426033f..44da6e63c 100644 --- a/internal/app/azldev/env.go +++ b/internal/app/azldev/env.go @@ -23,6 +23,7 @@ import ( "github.com/microsoft/azure-linux-dev-tools/internal/global/opctx" "github.com/microsoft/azure-linux-dev-tools/internal/lockfile" "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" + "github.com/microsoft/azure-linux-dev-tools/internal/rpm/mock" ) // Parameters used to construct an [Env]. @@ -227,6 +228,29 @@ func (env *Env) SetVerbose(verbose bool) { env.verbose = verbose } +// SetUniqueMockRoot configures unique mock roots for the current CLI action. +func (env *Env) SetUniqueMockRoot(enabled bool, action string) { + env.ctx = mock.WithUniqueRoots(env.ctx, enabled, action) +} + +// UniqueMockRootEnabled reports whether unique mock roots were requested for the current CLI action. +func (env *Env) UniqueMockRootEnabled() bool { + return mock.UniqueRootsEnabled(env.ctx) +} + +// WithMockRootComponents returns a shallow copy with component-specific mock root labels. +func (env *Env) WithMockRootComponents(names ...string) *Env { + return env.WithContext(mock.WithRootComponents(env.ctx, names...)) +} + +// WithContext returns a shallow copy using the supplied context for cancellation and values. +func (env *Env) WithContext(ctx context.Context) *Env { + childEnv := *env + childEnv.ctx = ctx + + return &childEnv +} + // NetworkRetries returns the maximum number of attempts for network operations. func (env *Env) NetworkRetries() int { return env.networkRetries diff --git a/internal/app/azldev/env_test.go b/internal/app/azldev/env_test.go index 7aa094960..423ffbe28 100644 --- a/internal/app/azldev/env_test.go +++ b/internal/app/azldev/env_test.go @@ -17,6 +17,7 @@ import ( "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev" "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/testutils" "github.com/microsoft/azure-linux-dev-tools/internal/projectconfig" + "github.com/microsoft/azure-linux-dev-tools/internal/rpm/mock" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -34,6 +35,38 @@ func setupTestLogger(logBuffer *bytes.Buffer) func() { } } +func TestEnvMockRootContext(t *testing.T) { + env := testutils.NewTestEnv(t).Env + env.SetUniqueMockRoot(true, "render") + + child := env.WithMockRootComponents("curl", "zlib") + + worker, cancel := child.WithCancel() + defer cancel() + + assert.Regexp(t, `^rndr-c_2-[a-z2-7]{6}$`, mock.NewRunner(worker, "/mock.cfg").UniqueExtension()) + assert.Regexp(t, `^rndr-[a-z2-7]{6}$`, mock.NewRunner(env, "/mock.cfg").UniqueExtension()) + + env.SetUniqueMockRoot(false, "render") + assert.Empty(t, mock.NewRunner(env, "/mock.cfg").UniqueExtension()) + assert.NotEmpty(t, mock.NewRunner(child, "/mock.cfg").UniqueExtension()) +} + +func TestEnvWithContext(t *testing.T) { + env := testutils.NewTestEnv(t).Env + + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + + child := env.WithContext(ctx) + assert.Equal(t, env.FS(), child.FS()) + assert.Equal(t, env.Config(), child.Config()) + cancel() + + require.ErrorIs(t, child.Err(), context.Canceled) + require.NoError(t, env.Err()) +} + func TestNewEnv(t *testing.T) { const ( testProjectRoot = "/non/existent/dir" diff --git a/internal/app/azldev/event.go b/internal/app/azldev/event.go index 329f1aa05..b27f90591 100644 --- a/internal/app/azldev/event.go +++ b/internal/app/azldev/event.go @@ -45,6 +45,9 @@ func (e *event) End() { } // Decrement our event nesting level. + e.parentEventListener.mu.Lock() + defer e.parentEventListener.mu.Unlock() + if e.parentEventListener.eventLevel > 0 { e.parentEventListener.eventLevel-- } diff --git a/internal/app/azldev/eventlistener.go b/internal/app/azldev/eventlistener.go index 7c509370d..dc4e76f83 100644 --- a/internal/app/azldev/eventlistener.go +++ b/internal/app/azldev/eventlistener.go @@ -9,11 +9,13 @@ import ( "log/slog" "os" "strings" + "sync" "github.com/microsoft/azure-linux-dev-tools/internal/global/opctx" ) type appEventListener struct { + mu sync.Mutex eventLevel int eventLogger *slog.Logger quiet bool @@ -41,6 +43,9 @@ func NewEventListener(eventLogger *slog.Logger, quiet, verbose bool) (*appEventL // //nolint:ireturn,nolintlint // We need to return an interface because of the interface definition. func (el *appEventListener) StartEvent(name string, args ...any) opctx.Event { + el.mu.Lock() + defer el.mu.Unlock() + if name != "" { const spacesPerLevel = 2 diff --git a/internal/app/azldev/eventlistener_test.go b/internal/app/azldev/eventlistener_test.go new file mode 100644 index 000000000..e64539d82 --- /dev/null +++ b/internal/app/azldev/eventlistener_test.go @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package azldev_test + +import ( + "log/slog" + "sync" + "testing" + + "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev" + "github.com/stretchr/testify/require" +) + +func TestAppEventListenerConcurrentEvents(t *testing.T) { + t.Parallel() + + listener, err := azldev.NewEventListener(slog.New(slog.DiscardHandler), true, false) + require.NoError(t, err) + + const eventCount = 100 + + var waitGroup sync.WaitGroup + waitGroup.Add(eventCount) + + for range eventCount { + go func() { + defer waitGroup.Done() + + listener.StartEvent("worker").End() + }() + } + + waitGroup.Wait() +} diff --git a/internal/providers/sourceproviders/customsourceprovider.go b/internal/providers/sourceproviders/customsourceprovider.go index f287c9277..8baf405b1 100644 --- a/internal/providers/sourceproviders/customsourceprovider.go +++ b/internal/providers/sourceproviders/customsourceprovider.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "strings" + "time" "github.com/microsoft/azure-linux-dev-tools/internal/app/azldev/core/components" "github.com/microsoft/azure-linux-dev-tools/internal/global/opctx" @@ -35,6 +36,8 @@ const customGenOutputDir = "/azldev-gen/output" // near the end of command output. const maxCustomScriptOutputBytes = 64 * 1024 +const customSourceCleanupTimeout = 5 * time.Minute + // customFileSourceProvider implements [FileSourceProvider] for source files with // [projectconfig.OriginTypeCustom]. It executes a user-supplied script inside a // fresh mock chroot and packages the output directory as a deterministic archive. @@ -276,7 +279,10 @@ func execScriptInChroot( defer func() { // Best-effort cleanup. Log on failure rather than overwriting the primary error. - if scrubErr := runner.ScrubRoot(ctx); scrubErr != nil { + cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), customSourceCleanupTimeout) + defer cancel() + + if scrubErr := runner.ScrubRoot(cleanupCtx); scrubErr != nil { slog.Warn("Failed to scrub mock root after custom source generation", "filename", ref.Filename, "error", scrubErr) diff --git a/internal/rpm/mock/mock.go b/internal/rpm/mock/mock.go index 566aa4973..feefbe440 100644 --- a/internal/rpm/mock/mock.go +++ b/internal/rpm/mock/mock.go @@ -42,13 +42,15 @@ type Runner struct { osEnv opctx.OSEnv cmdFactory opctx.CmdFactory eventListener opctx.EventListener + dryRunInfo opctx.DryRunnable // Verbosity verbose bool - mockConfigPath string - bindMounts []bindMountRequest - enableNetwork bool + mockConfigPath string + uniqueExtension string + bindMounts []bindMountRequest + enableNetwork bool // noPreClean requests that the root *not* be cleaned on command execution, even if mock deems // it ought to be. @@ -90,31 +92,35 @@ type bindMountRequest struct { // Constructs a new [Runner] that can be used to invoke mock. func NewRunner(ctx opctx.Ctx, mockConfigPath string) *Runner { return &Runner{ - fs: ctx.FS(), - osEnv: ctx.OSEnv(), - cmdFactory: ctx, - eventListener: ctx, - verbose: ctx.Verbose(), - mockConfigPath: mockConfigPath, + fs: ctx.FS(), + osEnv: ctx.OSEnv(), + cmdFactory: ctx, + eventListener: ctx, + dryRunInfo: ctx, + verbose: ctx.Verbose(), + mockConfigPath: mockConfigPath, + uniqueExtension: newUniqueExtension(ctx), } } // Clone creates a deep copy of the provided [Runner] instance. func (r *Runner) Clone() *Runner { return &Runner{ - fs: r.fs, - osEnv: r.osEnv, - cmdFactory: r.cmdFactory, - eventListener: r.eventListener, - verbose: r.verbose, - mockConfigPath: r.mockConfigPath, - bindMounts: deep.MustCopy(r.bindMounts), - enableNetwork: r.enableNetwork, - noPreClean: r.noPreClean, - unprivileged: r.unprivileged, - baseDir: r.baseDir, - rootDir: r.rootDir, - configOpts: deep.MustCopy(r.configOpts), + fs: r.fs, + osEnv: r.osEnv, + cmdFactory: r.cmdFactory, + eventListener: r.eventListener, + dryRunInfo: r.dryRunInfo, + verbose: r.verbose, + mockConfigPath: r.mockConfigPath, + uniqueExtension: r.uniqueExtension, + bindMounts: deep.MustCopy(r.bindMounts), + enableNetwork: r.enableNetwork, + noPreClean: r.noPreClean, + unprivileged: r.unprivileged, + baseDir: r.baseDir, + rootDir: r.rootDir, + configOpts: deep.MustCopy(r.configOpts), } } @@ -221,6 +227,11 @@ func (r *Runner) ConfigPath() string { return r.mockConfigPath } +// UniqueExtension returns the mock root extension, or empty when unique roots are disabled. +func (r *Runner) UniqueExtension() string { + return r.uniqueExtension +} + // InitRoot initializes a mock root. func (r *Runner) InitRoot(ctx context.Context) (err error) { slog.Debug("Initializing mock root", "dir", r.rootDir) @@ -621,7 +632,16 @@ func (r *Runner) ScrubRoot(ctx context.Context) error { mockArgs := r.getBaseArgs() - if r.baseDir != "" { + var bootstrapPath string + + if r.uniqueExtension != "" { + bootstrapPath, err = r.bootstrapCleanupPath(ctx) + if err != nil { + return err + } + } + + if r.baseDir != "" && r.uniqueExtension == "" { // Lie about the cache topdir so that mock doesn't delete our *actual* shared cache. mockArgs = append(mockArgs, "--config-opts", "cache_topdir="+r.baseDir) mockArgs = append(mockArgs, "--scrub", "all") @@ -629,6 +649,12 @@ func (r *Runner) ScrubRoot(ctx context.Context) error { mockArgs = append(mockArgs, "--scrub", "chroot") } + // A unique extension gives the bootstrap root a unique name too, so it would never be reused + // or cleaned up by a later invocation. + if r.uniqueExtension != "" && bootstrapPath == "" { + mockArgs = append(mockArgs, "--scrub", "bootstrap") + } + cmd := exec.CommandContext(ctx, MockBinary, mockArgs...) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr @@ -645,6 +671,94 @@ func (r *Runner) ScrubRoot(ctx context.Context) error { return fmt.Errorf("mock failed to scrub root:\n%w", err) } + if bootstrapPath != "" { + return r.removeBootstrapRoot(ctx, bootstrapPath) + } + + return nil +} + +// bootstrapCleanupPath returns the unique bootstrap directory to remove manually, or an empty +// string when mock supports scrubbing the bootstrap without deleting its shared cache. +func (r *Runner) bootstrapCleanupPath(ctx context.Context) (string, error) { + // Dry runs cannot discover capabilities or paths; only preview the wrapped scrub command. + if r.dryRunInfo.DryRun() { + return "", nil + } + + cmd, err := r.cmdFactory.Command(exec.CommandContext(ctx, MockBinary, "--help")) + if err != nil { + return "", fmt.Errorf("failed to create command to query mock capabilities:\n%w", err) + } + + help, err := cmd.RunAndGetOutput(ctx) + if err != nil { + return "", fmt.Errorf("failed to query mock capabilities:\n%w", err) + } + + if strings.Contains(help, "bootstrap-cache") { + return "", nil + } + + // Query the canonical root directory, ignoring custom '--rootdir' settings: mock always + // places the bootstrap beside this directory, even when the main root is elsewhere. + probe := r.Clone() + + probe.rootDir = "" + if probe.configOpts == nil { + probe.configOpts = make(map[string]string) + } + + probe.configOpts["rootdir"] = "{{basedir}}/{{root}}" + + rootPath, err := probe.GetRootPath(ctx) + if err != nil { + return "", fmt.Errorf("failed to locate unique mock bootstrap root:\n%w", err) + } + + rootPath = filepath.Clean(rootPath) + rootName := filepath.Base(rootPath) + suffix := "-" + r.uniqueExtension + + if !filepath.IsAbs(rootPath) || strings.ContainsAny(rootPath, "\r\n") || + !strings.HasSuffix(rootName, suffix) || len(rootName) <= len(suffix) { + return "", fmt.Errorf("refusing to derive bootstrap root from unexpected mock root path %#q", rootPath) + } + + bootstrapName := strings.TrimSuffix(rootName, suffix) + "-bootstrap" + suffix + + return filepath.Join(filepath.Dir(rootPath), bootstrapName), nil +} + +func (r *Runner) removeBootstrapRoot(ctx context.Context, bootstrapPath string) error { + exists, err := fileutils.Exists(r.fs, bootstrapPath) + if err != nil { + return fmt.Errorf("failed to check mock bootstrap root %#q:\n%w", bootstrapPath, err) + } + + if !exists { + return nil + } + + // Mock has exited and finalized both roots, unmounting their filesystems. The bootstrap + // contains root-owned files, so remove only its validated unique directory with sudo. + if !r.cmdFactory.CommandInSearchPath("sudo") { + return fmt.Errorf("sudo required to remove unique mock bootstrap root %#q", bootstrapPath) + } + + rawCmd := exec.CommandContext(ctx, "sudo", "rm", "--recursive", "--force", "--one-file-system", "--", bootstrapPath) + rawCmd.Stdout = os.Stdout + rawCmd.Stderr = os.Stderr + + cmd, err := r.cmdFactory.Command(rawCmd) + if err != nil { + return fmt.Errorf("failed to create command to remove mock bootstrap root:\n%w", err) + } + + if err := cmd.Run(ctx); err != nil { + return fmt.Errorf("failed to remove mock bootstrap root %#q:\n%w", bootstrapPath, err) + } + return nil } @@ -665,7 +779,11 @@ func (r *Runner) getBaseArgs() (args []string) { } args = append(args, "-r", r.mockConfigPath) + args = append(args, "--configdir", filepath.Dir(r.mockConfigPath)) + if r.uniqueExtension != "" { + args = append(args, "--uniqueext", r.uniqueExtension, "--disable-plugin", "yum_cache") + } if r.baseDir != "" { args = append(args, "--config-opts", "basedir="+r.baseDir) diff --git a/internal/rpm/mock/mock_test.go b/internal/rpm/mock/mock_test.go index 3d402c9ce..60a5e5198 100644 --- a/internal/rpm/mock/mock_test.go +++ b/internal/rpm/mock/mock_test.go @@ -8,6 +8,7 @@ import ( "fmt" "os/exec" "path/filepath" + "slices" "strings" "testing" @@ -27,6 +28,7 @@ const ( testOutputDirPath = "/output" testSRPMPath = "/output/component-1.0.0.src.rpm" testRPMPath = "/output/component-1.0.0.rpm" + testSudoBinary = "sudo" ) func newTestCtxWithMockPrereqsPresent() *testctx.TestCtx { @@ -119,6 +121,269 @@ func TestGetRootPath(t *testing.T) { assert.Equal(t, testMockRootPath, rootPath) } +func TestScrubRoot(t *testing.T) { + tests := []struct { + name string + unique bool + bootstrapCache bool + baseDir string + missingBootstrap bool + }{ + {name: "shared root"}, + {name: "shared private base", baseDir: "/private/mock"}, + {name: "unique modern mock", unique: true, bootstrapCache: true}, + {name: "unique legacy mock", unique: true}, + {name: "unique modern private base", unique: true, bootstrapCache: true, baseDir: "/private/mock"}, + {name: "unique legacy private base", unique: true, baseDir: "/private/mock"}, + {name: "unique missing bootstrap", unique: true, missingBootstrap: true}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ctx := newTestCtxWithMockPrereqsPresent() + ctx.Ctx = mock.WithUniqueRoots(ctx.Ctx, test.unique, "build") + ctx.CmdFactory.RegisterCommandInSearchPath(testSudoBinary) + runner := mock.NewRunner(ctx, testMockConfigPath).WithBaseDir(test.baseDir) + runner.WithRootDir("/custom/root") + runner.WithConfigOpts(map[string]string{"rootdir": "/configured/root"}) + + baseDir := test.baseDir + if baseDir == "" { + baseDir = "/configured/mock" + } + + rootName := "test" + bootstrapName := "test-bootstrap" + + if test.unique { + rootName += "-" + runner.UniqueExtension() + bootstrapName += "-" + runner.UniqueExtension() + } + + rootPath := filepath.Join(baseDir, rootName) + bootstrapPath := filepath.Join(baseDir, bootstrapName) + cachePath := "/var/cache/mock/test-bootstrap/root_cache/cache" + otherRoot := filepath.Join(baseDir, "test-bootstrap-other") + + sharedRoot := filepath.Join(baseDir, "test-bootstrap") + for _, path := range []string{rootPath, otherRoot, sharedRoot, filepath.Dir(cachePath)} { + require.NoError(t, fileutils.MkdirAll(ctx.FS(), path)) + } + + if !test.missingBootstrap { + require.NoError(t, fileutils.MkdirAll(ctx.FS(), bootstrapPath)) + } + + require.NoError(t, fileutils.WriteFile(ctx.FS(), cachePath, []byte("shared cache"), fileperms.PrivateFile)) + + var actions []string + + ctx.CmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) { + if slices.Contains(cmd.Args, "--help") { + actions = append(actions, "help") + + assert.Equal(t, []string{"mock", "--help"}, cmd.Args) + + if test.bootstrapCache { + return "--scrub=[all|bootstrap|chroot|cache|bootstrap-cache]", nil + } + + return "--scrub=[all|bootstrap|chroot|cache]", nil + } + + actions = append(actions, "path") + + assert.Contains(t, cmd.Args, "--print-root-path") + assert.Contains(t, cmd.Args, "rootdir={{basedir}}/{{root}}") + assert.NotContains(t, cmd.Args, "--rootdir") + assert.NotContains(t, cmd.Args, "rootdir=/configured/root") + + return rootPath + "\n", nil + } + ctx.CmdFactory.RunHandler = func(cmd *exec.Cmd) error { + if cmd.Args[0] == testSudoBinary { + actions = append(actions, "remove") + + assert.Equal(t, []string{ + testSudoBinary, "rm", "--recursive", "--force", "--one-file-system", "--", bootstrapPath, + }, cmd.Args) + + return ctx.FS().RemoveAll(bootstrapPath) + } + + actions = append(actions, "scrub") + + assert.Contains(t, cmd.Args, "--scrub") + assert.NotContains(t, cmd.Args, "bootstrap-cache") + assert.Equal(t, test.unique && test.bootstrapCache, slices.Contains(cmd.Args, "bootstrap")) + assert.Equal(t, !test.unique && test.baseDir != "", slices.Contains(cmd.Args, "all")) + require.NoError(t, ctx.FS().RemoveAll(rootPath)) + + if test.unique && test.bootstrapCache { + require.NoError(t, ctx.FS().RemoveAll(bootstrapPath)) + } + + return nil + } + + require.NoError(t, runner.ScrubRoot(ctx)) + + expectedActions := []string{"scrub"} + if test.unique { + expectedActions = []string{"help", "scrub"} + if !test.bootstrapCache { + expectedActions = []string{"help", "path", "scrub"} + if !test.missingBootstrap { + expectedActions = append(expectedActions, "remove") + } + } + } + + assert.Equal(t, expectedActions, actions) + + if test.unique { + exists, err := fileutils.Exists(ctx.FS(), bootstrapPath) + require.NoError(t, err) + assert.False(t, exists) + } + + for _, path := range []string{otherRoot, sharedRoot, cachePath} { + exists, err := fileutils.Exists(ctx.FS(), path) + require.NoError(t, err) + assert.True(t, exists, path) + } + }) + } +} + +func TestScrubRootDryRun(t *testing.T) { + for _, baseDir := range []string{"", "/private/mock"} { + t.Run("base="+baseDir, func(t *testing.T) { + ctx := newTestCtxWithMockPrereqsPresent() + ctx.DryRunValue = true + ctx.Ctx = mock.WithUniqueRoots(ctx.Ctx, true, "build") + runner := mock.NewRunner(ctx, testMockConfigPath).WithBaseDir(baseDir) + + ctx.CmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) { + t.Errorf("unexpected discovery command during dry run: %v", cmd.Args) + + return "", nil + } + + var commands [][]string + + ctx.CmdFactory.RunHandler = func(cmd *exec.Cmd) error { + commands = append(commands, cmd.Args) + + return nil + } + + for _, candidate := range []*mock.Runner{runner, runner.Clone()} { + require.NoError(t, candidate.ScrubRoot(ctx)) + } + + require.Len(t, commands, 2) + + for _, args := range commands { + assert.Equal(t, "mock", args[0]) + assert.Contains(t, args, "--uniqueext") + assert.Contains(t, args, runner.UniqueExtension()) + assert.Equal(t, []string{"--scrub", "chroot", "--scrub", "bootstrap"}, args[len(args)-4:]) + assert.NotContains(t, args, "all") + assert.NotContains(t, args, "bootstrap-cache") + } + }) + } +} + +func TestScrubRootRejectsUnexpectedPaths(t *testing.T) { + for _, path := range []string{ + "", "/", "/var/lib/mock/test", "relative-EXTENSION", + "/var/lib/mock/-EXTENSION", "/var/lib/mock/unsafe\nroot-EXTENSION", + } { + t.Run(path, func(t *testing.T) { + ctx := newTestCtxWithMockPrereqsPresent() + ctx.Ctx = mock.WithUniqueRoots(ctx.Ctx, true, "build") + runner := mock.NewRunner(ctx, testMockConfigPath) + ctx.CmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) { + if slices.Contains(cmd.Args, "--help") { + return "--scrub=[bootstrap|chroot]", nil + } + + return strings.ReplaceAll(path, "EXTENSION", runner.UniqueExtension()), nil + } + ctx.CmdFactory.RunHandler = func(cmd *exec.Cmd) error { + t.Fatalf("unexpected destructive command: %v", cmd.Args) + + return nil + } + + err := runner.ScrubRoot(ctx) + require.ErrorContains(t, err, "refusing to derive bootstrap root") + }) + } +} + +func TestScrubRootFailures(t *testing.T) { + for _, stage := range []string{"help", "path", "scrub", "remove", "missing sudo"} { + t.Run(stage, func(t *testing.T) { + ctx := newTestCtxWithMockPrereqsPresent() + + ctx.Ctx = mock.WithUniqueRoots(ctx.Ctx, true, "build") + if stage != "missing sudo" { + ctx.CmdFactory.RegisterCommandInSearchPath(testSudoBinary) + } + + runner := mock.NewRunner(ctx, testMockConfigPath) + rootPath := "/var/lib/mock/test-" + runner.UniqueExtension() + bootstrapPath := "/var/lib/mock/test-bootstrap-" + runner.UniqueExtension() + require.NoError(t, fileutils.MkdirAll(ctx.FS(), bootstrapPath)) + + testError := errors.New("injected cleanup failure") + ctx.CmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) { + if slices.Contains(cmd.Args, "--help") { + if stage == "help" { + return "", testError + } + + return "--scrub=[bootstrap|chroot]", nil + } + + if stage == "path" { + return "", testError + } + + return rootPath, nil + } + ctx.CmdFactory.RunHandler = func(cmd *exec.Cmd) error { + if cmd.Args[0] == testSudoBinary { + assert.Equal(t, "remove", stage) + } else { + assert.Contains(t, []string{"scrub", "remove", "missing sudo"}, stage) + assert.NotContains(t, cmd.Args, "bootstrap") + } + + if stage == "scrub" || stage == "remove" && cmd.Args[0] == testSudoBinary { + return testError + } + + return nil + } + + err := runner.ScrubRoot(ctx) + if stage == "missing sudo" { + require.ErrorContains(t, err, "sudo required") + } else { + require.ErrorIs(t, err, testError) + } + + exists, err := fileutils.Exists(ctx.FS(), bootstrapPath) + require.NoError(t, err) + assert.True(t, exists) + }) + } +} + func TestBuildSRPM(t *testing.T) { ctx := newTestCtxWithMockPrereqsPresent() executedCmds := []string{} diff --git a/internal/rpm/mock/uniqueroot.go b/internal/rpm/mock/uniqueroot.go new file mode 100644 index 000000000..1bb73e1f6 --- /dev/null +++ b/internal/rpm/mock/uniqueroot.go @@ -0,0 +1,113 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package mock + +import ( + "context" + "crypto/rand" + "strconv" + "strings" +) + +type uniqueRootKey struct{} + +const ( + maxUniqueExtensionLength = 24 + uniqueRandomLength = 6 + maxRootActionLength = 4 + maxRootActionWithoutComponents = 8 + maxRootComponentLength = maxUniqueExtensionLength - maxRootActionLength - uniqueRandomLength - 2 +) + +type uniqueRootOptions struct { + enabled bool + action string + components []string +} + +// WithUniqueRoots configures newly created runners to use distinct mock roots. +func WithUniqueRoots(ctx context.Context, enabled bool, action string) context.Context { + return context.WithValue(ctx, uniqueRootKey{}, uniqueRootOptions{ + enabled: enabled, + action: action, + }) +} + +// UniqueRootsEnabled reports whether ctx was configured with [WithUniqueRoots] enabled. +func UniqueRootsEnabled(ctx context.Context) bool { + options, _ := ctx.Value(uniqueRootKey{}).(uniqueRootOptions) + + return options.enabled +} + +// WithRootComponents specializes the component label for newly created runners, +// without changing whether unique roots are enabled. +func WithRootComponents(ctx context.Context, components ...string) context.Context { + options, _ := ctx.Value(uniqueRootKey{}).(uniqueRootOptions) + + options.components = append([]string(nil), components...) + + return context.WithValue(ctx, uniqueRootKey{}, options) +} + +func newUniqueExtension(ctx context.Context) string { + options, _ := ctx.Value(uniqueRootKey{}).(uniqueRootOptions) + if !options.enabled { + return "" + } + + action := rootAction(options.action) + + label := action[:min(len(action), maxRootActionWithoutComponents)] + if len(options.components) > 0 { + action = action[:min(len(action), maxRootActionLength)] + component := rootLabel(options.components[0]) + + componentLabel := component[:min(len(component), maxRootComponentLength)] + if len(options.components) > 1 { + count := strconv.Itoa(len(options.components)) + + batchLabel := component[:min(len(component), 1)] + "_" + count + if len(batchLabel) <= maxRootComponentLength { + componentLabel = batchLabel + } + } + + label = action + "-" + componentLabel + } + + // Reserve six random base32 characters and a separator within the 24-byte limit. + return label + "-" + strings.ToLower(rand.Text()[:uniqueRandomLength]) +} + +func rootAction(action string) string { + action = rootLabel(action) + switch action { + case "build", "build-rpms": + return "bld" + case "render": + return "rndr" + case "query": + return "qry" + case "shell": + return "shl" + case "": + return "mock" + default: + return action + } +} + +func rootLabel(value string) string { + value = strings.ToLower(value) + value = strings.Map(func(char rune) rune { + if char >= 'a' && char <= 'z' || char >= '0' && char <= '9' || char == '_' || char == '-' { + return char + } + + return '-' + }, value) + + return strings.Trim(value, "-_") +} diff --git a/internal/rpm/mock/uniqueroot_test.go b/internal/rpm/mock/uniqueroot_test.go new file mode 100644 index 000000000..79613cde5 --- /dev/null +++ b/internal/rpm/mock/uniqueroot_test.go @@ -0,0 +1,173 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +package mock_test + +import ( + "os/exec" + "slices" + "strings" + "testing" + + "github.com/microsoft/azure-linux-dev-tools/internal/rpm/mock" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestUniqueExtensionLabels(t *testing.T) { + tests := []struct { + name string + action string + components []string + prefix string + }{ + {name: "shell without packages", action: "shell", prefix: "shl-"}, + {name: "shell with packages", action: "shell", components: []string{"curl"}, prefix: "shl-curl-"}, + {name: "build without components", action: "build", prefix: "bld-"}, + {name: "build with one component", action: "build", components: []string{"curl"}, prefix: "bld-curl-"}, + {name: "advanced build", action: "build-rpms", components: []string{"curl"}, prefix: "bld-curl-"}, + {name: "render without components", action: "render", prefix: "rndr-"}, + {name: "render with one component", action: "render", components: []string{"curl"}, prefix: "rndr-curl-"}, + {name: "query without components", action: "query", prefix: "qry-"}, + {name: "query with one component", action: "query", components: []string{"curl"}, prefix: "qry-curl-"}, + { + name: "exact middle field and total limits", action: "render", + components: []string{"abcdefghijkl"}, prefix: "rndr-abcdefghijkl-", + }, + { + name: "middle field over limit", action: "render", + components: []string{"abcdefghijklm"}, prefix: "rndr-abcdefghijkl-", + }, + { + name: "middle field capped with shorter action", action: "build", + components: []string{"abcdefghijklm"}, prefix: "bld-abcdefghijkl-", + }, + { + name: "multiple components", action: "render", + components: []string{"curl", "zlib"}, prefix: "rndr-c_2-", + }, + { + name: "large batch", action: "render", + components: append([]string{"curl"}, make([]string, 999)...), prefix: "rndr-c_1000-", + }, + { + name: "large shell package list", action: "shell", + components: append([]string{"curl"}, make([]string, 999)...), prefix: "shl-c_1000-", + }, + { + name: "two digit batch count", action: "build", + components: append([]string{"curl"}, make([]string, 9)...), prefix: "bld-c_10-", + }, + { + name: "three digit batch count", action: "build", + components: append([]string{"curl"}, make([]string, 99)...), prefix: "bld-c_100-", + }, + { + name: "long component label", action: "query", + components: []string{strings.Repeat("rust-component", 10)}, prefix: "qry-rust-compone-", + }, + {name: "long action without components", action: "build-source-rpms", prefix: "build-so-"}, + {name: "allowed character boundaries", action: "AZ_az-09", prefix: "az_az-09-"}, + { + name: "unsafe characters", action: "BUILD", components: []string{"a/b \t\u00e9"}, + prefix: "bld-a-b-", + }, + {name: "empty action", prefix: "mock-"}, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + ctx := newTestCtxWithMockPrereqsPresent() + ctx.Ctx = mock.WithUniqueRoots(ctx.Ctx, true, test.action) + ctx.Ctx = mock.WithRootComponents(ctx.Ctx, test.components...) + + runner := mock.NewRunner(ctx, testMockConfigPath) + extension := runner.UniqueExtension() + + assert.LessOrEqual(t, len(extension), 24) + assert.Regexp(t, `^[a-z0-9_-]+$`, extension) + assert.True(t, strings.HasPrefix(extension, test.prefix), extension) + assert.Regexp(t, `^[a-z2-7]{6}$`, strings.TrimPrefix(extension, test.prefix)) + }) + } +} + +func TestUniqueExtensionLifecycle(t *testing.T) { + ctx := newTestCtxWithMockPrereqsPresent() + ctx.Ctx = mock.WithUniqueRoots(ctx.Ctx, true, "build") + ctx.Ctx = mock.WithRootComponents(ctx.Ctx, "curl") + + var extensions []string + + capture := func(cmd *exec.Cmd) { + t.Helper() + + index := slices.Index(cmd.Args, "--uniqueext") + require.Positive(t, index) + require.Less(t, index+1, len(cmd.Args)) + extensions = append(extensions, cmd.Args[index+1]) + + pluginIndex := slices.Index(cmd.Args, "--disable-plugin") + require.Positive(t, pluginIndex) + require.Less(t, pluginIndex+1, len(cmd.Args)) + assert.Equal(t, "yum_cache", cmd.Args[pluginIndex+1]) + } + ctx.CmdFactory.RunHandler = func(cmd *exec.Cmd) error { + capture(cmd) + + return nil + } + ctx.CmdFactory.RunAndGetOutputHandler = func(cmd *exec.Cmd) (string, error) { + if slices.Contains(cmd.Args, "--help") { + return "--scrub=[chroot|bootstrap|bootstrap-cache]", nil + } + + capture(cmd) + + return testMockRootPath, nil + } + + runner := mock.NewRunner(ctx, testMockConfigPath) + require.NoError(t, runner.InitRoot(ctx)) + require.NoError(t, runner.InstallPackages(ctx, []string{"rpm-build"})) + require.NoError(t, runner.BuildSRPM(ctx, testSpecPath, testSourceDirPath, testOutputDirPath, mock.SRPMBuildOptions{})) + require.NoError(t, runner.BuildRPM(ctx, testSRPMPath, testOutputDirPath, mock.RPMBuildOptions{})) + _, err := runner.GetRootPath(ctx) + require.NoError(t, err) + + cmd, err := runner.Clone().CmdInChroot(ctx, []string{"true"}, false) + require.NoError(t, err) + require.NoError(t, cmd.Run(ctx)) + require.NoError(t, runner.Clone().ScrubRoot(ctx)) + + require.Len(t, extensions, 7) + + for _, extension := range extensions { + assert.Equal(t, runner.UniqueExtension(), extension) + } + + assert.NotEqual(t, runner.UniqueExtension(), mock.NewRunner(ctx, testMockConfigPath).UniqueExtension()) +} + +func TestUniqueExtensionDisabled(t *testing.T) { + for name, explicitlyDisabled := range map[string]bool{"default": false, "explicit": true} { + t.Run(name, func(t *testing.T) { + ctx := newTestCtxWithMockPrereqsPresent() + if explicitlyDisabled { + ctx.Ctx = mock.WithUniqueRoots(ctx.Ctx, false, "build") + } + + ctx.Ctx = mock.WithRootComponents(ctx.Ctx, "curl") + ctx.CmdFactory.RunHandler = func(cmd *exec.Cmd) error { + assert.NotContains(t, cmd.Args, "--uniqueext") + assert.NotContains(t, cmd.Args, "--disable-plugin") + + return nil + } + + runner := mock.NewRunner(ctx, testMockConfigPath) + assert.Empty(t, runner.UniqueExtension()) + require.NoError(t, runner.InitRoot(ctx)) + }) + } +} diff --git a/scenario/__snapshots__/TestMCPServerMode_1.snap.json b/scenario/__snapshots__/TestMCPServerMode_1.snap.json index 663b83ff7..895ecf4a4 100755 --- a/scenario/__snapshots__/TestMCPServerMode_1.snap.json +++ b/scenario/__snapshots__/TestMCPServerMode_1.snap.json @@ -101,6 +101,11 @@ "description": "Git ref to compare to", "type": "string" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "verbose": { "default": false, "description": "enable verbose output", @@ -212,6 +217,11 @@ "description": "Spec path", "type": "string" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "verbose": { "default": false, "description": "enable verbose output", @@ -321,6 +331,11 @@ "description": "Spec path", "type": "string" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "verbose": { "default": false, "description": "enable verbose output", @@ -420,6 +435,11 @@ "description": "Spec path", "type": "string" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "verbose": { "default": false, "description": "enable verbose output", @@ -534,6 +554,11 @@ "description": "Spec path", "type": "string" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "upstream-status": { "default": "", "description": "only include entries whose metadata declares this upstream status ('upstreamed', 'upstreamable', 'needs-upstream-hook', 'inapplicable', or 'unknown')", @@ -621,6 +646,11 @@ "description": "only enable minimal output", "type": "boolean" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "verbose": { "default": false, "description": "enable verbose output", @@ -699,6 +729,11 @@ "description": "only enable minimal output", "type": "boolean" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "verbose": { "default": false, "description": "enable verbose output", @@ -782,6 +817,11 @@ "description": "skill to print", "type": "string" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "verbose": { "default": false, "description": "enable verbose output", @@ -875,6 +915,11 @@ "description": "only enable minimal output", "type": "boolean" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "verbose": { "default": false, "description": "enable verbose output", @@ -955,6 +1000,11 @@ "description": "only enable minimal output", "type": "boolean" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "verbose": { "default": false, "description": "enable verbose output", @@ -1052,6 +1102,11 @@ "description": "Also synthesize '-debuginfo' packages (per reported package) and '-debugsource' packages (per component)", "type": "boolean" }, + "unique-mockroot": { + "default": false, + "description": "use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently", + "type": "boolean" + }, "verbose": { "default": false, "description": "enable verbose output", diff --git a/scenario/__snapshots__/TestSnapshotsContainer_--bogus-flag_stderr_1.snap b/scenario/__snapshots__/TestSnapshotsContainer_--bogus-flag_stderr_1.snap index 6832b09a4..5d1cbf8a8 100755 --- a/scenario/__snapshots__/TestSnapshotsContainer_--bogus-flag_stderr_1.snap +++ b/scenario/__snapshots__/TestSnapshotsContainer_--bogus-flag_stderr_1.snap @@ -29,6 +29,7 @@ Flags: --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --version version for azldev --without-lockfile preview: track resolved upstream commits in generated config instead of lock files diff --git a/scenario/__snapshots__/TestSnapshotsContainer_--help_stdout_1.snap b/scenario/__snapshots__/TestSnapshotsContainer_--help_stdout_1.snap index e15aef19c..c1adbdc90 100755 --- a/scenario/__snapshots__/TestSnapshotsContainer_--help_stdout_1.snap +++ b/scenario/__snapshots__/TestSnapshotsContainer_--help_stdout_1.snap @@ -36,6 +36,7 @@ Flags: --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --version version for azldev --without-lockfile preview: track resolved upstream commits in generated config instead of lock files diff --git a/scenario/__snapshots__/TestSnapshotsContainer_help_stdout_1.snap b/scenario/__snapshots__/TestSnapshotsContainer_help_stdout_1.snap index e15aef19c..c1adbdc90 100755 --- a/scenario/__snapshots__/TestSnapshotsContainer_help_stdout_1.snap +++ b/scenario/__snapshots__/TestSnapshotsContainer_help_stdout_1.snap @@ -36,6 +36,7 @@ Flags: --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --version version for azldev --without-lockfile preview: track resolved upstream commits in generated config instead of lock files diff --git a/scenario/__snapshots__/TestSnapshots_--bogus-flag_stderr_1.snap b/scenario/__snapshots__/TestSnapshots_--bogus-flag_stderr_1.snap index 6832b09a4..5d1cbf8a8 100755 --- a/scenario/__snapshots__/TestSnapshots_--bogus-flag_stderr_1.snap +++ b/scenario/__snapshots__/TestSnapshots_--bogus-flag_stderr_1.snap @@ -29,6 +29,7 @@ Flags: --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --version version for azldev --without-lockfile preview: track resolved upstream commits in generated config instead of lock files diff --git a/scenario/__snapshots__/TestSnapshots_--help_stdout_1.snap b/scenario/__snapshots__/TestSnapshots_--help_stdout_1.snap index e15aef19c..c1adbdc90 100755 --- a/scenario/__snapshots__/TestSnapshots_--help_stdout_1.snap +++ b/scenario/__snapshots__/TestSnapshots_--help_stdout_1.snap @@ -36,6 +36,7 @@ Flags: --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --version version for azldev --without-lockfile preview: track resolved upstream commits in generated config instead of lock files diff --git a/scenario/__snapshots__/TestSnapshots_--help_with_color_stdout_1.snap b/scenario/__snapshots__/TestSnapshots_--help_with_color_stdout_1.snap index 4a47c31e2..82d31d4fe 100755 --- a/scenario/__snapshots__/TestSnapshots_--help_with_color_stdout_1.snap +++ b/scenario/__snapshots__/TestSnapshots_--help_with_color_stdout_1.snap @@ -36,6 +36,7 @@ Meta commands: --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --version version for azldev --without-lockfile preview: track resolved upstream commits in generated config instead of lock files diff --git a/scenario/__snapshots__/TestSnapshots_help_stdout_1.snap b/scenario/__snapshots__/TestSnapshots_help_stdout_1.snap index e15aef19c..c1adbdc90 100755 --- a/scenario/__snapshots__/TestSnapshots_help_stdout_1.snap +++ b/scenario/__snapshots__/TestSnapshots_help_stdout_1.snap @@ -36,6 +36,7 @@ Flags: --permissive-config do not fail on unknown fields in TOML config files -C, --project string path to Azure Linux project -q, --quiet only enable minimal output + --unique-mockroot use unique mock roots labeled with the action, components, and a random suffix (at most 24 characters); allows 'component build' to build multiple components concurrently -v, --verbose enable verbose output --version version for azldev --without-lockfile preview: track resolved upstream commits in generated config instead of lock files